api.c 2.2 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608
  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. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #ifndef USER_CRYPTO_ERROR
  215. #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
  216. #endif
  217. #endif
  218. #ifndef NO_SIG_WRAPPER
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #endif
  221. #ifdef HAVE_AESCCM
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #endif
  224. #ifdef HAVE_PKCS7
  225. #include <wolfssl/wolfcrypt/pkcs7.h>
  226. #include <wolfssl/wolfcrypt/asn.h>
  227. #ifdef HAVE_LIBZ
  228. #include <wolfssl/wolfcrypt/compress.h>
  229. #endif
  230. #endif
  231. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  232. #include <wolfssl/wolfcrypt/asn.h>
  233. #endif
  234. #ifndef NO_DSA
  235. #include <wolfssl/wolfcrypt/dsa.h>
  236. #ifndef ONEK_BUF
  237. #define ONEK_BUF 1024
  238. #endif
  239. #ifndef TWOK_BUF
  240. #define TWOK_BUF 2048
  241. #endif
  242. #ifndef FOURK_BUF
  243. #define FOURK_BUF 4096
  244. #endif
  245. #ifndef DSA_SIG_SIZE
  246. #define DSA_SIG_SIZE 40
  247. #endif
  248. #ifndef MAX_DSA_PARAM_SIZE
  249. #define MAX_DSA_PARAM_SIZE 256
  250. #endif
  251. #endif
  252. #ifdef WOLFSSL_CMAC
  253. #include <wolfssl/wolfcrypt/cmac.h>
  254. #endif
  255. #ifdef HAVE_ED25519
  256. #include <wolfssl/wolfcrypt/ed25519.h>
  257. #endif
  258. #ifdef HAVE_CURVE25519
  259. #include <wolfssl/wolfcrypt/curve25519.h>
  260. #endif
  261. #ifdef HAVE_ED448
  262. #include <wolfssl/wolfcrypt/ed448.h>
  263. #endif
  264. #ifdef HAVE_CURVE448
  265. #include <wolfssl/wolfcrypt/curve448.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #ifdef HAVE_ECC
  312. #include <wolfssl/openssl/ecdsa.h>
  313. #endif
  314. #ifdef HAVE_PKCS7
  315. #include <wolfssl/openssl/pkcs7.h>
  316. #endif
  317. #ifdef HAVE_ED25519
  318. #include <wolfssl/openssl/ed25519.h>
  319. #endif
  320. #ifdef HAVE_ED448
  321. #include <wolfssl/openssl/ed448.h>
  322. #endif
  323. #endif /* OPENSSL_EXTRA */
  324. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  325. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  326. #include <wolfssl/wolfcrypt/srp.h>
  327. #endif
  328. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  329. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  330. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  331. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  332. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  333. || !defined(WOLFSSL_NO_TLS12)
  334. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  335. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  336. * number tracking */
  337. #include "wolfssl/internal.h"
  338. #endif
  339. /* force enable test buffers */
  340. #ifndef USE_CERT_BUFFERS_2048
  341. #define USE_CERT_BUFFERS_2048
  342. #endif
  343. #ifndef USE_CERT_BUFFERS_256
  344. #define USE_CERT_BUFFERS_256
  345. #endif
  346. #include <wolfssl/certs_test.h>
  347. #include "tests/utils.h"
  348. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  349. /* FIPS build has replaced ecc.h. */
  350. #define wc_ecc_key_get_priv(key) (&((key)->k))
  351. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  352. #endif
  353. typedef struct testVector {
  354. const char* input;
  355. const char* output;
  356. size_t inLen;
  357. size_t outLen;
  358. } testVector;
  359. #if defined(HAVE_PKCS7)
  360. typedef struct {
  361. const byte* content;
  362. word32 contentSz;
  363. int contentOID;
  364. int encryptOID;
  365. int keyWrapOID;
  366. int keyAgreeOID;
  367. byte* cert;
  368. size_t certSz;
  369. byte* privateKey;
  370. word32 privateKeySz;
  371. } pkcs7EnvelopedVector;
  372. #ifndef NO_PKCS7_ENCRYPTED_DATA
  373. typedef struct {
  374. const byte* content;
  375. word32 contentSz;
  376. int contentOID;
  377. int encryptOID;
  378. byte* encryptionKey;
  379. word32 encryptionKeySz;
  380. } pkcs7EncryptedVector;
  381. #endif
  382. #endif /* HAVE_PKCS7 */
  383. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  384. typedef int (*ssl_cb)(WOLFSSL* ssl);
  385. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  386. typedef struct test_ssl_cbf {
  387. method_provider method;
  388. ctx_cb ctx_ready;
  389. ssl_cb ssl_ready;
  390. ssl_cb on_result;
  391. ssl_cb on_cleanup;
  392. WOLFSSL_CTX* ctx;
  393. const char* caPemFile;
  394. const char* certPemFile;
  395. const char* keyPemFile;
  396. const char* crlPemFile;
  397. #ifdef WOLFSSL_STATIC_MEMORY
  398. byte* mem;
  399. word32 memSz;
  400. wolfSSL_method_func method_ex;
  401. #endif
  402. int devId;
  403. int return_code;
  404. int last_err;
  405. unsigned char isSharedCtx:1;
  406. unsigned char loadToSSL:1;
  407. unsigned char ticNoInit:1;
  408. unsigned char doUdp:1;
  409. } test_ssl_cbf;
  410. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  411. typedef struct test_ssl_memio_ctx {
  412. WOLFSSL_CTX* s_ctx;
  413. WOLFSSL_CTX* c_ctx;
  414. WOLFSSL* s_ssl;
  415. WOLFSSL* c_ssl;
  416. const char* c_ciphers;
  417. const char* s_ciphers;
  418. char* c_msg;
  419. int c_msglen;
  420. char* s_msg;
  421. int s_msglen;
  422. test_ssl_cbf s_cb;
  423. test_ssl_cbf c_cb;
  424. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  425. int c_len;
  426. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  427. int s_len;
  428. } test_ssl_memio_ctx;
  429. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  430. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  431. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  432. const char* currentTestName;
  433. char tmpDirName[16];
  434. int tmpDirNameSet = 0;
  435. #endif
  436. /*----------------------------------------------------------------------------*
  437. | Constants
  438. *----------------------------------------------------------------------------*/
  439. /* Test result constants and macros. */
  440. /* Test succeeded. */
  441. #define TEST_SUCCESS (1)
  442. /* Test failed. */
  443. #define TEST_FAIL (0)
  444. /* Test skipped - not run. */
  445. #define TEST_SKIPPED (-7777)
  446. /* Returns the result based on whether check is true.
  447. *
  448. * @param [in] check Condition for success.
  449. * @return When condition is true: TEST_SUCCESS.
  450. * @return When condition is false: TEST_FAIL.
  451. */
  452. #ifdef DEBUG_WOLFSSL_VERBOSE
  453. #define XSTRINGIFY(s) STRINGIFY(s)
  454. #define STRINGIFY(s) #s
  455. #define TEST_RES_CHECK(check) ({ \
  456. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  457. if (_ret == TEST_FAIL) { \
  458. fprintf(stderr, " check \"%s\" at %d ", \
  459. XSTRINGIFY(check), __LINE__); \
  460. } \
  461. _ret; })
  462. #else
  463. #define TEST_RES_CHECK(check) \
  464. ((check) ? TEST_SUCCESS : TEST_FAIL)
  465. #endif /* DEBUG_WOLFSSL_VERBOSE */
  466. #define TEST_STRING "Everyone gets Friday off."
  467. #define TEST_STRING_SZ 25
  468. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  469. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  470. #define TEST_RSA_BITS 1024
  471. #else
  472. #define TEST_RSA_BITS 2048
  473. #endif
  474. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  475. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  476. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  477. static const char* bogusFile =
  478. #ifdef _WIN32
  479. "NUL"
  480. #else
  481. "/dev/null"
  482. #endif
  483. ;
  484. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  485. enum {
  486. TESTING_RSA = 1,
  487. TESTING_ECC = 2
  488. };
  489. #ifdef WOLFSSL_QNX_CAAM
  490. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  491. static int testDevId = WOLFSSL_CAAM_DEVID;
  492. #else
  493. static int testDevId = INVALID_DEVID;
  494. #endif
  495. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  496. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  497. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  498. #define HAVE_IO_TESTS_DEPENDENCIES
  499. #endif
  500. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  501. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  502. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  503. #endif
  504. /*----------------------------------------------------------------------------*
  505. | BIO with fixed read/write size
  506. *----------------------------------------------------------------------------*/
  507. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  508. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  509. int len)
  510. {
  511. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  512. len = 0;
  513. }
  514. else {
  515. if (bio->wrSz - bio->wrIdx < len) {
  516. len = bio->wrSz - bio->wrIdx;
  517. }
  518. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  519. bio->wrIdx += len;
  520. }
  521. return len;
  522. }
  523. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  524. {
  525. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  526. len = 0;
  527. }
  528. else {
  529. if (bio->wrSz - bio->rdIdx < len) {
  530. len = bio->wrSz - bio->rdIdx;
  531. }
  532. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  533. bio->rdIdx += len;
  534. }
  535. return len;
  536. }
  537. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  538. {
  539. static WOLFSSL_BIO_METHOD meth;
  540. meth.type = WOLFSSL_BIO_BIO;
  541. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  542. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  543. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  544. return &meth;
  545. }
  546. #endif
  547. /*----------------------------------------------------------------------------*
  548. | Setup
  549. *----------------------------------------------------------------------------*/
  550. static int test_wolfSSL_Init(void)
  551. {
  552. EXPECT_DECLS;
  553. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  554. return EXPECT_RESULT();
  555. }
  556. static int test_wolfSSL_Cleanup(void)
  557. {
  558. EXPECT_DECLS;
  559. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  560. return EXPECT_RESULT();
  561. }
  562. /* Initialize the wolfCrypt state.
  563. * POST: 0 success.
  564. */
  565. static int test_wolfCrypt_Init(void)
  566. {
  567. EXPECT_DECLS;
  568. ExpectIntEQ(wolfCrypt_Init(), 0);
  569. return EXPECT_RESULT();
  570. } /* END test_wolfCrypt_Init */
  571. static int test_wolfCrypt_Cleanup(void)
  572. {
  573. EXPECT_DECLS;
  574. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  575. return EXPECT_RESULT();
  576. }
  577. /*----------------------------------------------------------------------------*
  578. | Platform dependent function test
  579. *----------------------------------------------------------------------------*/
  580. static int test_fileAccess(void)
  581. {
  582. EXPECT_DECLS;
  583. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  584. const char *fname[] = {
  585. svrCertFile, svrKeyFile, caCertFile,
  586. eccCertFile, eccKeyFile, eccRsaCertFile,
  587. cliCertFile, cliCertDerFile, cliKeyFile,
  588. dhParamFile,
  589. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  590. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  591. NULL
  592. };
  593. const char derfile[] = "./certs/server-cert.der";
  594. XFILE f = XBADFILE;
  595. size_t sz;
  596. byte *buff = NULL;
  597. int i;
  598. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  599. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  600. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  601. XFCLOSE(f);
  602. }
  603. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  604. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  605. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  606. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  607. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  608. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  609. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  610. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  611. XFCLOSE(f);
  612. #endif
  613. return EXPECT_RESULT();
  614. }
  615. /*----------------------------------------------------------------------------*
  616. | Method Allocators
  617. *----------------------------------------------------------------------------*/
  618. static int test_wolfSSL_Method_Allocators(void)
  619. {
  620. EXPECT_DECLS;
  621. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  622. do { \
  623. WOLFSSL_METHOD *method = NULL; \
  624. condition(method = allocator()); \
  625. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  626. } while (0)
  627. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  628. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  629. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  630. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  631. #ifndef NO_OLD_TLS
  632. #ifdef WOLFSSL_ALLOW_SSLV3
  633. #ifndef NO_WOLFSSL_SERVER
  634. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  635. #endif
  636. #ifndef NO_WOLFSSL_CLIENT
  637. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  638. #endif
  639. #endif
  640. #ifdef WOLFSSL_ALLOW_TLSV10
  641. #ifndef NO_WOLFSSL_SERVER
  642. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  643. #endif
  644. #ifndef NO_WOLFSSL_CLIENT
  645. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  646. #endif
  647. #endif
  648. #ifndef NO_WOLFSSL_SERVER
  649. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  650. #endif
  651. #ifndef NO_WOLFSSL_CLIENT
  652. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  653. #endif
  654. #endif /* !NO_OLD_TLS */
  655. #ifndef WOLFSSL_NO_TLS12
  656. #ifndef NO_WOLFSSL_SERVER
  657. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  658. #endif
  659. #ifndef NO_WOLFSSL_CLIENT
  660. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  661. #endif
  662. #endif /* !WOLFSSL_NO_TLS12 */
  663. #ifdef WOLFSSL_TLS13
  664. #ifndef NO_WOLFSSL_SERVER
  665. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  666. #endif
  667. #ifndef NO_WOLFSSL_CLIENT
  668. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  669. #endif
  670. #endif /* WOLFSSL_TLS13 */
  671. #ifndef NO_WOLFSSL_SERVER
  672. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  673. #endif
  674. #ifndef NO_WOLFSSL_CLIENT
  675. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  676. #endif
  677. #ifdef WOLFSSL_DTLS
  678. #ifndef NO_OLD_TLS
  679. #ifndef NO_WOLFSSL_SERVER
  680. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  681. #endif
  682. #ifndef NO_WOLFSSL_CLIENT
  683. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  684. #endif
  685. #endif
  686. #ifndef WOLFSSL_NO_TLS12
  687. #ifndef NO_WOLFSSL_SERVER
  688. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  689. #endif
  690. #ifndef NO_WOLFSSL_CLIENT
  691. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  692. #endif
  693. #endif
  694. #endif /* WOLFSSL_DTLS */
  695. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  696. /* Stubs */
  697. #ifndef NO_WOLFSSL_SERVER
  698. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  699. #endif
  700. #ifndef NO_WOLFSSL_CLIENT
  701. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  702. #endif
  703. #endif
  704. /* Test Either Method (client or server) */
  705. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  706. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  707. #ifndef NO_OLD_TLS
  708. #ifdef WOLFSSL_ALLOW_TLSV10
  709. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  710. #endif
  711. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  712. #endif /* !NO_OLD_TLS */
  713. #ifndef WOLFSSL_NO_TLS12
  714. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  715. #endif /* !WOLFSSL_NO_TLS12 */
  716. #ifdef WOLFSSL_TLS13
  717. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  718. #endif /* WOLFSSL_TLS13 */
  719. #ifdef WOLFSSL_DTLS
  720. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  721. #ifndef NO_OLD_TLS
  722. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  723. #endif /* !NO_OLD_TLS */
  724. #ifndef WOLFSSL_NO_TLS12
  725. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  726. #endif /* !WOLFSSL_NO_TLS12 */
  727. #endif /* WOLFSSL_DTLS */
  728. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  729. return EXPECT_RESULT();
  730. }
  731. /*----------------------------------------------------------------------------*
  732. | Context
  733. *----------------------------------------------------------------------------*/
  734. #ifndef NO_WOLFSSL_SERVER
  735. static int test_wolfSSL_CTX_new(void)
  736. {
  737. EXPECT_DECLS;
  738. WOLFSSL_CTX *ctx;
  739. WOLFSSL_METHOD* method;
  740. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  741. ExpectNotNull(method = wolfSSLv23_server_method());
  742. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  743. wolfSSL_CTX_free(ctx);
  744. return EXPECT_RESULT();
  745. }
  746. #endif
  747. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  748. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  749. static int test_for_double_Free(void)
  750. {
  751. EXPECT_DECLS;
  752. WOLFSSL_CTX* ctx = NULL;
  753. WOLFSSL* ssl = NULL;
  754. int skipTest = 0;
  755. const char* testCertFile;
  756. const char* testKeyFile;
  757. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  758. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  759. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  760. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  761. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  762. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  763. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  764. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  765. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  766. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  767. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  768. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  769. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  770. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  771. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  772. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  773. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  774. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  775. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  776. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  777. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  778. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  779. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  780. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  781. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  782. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  783. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  784. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  785. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  786. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  787. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  788. #ifdef OPENSSL_EXTRA
  789. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  790. "!SRP:!kRSA:!aNULL:!eNULL";
  791. #endif
  792. #ifndef NO_RSA
  793. testCertFile = svrCertFile;
  794. testKeyFile = svrKeyFile;
  795. #elif defined(HAVE_ECC)
  796. testCertFile = eccCertFile;
  797. testKeyFile = eccKeyFile;
  798. #else
  799. skipTest = 1;
  800. #endif
  801. if (skipTest != 1) {
  802. #ifndef NO_WOLFSSL_SERVER
  803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  804. #else
  805. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  806. #endif
  807. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  808. WOLFSSL_FILETYPE_PEM));
  809. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  810. WOLFSSL_FILETYPE_PEM));
  811. ExpectNotNull(ssl = wolfSSL_new(ctx));
  812. /* First test freeing SSL, then CTX */
  813. wolfSSL_free(ssl);
  814. ssl = NULL;
  815. wolfSSL_CTX_free(ctx);
  816. ctx = NULL;
  817. #ifndef NO_WOLFSSL_CLIENT
  818. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  819. #else
  820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  821. #endif
  822. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  823. WOLFSSL_FILETYPE_PEM));
  824. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  825. WOLFSSL_FILETYPE_PEM));
  826. ExpectNotNull(ssl = wolfSSL_new(ctx));
  827. /* Next test freeing CTX then SSL */
  828. wolfSSL_CTX_free(ctx);
  829. ctx = NULL;
  830. wolfSSL_free(ssl);
  831. ssl = NULL;
  832. #ifndef NO_WOLFSSL_SERVER
  833. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  834. #else
  835. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  836. #endif
  837. /* Test setting ciphers at ctx level */
  838. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  839. WOLFSSL_FILETYPE_PEM));
  840. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  841. WOLFSSL_FILETYPE_PEM));
  842. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  843. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  844. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  845. /* only update TLSv13 suites */
  846. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  847. #endif
  848. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  849. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  850. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  851. /* only update pre-TLSv13 suites */
  852. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  853. "ECDHE-RSA-AES128-GCM-SHA256"));
  854. #endif
  855. #ifdef OPENSSL_EXTRA
  856. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  857. #endif
  858. ExpectNotNull(ssl = wolfSSL_new(ctx));
  859. wolfSSL_CTX_free(ctx);
  860. ctx = NULL;
  861. wolfSSL_free(ssl);
  862. ssl = NULL;
  863. #ifndef NO_WOLFSSL_CLIENT
  864. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  865. #else
  866. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  867. #endif
  868. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  869. WOLFSSL_FILETYPE_PEM));
  870. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  871. WOLFSSL_FILETYPE_PEM));
  872. ExpectNotNull(ssl = wolfSSL_new(ctx));
  873. /* test setting ciphers at SSL level */
  874. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  875. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  876. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  877. /* only update TLSv13 suites */
  878. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  879. #endif
  880. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  881. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  882. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  883. /* only update pre-TLSv13 suites */
  884. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  885. #endif
  886. wolfSSL_CTX_free(ctx);
  887. ctx = NULL;
  888. wolfSSL_free(ssl);
  889. ssl = NULL;
  890. }
  891. return EXPECT_RESULT();
  892. }
  893. #endif
  894. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  895. {
  896. EXPECT_DECLS;
  897. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  898. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  899. (!defined(NO_RSA) || defined(HAVE_ECC))
  900. const char* testCertFile;
  901. const char* testKeyFile;
  902. WOLFSSL_CTX* ctx = NULL;
  903. WOLFSSL* ssl = NULL;
  904. const byte cipherList[] =
  905. {
  906. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  907. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  908. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  909. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  910. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  911. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  912. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  913. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  914. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  915. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  916. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  917. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  918. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  919. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  920. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  921. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  922. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  923. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  924. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  925. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  926. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  927. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  928. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  929. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  930. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  931. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  932. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  933. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  934. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  935. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  936. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  937. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  938. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  939. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  940. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  941. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  942. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  943. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  944. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  945. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  946. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  947. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  948. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  949. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  950. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  951. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  952. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  953. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  954. /* SHA256 */
  955. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  956. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  957. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  958. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  959. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  960. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  961. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  962. /* SHA384 */
  963. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  964. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  965. /* AES-GCM */
  966. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  967. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  968. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  969. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  970. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  971. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  972. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  973. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  974. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  975. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  976. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  977. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  978. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  979. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  980. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  981. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  982. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  983. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  984. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  985. * also, in some of the other AES-CCM suites
  986. * there will be second byte number conflicts
  987. * with non-ECC AES-GCM */
  988. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  989. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  990. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  991. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  992. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  993. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  994. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  995. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  996. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  997. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  998. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  999. /* Camellia */
  1000. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1001. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1002. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1003. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1004. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1005. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1006. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1007. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1008. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1009. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1010. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1011. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1012. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1013. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1014. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1015. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1016. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1017. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1018. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1019. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1020. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1021. /* TLS v1.3 cipher suites */
  1022. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1023. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1024. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1025. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1026. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1027. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1028. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1029. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1030. };
  1031. #ifndef NO_RSA
  1032. testCertFile = svrCertFile;
  1033. testKeyFile = svrKeyFile;
  1034. #elif defined(HAVE_ECC)
  1035. testCertFile = eccCertFile;
  1036. testKeyFile = eccKeyFile;
  1037. #endif
  1038. #ifndef NO_WOLFSSL_SERVER
  1039. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1040. #else
  1041. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1042. #endif
  1043. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1044. sizeof(cipherList)));
  1045. wolfSSL_CTX_free(ctx);
  1046. ctx = NULL;
  1047. #ifndef NO_WOLFSSL_SERVER
  1048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1049. #else
  1050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1051. #endif
  1052. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1053. WOLFSSL_FILETYPE_PEM));
  1054. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1055. WOLFSSL_FILETYPE_PEM));
  1056. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1057. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1058. sizeof(cipherList)));
  1059. wolfSSL_free(ssl);
  1060. wolfSSL_CTX_free(ctx);
  1061. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1062. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1063. return EXPECT_RESULT();
  1064. }
  1065. static int test_wolfSSL_CTX_use_certificate_file(void)
  1066. {
  1067. EXPECT_DECLS;
  1068. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1069. WOLFSSL_CTX *ctx = NULL;
  1070. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1071. /* invalid context */
  1072. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1073. WOLFSSL_FILETYPE_PEM));
  1074. /* invalid cert file */
  1075. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1076. WOLFSSL_FILETYPE_PEM));
  1077. /* invalid cert type */
  1078. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1079. #ifdef NO_RSA
  1080. /* rsa needed */
  1081. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1082. WOLFSSL_FILETYPE_PEM));
  1083. #else
  1084. /* success */
  1085. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1086. WOLFSSL_FILETYPE_PEM));
  1087. #endif
  1088. wolfSSL_CTX_free(ctx);
  1089. #endif
  1090. return EXPECT_RESULT();
  1091. }
  1092. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1093. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1094. {
  1095. EXPECT_DECLS;
  1096. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1097. WOLFSSL_CTX* ctx = NULL;
  1098. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1099. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1100. server_cert_der_2048), WOLFSSL_SUCCESS);
  1101. wolfSSL_CTX_free(ctx);
  1102. #endif
  1103. return EXPECT_RESULT();
  1104. }
  1105. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1106. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1107. * context using buffer.
  1108. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1109. * --enable-testcert flag.
  1110. */
  1111. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1112. {
  1113. EXPECT_DECLS;
  1114. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1115. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1116. WOLFSSL_CTX* ctx = NULL;
  1117. int ret;
  1118. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1119. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1120. server_cert_der_2048, sizeof_server_cert_der_2048,
  1121. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1122. wolfSSL_CTX_free(ctx);
  1123. #endif
  1124. return EXPECT_RESULT();
  1125. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1126. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1127. {
  1128. EXPECT_DECLS;
  1129. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1130. WOLFSSL_CTX *ctx = NULL;
  1131. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1132. /* invalid context */
  1133. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1134. WOLFSSL_FILETYPE_PEM));
  1135. /* invalid key file */
  1136. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1137. WOLFSSL_FILETYPE_PEM));
  1138. /* invalid key type */
  1139. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1140. /* success */
  1141. #ifdef NO_RSA
  1142. /* rsa needed */
  1143. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1144. WOLFSSL_FILETYPE_PEM));
  1145. #else
  1146. /* success */
  1147. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1148. WOLFSSL_FILETYPE_PEM));
  1149. #endif
  1150. wolfSSL_CTX_free(ctx);
  1151. #endif
  1152. return EXPECT_RESULT();
  1153. }
  1154. /* test both file and buffer versions along with unloading trusted peer certs */
  1155. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1156. {
  1157. EXPECT_DECLS;
  1158. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1159. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1160. WOLFSSL_CTX *ctx = NULL;
  1161. WOLFSSL* ssl = NULL;
  1162. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1163. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1164. #if !defined(NO_FILESYSTEM)
  1165. /* invalid file */
  1166. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1167. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1168. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1169. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1170. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1171. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1172. /* success */
  1173. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1174. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1175. /* unload cert */
  1176. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1177. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1178. /* invalid file */
  1179. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1180. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1181. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1182. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1183. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1184. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1185. /* success */
  1186. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1187. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1188. #ifdef WOLFSSL_LOCAL_X509_STORE
  1189. /* unload cert */
  1190. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1191. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1192. #endif
  1193. #endif
  1194. /* Test of loading certs from buffers */
  1195. /* invalid buffer */
  1196. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1197. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1198. /* success */
  1199. #ifdef USE_CERT_BUFFERS_1024
  1200. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1201. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1202. #endif
  1203. #ifdef USE_CERT_BUFFERS_2048
  1204. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1205. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1206. #endif
  1207. /* unload cert */
  1208. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1209. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1210. wolfSSL_free(ssl);
  1211. wolfSSL_CTX_free(ctx);
  1212. #endif
  1213. return EXPECT_RESULT();
  1214. }
  1215. static int test_wolfSSL_CTX_load_verify_locations(void)
  1216. {
  1217. EXPECT_DECLS;
  1218. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1219. WOLFSSL_CTX *ctx = NULL;
  1220. #ifndef NO_RSA
  1221. WOLFSSL_CERT_MANAGER* cm = NULL;
  1222. #ifdef PERSIST_CERT_CACHE
  1223. int cacheSz = 0;
  1224. unsigned char* cache = NULL;
  1225. int used = 0;
  1226. #ifndef NO_FILESYSTEM
  1227. const char* cacheFile = "./tests/cert_cache.tmp";
  1228. #endif
  1229. int i;
  1230. int t;
  1231. int* p;
  1232. #endif
  1233. #endif
  1234. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1235. const char* load_certs_path = "./certs/external";
  1236. const char* load_no_certs_path = "./examples";
  1237. const char* load_expired_path = "./certs/test/expired";
  1238. #endif
  1239. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1240. /* invalid arguments */
  1241. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1242. WOLFSSL_FAILURE);
  1243. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1244. WOLFSSL_FAILURE);
  1245. /* invalid ca file */
  1246. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1247. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1248. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1249. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1250. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1251. /* invalid path */
  1252. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1253. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1254. #endif
  1255. /* load ca cert */
  1256. #ifdef NO_RSA
  1257. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1258. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1259. #else /* Skip the following test without RSA certs. */
  1260. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1261. WOLFSSL_SUCCESS);
  1262. #ifdef PERSIST_CERT_CACHE
  1263. /* Get cert cache size */
  1264. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1265. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1266. DYNAMIC_TYPE_TMP_BUFFER));
  1267. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1268. BAD_FUNC_ARG);
  1269. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1270. BAD_FUNC_ARG);
  1271. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1272. BAD_FUNC_ARG);
  1273. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1274. BAD_FUNC_ARG);
  1275. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1276. BAD_FUNC_ARG);
  1277. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1278. BAD_FUNC_ARG);
  1279. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1280. BAD_FUNC_ARG);
  1281. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1282. BAD_FUNC_ARG);
  1283. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1284. BAD_FUNC_ARG);
  1285. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1286. BUFFER_E);
  1287. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1288. ExpectIntEQ(cacheSz, used);
  1289. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1290. BAD_FUNC_ARG);
  1291. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1292. BAD_FUNC_ARG);
  1293. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1294. BAD_FUNC_ARG);
  1295. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1296. BAD_FUNC_ARG);
  1297. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1298. BAD_FUNC_ARG);
  1299. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1300. BAD_FUNC_ARG);
  1301. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1302. BAD_FUNC_ARG);
  1303. /* Smaller than header. */
  1304. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1305. for (i = 1; i < cacheSz; i++) {
  1306. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1307. BUFFER_E);
  1308. }
  1309. if (EXPECT_SUCCESS()) {
  1310. /* Modify header for bad results! */
  1311. p = (int*)cache;
  1312. /* version */
  1313. t = p[0]; p[0] = 0xff;
  1314. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1315. CACHE_MATCH_ERROR);
  1316. p[0] = t; p++;
  1317. /* rows */
  1318. t = p[0]; p[0] = 0xff;
  1319. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1320. CACHE_MATCH_ERROR);
  1321. p[0] = t; p++;
  1322. /* columns[0] */
  1323. t = p[0]; p[0] = -1;
  1324. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1325. PARSE_ERROR);
  1326. p[0] = t; p += CA_TABLE_SIZE;
  1327. /* signerSz*/
  1328. t = p[0]; p[0] = 0xff;
  1329. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1330. CACHE_MATCH_ERROR);
  1331. p[0] = t;
  1332. }
  1333. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1334. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1335. #ifndef NO_FILESYSTEM
  1336. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1337. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1338. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1339. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1340. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1341. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1342. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1343. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1344. WOLFSSL_BAD_FILE);
  1345. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1346. /* File contents is not a cache. */
  1347. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1348. CACHE_MATCH_ERROR);
  1349. #endif
  1350. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1351. #endif
  1352. /* Test unloading CA's */
  1353. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1354. #ifdef PERSIST_CERT_CACHE
  1355. /* Verify no certs (result is less than cacheSz) */
  1356. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1357. #endif
  1358. /* load ca cert again */
  1359. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1360. WOLFSSL_SUCCESS);
  1361. /* Test getting CERT_MANAGER */
  1362. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1363. /* Test unloading CA's using CM */
  1364. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1365. #ifdef PERSIST_CERT_CACHE
  1366. /* Verify no certs (result is less than cacheSz) */
  1367. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1368. #endif
  1369. #endif
  1370. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1371. /* Test loading CA certificates using a path */
  1372. #ifdef NO_RSA
  1373. /* failure here okay since certs in external directory are RSA */
  1374. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1375. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1376. #else
  1377. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1378. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1379. #endif
  1380. /* Test loading path with no files */
  1381. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1382. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1383. /* Test loading expired CA certificates */
  1384. #ifdef NO_RSA
  1385. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1386. load_expired_path,
  1387. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1388. WOLFSSL_SUCCESS);
  1389. #else
  1390. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1391. load_expired_path,
  1392. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1393. WOLFSSL_SUCCESS);
  1394. #endif
  1395. /* Test loading CA certificates and ignoring all errors */
  1396. #ifdef NO_RSA
  1397. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1398. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1399. #else
  1400. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1401. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1402. #endif
  1403. #endif
  1404. wolfSSL_CTX_free(ctx);
  1405. #endif
  1406. return EXPECT_RESULT();
  1407. }
  1408. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1409. {
  1410. int res = TEST_SKIPPED;
  1411. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1412. (!defined(NO_RSA) || defined(HAVE_ECC))
  1413. WOLFSSL_CTX* ctx;
  1414. byte dirValid = 0;
  1415. int ret = 0;
  1416. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1417. if (ctx == NULL) {
  1418. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1419. ret = -1;
  1420. }
  1421. if (ret == 0) {
  1422. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1423. dirValid = 1;
  1424. #else
  1425. word32 numDirs;
  1426. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1427. if (caDirs == NULL || numDirs == 0) {
  1428. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1429. ret = -1;
  1430. }
  1431. else {
  1432. ReadDirCtx dirCtx;
  1433. word32 i;
  1434. for (i = 0; i < numDirs; ++i) {
  1435. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1436. /* Directory isn't empty. */
  1437. dirValid = 1;
  1438. wc_ReadDirClose(&dirCtx);
  1439. break;
  1440. }
  1441. }
  1442. }
  1443. #endif
  1444. }
  1445. /*
  1446. * If the directory isn't empty, we should be able to load CA
  1447. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1448. * usable.
  1449. */
  1450. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1451. WOLFSSL_SUCCESS) {
  1452. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1453. ret = -1;
  1454. }
  1455. #ifdef OPENSSL_EXTRA
  1456. if (ret == 0 &&
  1457. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1458. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1459. ret = -1;
  1460. }
  1461. #endif /* OPENSSL_EXTRA */
  1462. wolfSSL_CTX_free(ctx);
  1463. res = TEST_RES_CHECK(ret == 0);
  1464. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1465. return res;
  1466. }
  1467. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1468. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1469. int file_type)
  1470. {
  1471. int ret;
  1472. WOLFSSL_CERT_MANAGER* cm;
  1473. cm = wolfSSL_CertManagerNew();
  1474. if (cm == NULL) {
  1475. fprintf(stderr, "test_cm_load_ca failed\n");
  1476. return -1;
  1477. }
  1478. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1479. wolfSSL_CertManagerFree(cm);
  1480. return ret;
  1481. }
  1482. static int test_cm_load_ca_file(const char* ca_cert_file)
  1483. {
  1484. int ret = 0;
  1485. byte* cert_buf = NULL;
  1486. size_t cert_sz = 0;
  1487. #if defined(WOLFSSL_PEM_TO_DER)
  1488. DerBuffer* pDer = NULL;
  1489. #endif
  1490. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1491. if (ret == 0) {
  1492. /* normal test */
  1493. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1494. if (ret == WOLFSSL_SUCCESS) {
  1495. /* test including null terminator in length */
  1496. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1497. if (tmp == NULL) {
  1498. ret = MEMORY_E;
  1499. }
  1500. else {
  1501. cert_buf = tmp;
  1502. cert_buf[cert_sz] = '\0';
  1503. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1504. WOLFSSL_FILETYPE_PEM);
  1505. }
  1506. }
  1507. #if defined(WOLFSSL_PEM_TO_DER)
  1508. if (ret == WOLFSSL_SUCCESS) {
  1509. /* test loading DER */
  1510. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1511. if (ret == 0 && pDer != NULL) {
  1512. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1513. WOLFSSL_FILETYPE_ASN1);
  1514. wc_FreeDer(&pDer);
  1515. }
  1516. }
  1517. #endif
  1518. }
  1519. free(cert_buf);
  1520. return ret;
  1521. }
  1522. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1523. int file_type, word32 flags)
  1524. {
  1525. int ret;
  1526. WOLFSSL_CERT_MANAGER* cm;
  1527. cm = wolfSSL_CertManagerNew();
  1528. if (cm == NULL) {
  1529. fprintf(stderr, "test_cm_load_ca failed\n");
  1530. return -1;
  1531. }
  1532. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1533. 0, flags);
  1534. wolfSSL_CertManagerFree(cm);
  1535. return ret;
  1536. }
  1537. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1538. {
  1539. int ret = 0;
  1540. byte* cert_buf = NULL;
  1541. size_t cert_sz = 0;
  1542. #if defined(WOLFSSL_PEM_TO_DER)
  1543. DerBuffer* pDer = NULL;
  1544. #endif
  1545. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1546. if (ret == 0) {
  1547. /* normal test */
  1548. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1549. WOLFSSL_FILETYPE_PEM, flags);
  1550. if (ret == WOLFSSL_SUCCESS) {
  1551. /* test including null terminator in length */
  1552. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1553. if (tmp == NULL) {
  1554. ret = MEMORY_E;
  1555. }
  1556. else {
  1557. cert_buf = tmp;
  1558. cert_buf[cert_sz] = '\0';
  1559. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1560. WOLFSSL_FILETYPE_PEM, flags);
  1561. }
  1562. }
  1563. #if defined(WOLFSSL_PEM_TO_DER)
  1564. if (ret == WOLFSSL_SUCCESS) {
  1565. /* test loading DER */
  1566. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1567. if (ret == 0 && pDer != NULL) {
  1568. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1569. WOLFSSL_FILETYPE_ASN1, flags);
  1570. wc_FreeDer(&pDer);
  1571. }
  1572. }
  1573. #endif
  1574. }
  1575. free(cert_buf);
  1576. return ret;
  1577. }
  1578. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1579. static int test_wolfSSL_CertManagerAPI(void)
  1580. {
  1581. EXPECT_DECLS;
  1582. #ifndef NO_CERTS
  1583. WOLFSSL_CERT_MANAGER* cm = NULL;
  1584. unsigned char c;
  1585. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1586. wolfSSL_CertManagerFree(NULL);
  1587. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1588. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1589. #ifdef WOLFSSL_TRUST_PEER_CERT
  1590. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1591. #endif
  1592. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1593. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1594. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1595. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1596. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1597. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1598. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1599. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1600. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1601. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1602. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1603. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1604. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1605. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1606. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1607. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1608. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1609. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1610. WOLFSSL_BAD_FILETYPE);
  1611. #endif
  1612. #if !defined(NO_FILESYSTEM)
  1613. {
  1614. const char* ca_cert = "./certs/ca-cert.pem";
  1615. const char* ca_cert_der = "./certs/ca-cert.der";
  1616. const char* ca_path = "./certs";
  1617. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1618. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1619. BAD_FUNC_ARG);
  1620. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1621. BAD_FUNC_ARG);
  1622. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1623. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1624. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1625. WOLFSSL_BAD_FILETYPE);
  1626. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1627. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1628. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  1629. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  1630. #endif
  1631. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  1632. WOLFSSL_FATAL_ERROR);
  1633. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  1634. WOLFSSL_FATAL_ERROR);
  1635. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  1636. WOLFSSL_FATAL_ERROR);
  1637. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  1638. WOLFSSL_FATAL_ERROR);
  1639. }
  1640. #endif
  1641. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  1642. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  1643. #elif !defined(HAVE_CRL)
  1644. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  1645. #endif
  1646. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  1647. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  1648. #ifdef HAVE_CRL
  1649. /* Test APIs when CRL is disabled. */
  1650. #ifdef HAVE_CRL_IO
  1651. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  1652. #endif
  1653. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  1654. sizeof_server_cert_der_2048), 1);
  1655. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  1656. #endif
  1657. /* OCSP */
  1658. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  1659. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  1660. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  1661. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  1662. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1663. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  1664. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  1665. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1666. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  1667. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1668. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  1669. #endif
  1670. #ifdef HAVE_OCSP
  1671. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  1672. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  1673. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  1674. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  1675. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  1676. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  1677. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  1678. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  1679. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1680. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  1681. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1682. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  1683. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  1684. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  1685. BAD_FUNC_ARG);
  1686. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  1687. BAD_FUNC_ARG);
  1688. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  1689. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  1690. BAD_FUNC_ARG);
  1691. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  1692. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  1693. /* Test APIs when OCSP is disabled. */
  1694. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  1695. NULL, NULL, NULL, NULL), 1);
  1696. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  1697. #endif
  1698. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  1699. wolfSSL_CertManagerFree(cm);
  1700. wolfSSL_CertManagerFree(cm);
  1701. cm = NULL;
  1702. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1703. #ifdef HAVE_OCSP
  1704. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  1705. WOLFSSL_OCSP_CHECKALL), 1);
  1706. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  1707. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  1708. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1709. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1710. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  1711. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  1712. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  1713. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  1714. #endif
  1715. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1716. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  1717. #endif
  1718. #ifdef WOLFSSL_TRUST_PEER_CERT
  1719. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  1720. #endif
  1721. wolfSSL_CertManagerFree(cm);
  1722. #endif
  1723. return EXPECT_RESULT();
  1724. }
  1725. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1726. {
  1727. EXPECT_DECLS;
  1728. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1729. const char* ca_cert = "./certs/ca-cert.pem";
  1730. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1731. int ret;
  1732. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  1733. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1734. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1735. #elif defined(NO_RSA)
  1736. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1737. #else
  1738. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1739. #endif
  1740. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  1741. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1742. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1743. #elif defined(NO_RSA)
  1744. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1745. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1746. !defined(NO_ASN_TIME)
  1747. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1748. #else
  1749. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1750. #endif
  1751. #endif
  1752. return EXPECT_RESULT();
  1753. }
  1754. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  1755. {
  1756. EXPECT_DECLS;
  1757. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1758. const char* ca_cert = "./certs/ca-cert.pem";
  1759. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1760. int ret;
  1761. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  1762. 1);
  1763. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1764. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1765. #elif defined(NO_RSA)
  1766. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1767. #else
  1768. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1769. #endif
  1770. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  1771. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  1772. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1773. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1774. #elif defined(NO_RSA)
  1775. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  1776. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1777. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1778. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1779. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  1780. #else
  1781. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  1782. #endif
  1783. #endif
  1784. return EXPECT_RESULT();
  1785. }
  1786. static int test_wolfSSL_CertManagerGetCerts(void)
  1787. {
  1788. EXPECT_DECLS;
  1789. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1790. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1791. defined(WOLFSSL_SIGNER_DER_CERT)
  1792. WOLFSSL_CERT_MANAGER* cm = NULL;
  1793. WOLFSSL_STACK* sk = NULL;
  1794. X509* x509 = NULL;
  1795. X509* cert1 = NULL;
  1796. FILE* file1 = NULL;
  1797. #ifdef DEBUG_WOLFSSL_VERBOSE
  1798. WOLFSSL_BIO* bio = NULL;
  1799. #endif
  1800. int i = 0;
  1801. int ret = 0;
  1802. const byte* der;
  1803. int derSz = 0;
  1804. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  1805. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1806. if (file1 != NULL) {
  1807. fclose(file1);
  1808. }
  1809. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  1810. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1811. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1812. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1813. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1814. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1815. * and full OpenSSL compatibility */
  1816. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1817. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  1818. #else
  1819. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1820. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  1821. #endif
  1822. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1823. "./certs/ca-cert.pem", NULL));
  1824. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1825. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  1826. ExpectNotNull(x509 = sk_X509_value(sk, i));
  1827. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1828. #ifdef DEBUG_WOLFSSL_VERBOSE
  1829. bio = BIO_new(wolfSSL_BIO_s_file());
  1830. if (bio != NULL) {
  1831. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  1832. X509_print(bio, x509);
  1833. BIO_free(bio);
  1834. }
  1835. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1836. }
  1837. wolfSSL_X509_free(cert1);
  1838. sk_X509_pop_free(sk, NULL);
  1839. wolfSSL_CertManagerFree(cm);
  1840. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1841. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1842. defined(WOLFSSL_SIGNER_DER_CERT) */
  1843. return EXPECT_RESULT();
  1844. }
  1845. static int test_wolfSSL_CertManagerSetVerify(void)
  1846. {
  1847. EXPECT_DECLS;
  1848. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1849. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1850. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1851. WOLFSSL_CERT_MANAGER* cm = NULL;
  1852. int tmp = myVerifyAction;
  1853. const char* ca_cert = "./certs/ca-cert.pem";
  1854. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1855. wolfSSL_CertManagerSetVerify(NULL, NULL);
  1856. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  1857. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1858. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1859. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1860. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  1861. #else
  1862. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  1863. WOLFSSL_SUCCESS);
  1864. #endif
  1865. /* Use the test CB that always accepts certs */
  1866. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1867. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  1868. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1869. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1870. {
  1871. const char* verifyCert = "./certs/server-cert.der";
  1872. /* Use the test CB that always fails certs */
  1873. myVerifyAction = VERIFY_FORCE_FAIL;
  1874. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  1875. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  1876. }
  1877. #endif
  1878. wolfSSL_CertManagerFree(cm);
  1879. myVerifyAction = tmp;
  1880. #endif
  1881. return EXPECT_RESULT();
  1882. }
  1883. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1884. defined(DEBUG_UNIT_TEST_CERTS)
  1885. /* Used when debugging name constraint tests. Not static to allow use in
  1886. * multiple locations with complex define guards. */
  1887. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1888. {
  1889. BIO* out = BIO_new_file(fileName, "wb");
  1890. if (out != NULL) {
  1891. PEM_write_bio_X509(out, x509);
  1892. BIO_free(out);
  1893. }
  1894. }
  1895. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1896. {
  1897. BIO* out = BIO_new_file(fileName, "wb");
  1898. if (out != NULL) {
  1899. BIO_write(out, der, derSz);
  1900. BIO_free(out);
  1901. }
  1902. }
  1903. #else
  1904. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  1905. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  1906. #endif
  1907. static int test_wolfSSL_CertManagerNameConstraint(void)
  1908. {
  1909. EXPECT_DECLS;
  1910. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1911. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1912. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1913. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1914. !defined(NO_SHA256)
  1915. WOLFSSL_CERT_MANAGER* cm = NULL;
  1916. WOLFSSL_EVP_PKEY *priv = NULL;
  1917. WOLFSSL_X509_NAME* name = NULL;
  1918. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1919. const char* server_cert = "./certs/test/server-goodcn.pem";
  1920. int i = 0;
  1921. static const byte extNameConsOid[] = {85, 29, 30};
  1922. RsaKey key;
  1923. WC_RNG rng;
  1924. byte *der = NULL;
  1925. int derSz = 0;
  1926. word32 idx = 0;
  1927. byte *pt;
  1928. WOLFSSL_X509 *x509 = NULL;
  1929. WOLFSSL_X509 *ca = NULL;
  1930. wc_InitRng(&rng);
  1931. /* load in CA private key for signing */
  1932. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1933. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1934. sizeof_server_key_der_2048), 0);
  1935. /* get ca certificate then alter it */
  1936. ExpectNotNull(der =
  1937. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1938. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1939. WOLFSSL_FILETYPE_ASN1));
  1940. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1941. if (EXPECT_SUCCESS() && (der != NULL)) {
  1942. XMEMCPY(der, pt, derSz);
  1943. /* find the name constraint extension and alter it */
  1944. pt = der;
  1945. for (i = 0; i < derSz - 3; i++) {
  1946. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1947. pt += 3;
  1948. break;
  1949. }
  1950. pt++;
  1951. }
  1952. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1953. /* go to the length value and set it to 0 */
  1954. while (i < derSz && *pt != 0x81) {
  1955. pt++;
  1956. i++;
  1957. }
  1958. ExpectIntNE(i, derSz); /* did not place to alter */
  1959. pt++;
  1960. *pt = 0x00;
  1961. }
  1962. /* resign the altered certificate */
  1963. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1964. FOURK_BUF, &key, NULL, &rng)), 0);
  1965. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1966. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1967. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1968. wolfSSL_CertManagerFree(cm);
  1969. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1970. wolfSSL_X509_free(x509);
  1971. wc_FreeRsaKey(&key);
  1972. wc_FreeRng(&rng);
  1973. /* add email alt name to satisfy constraint */
  1974. pt = (byte*)server_key_der_2048;
  1975. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1976. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1977. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1978. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1979. WOLFSSL_FILETYPE_ASN1));
  1980. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1981. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1982. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1983. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1984. /* Good cert test with proper alt email name */
  1985. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1986. WOLFSSL_FILETYPE_PEM));
  1987. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1988. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1989. name = NULL;
  1990. ExpectNotNull(name = X509_NAME_new());
  1991. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1992. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1993. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1994. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1995. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1996. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1997. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1998. X509_NAME_free(name);
  1999. name = NULL;
  2000. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2001. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2002. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2003. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2004. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2005. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2006. wolfSSL_X509_free(x509);
  2007. x509 = NULL;
  2008. /* Cert with bad alt name list */
  2009. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2010. WOLFSSL_FILETYPE_PEM));
  2011. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2012. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2013. name = NULL;
  2014. ExpectNotNull(name = X509_NAME_new());
  2015. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2016. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2017. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2018. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2019. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2020. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2021. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2022. X509_NAME_free(name);
  2023. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2024. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2025. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2026. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2027. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2028. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2029. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2030. wolfSSL_CertManagerFree(cm);
  2031. wolfSSL_X509_free(x509);
  2032. wolfSSL_X509_free(ca);
  2033. wolfSSL_EVP_PKEY_free(priv);
  2034. #endif
  2035. return EXPECT_RESULT();
  2036. }
  2037. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2038. {
  2039. EXPECT_DECLS;
  2040. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2041. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2042. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2043. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2044. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2045. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2046. const char* server_cert = "./certs/server-cert.pem";
  2047. WOLFSSL_CERT_MANAGER* cm = NULL;
  2048. WOLFSSL_X509 *x509 = NULL;
  2049. WOLFSSL_X509 *ca = NULL;
  2050. const unsigned char *der = NULL;
  2051. const unsigned char *pt;
  2052. WOLFSSL_EVP_PKEY *priv = NULL;
  2053. WOLFSSL_X509_NAME* name = NULL;
  2054. int derSz = 0;
  2055. /* C=US*/
  2056. char altName[] = {
  2057. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2058. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2059. };
  2060. /* C=ID */
  2061. char altNameFail[] = {
  2062. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2063. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2064. };
  2065. /* C=US ST=California*/
  2066. char altNameExc[] = {
  2067. 0x30, 0x22,
  2068. 0x31, 0x0B,
  2069. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2070. 0x31, 0x13,
  2071. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2072. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2073. };
  2074. /* load in CA private key for signing */
  2075. pt = ca_key_der_2048;
  2076. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2077. sizeof_ca_key_der_2048));
  2078. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2079. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2080. WOLFSSL_FILETYPE_ASN1));
  2081. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2082. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2083. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2084. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2085. WOLFSSL_FILETYPE_PEM));
  2086. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2087. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2088. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2089. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2090. #else
  2091. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2092. #endif
  2093. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2094. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2095. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2096. /* add in matching DIR alt name and resign */
  2097. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2098. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2099. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2100. #else
  2101. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2102. #endif
  2103. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2104. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2105. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2106. wolfSSL_X509_free(x509);
  2107. x509 = NULL;
  2108. /* check verify fail */
  2109. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2110. WOLFSSL_FILETYPE_PEM));
  2111. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2112. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2113. /* add in miss matching DIR alt name and resign */
  2114. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2115. ASN_DIR_TYPE);
  2116. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2117. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2118. #else
  2119. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2120. #endif
  2121. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2122. #ifndef WOLFSSL_NO_ASN_STRICT
  2123. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2124. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2125. #else
  2126. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2127. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2128. #endif
  2129. /* check that it still fails if one bad altname and one good altname is in
  2130. * the certificate */
  2131. wolfSSL_X509_free(x509);
  2132. x509 = NULL;
  2133. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2134. WOLFSSL_FILETYPE_PEM));
  2135. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2136. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2137. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2138. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2139. ASN_DIR_TYPE);
  2140. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2141. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2142. #else
  2143. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2144. #endif
  2145. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2146. #ifndef WOLFSSL_NO_ASN_STRICT
  2147. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2148. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2149. #else
  2150. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2151. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2152. #endif
  2153. /* check it fails with switching position of bad altname */
  2154. wolfSSL_X509_free(x509);
  2155. x509 = NULL;
  2156. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2157. WOLFSSL_FILETYPE_PEM));
  2158. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2159. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2160. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2161. ASN_DIR_TYPE);
  2162. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2163. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2164. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2165. #else
  2166. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2167. #endif
  2168. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2169. #ifndef WOLFSSL_NO_ASN_STRICT
  2170. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2171. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2172. #else
  2173. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2174. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2175. #endif
  2176. wolfSSL_CertManagerFree(cm);
  2177. wolfSSL_X509_free(x509);
  2178. x509 = NULL;
  2179. wolfSSL_X509_free(ca);
  2180. ca = NULL;
  2181. /* now test with excluded name constraint */
  2182. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2183. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2184. WOLFSSL_FILETYPE_ASN1));
  2185. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2186. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2187. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2188. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2189. WOLFSSL_FILETYPE_PEM));
  2190. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2191. ASN_DIR_TYPE);
  2192. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2193. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2194. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2195. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2196. #else
  2197. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2198. #endif
  2199. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2200. #ifndef WOLFSSL_NO_ASN_STRICT
  2201. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2202. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2203. #else
  2204. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2205. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2206. #endif
  2207. wolfSSL_CertManagerFree(cm);
  2208. wolfSSL_X509_free(x509);
  2209. wolfSSL_X509_free(ca);
  2210. wolfSSL_EVP_PKEY_free(priv);
  2211. #endif
  2212. return EXPECT_RESULT();
  2213. }
  2214. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2215. {
  2216. EXPECT_DECLS;
  2217. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2218. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2219. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2220. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2221. !defined(NO_SHA256)
  2222. WOLFSSL_CERT_MANAGER* cm = NULL;
  2223. WOLFSSL_EVP_PKEY *priv = NULL;
  2224. WOLFSSL_X509_NAME* name = NULL;
  2225. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2226. const char* server_cert = "./certs/test/server-goodcn.pem";
  2227. byte *der = NULL;
  2228. int derSz = 0;
  2229. byte *pt;
  2230. WOLFSSL_X509 *x509 = NULL;
  2231. WOLFSSL_X509 *ca = NULL;
  2232. pt = (byte*)server_key_der_2048;
  2233. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2234. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2235. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2236. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2237. WOLFSSL_FILETYPE_ASN1));
  2238. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2239. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2240. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2241. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2242. /* check satisfying .wolfssl.com constraint passes */
  2243. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2244. WOLFSSL_FILETYPE_PEM));
  2245. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2246. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2247. name = NULL;
  2248. ExpectNotNull(name = X509_NAME_new());
  2249. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2250. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2251. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2252. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2253. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2254. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2255. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2256. X509_NAME_free(name);
  2257. name = NULL;
  2258. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2259. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2260. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2261. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2262. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2263. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2264. wolfSSL_X509_free(x509);
  2265. x509 = NULL;
  2266. /* check satisfying .random.com constraint passes */
  2267. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2268. WOLFSSL_FILETYPE_PEM));
  2269. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2270. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2271. name = NULL;
  2272. ExpectNotNull(name = X509_NAME_new());
  2273. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2274. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2275. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2276. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2277. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2278. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2279. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2280. X509_NAME_free(name);
  2281. name = NULL;
  2282. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2283. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2284. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2285. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2286. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2287. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2288. wolfSSL_X509_free(x509);
  2289. x509 = NULL;
  2290. /* check fail case when neither constraint is matched */
  2291. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2292. WOLFSSL_FILETYPE_PEM));
  2293. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2294. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2295. name = NULL;
  2296. ExpectNotNull(name = X509_NAME_new());
  2297. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2298. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2299. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2300. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2301. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2302. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2303. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2304. X509_NAME_free(name);
  2305. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2306. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2307. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2308. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2309. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2310. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2311. wolfSSL_CertManagerFree(cm);
  2312. wolfSSL_X509_free(x509);
  2313. wolfSSL_X509_free(ca);
  2314. wolfSSL_EVP_PKEY_free(priv);
  2315. #endif
  2316. return EXPECT_RESULT();
  2317. }
  2318. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2319. {
  2320. EXPECT_DECLS;
  2321. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2322. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2323. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2324. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2325. !defined(NO_SHA256)
  2326. WOLFSSL_CERT_MANAGER* cm = NULL;
  2327. WOLFSSL_EVP_PKEY *priv = NULL;
  2328. WOLFSSL_X509_NAME* name = NULL;
  2329. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2330. const char* server_cert = "./certs/test/server-goodcn.pem";
  2331. byte *der = NULL;
  2332. int derSz;
  2333. byte *pt;
  2334. WOLFSSL_X509 *x509 = NULL;
  2335. WOLFSSL_X509 *ca = NULL;
  2336. pt = (byte*)server_key_der_2048;
  2337. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2338. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2339. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2340. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2341. WOLFSSL_FILETYPE_ASN1));
  2342. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2343. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2344. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2345. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2346. /* check satisfying wolfssl.com constraint passes */
  2347. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2348. WOLFSSL_FILETYPE_PEM));
  2349. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2350. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2351. name = NULL;
  2352. ExpectNotNull(name = X509_NAME_new());
  2353. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2354. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2355. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2356. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2357. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2358. X509_NAME_free(name);
  2359. name = NULL;
  2360. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2361. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2362. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2363. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2364. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2365. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2366. wolfSSL_X509_free(x509);
  2367. x509 = NULL;
  2368. /* check satisfying example.com constraint passes */
  2369. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2370. WOLFSSL_FILETYPE_PEM));
  2371. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2372. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2373. name = NULL;
  2374. ExpectNotNull(name = X509_NAME_new());
  2375. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2376. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2377. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2378. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2379. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2380. X509_NAME_free(name);
  2381. name = NULL;
  2382. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2383. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2384. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2385. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2386. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2387. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2388. wolfSSL_X509_free(x509);
  2389. x509 = NULL;
  2390. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2391. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2392. WOLFSSL_FILETYPE_PEM));
  2393. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2394. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2395. name = NULL;
  2396. ExpectNotNull(name = X509_NAME_new());
  2397. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2398. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2399. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2400. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2401. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2402. X509_NAME_free(name);
  2403. name = NULL;
  2404. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2405. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2406. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2407. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2408. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2409. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2410. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2411. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2412. wolfSSL_X509_free(x509);
  2413. x509 = NULL;
  2414. /* check fail when one DNS in the list is bad */
  2415. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2416. WOLFSSL_FILETYPE_PEM));
  2417. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2418. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2419. name = NULL;
  2420. ExpectNotNull(name = X509_NAME_new());
  2421. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2422. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2423. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2424. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2425. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2426. X509_NAME_free(name);
  2427. name = NULL;
  2428. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2429. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2430. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2431. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2432. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2433. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2434. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2435. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2436. wolfSSL_X509_free(x509);
  2437. x509 = NULL;
  2438. /* check fail case when neither constraint is matched */
  2439. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2440. WOLFSSL_FILETYPE_PEM));
  2441. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2442. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2443. name = NULL;
  2444. ExpectNotNull(name = X509_NAME_new());
  2445. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2446. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2447. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2448. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2449. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2450. X509_NAME_free(name);
  2451. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2452. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2453. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2454. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2455. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2456. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2457. wolfSSL_CertManagerFree(cm);
  2458. wolfSSL_X509_free(x509);
  2459. wolfSSL_X509_free(ca);
  2460. wolfSSL_EVP_PKEY_free(priv);
  2461. #endif
  2462. return EXPECT_RESULT();
  2463. }
  2464. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2465. {
  2466. EXPECT_DECLS;
  2467. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2468. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2469. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2470. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2471. !defined(NO_SHA256)
  2472. WOLFSSL_CERT_MANAGER* cm = NULL;
  2473. WOLFSSL_EVP_PKEY *priv = NULL;
  2474. WOLFSSL_X509_NAME* name = NULL;
  2475. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2476. const char* server_cert = "./certs/test/server-goodcn.pem";
  2477. byte *der = NULL;
  2478. int derSz;
  2479. byte *pt;
  2480. WOLFSSL_X509 *x509 = NULL;
  2481. WOLFSSL_X509 *ca = NULL;
  2482. pt = (byte*)server_key_der_2048;
  2483. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2484. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2485. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2486. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2487. WOLFSSL_FILETYPE_ASN1));
  2488. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2489. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2490. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2491. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2492. /* check satisfying wolfssl.com constraint passes */
  2493. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2494. WOLFSSL_FILETYPE_PEM));
  2495. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2496. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2497. name = NULL;
  2498. ExpectNotNull(name = X509_NAME_new());
  2499. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2500. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2501. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2502. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2503. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2504. X509_NAME_free(name);
  2505. name = NULL;
  2506. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2507. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2508. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2509. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2510. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2511. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2512. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2513. wolfSSL_X509_free(x509);
  2514. x509 = NULL;
  2515. /* fail with DNS check because of common name */
  2516. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2517. WOLFSSL_FILETYPE_PEM));
  2518. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2519. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2520. name = NULL;
  2521. ExpectNotNull(name = X509_NAME_new());
  2522. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2523. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2524. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2525. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2526. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2527. X509_NAME_free(name);
  2528. name = NULL;
  2529. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2530. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2531. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2532. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2533. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2534. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2535. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2536. wolfSSL_X509_free(x509);
  2537. x509 = NULL;
  2538. /* fail on permitted DNS name constraint */
  2539. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2540. WOLFSSL_FILETYPE_PEM));
  2541. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2542. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2543. name = NULL;
  2544. ExpectNotNull(name = X509_NAME_new());
  2545. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2546. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2547. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2548. X509_NAME_free(name);
  2549. name = NULL;
  2550. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2551. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2552. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2553. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2554. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2555. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2556. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2557. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2558. wolfSSL_X509_free(x509);
  2559. x509 = NULL;
  2560. /* fail on permitted email name constraint */
  2561. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2562. WOLFSSL_FILETYPE_PEM));
  2563. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2564. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2565. name = NULL;
  2566. ExpectNotNull(name = X509_NAME_new());
  2567. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2568. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2569. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2570. X509_NAME_free(name);
  2571. name = NULL;
  2572. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2573. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2574. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2575. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2576. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2577. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2578. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2579. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2580. wolfSSL_X509_free(x509);
  2581. x509 = NULL;
  2582. /* success with empty email name */
  2583. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2584. WOLFSSL_FILETYPE_PEM));
  2585. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2586. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2587. name = NULL;
  2588. ExpectNotNull(name = X509_NAME_new());
  2589. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2590. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2591. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2592. X509_NAME_free(name);
  2593. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2594. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2595. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2596. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2597. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2598. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2599. wolfSSL_X509_free(x509);
  2600. wolfSSL_CertManagerFree(cm);
  2601. wolfSSL_X509_free(ca);
  2602. wolfSSL_EVP_PKEY_free(priv);
  2603. #endif
  2604. return EXPECT_RESULT();
  2605. }
  2606. static int test_wolfSSL_CertManagerCRL(void)
  2607. {
  2608. EXPECT_DECLS;
  2609. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2610. !defined(NO_RSA)
  2611. const char* ca_cert = "./certs/ca-cert.pem";
  2612. const char* crl1 = "./certs/crl/crl.pem";
  2613. const char* crl2 = "./certs/crl/crl2.pem";
  2614. const unsigned char crl_buff[] = {
  2615. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2616. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2617. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2618. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2619. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2620. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2621. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2622. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  2623. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  2624. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  2625. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  2626. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  2627. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  2628. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  2629. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2630. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  2631. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  2632. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  2633. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2634. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  2635. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2636. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  2637. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  2638. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  2639. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  2640. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  2641. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  2642. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  2643. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  2644. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  2645. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  2646. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  2647. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  2648. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  2649. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  2650. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  2651. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  2652. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  2653. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  2654. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  2655. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  2656. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  2657. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  2658. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  2659. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  2660. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  2661. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  2662. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  2663. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  2664. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  2665. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  2666. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  2667. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  2668. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  2669. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  2670. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  2671. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  2672. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  2673. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  2674. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  2675. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  2676. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  2677. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  2678. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  2679. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  2680. };
  2681. WOLFSSL_CERT_MANAGER* cm = NULL;
  2682. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2683. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  2684. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2685. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  2686. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  2687. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  2688. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  2689. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  2690. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  2691. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  2692. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  2693. BAD_FUNC_ARG);
  2694. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  2695. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  2696. BAD_FUNC_ARG);
  2697. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  2698. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  2699. BAD_FUNC_ARG);
  2700. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2701. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  2702. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  2703. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  2704. #ifdef HAVE_CRL_IO
  2705. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  2706. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2707. #endif
  2708. #ifndef NO_FILESYSTEM
  2709. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  2710. 0), BAD_FUNC_ARG);
  2711. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  2712. 0), BAD_FUNC_ARG);
  2713. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2714. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  2715. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  2716. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2717. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2718. BAD_FUNC_ARG);
  2719. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  2720. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  2721. ASN_PARSE_E);
  2722. #endif
  2723. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  2724. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2725. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  2726. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2727. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  2728. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2729. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  2730. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2731. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  2732. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2733. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  2734. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2735. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  2736. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2737. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  2738. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2739. ExpectIntEQ(WOLFSSL_SUCCESS,
  2740. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2741. ExpectIntEQ(WOLFSSL_SUCCESS,
  2742. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2743. ExpectIntEQ(WOLFSSL_SUCCESS,
  2744. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2745. wolfSSL_CertManagerFreeCRL(cm);
  2746. ExpectIntEQ(WOLFSSL_SUCCESS,
  2747. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2748. ExpectIntEQ(WOLFSSL_SUCCESS,
  2749. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2750. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2751. sizeof_server_cert_der_2048), CRL_MISSING);
  2752. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  2753. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  2754. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  2755. WOLFSSL_FILETYPE_ASN1), 1);
  2756. wolfSSL_CertManagerFree(cm);
  2757. #endif
  2758. return EXPECT_RESULT();
  2759. }
  2760. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  2761. {
  2762. EXPECT_DECLS;
  2763. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  2764. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  2765. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  2766. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  2767. defined(HAVE_LIGHTY)
  2768. WOLFSSL_CERT_MANAGER* cm = NULL;
  2769. /* Raw OCSP response bytes captured using the following setup:
  2770. * - Run responder with
  2771. * openssl ocsp -port 9999 -ndays 9999
  2772. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  2773. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  2774. * -rkey certs/ocsp/ocsp-responder-key.pem
  2775. * -CA certs/ocsp/intermediate1-ca-cert.pem
  2776. * - Run client with
  2777. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  2778. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  2779. * -cert certs/ocsp/server1-cert.pem
  2780. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  2781. * - Copy raw response from Wireshark.
  2782. */
  2783. byte response[] = {
  2784. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  2785. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  2786. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  2787. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  2788. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  2789. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  2790. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  2791. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  2792. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  2793. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  2794. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  2795. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  2796. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  2797. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  2798. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  2799. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  2800. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  2801. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  2802. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  2803. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  2804. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  2805. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  2806. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  2807. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  2808. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  2809. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  2810. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  2811. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  2812. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  2813. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  2814. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  2815. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  2816. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  2817. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  2818. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  2819. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  2820. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  2821. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  2822. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  2823. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  2824. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  2825. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2826. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2827. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  2828. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  2829. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  2830. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  2831. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  2832. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  2833. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  2834. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  2835. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  2836. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  2837. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  2838. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  2839. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  2840. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  2841. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  2842. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  2843. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  2844. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  2845. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  2846. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  2847. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  2848. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  2849. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  2850. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  2851. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  2852. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  2853. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  2854. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  2855. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  2856. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  2857. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  2858. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  2859. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  2860. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  2861. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  2862. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  2863. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  2864. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  2865. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  2866. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  2867. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  2868. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  2869. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  2870. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  2871. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  2872. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  2873. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  2874. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  2875. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  2876. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  2877. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  2878. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  2879. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  2880. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  2881. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  2882. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  2883. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  2884. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  2885. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  2886. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  2887. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  2888. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  2889. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  2890. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  2891. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  2892. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  2893. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  2894. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  2895. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  2896. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  2897. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  2898. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  2899. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  2900. 0x59, 0x59, 0xa0, 0x07
  2901. };
  2902. OcspEntry entry[1];
  2903. CertStatus status[1];
  2904. OcspRequest* request = NULL;
  2905. #ifndef NO_FILESYSTEM
  2906. const char* ca_cert = "./certs/ca-cert.pem";
  2907. #endif
  2908. byte serial[] = {0x05};
  2909. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  2910. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  2911. XMEMSET(entry, 0, sizeof(OcspEntry));
  2912. XMEMSET(status, 0, sizeof(CertStatus));
  2913. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  2914. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  2915. DYNAMIC_TYPE_OCSP_REQUEST));
  2916. if ((request != NULL) && (request->serial != NULL)) {
  2917. request->serialSz = sizeof(serial);
  2918. XMEMCPY(request->serial, serial, sizeof(serial));
  2919. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  2920. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  2921. }
  2922. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2923. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  2924. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  2925. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  2926. /* Response should be valid. */
  2927. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  2928. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  2929. /* Flip a byte in the request serial number, response should be invalid
  2930. * now. */
  2931. if ((request != NULL) && (request->serial != NULL))
  2932. request->serial[0] ^= request->serial[0];
  2933. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  2934. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  2935. #ifndef NO_FILESYSTEM
  2936. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  2937. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  2938. ExpectIntEQ(WOLFSSL_SUCCESS,
  2939. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2940. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  2941. sizeof(server_cert_der_2048)), 1);
  2942. #endif
  2943. wolfSSL_OCSP_REQUEST_free(request);
  2944. wolfSSL_CertManagerFree(cm);
  2945. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  2946. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  2947. #endif /* HAVE_OCSP */
  2948. return EXPECT_RESULT();
  2949. }
  2950. static int test_wolfSSL_CheckOCSPResponse(void)
  2951. {
  2952. EXPECT_DECLS;
  2953. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  2954. const char* responseFile = "./certs/ocsp/test-response.der";
  2955. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  2956. const char* responseNoInternFile =
  2957. "./certs/ocsp/test-response-nointern.der";
  2958. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  2959. OcspResponse* res = NULL;
  2960. byte data[4096];
  2961. const unsigned char* pt;
  2962. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  2963. * gcc sanitizer with --enable-heapmath.
  2964. */
  2965. XFILE f = XBADFILE;
  2966. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  2967. WOLFSSL_X509_STORE* st = NULL;
  2968. WOLFSSL_X509* issuer = NULL;
  2969. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  2970. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  2971. if (f != XBADFILE) {
  2972. XFCLOSE(f);
  2973. f = XBADFILE;
  2974. }
  2975. pt = data;
  2976. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  2977. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  2978. SSL_FILETYPE_PEM));
  2979. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  2980. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  2981. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  2982. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  2983. wolfSSL_OCSP_BASICRESP_free(bs);
  2984. bs = NULL;
  2985. wolfSSL_OCSP_RESPONSE_free(res);
  2986. res = NULL;
  2987. wolfSSL_X509_STORE_free(st);
  2988. st = NULL;
  2989. wolfSSL_X509_free(issuer);
  2990. issuer = NULL;
  2991. /* check loading a response with optional certs */
  2992. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  2993. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  2994. if (f != XBADFILE)
  2995. XFCLOSE(f);
  2996. f = XBADFILE;
  2997. pt = data;
  2998. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  2999. wolfSSL_OCSP_RESPONSE_free(res);
  3000. res = NULL;
  3001. /* check loading a response with multiple certs */
  3002. {
  3003. WOLFSSL_CERT_MANAGER* cm = NULL;
  3004. OcspEntry *entry = NULL;
  3005. CertStatus* status = NULL;
  3006. OcspRequest* request = NULL;
  3007. byte serial1[] = {0x01};
  3008. byte serial[] = {0x02};
  3009. byte issuerHash[] = {
  3010. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3011. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3012. 0x37, 0x15, 0x8A, 0x88
  3013. };
  3014. byte issuerKeyHash[] = {
  3015. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3016. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3017. 0x7E, 0x72, 0x15, 0x21
  3018. };
  3019. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3020. DYNAMIC_TYPE_OPENSSL));
  3021. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3022. DYNAMIC_TYPE_OPENSSL));
  3023. if (entry != NULL)
  3024. XMEMSET(entry, 0, sizeof(OcspEntry));
  3025. if (status != NULL)
  3026. XMEMSET(status, 0, sizeof(CertStatus));
  3027. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3028. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3029. DYNAMIC_TYPE_OCSP_REQUEST));
  3030. if (request != NULL && request->serial != NULL) {
  3031. request->serialSz = sizeof(serial);
  3032. XMEMCPY(request->serial, serial, sizeof(serial));
  3033. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3034. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3035. sizeof(issuerKeyHash));
  3036. }
  3037. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3038. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3039. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3040. WOLFSSL_SUCCESS);
  3041. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3042. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3043. if (f != XBADFILE)
  3044. XFCLOSE(f);
  3045. f = XBADFILE;
  3046. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3047. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3048. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3049. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3050. ExpectNotNull(entry->status);
  3051. if (request != NULL && request->serial != NULL)
  3052. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3053. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3054. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3055. /* store both status's in the entry to check that "next" is not
  3056. * overwritten */
  3057. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3058. status->next = entry->status;
  3059. entry->status = status;
  3060. }
  3061. if (request != NULL && request->serial != NULL)
  3062. XMEMCPY(request->serial, serial, sizeof(serial));
  3063. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3064. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3065. ExpectNotNull(entry->status->next);
  3066. /* compare the status found */
  3067. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3068. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3069. status->serialSz), 0);
  3070. if (status != NULL && entry != NULL && entry->status != status) {
  3071. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3072. }
  3073. wolfSSL_OCSP_CERTID_free(entry);
  3074. wolfSSL_OCSP_REQUEST_free(request);
  3075. wolfSSL_CertManagerFree(cm);
  3076. }
  3077. #if defined(WC_RSA_PSS)
  3078. {
  3079. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3080. /* check loading a response with RSA-PSS signature */
  3081. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3082. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3083. if (f != XBADFILE)
  3084. XFCLOSE(f);
  3085. pt = data;
  3086. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3087. /* try to verify the response */
  3088. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3089. SSL_FILETYPE_PEM));
  3090. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3091. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3092. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3093. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3094. WOLFSSL_SUCCESS);
  3095. wolfSSL_OCSP_BASICRESP_free(bs);
  3096. wolfSSL_OCSP_RESPONSE_free(res);
  3097. wolfSSL_X509_STORE_free(st);
  3098. wolfSSL_X509_free(issuer);
  3099. }
  3100. #endif
  3101. #endif /* HAVE_OCSP */
  3102. return EXPECT_RESULT();
  3103. }
  3104. static int test_wolfSSL_FPKI(void)
  3105. {
  3106. EXPECT_DECLS;
  3107. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3108. XFILE f = XBADFILE;
  3109. const char* fpkiCert = "./certs/fpki-cert.der";
  3110. DecodedCert cert;
  3111. byte buf[4096];
  3112. byte* uuid = NULL;
  3113. byte* fascn = NULL;
  3114. word32 fascnSz;
  3115. word32 uuidSz;
  3116. int bytes = 0;
  3117. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3118. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3119. if (f != XBADFILE)
  3120. XFCLOSE(f);
  3121. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3122. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3123. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3124. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3125. DYNAMIC_TYPE_TMP_BUFFER));
  3126. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3127. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3128. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3129. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3130. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3131. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3132. wc_FreeDecodedCert(&cert);
  3133. #endif
  3134. return EXPECT_RESULT();
  3135. }
  3136. /* use RID in confuncture with other names to test parsing of unknown other
  3137. * names */
  3138. static int test_wolfSSL_OtherName(void)
  3139. {
  3140. EXPECT_DECLS;
  3141. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3142. XFILE f = XBADFILE;
  3143. const char* ridCert = "./certs/rid-cert.der";
  3144. DecodedCert cert;
  3145. byte buf[4096];
  3146. int bytes = 0;
  3147. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3148. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3149. if (f != XBADFILE)
  3150. XFCLOSE(f);
  3151. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3152. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3153. wc_FreeDecodedCert(&cert);
  3154. #endif
  3155. return EXPECT_RESULT();
  3156. }
  3157. static int test_wolfSSL_CertRsaPss(void)
  3158. {
  3159. EXPECT_DECLS;
  3160. /* FIPS v2 and below don't support long salts. */
  3161. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3162. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3163. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3164. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3165. XFILE f = XBADFILE;
  3166. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3167. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3168. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3169. RSA_MAX_SIZE >= 3072
  3170. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3171. #endif
  3172. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3173. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3174. #endif
  3175. DecodedCert cert;
  3176. byte buf[4096];
  3177. int bytes = 0;
  3178. WOLFSSL_CERT_MANAGER* cm = NULL;
  3179. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3180. ExpectIntEQ(WOLFSSL_SUCCESS,
  3181. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3182. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3183. ExpectIntEQ(WOLFSSL_SUCCESS,
  3184. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3185. #endif
  3186. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3187. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3188. if (f != XBADFILE) {
  3189. XFCLOSE(f);
  3190. f = XBADFILE;
  3191. }
  3192. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3193. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3194. wc_FreeDecodedCert(&cert);
  3195. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3196. RSA_MAX_SIZE >= 3072
  3197. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3198. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3199. if (f != XBADFILE)
  3200. XFCLOSE(f);
  3201. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3202. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3203. wc_FreeDecodedCert(&cert);
  3204. #endif
  3205. wolfSSL_CertManagerFree(cm);
  3206. #endif
  3207. return EXPECT_RESULT();
  3208. }
  3209. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3210. {
  3211. EXPECT_DECLS;
  3212. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3213. !defined(NO_WOLFSSL_CLIENT)
  3214. WOLFSSL_CTX* ctx = NULL;
  3215. const char* ca_cert = "./certs/ca-cert.pem";
  3216. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3217. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3218. /* test good CA */
  3219. ExpectTrue(WOLFSSL_SUCCESS ==
  3220. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3221. WOLFSSL_LOAD_FLAG_NONE));
  3222. /* test expired CA */
  3223. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3224. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3225. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3226. #else
  3227. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3228. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3229. #endif
  3230. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3231. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3232. wolfSSL_CTX_free(ctx);
  3233. #endif
  3234. return EXPECT_RESULT();
  3235. }
  3236. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3237. {
  3238. EXPECT_DECLS;
  3239. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3240. defined(USE_CERT_BUFFERS_2048)
  3241. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3242. WOLFSSL_CTX* ctx;
  3243. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3244. byte ca_expired_cert[TWOK_BUF];
  3245. word32 sizeof_ca_expired_cert = 0;
  3246. XFILE fp = XBADFILE;
  3247. #ifndef NO_WOLFSSL_CLIENT
  3248. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3249. #else
  3250. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3251. #endif
  3252. ExpectNotNull(ctx);
  3253. /* test good CA */
  3254. ExpectTrue(WOLFSSL_SUCCESS ==
  3255. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3256. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3257. WOLFSSL_LOAD_FLAG_NONE));
  3258. /* load expired CA */
  3259. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3260. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3261. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3262. sizeof(ca_expired_cert), fp), 0);
  3263. if (fp != XBADFILE)
  3264. XFCLOSE(fp);
  3265. /* test expired CA failure */
  3266. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3267. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3268. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3269. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3270. #else
  3271. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3272. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3273. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3274. #endif
  3275. /* test expired CA success */
  3276. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3277. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3278. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3279. wolfSSL_CTX_free(ctx);
  3280. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3281. #endif
  3282. return EXPECT_RESULT();
  3283. }
  3284. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3285. {
  3286. EXPECT_DECLS;
  3287. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3288. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3289. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3290. WOLFSSL_CTX* ctx = NULL;
  3291. #ifndef NO_WOLFSSL_CLIENT
  3292. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3293. #else
  3294. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3295. #endif
  3296. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3297. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3298. WOLFSSL_FILETYPE_ASN1));
  3299. wolfSSL_CTX_free(ctx);
  3300. #endif
  3301. return EXPECT_RESULT();
  3302. }
  3303. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3304. {
  3305. EXPECT_DECLS;
  3306. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3307. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3308. WOLFSSL_CTX* ctx;
  3309. WOLFSSL* ssl = NULL;
  3310. const char *certChain[] = {
  3311. "./certs/intermediate/client-int-cert.pem",
  3312. "./certs/intermediate/ca-int2-cert.pem",
  3313. "./certs/intermediate/ca-int-cert.pem",
  3314. "./certs/ca-cert.pem",
  3315. NULL
  3316. };
  3317. const char** cert;
  3318. WOLFSSL_X509* x509 = NULL;
  3319. WOLF_STACK_OF(X509)* chain = NULL;
  3320. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3321. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3322. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3323. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3324. WOLFSSL_FILETYPE_PEM));
  3325. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3326. X509_free(x509);
  3327. x509 = NULL;
  3328. }
  3329. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3330. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3331. WOLFSSL_FILETYPE_PEM));
  3332. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3333. X509_free(x509);
  3334. x509 = NULL;
  3335. }
  3336. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3337. ExpectIntEQ(sk_X509_num(chain), 3);
  3338. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3339. ExpectIntEQ(sk_X509_num(chain), 3);
  3340. SSL_free(ssl);
  3341. SSL_CTX_free(ctx);
  3342. #endif
  3343. return EXPECT_RESULT();
  3344. }
  3345. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3346. {
  3347. EXPECT_DECLS;
  3348. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3349. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3350. const char* server_chain_der = "./certs/server-cert-chain.der";
  3351. const char* client_single_pem = "./certs/client-cert.pem";
  3352. WOLFSSL_CTX* ctx;
  3353. (void)server_chain_der;
  3354. (void)client_single_pem;
  3355. (void)ctx;
  3356. #ifndef NO_WOLFSSL_CLIENT
  3357. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3358. #else
  3359. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3360. #endif
  3361. ExpectNotNull(ctx);
  3362. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3363. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3364. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3365. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3366. wolfSSL_CTX_free(ctx);
  3367. #endif
  3368. return EXPECT_RESULT();
  3369. }
  3370. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3371. {
  3372. EXPECT_DECLS;
  3373. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3374. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3375. WOLFSSL_CTX *ctx = NULL;
  3376. (void)ctx;
  3377. #ifndef NO_WOLFSSL_CLIENT
  3378. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3379. #else
  3380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3381. #endif
  3382. /* invalid context */
  3383. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3384. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3385. /* invalid dhParamFile file */
  3386. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3387. NULL, WOLFSSL_FILETYPE_PEM));
  3388. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3389. bogusFile, WOLFSSL_FILETYPE_PEM));
  3390. /* success */
  3391. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3392. WOLFSSL_FILETYPE_PEM));
  3393. wolfSSL_CTX_free(ctx);
  3394. #endif
  3395. return EXPECT_RESULT();
  3396. }
  3397. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3398. {
  3399. EXPECT_DECLS;
  3400. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3401. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3402. WOLFSSL_CTX *ctx = NULL;
  3403. #ifndef NO_WOLFSSL_CLIENT
  3404. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3405. #else
  3406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3407. #endif
  3408. /* invalid context */
  3409. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3410. dh_key_der_2048, sizeof_dh_key_der_2048,
  3411. WOLFSSL_FILETYPE_ASN1));
  3412. /* invalid dhParamFile file */
  3413. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3414. 0, WOLFSSL_FILETYPE_ASN1));
  3415. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3416. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3417. WOLFSSL_FILETYPE_ASN1));
  3418. /* success */
  3419. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3420. dh_key_der_2048, sizeof_dh_key_der_2048,
  3421. WOLFSSL_FILETYPE_ASN1));
  3422. wolfSSL_CTX_free(ctx);
  3423. #endif
  3424. return EXPECT_RESULT();
  3425. }
  3426. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3427. {
  3428. EXPECT_DECLS;
  3429. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3430. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3431. WOLFSSL_CTX *ctx;
  3432. (void)ctx;
  3433. #ifndef NO_WOLFSSL_CLIENT
  3434. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3435. #else
  3436. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3437. #endif
  3438. ExpectNotNull(ctx);
  3439. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3440. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3441. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3442. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3443. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3444. dh_key_der_2048, sizeof_dh_key_der_2048,
  3445. WOLFSSL_FILETYPE_ASN1));
  3446. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3447. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3448. dh_key_der_2048, sizeof_dh_key_der_2048,
  3449. WOLFSSL_FILETYPE_ASN1));
  3450. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3451. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3452. dh_key_der_2048, sizeof_dh_key_der_2048,
  3453. WOLFSSL_FILETYPE_ASN1));
  3454. wolfSSL_CTX_free(ctx);
  3455. #endif
  3456. return EXPECT_RESULT();
  3457. }
  3458. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3459. {
  3460. EXPECT_DECLS;
  3461. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3462. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3463. WOLFSSL_CTX* ctx = NULL;
  3464. const char* derCert = "./certs/server-cert.der";
  3465. const char* nullPath = NULL;
  3466. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3467. const char* emptyPath = "";
  3468. /* der load Case 1 ctx NULL */
  3469. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3470. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3471. #ifndef NO_WOLFSSL_CLIENT
  3472. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3473. #else
  3474. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3475. #endif
  3476. /* Case 2 filePath NULL */
  3477. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3478. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3479. /* Case 3 invalid format */
  3480. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3481. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3482. /* Case 4 filePath not valid */
  3483. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3484. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3485. /* Case 5 filePath empty */
  3486. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3487. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3488. #ifndef NO_RSA
  3489. /* Case 6 success case */
  3490. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3491. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3492. #endif
  3493. wolfSSL_CTX_free(ctx);
  3494. #endif
  3495. return EXPECT_RESULT();
  3496. }
  3497. static int test_wolfSSL_CTX_enable_disable(void)
  3498. {
  3499. EXPECT_DECLS;
  3500. #ifndef NO_CERTS
  3501. WOLFSSL_CTX* ctx = NULL;
  3502. #ifdef HAVE_CRL
  3503. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  3504. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  3505. #endif
  3506. #ifdef HAVE_OCSP
  3507. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  3508. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  3509. #endif
  3510. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3511. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3512. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  3513. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  3514. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3515. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  3516. #endif
  3517. #ifndef NO_WOLFSSL_CLIENT
  3518. #ifdef HAVE_EXTENDED_MASTER
  3519. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  3520. #endif
  3521. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3522. #ifdef HAVE_EXTENDED_MASTER
  3523. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  3524. #endif
  3525. #elif !defined(NO_WOLFSSL_SERVER)
  3526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3527. #endif
  3528. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3529. #ifdef HAVE_CRL
  3530. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  3531. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  3532. #endif
  3533. #ifdef HAVE_OCSP
  3534. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  3535. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  3536. WOLFSSL_SUCCESS);
  3537. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  3538. WOLFSSL_SUCCESS);
  3539. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  3540. WOLFSSL_SUCCESS);
  3541. #endif
  3542. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  3543. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  3544. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3545. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  3546. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3547. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  3548. #endif
  3549. wolfSSL_CTX_free(ctx);
  3550. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3551. #endif /* NO_CERTS */
  3552. return EXPECT_RESULT();
  3553. }
  3554. static int test_wolfSSL_CTX_ticket_API(void)
  3555. {
  3556. EXPECT_DECLS;
  3557. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  3558. WOLFSSL_CTX* ctx = NULL;
  3559. void *userCtx = (void*)"this is my ctx";
  3560. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3561. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  3562. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  3563. wolfSSL_CTX_free(ctx);
  3564. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  3565. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  3566. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  3567. return EXPECT_RESULT();
  3568. }
  3569. static int test_wolfSSL_set_minmax_proto_version(void)
  3570. {
  3571. EXPECT_DECLS;
  3572. #ifdef OPENSSL_EXTRA
  3573. WOLFSSL_CTX *ctx = NULL;
  3574. WOLFSSL *ssl = NULL;
  3575. (void)ssl;
  3576. #ifndef NO_WOLFSSL_CLIENT
  3577. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3578. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3579. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3580. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3581. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3582. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3583. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3584. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  3585. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3586. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  3587. wolfSSL_free(ssl);
  3588. wolfSSL_CTX_free(ctx);
  3589. ctx = NULL;
  3590. #endif
  3591. #ifndef NO_WOLFSSL_SERVER
  3592. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3593. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  3594. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  3595. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  3596. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  3597. wolfSSL_CTX_free(ctx);
  3598. #endif
  3599. #endif
  3600. return EXPECT_RESULT();
  3601. }
  3602. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  3603. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  3604. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  3605. {
  3606. EXPECT_DECLS;
  3607. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  3608. return EXPECT_RESULT();
  3609. }
  3610. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  3611. {
  3612. EXPECT_DECLS;
  3613. /* Set TLS 1.2 */
  3614. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  3615. WOLFSSL_SUCCESS);
  3616. return EXPECT_RESULT();
  3617. }
  3618. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  3619. * what was set at ctx creation. */
  3620. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3621. {
  3622. EXPECT_DECLS;
  3623. test_ssl_cbf client_cbs;
  3624. test_ssl_cbf server_cbs;
  3625. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  3626. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  3627. client_cbs.method = wolfTLS_client_method;
  3628. server_cbs.method = wolfTLS_server_method;
  3629. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  3630. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3631. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  3632. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  3633. &server_cbs, NULL), TEST_SUCCESS);
  3634. return EXPECT_RESULT();
  3635. }
  3636. #else
  3637. static int test_wolfSSL_CTX_set_max_proto_version(void)
  3638. {
  3639. return TEST_SKIPPED;
  3640. }
  3641. #endif
  3642. /*----------------------------------------------------------------------------*
  3643. | SSL
  3644. *----------------------------------------------------------------------------*/
  3645. static int test_server_wolfSSL_new(void)
  3646. {
  3647. EXPECT_DECLS;
  3648. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3649. !defined(NO_WOLFSSL_SERVER)
  3650. WOLFSSL_CTX *ctx = NULL;
  3651. WOLFSSL_CTX *ctx_nocert = NULL;
  3652. WOLFSSL *ssl = NULL;
  3653. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3654. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3655. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3656. WOLFSSL_FILETYPE_PEM));
  3657. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3658. WOLFSSL_FILETYPE_PEM));
  3659. /* invalid context */
  3660. ExpectNull(ssl = wolfSSL_new(NULL));
  3661. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  3662. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  3663. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  3664. #endif
  3665. /* success */
  3666. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3667. wolfSSL_free(ssl);
  3668. wolfSSL_CTX_free(ctx);
  3669. wolfSSL_CTX_free(ctx_nocert);
  3670. #endif
  3671. return EXPECT_RESULT();
  3672. }
  3673. static int test_client_wolfSSL_new(void)
  3674. {
  3675. EXPECT_DECLS;
  3676. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3677. !defined(NO_WOLFSSL_CLIENT)
  3678. WOLFSSL_CTX *ctx = NULL;
  3679. WOLFSSL_CTX *ctx_nocert = NULL;
  3680. WOLFSSL *ssl = NULL;
  3681. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3682. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3683. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  3684. /* invalid context */
  3685. ExpectNull(ssl = wolfSSL_new(NULL));
  3686. /* success */
  3687. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  3688. wolfSSL_free(ssl);
  3689. ssl = NULL;
  3690. /* success */
  3691. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3692. wolfSSL_free(ssl);
  3693. wolfSSL_CTX_free(ctx);
  3694. wolfSSL_CTX_free(ctx_nocert);
  3695. #endif
  3696. return EXPECT_RESULT();
  3697. }
  3698. static int test_wolfSSL_SetTmpDH_file(void)
  3699. {
  3700. EXPECT_DECLS;
  3701. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3702. !defined(NO_WOLFSSL_SERVER)
  3703. WOLFSSL_CTX *ctx = NULL;
  3704. WOLFSSL *ssl = NULL;
  3705. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3706. #ifndef NO_RSA
  3707. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  3708. WOLFSSL_FILETYPE_PEM));
  3709. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  3710. WOLFSSL_FILETYPE_PEM));
  3711. #elif defined(HAVE_ECC)
  3712. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  3713. WOLFSSL_FILETYPE_PEM));
  3714. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  3715. WOLFSSL_FILETYPE_PEM));
  3716. #elif defined(HAVE_ED25519)
  3717. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  3718. WOLFSSL_FILETYPE_PEM));
  3719. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  3720. WOLFSSL_FILETYPE_PEM));
  3721. #elif defined(HAVE_ED448)
  3722. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  3723. WOLFSSL_FILETYPE_PEM));
  3724. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  3725. WOLFSSL_FILETYPE_PEM));
  3726. #endif
  3727. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3728. /* invalid ssl */
  3729. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  3730. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3731. /* invalid dhParamFile file */
  3732. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3733. NULL, WOLFSSL_FILETYPE_PEM));
  3734. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  3735. bogusFile, WOLFSSL_FILETYPE_PEM));
  3736. /* success */
  3737. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  3738. WOLFSSL_FILETYPE_PEM));
  3739. wolfSSL_free(ssl);
  3740. wolfSSL_CTX_free(ctx);
  3741. #endif
  3742. return EXPECT_RESULT();
  3743. }
  3744. static int test_wolfSSL_SetTmpDH_buffer(void)
  3745. {
  3746. EXPECT_DECLS;
  3747. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3748. WOLFSSL_CTX *ctx = NULL;
  3749. WOLFSSL *ssl = NULL;
  3750. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3751. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3752. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3753. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3754. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3755. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3756. /* invalid ssl */
  3757. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  3758. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3759. /* invalid dhParamFile file */
  3760. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  3761. 0, WOLFSSL_FILETYPE_ASN1));
  3762. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  3763. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3764. /* success */
  3765. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3766. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3767. wolfSSL_free(ssl);
  3768. wolfSSL_CTX_free(ctx);
  3769. #endif
  3770. return EXPECT_RESULT();
  3771. }
  3772. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3773. {
  3774. EXPECT_DECLS;
  3775. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3776. WOLFSSL_CTX *ctx = NULL;
  3777. WOLFSSL_CTX *ctx2 = NULL;
  3778. WOLFSSL *ssl = NULL;
  3779. WOLFSSL *ssl2 = NULL;
  3780. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3781. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3782. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3783. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3784. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3785. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3786. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3787. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3788. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3789. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3790. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3791. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3792. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3793. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  3794. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3795. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3796. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3797. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3798. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3799. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3800. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3801. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3802. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3803. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3804. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3805. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3806. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3807. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3808. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3809. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3810. wolfSSL_free(ssl2);
  3811. wolfSSL_CTX_free(ctx2);
  3812. wolfSSL_free(ssl);
  3813. wolfSSL_CTX_free(ctx);
  3814. #endif
  3815. return EXPECT_RESULT();
  3816. }
  3817. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3818. * allowed.
  3819. * POST: return 1 on success.
  3820. */
  3821. static int test_wolfSSL_SetMinVersion(void)
  3822. {
  3823. int res = TEST_SKIPPED;
  3824. #ifndef NO_WOLFSSL_CLIENT
  3825. int failFlag = WOLFSSL_SUCCESS;
  3826. WOLFSSL_CTX* ctx = NULL;
  3827. WOLFSSL* ssl = NULL;
  3828. int itr;
  3829. #ifndef NO_OLD_TLS
  3830. const int versions[] = {
  3831. #ifdef WOLFSSL_ALLOW_TLSV10
  3832. WOLFSSL_TLSV1,
  3833. #endif
  3834. WOLFSSL_TLSV1_1,
  3835. WOLFSSL_TLSV1_2};
  3836. #elif !defined(WOLFSSL_NO_TLS12)
  3837. const int versions[] = { WOLFSSL_TLSV1_2 };
  3838. #else
  3839. const int versions[] = { WOLFSSL_TLSV1_3 };
  3840. #endif
  3841. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3842. ssl = wolfSSL_new(ctx);
  3843. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  3844. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  3845. failFlag = WOLFSSL_FAILURE;
  3846. }
  3847. }
  3848. wolfSSL_free(ssl);
  3849. wolfSSL_CTX_free(ctx);
  3850. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  3851. #endif
  3852. return res;
  3853. } /* END test_wolfSSL_SetMinVersion */
  3854. #ifdef OPENSSL_EXTRA
  3855. static int test_ED25519(void)
  3856. {
  3857. EXPECT_DECLS;
  3858. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3859. defined(WOLFSSL_KEY_GEN)
  3860. byte priv[ED25519_PRV_KEY_SIZE];
  3861. unsigned int privSz = (unsigned int)sizeof(priv);
  3862. byte pub[ED25519_PUB_KEY_SIZE];
  3863. unsigned int pubSz = (unsigned int)sizeof(pub);
  3864. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3865. const char* msg = TEST_STRING;
  3866. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3867. byte sig[ED25519_SIG_SIZE];
  3868. unsigned int sigSz = (unsigned int)sizeof(sig);
  3869. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3870. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3871. WOLFSSL_SUCCESS);
  3872. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3873. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3874. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3875. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3876. &sigSz), WOLFSSL_SUCCESS);
  3877. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  3878. #ifdef HAVE_ED25519_VERIFY
  3879. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3880. sigSz), WOLFSSL_SUCCESS);
  3881. #endif /* HAVE_ED25519_VERIFY */
  3882. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3883. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3884. return EXPECT_RESULT();
  3885. }
  3886. static int test_ED448(void)
  3887. {
  3888. EXPECT_DECLS;
  3889. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3890. defined(WOLFSSL_KEY_GEN)
  3891. byte priv[ED448_PRV_KEY_SIZE];
  3892. unsigned int privSz = (unsigned int)sizeof(priv);
  3893. byte pub[ED448_PUB_KEY_SIZE];
  3894. unsigned int pubSz = (unsigned int)sizeof(pub);
  3895. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3896. const char* msg = TEST_STRING;
  3897. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3898. byte sig[ED448_SIG_SIZE];
  3899. unsigned int sigSz = (unsigned int)sizeof(sig);
  3900. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3901. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3902. WOLFSSL_SUCCESS);
  3903. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3904. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3905. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3906. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3907. &sigSz), WOLFSSL_SUCCESS);
  3908. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  3909. #ifdef HAVE_ED448_VERIFY
  3910. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3911. sigSz), WOLFSSL_SUCCESS);
  3912. #endif /* HAVE_ED448_VERIFY */
  3913. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3914. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3915. return EXPECT_RESULT();
  3916. }
  3917. #endif /* OPENSSL_EXTRA */
  3918. #include <wolfssl/openssl/pem.h>
  3919. /*----------------------------------------------------------------------------*
  3920. | EVP
  3921. *----------------------------------------------------------------------------*/
  3922. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3923. {
  3924. EXPECT_DECLS;
  3925. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3926. WOLFSSL_BIO* rbio = NULL;
  3927. WOLFSSL_BIO* wbio = NULL;
  3928. WOLFSSL_EVP_PKEY* pkey = NULL;
  3929. char line[256] = { 0 };
  3930. char line1[256] = { 0 };
  3931. int i;
  3932. /* test error cases */
  3933. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3934. /*
  3935. * test RSA public key print
  3936. * in this test, pass '3' for indent
  3937. */
  3938. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3939. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3940. sizeof_client_keypub_der_1024));
  3941. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3942. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3943. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3944. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3945. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3946. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  3947. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3948. strcpy(line1, " Modulus:\n");
  3949. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3950. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3951. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3952. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3953. /* skip to the end of modulus element*/
  3954. for (i = 0; i < 8 ;i++) {
  3955. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3956. }
  3957. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3958. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3959. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3960. /* should reach EOF */
  3961. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  3962. EVP_PKEY_free(pkey);
  3963. pkey = NULL;
  3964. BIO_free(rbio);
  3965. BIO_free(wbio);
  3966. rbio = NULL;
  3967. wbio = NULL;
  3968. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3969. /*
  3970. * test DSA public key print
  3971. */
  3972. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3973. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3974. sizeof_dsa_pub_key_der_2048));
  3975. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  3976. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  3977. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3978. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3979. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3980. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3981. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3982. strcpy(line1, "pub:\n");
  3983. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3984. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3985. strcpy(line1,
  3986. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3987. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3988. /* skip to the end of pub element*/
  3989. for (i = 0; i < 17 ;i++) {
  3990. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3991. }
  3992. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3993. strcpy(line1, "P:\n");
  3994. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3995. /* skip to the end of P element*/
  3996. for (i = 0; i < 18 ;i++) {
  3997. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  3998. }
  3999. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4000. strcpy(line1, "Q:\n");
  4001. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4002. /* skip to the end of Q element*/
  4003. for (i = 0; i < 3 ;i++) {
  4004. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4005. }
  4006. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4007. strcpy(line1, "G:\n");
  4008. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4009. /* skip to the end of G element*/
  4010. for (i = 0; i < 18 ;i++) {
  4011. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4012. }
  4013. /* should reach EOF */
  4014. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4015. EVP_PKEY_free(pkey);
  4016. pkey = NULL;
  4017. BIO_free(rbio);
  4018. BIO_free(wbio);
  4019. rbio = NULL;
  4020. wbio = NULL;
  4021. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4022. /*
  4023. * test ECC public key print
  4024. */
  4025. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4026. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4027. sizeof_ecc_clikeypub_der_256));
  4028. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4029. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4030. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4031. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4032. strcpy(line1, "Public-Key: (256 bit)\n");
  4033. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4034. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4035. strcpy(line1, "pub:\n");
  4036. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4037. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4038. strcpy(line1,
  4039. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4040. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4041. /* skip to the end of pub element*/
  4042. for (i = 0; i < 4 ;i++) {
  4043. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4044. }
  4045. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4046. strcpy(line1, "ASN1 OID: prime256v1\n");
  4047. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4048. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4049. strcpy(line1, "NIST CURVE: P-256\n");
  4050. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4051. /* should reach EOF */
  4052. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4053. EVP_PKEY_free(pkey);
  4054. pkey = NULL;
  4055. BIO_free(rbio);
  4056. BIO_free(wbio);
  4057. rbio = NULL;
  4058. wbio = NULL;
  4059. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4060. /*
  4061. * test DH public key print
  4062. */
  4063. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4064. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4065. sizeof_dh_pub_key_der_2048));
  4066. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4067. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4068. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4069. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4070. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4071. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4072. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4073. strcpy(line1, "public-key:\n");
  4074. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4075. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4076. strcpy(line1,
  4077. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4078. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4079. /* skip to the end of public-key element*/
  4080. for (i = 0; i < 17 ;i++) {
  4081. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4082. }
  4083. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4084. strcpy(line1, "prime:\n");
  4085. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4086. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4087. strcpy(line1,
  4088. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4089. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4090. /* skip to the end of prime element*/
  4091. for (i = 0; i < 17 ;i++) {
  4092. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4093. }
  4094. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4095. strcpy(line1, "generator: 2 (0x02)\n");
  4096. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4097. /* should reach EOF */
  4098. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4099. EVP_PKEY_free(pkey);
  4100. pkey = NULL;
  4101. BIO_free(rbio);
  4102. BIO_free(wbio);
  4103. rbio = NULL;
  4104. wbio = NULL;
  4105. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4106. /* to prevent "unused variable" warning */
  4107. (void)pkey;
  4108. (void)wbio;
  4109. (void)rbio;
  4110. (void)line;
  4111. (void)line1;
  4112. (void)i;
  4113. #endif /* OPENSSL_EXTRA */
  4114. return EXPECT_RESULT();
  4115. }
  4116. /* Test functions for base64 encode/decode */
  4117. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4118. {
  4119. EXPECT_DECLS;
  4120. #if defined(OPENSSL_EXTRA) && \
  4121. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4122. EVP_ENCODE_CTX* ctx = NULL;
  4123. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4124. ExpectIntEQ(ctx->remaining,0);
  4125. ExpectIntEQ(ctx->data[0],0);
  4126. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4127. EVP_ENCODE_CTX_free(ctx);
  4128. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4129. return EXPECT_RESULT();
  4130. }
  4131. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4132. {
  4133. EXPECT_DECLS;
  4134. #if defined(OPENSSL_EXTRA) && \
  4135. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4136. EVP_ENCODE_CTX* ctx = NULL;
  4137. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4138. EVP_ENCODE_CTX_free(ctx);
  4139. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4140. return EXPECT_RESULT();
  4141. }
  4142. static int test_wolfSSL_EVP_EncodeInit(void)
  4143. {
  4144. EXPECT_DECLS;
  4145. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4146. EVP_ENCODE_CTX* ctx = NULL;
  4147. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4148. ExpectIntEQ(ctx->remaining, 0);
  4149. ExpectIntEQ(ctx->data[0], 0);
  4150. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4151. if (ctx != NULL) {
  4152. /* make ctx dirty */
  4153. ctx->remaining = 10;
  4154. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4155. }
  4156. EVP_EncodeInit(ctx);
  4157. ExpectIntEQ(ctx->remaining, 0);
  4158. ExpectIntEQ(ctx->data[0], 0);
  4159. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4160. EVP_ENCODE_CTX_free(ctx);
  4161. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4162. return EXPECT_RESULT();
  4163. }
  4164. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4165. {
  4166. EXPECT_DECLS;
  4167. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4168. int outl;
  4169. int total;
  4170. const unsigned char plain0[] = {"Th"};
  4171. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4172. const unsigned char plain2[] = {"This is additional data."};
  4173. const unsigned char enc0[] = {"VGg=\n"};
  4174. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4175. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4176. const unsigned char enc2[] =
  4177. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4178. unsigned char encOutBuff[300];
  4179. EVP_ENCODE_CTX* ctx = NULL;
  4180. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4181. EVP_EncodeInit(ctx);
  4182. /* illegal parameter test */
  4183. ExpectIntEQ(
  4184. EVP_EncodeUpdate(
  4185. NULL, /* pass NULL as ctx */
  4186. encOutBuff,
  4187. &outl,
  4188. plain1,
  4189. sizeof(plain1)-1),
  4190. 0 /* expected result code 0: fail */
  4191. );
  4192. ExpectIntEQ(
  4193. EVP_EncodeUpdate(
  4194. ctx,
  4195. NULL, /* pass NULL as out buff */
  4196. &outl,
  4197. plain1,
  4198. sizeof(plain1)-1),
  4199. 0 /* expected result code 0: fail */
  4200. );
  4201. ExpectIntEQ(
  4202. EVP_EncodeUpdate(
  4203. ctx,
  4204. encOutBuff,
  4205. NULL, /* pass NULL as outl */
  4206. plain1,
  4207. sizeof(plain1)-1),
  4208. 0 /* expected result code 0: fail */
  4209. );
  4210. ExpectIntEQ(
  4211. EVP_EncodeUpdate(
  4212. ctx,
  4213. encOutBuff,
  4214. &outl,
  4215. NULL, /* pass NULL as in */
  4216. sizeof(plain1)-1),
  4217. 0 /* expected result code 0: fail */
  4218. );
  4219. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  4220. /* meaningless parameter test */
  4221. ExpectIntEQ(
  4222. EVP_EncodeUpdate(
  4223. ctx,
  4224. encOutBuff,
  4225. &outl,
  4226. plain1,
  4227. 0), /* pass zero input */
  4228. 1 /* expected result code 1: success */
  4229. );
  4230. /* very small data encoding test */
  4231. EVP_EncodeInit(ctx);
  4232. ExpectIntEQ(
  4233. EVP_EncodeUpdate(
  4234. ctx,
  4235. encOutBuff,
  4236. &outl,
  4237. plain0,
  4238. sizeof(plain0)-1),
  4239. 1 /* expected result code 1: success */
  4240. );
  4241. ExpectIntEQ(outl,0);
  4242. if (EXPECT_SUCCESS()) {
  4243. EVP_EncodeFinal(
  4244. ctx,
  4245. encOutBuff + outl,
  4246. &outl);
  4247. }
  4248. ExpectIntEQ( outl, sizeof(enc0)-1);
  4249. ExpectIntEQ(
  4250. XSTRNCMP(
  4251. (const char*)encOutBuff,
  4252. (const char*)enc0,sizeof(enc0) ),
  4253. 0);
  4254. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4255. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  4256. sizeof(enc0)-1);
  4257. ExpectIntEQ(
  4258. XSTRNCMP(
  4259. (const char*)encOutBuff,
  4260. (const char*)enc0,sizeof(enc0) ),
  4261. 0);
  4262. /* pass small size( < 48bytes ) input, then make sure they are not
  4263. * encoded and just stored in ctx
  4264. */
  4265. EVP_EncodeInit(ctx);
  4266. total = 0;
  4267. outl = 0;
  4268. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  4269. ExpectIntEQ(
  4270. EVP_EncodeUpdate(
  4271. ctx,
  4272. encOutBuff, /* buffer for output */
  4273. &outl, /* size of output */
  4274. plain1, /* input */
  4275. sizeof(plain1)-1), /* size of input */
  4276. 1); /* expected result code 1:success */
  4277. total += outl;
  4278. ExpectIntEQ(outl, 0); /* no output expected */
  4279. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  4280. ExpectTrue(
  4281. XSTRNCMP((const char*)(ctx->data),
  4282. (const char*)plain1,
  4283. ctx->remaining) ==0 );
  4284. ExpectTrue(encOutBuff[0] == 0);
  4285. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  4286. * the stored data and the new input together
  4287. */
  4288. ExpectIntEQ(
  4289. EVP_EncodeUpdate(
  4290. ctx,
  4291. encOutBuff + outl, /* buffer for output */
  4292. &outl, /* size of output */
  4293. plain2, /* additional input */
  4294. sizeof(plain2) -1), /* size of additional input */
  4295. 1); /* expected result code 1:success */
  4296. total += outl;
  4297. ExpectIntNE(outl, 0); /* some output is expected this time*/
  4298. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  4299. ExpectIntEQ(
  4300. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  4301. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  4302. EVP_EncodeFinal(
  4303. ctx,
  4304. encOutBuff + outl,
  4305. &outl);
  4306. total += outl;
  4307. ExpectIntNE(total,0);
  4308. ExpectIntNE(outl,0);
  4309. ExpectIntEQ(XSTRNCMP(
  4310. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  4311. /* test with illeagal parameters */
  4312. outl = 1;
  4313. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  4314. ExpectIntEQ(outl, 0);
  4315. outl = 1;
  4316. EVP_EncodeFinal(ctx, NULL, &outl);
  4317. ExpectIntEQ(outl, 0);
  4318. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  4319. EVP_EncodeFinal(NULL, NULL, NULL);
  4320. EVP_ENCODE_CTX_free(ctx);
  4321. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4322. return EXPECT_RESULT();
  4323. }
  4324. static int test_wolfSSL_EVP_EncodeFinal(void)
  4325. {
  4326. int res = TEST_SKIPPED;
  4327. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4328. /* tests for wolfSSL_EVP_EncodeFinal are included in
  4329. * test_wolfSSL_EVP_EncodeUpdate
  4330. */
  4331. res = TEST_SUCCESS;
  4332. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4333. return res;
  4334. }
  4335. static int test_wolfSSL_EVP_DecodeInit(void)
  4336. {
  4337. EXPECT_DECLS;
  4338. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4339. EVP_ENCODE_CTX* ctx = NULL;
  4340. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  4341. ExpectIntEQ( ctx->remaining,0);
  4342. ExpectIntEQ( ctx->data[0],0);
  4343. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4344. if (ctx != NULL) {
  4345. /* make ctx dirty */
  4346. ctx->remaining = 10;
  4347. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  4348. }
  4349. EVP_DecodeInit(ctx);
  4350. ExpectIntEQ( ctx->remaining,0);
  4351. ExpectIntEQ( ctx->data[0],0);
  4352. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  4353. EVP_ENCODE_CTX_free(ctx);
  4354. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4355. return EXPECT_RESULT();
  4356. }
  4357. static int test_wolfSSL_EVP_DecodeUpdate(void)
  4358. {
  4359. EXPECT_DECLS;
  4360. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4361. int outl;
  4362. unsigned char decOutBuff[300];
  4363. EVP_ENCODE_CTX* ctx = NULL;
  4364. static const unsigned char enc1[] =
  4365. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4366. /* const unsigned char plain1[] =
  4367. {"This is a base64 decoding test."} */
  4368. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4369. EVP_DecodeInit(ctx);
  4370. /* illegal parameter tests */
  4371. /* pass NULL as ctx */
  4372. ExpectIntEQ(
  4373. EVP_DecodeUpdate(
  4374. NULL, /* pass NULL as ctx */
  4375. decOutBuff,
  4376. &outl,
  4377. enc1,
  4378. sizeof(enc1)-1),
  4379. -1 /* expected result code -1: fail */
  4380. );
  4381. ExpectIntEQ( outl, 0);
  4382. /* pass NULL as output */
  4383. ExpectIntEQ(
  4384. EVP_DecodeUpdate(
  4385. ctx,
  4386. NULL, /* pass NULL as out buff */
  4387. &outl,
  4388. enc1,
  4389. sizeof(enc1)-1),
  4390. -1 /* expected result code -1: fail */
  4391. );
  4392. ExpectIntEQ( outl, 0);
  4393. /* pass NULL as outl */
  4394. ExpectIntEQ(
  4395. EVP_DecodeUpdate(
  4396. ctx,
  4397. decOutBuff,
  4398. NULL, /* pass NULL as outl */
  4399. enc1,
  4400. sizeof(enc1)-1),
  4401. -1 /* expected result code -1: fail */
  4402. );
  4403. /* pass NULL as input */
  4404. ExpectIntEQ(
  4405. EVP_DecodeUpdate(
  4406. ctx,
  4407. decOutBuff,
  4408. &outl,
  4409. NULL, /* pass NULL as in */
  4410. sizeof(enc1)-1),
  4411. -1 /* expected result code -1: fail */
  4412. );
  4413. ExpectIntEQ( outl, 0);
  4414. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  4415. /* pass zero length input */
  4416. ExpectIntEQ(
  4417. EVP_DecodeUpdate(
  4418. ctx,
  4419. decOutBuff,
  4420. &outl,
  4421. enc1,
  4422. 0), /* pass zero as input len */
  4423. 1 /* expected result code 1: success */
  4424. );
  4425. /* decode correct base64 string */
  4426. {
  4427. static const unsigned char enc2[] =
  4428. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  4429. static const unsigned char plain2[] =
  4430. {"This is a base64 decoding test."};
  4431. EVP_EncodeInit(ctx);
  4432. ExpectIntEQ(
  4433. EVP_DecodeUpdate(
  4434. ctx,
  4435. decOutBuff,
  4436. &outl,
  4437. enc2,
  4438. sizeof(enc2)-1),
  4439. 0 /* expected result code 0: success */
  4440. );
  4441. ExpectIntEQ(outl,sizeof(plain2) -1);
  4442. ExpectIntEQ(
  4443. EVP_DecodeFinal(
  4444. ctx,
  4445. decOutBuff + outl,
  4446. &outl),
  4447. 1 /* expected result code 1: success */
  4448. );
  4449. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  4450. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4451. sizeof(plain2) -1 ),0);
  4452. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  4453. sizeof(plain2)-1);
  4454. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  4455. sizeof(plain2) -1 ),0);
  4456. }
  4457. /* decode correct base64 string which does not have '\n' in its last*/
  4458. {
  4459. static const unsigned char enc3[] =
  4460. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  4461. static const unsigned char plain3[] =
  4462. {"This is a base64 decoding test."}; /* 31 chars */
  4463. EVP_EncodeInit(ctx);
  4464. ExpectIntEQ(
  4465. EVP_DecodeUpdate(
  4466. ctx,
  4467. decOutBuff,
  4468. &outl,
  4469. enc3,
  4470. sizeof(enc3)-1),
  4471. 0 /* expected result code 0: success */
  4472. );
  4473. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4474. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4475. sizeof(plain3) -1 ),0);
  4476. ExpectIntEQ(
  4477. EVP_DecodeFinal(
  4478. ctx,
  4479. decOutBuff + outl,
  4480. &outl),
  4481. 1 /* expected result code 1: success */
  4482. );
  4483. ExpectIntEQ(outl,0 );
  4484. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4485. sizeof(plain3)-1);
  4486. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4487. sizeof(plain3) -1 ),0);
  4488. }
  4489. /* decode string which has a padding char ('=') in the illegal position*/
  4490. {
  4491. static const unsigned char enc4[] =
  4492. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4493. EVP_EncodeInit(ctx);
  4494. ExpectIntEQ(
  4495. EVP_DecodeUpdate(
  4496. ctx,
  4497. decOutBuff,
  4498. &outl,
  4499. enc4,
  4500. sizeof(enc4)-1),
  4501. -1 /* expected result code -1: error */
  4502. );
  4503. ExpectIntEQ(outl,0);
  4504. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4505. }
  4506. /* small data decode test */
  4507. {
  4508. static const unsigned char enc00[] = {"VG"};
  4509. static const unsigned char enc01[] = {"g=\n"};
  4510. static const unsigned char plain4[] = {"Th"};
  4511. EVP_EncodeInit(ctx);
  4512. ExpectIntEQ(
  4513. EVP_DecodeUpdate(
  4514. ctx,
  4515. decOutBuff,
  4516. &outl,
  4517. enc00,
  4518. sizeof(enc00)-1),
  4519. 1 /* expected result code 1: success */
  4520. );
  4521. ExpectIntEQ(outl,0);
  4522. ExpectIntEQ(
  4523. EVP_DecodeUpdate(
  4524. ctx,
  4525. decOutBuff + outl,
  4526. &outl,
  4527. enc01,
  4528. sizeof(enc01)-1),
  4529. 0 /* expected result code 0: success */
  4530. );
  4531. ExpectIntEQ(outl,sizeof(plain4)-1);
  4532. /* test with illegal parameters */
  4533. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4534. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4535. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4536. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4537. if (EXPECT_SUCCESS()) {
  4538. EVP_DecodeFinal(
  4539. ctx,
  4540. decOutBuff + outl,
  4541. &outl);
  4542. }
  4543. ExpectIntEQ( outl, 0);
  4544. ExpectIntEQ(
  4545. XSTRNCMP(
  4546. (const char*)decOutBuff,
  4547. (const char*)plain4,sizeof(plain4)-1 ),
  4548. 0);
  4549. }
  4550. EVP_ENCODE_CTX_free(ctx);
  4551. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4552. return EXPECT_RESULT();
  4553. }
  4554. static int test_wolfSSL_EVP_DecodeFinal(void)
  4555. {
  4556. int res = TEST_SKIPPED;
  4557. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4558. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4559. * test_wolfSSL_EVP_DecodeUpdate
  4560. */
  4561. res = TEST_SUCCESS;
  4562. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4563. return res;
  4564. }
  4565. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4566. */
  4567. #ifdef OPENSSL_EXTRA
  4568. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4569. {
  4570. EXPECT_DECLS;
  4571. #ifndef NO_AES
  4572. const WOLFSSL_EVP_CIPHER* c;
  4573. c = wolfSSL_EVP_get_cipherbynid(419);
  4574. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4575. defined(WOLFSSL_AES_128)
  4576. ExpectNotNull(c);
  4577. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  4578. #else
  4579. ExpectNull(c);
  4580. #endif
  4581. c = wolfSSL_EVP_get_cipherbynid(423);
  4582. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4583. defined(WOLFSSL_AES_192)
  4584. ExpectNotNull(c);
  4585. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  4586. #else
  4587. ExpectNull(c);
  4588. #endif
  4589. c = wolfSSL_EVP_get_cipherbynid(427);
  4590. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4591. defined(WOLFSSL_AES_256)
  4592. ExpectNotNull(c);
  4593. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  4594. #else
  4595. ExpectNull(c);
  4596. #endif
  4597. c = wolfSSL_EVP_get_cipherbynid(904);
  4598. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4599. ExpectNotNull(c);
  4600. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  4601. #else
  4602. ExpectNull(c);
  4603. #endif
  4604. c = wolfSSL_EVP_get_cipherbynid(905);
  4605. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4606. ExpectNotNull(c);
  4607. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  4608. #else
  4609. ExpectNull(c);
  4610. #endif
  4611. c = wolfSSL_EVP_get_cipherbynid(906);
  4612. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4613. ExpectNotNull(c);
  4614. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  4615. #else
  4616. ExpectNull(c);
  4617. #endif
  4618. c = wolfSSL_EVP_get_cipherbynid(418);
  4619. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4620. ExpectNotNull(c);
  4621. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  4622. #else
  4623. ExpectNull(c);
  4624. #endif
  4625. c = wolfSSL_EVP_get_cipherbynid(422);
  4626. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4627. ExpectNotNull(c);
  4628. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  4629. #else
  4630. ExpectNull(c);
  4631. #endif
  4632. c = wolfSSL_EVP_get_cipherbynid(426);
  4633. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4634. ExpectNotNull(c);
  4635. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  4636. #else
  4637. ExpectNull(c);
  4638. #endif
  4639. #endif /* !NO_AES */
  4640. #ifndef NO_DES3
  4641. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4642. #ifdef WOLFSSL_DES_ECB
  4643. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4644. #endif
  4645. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4646. #ifdef WOLFSSL_DES_ECB
  4647. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4648. #endif
  4649. #endif /* !NO_DES3 */
  4650. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4651. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4652. #endif
  4653. /* test for nid is out of range */
  4654. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  4655. return EXPECT_RESULT();
  4656. }
  4657. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4658. {
  4659. EXPECT_DECLS;
  4660. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4661. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4662. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4663. const EVP_CIPHER *test;
  4664. byte key[AES_BLOCK_SIZE] = {0};
  4665. byte iv[AES_BLOCK_SIZE] = {0};
  4666. ExpectNotNull(ctx);
  4667. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4668. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4669. test = EVP_CIPHER_CTX_cipher(ctx);
  4670. ExpectTrue(init == test);
  4671. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4672. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4673. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4674. EVP_CIPHER_CTX_free(ctx);
  4675. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4676. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4677. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4678. return EXPECT_RESULT();
  4679. }
  4680. #endif /* OPENSSL_EXTRA */
  4681. /*----------------------------------------------------------------------------*
  4682. | IO
  4683. *----------------------------------------------------------------------------*/
  4684. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  4685. defined(HAVE_IO_TESTS_DEPENDENCIES)
  4686. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4687. #ifdef WC_SHA512_DIGEST_SIZE
  4688. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4689. #else
  4690. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4691. #endif
  4692. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4693. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4694. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4695. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4696. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4697. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4698. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4699. defined(HAVE_AES_CBC)
  4700. typedef struct openssl_key_ctx {
  4701. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4702. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4703. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4704. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4705. } openssl_key_ctx;
  4706. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4707. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4708. static WC_INLINE int OpenSSLTicketInit(void)
  4709. {
  4710. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4711. if (ret != 0) return ret;
  4712. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4713. sizeof(myOpenSSLKey_ctx.name));
  4714. if (ret != 0) return ret;
  4715. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4716. sizeof(myOpenSSLKey_ctx.key));
  4717. if (ret != 0) return ret;
  4718. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4719. sizeof(myOpenSSLKey_ctx.hmacKey));
  4720. if (ret != 0) return ret;
  4721. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4722. sizeof(myOpenSSLKey_ctx.iv));
  4723. if (ret != 0) return ret;
  4724. return 0;
  4725. }
  4726. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4727. byte name[WOLFSSL_TICKET_NAME_SZ],
  4728. byte iv[WOLFSSL_TICKET_IV_SZ],
  4729. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4730. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4731. (void)ssl;
  4732. if (enc) {
  4733. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4734. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4735. }
  4736. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4737. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4738. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4739. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4740. return 0;
  4741. }
  4742. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4743. if (enc)
  4744. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4745. else
  4746. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4747. return 1;
  4748. }
  4749. static WC_INLINE void OpenSSLTicketCleanup(void)
  4750. {
  4751. wc_FreeRng(&myOpenSSLKey_rng);
  4752. }
  4753. #endif
  4754. #endif
  4755. /* helper functions */
  4756. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  4757. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  4758. void *ctx)
  4759. {
  4760. struct test_ssl_memio_ctx *test_ctx;
  4761. byte *buf;
  4762. int *len;
  4763. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4764. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4765. buf = test_ctx->c_buff;
  4766. len = &test_ctx->c_len;
  4767. }
  4768. else {
  4769. buf = test_ctx->s_buff;
  4770. len = &test_ctx->s_len;
  4771. }
  4772. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  4773. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  4774. XMEMCPY(buf + *len, data, sz);
  4775. *len += sz;
  4776. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  4777. {
  4778. /* This can be imported into Wireshark by transforming the file with
  4779. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  4780. * And then loading test_output.dump.hex into Wireshark using the
  4781. * "Import from Hex Dump..." option ion and selecting the TCP
  4782. * encapsulation option. */
  4783. char dump_file_name[64];
  4784. WOLFSSL_BIO *dump_file;
  4785. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  4786. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  4787. if (dump_file != NULL) {
  4788. (void)wolfSSL_BIO_write(dump_file, data, sz);
  4789. wolfSSL_BIO_free(dump_file);
  4790. }
  4791. }
  4792. #endif
  4793. return sz;
  4794. }
  4795. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  4796. void *ctx)
  4797. {
  4798. struct test_ssl_memio_ctx *test_ctx;
  4799. int read_sz;
  4800. byte *buf;
  4801. int *len;
  4802. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  4803. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  4804. buf = test_ctx->s_buff;
  4805. len = &test_ctx->s_len;
  4806. }
  4807. else {
  4808. buf = test_ctx->c_buff;
  4809. len = &test_ctx->c_len;
  4810. }
  4811. if (*len == 0)
  4812. return WOLFSSL_CBIO_ERR_WANT_READ;
  4813. read_sz = sz < *len ? sz : *len;
  4814. XMEMCPY(data, buf, read_sz);
  4815. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  4816. *len -= read_sz;
  4817. return read_sz;
  4818. }
  4819. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  4820. {
  4821. EXPECT_DECLS;
  4822. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4823. int c_sharedCtx = 0;
  4824. int s_sharedCtx = 0;
  4825. #endif
  4826. const char* certFile = svrCertFile;
  4827. const char* keyFile = svrKeyFile;
  4828. /********************************
  4829. * Create WOLFSSL_CTX for client.
  4830. ********************************/
  4831. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4832. if (ctx->c_ctx != NULL) {
  4833. c_sharedCtx = ctx->c_cb.isSharedCtx;
  4834. }
  4835. else
  4836. #endif
  4837. {
  4838. WOLFSSL_METHOD* method = NULL;
  4839. if (ctx->c_cb.method != NULL) {
  4840. method = ctx->c_cb.method();
  4841. }
  4842. else {
  4843. method = wolfSSLv23_client_method();
  4844. }
  4845. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  4846. }
  4847. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  4848. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  4849. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4850. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  4851. #endif
  4852. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx, caCertFile, 0),
  4853. WOLFSSL_SUCCESS);
  4854. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4855. if (!c_sharedCtx)
  4856. #endif
  4857. {
  4858. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx->c_ctx, cliCertFile,
  4859. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4860. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, cliKeyFile,
  4861. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4862. }
  4863. #ifdef HAVE_CRL
  4864. if (ctx->c_cb.crlPemFile != NULL) {
  4865. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  4866. WOLFSSL_SUCCESS);
  4867. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  4868. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4869. }
  4870. #endif
  4871. if (ctx->c_ciphers != NULL) {
  4872. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  4873. WOLFSSL_SUCCESS);
  4874. }
  4875. if (ctx->c_cb.ctx_ready != NULL) {
  4876. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  4877. }
  4878. /********************************
  4879. * Create WOLFSSL_CTX for server.
  4880. ********************************/
  4881. if (ctx->s_ctx != NULL) {
  4882. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4883. s_sharedCtx = 1;
  4884. #endif
  4885. ctx->s_cb.isSharedCtx = 1;
  4886. }
  4887. else
  4888. {
  4889. WOLFSSL_METHOD* method = NULL;
  4890. if (ctx->s_cb.method != NULL) {
  4891. method = ctx->s_cb.method();
  4892. }
  4893. else {
  4894. method = wolfSSLv23_server_method();
  4895. }
  4896. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  4897. ctx->s_cb.isSharedCtx = 0;
  4898. }
  4899. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  4900. #if defined(HAVE_SESSION_TICKET) && \
  4901. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4902. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4903. OpenSSLTicketInit();
  4904. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  4905. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4906. TicketInit();
  4907. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  4908. #endif
  4909. #endif
  4910. }
  4911. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  4912. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  4913. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  4914. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4915. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx, cliCertFile, 0),
  4916. WOLFSSL_SUCCESS);
  4917. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4918. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  4919. #endif
  4920. if (ctx->s_cb.certPemFile != NULL) {
  4921. certFile = ctx->s_cb.certPemFile;
  4922. }
  4923. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4924. if (!s_sharedCtx)
  4925. #endif
  4926. {
  4927. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx->s_ctx, certFile,
  4928. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4929. }
  4930. if (ctx->s_cb.keyPemFile != NULL) {
  4931. keyFile = ctx->s_cb.keyPemFile;
  4932. }
  4933. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4934. if (!s_sharedCtx)
  4935. #endif
  4936. {
  4937. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, keyFile,
  4938. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4939. }
  4940. if (ctx->s_ciphers != NULL) {
  4941. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  4942. WOLFSSL_SUCCESS);
  4943. }
  4944. if (ctx->s_cb.ctx_ready != NULL) {
  4945. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  4946. }
  4947. /****************************
  4948. * Create WOLFSSL for client.
  4949. ****************************/
  4950. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  4951. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  4952. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  4953. if (0
  4954. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4955. || c_sharedCtx
  4956. #endif
  4957. )
  4958. {
  4959. ExpectIntEQ(wolfSSL_use_certificate_file(ctx->c_ssl, cliCertFile,
  4960. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4961. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, cliKeyFile,
  4962. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4963. }
  4964. if (ctx->c_cb.ssl_ready != NULL) {
  4965. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  4966. }
  4967. /****************************
  4968. * Create WOLFSSL for server.
  4969. ****************************/
  4970. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  4971. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  4972. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  4973. if (0
  4974. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4975. || s_sharedCtx
  4976. #endif
  4977. )
  4978. {
  4979. ExpectIntEQ(wolfSSL_use_certificate_file(ctx->s_ssl, certFile,
  4980. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4981. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, keyFile,
  4982. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  4983. }
  4984. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4985. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4986. #elif !defined(NO_DH)
  4987. /* will repick suites with DHE, higher priority than PSK */
  4988. SetDH(ctx->s_ssl);
  4989. #endif
  4990. if (ctx->s_cb.ssl_ready != NULL) {
  4991. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  4992. }
  4993. return EXPECT_RESULT();
  4994. }
  4995. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  4996. int* rounds)
  4997. {
  4998. int handshake_complete = 0;
  4999. int hs_c = 0;
  5000. int hs_s = 0;
  5001. int failing_s = 0;
  5002. int failing_c = 0;
  5003. int ret;
  5004. int err;
  5005. if (rounds != NULL) {
  5006. *rounds = 0;
  5007. }
  5008. while ((!handshake_complete) && (max_rounds > 0)) {
  5009. if (!hs_c) {
  5010. wolfSSL_SetLoggingPrefix("client");
  5011. ret = wolfSSL_connect(ctx->c_ssl);
  5012. wolfSSL_SetLoggingPrefix(NULL);
  5013. if (ret == WOLFSSL_SUCCESS) {
  5014. hs_c = 1;
  5015. }
  5016. else {
  5017. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5018. if (err != WOLFSSL_ERROR_WANT_READ &&
  5019. err != WOLFSSL_ERROR_WANT_WRITE) {
  5020. failing_c = 1;
  5021. hs_c = 1;
  5022. if (failing_c && failing_s) {
  5023. break;
  5024. }
  5025. }
  5026. }
  5027. }
  5028. if (!hs_s) {
  5029. wolfSSL_SetLoggingPrefix("server");
  5030. ret = wolfSSL_accept(ctx->s_ssl);
  5031. wolfSSL_SetLoggingPrefix(NULL);
  5032. if (ret == WOLFSSL_SUCCESS) {
  5033. hs_s = 1;
  5034. }
  5035. else {
  5036. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5037. if (err != WOLFSSL_ERROR_WANT_READ &&
  5038. err != WOLFSSL_ERROR_WANT_WRITE) {
  5039. failing_s = 1;
  5040. hs_s = 1;
  5041. if (failing_c && failing_s) {
  5042. break;
  5043. }
  5044. }
  5045. }
  5046. }
  5047. handshake_complete = hs_c && hs_s;
  5048. max_rounds--;
  5049. if (rounds != NULL) {
  5050. *rounds += 1;
  5051. }
  5052. }
  5053. if (!handshake_complete) {
  5054. return TEST_FAIL;
  5055. }
  5056. return TEST_SUCCESS;
  5057. }
  5058. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5059. {
  5060. EXPECT_DECLS;
  5061. char input[1024];
  5062. int idx = 0;
  5063. const char* msg_c = "hello wolfssl!";
  5064. int msglen_c = (int)XSTRLEN(msg_c);
  5065. const char* msg_s = "I hear you fa shizzle!";
  5066. int msglen_s = (int)XSTRLEN(msg_s);
  5067. if (ctx->c_msg != NULL) {
  5068. msg_c = ctx->c_msg;
  5069. msglen_c = ctx->c_msglen;
  5070. }
  5071. if (ctx->s_msg != NULL) {
  5072. msg_s = ctx->s_msg;
  5073. msglen_s = ctx->s_msglen;
  5074. }
  5075. wolfSSL_SetLoggingPrefix("client");
  5076. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5077. wolfSSL_SetLoggingPrefix("server");
  5078. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5079. if (idx >= 0) {
  5080. input[idx] = '\0';
  5081. }
  5082. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5083. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5084. ctx->s_cb.return_code = EXPECT_RESULT();
  5085. wolfSSL_SetLoggingPrefix("client");
  5086. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5087. wolfSSL_SetLoggingPrefix(NULL);
  5088. if (idx >= 0) {
  5089. input[idx] = '\0';
  5090. }
  5091. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5092. ctx->c_cb.return_code = EXPECT_RESULT();
  5093. if (ctx->c_cb.on_result != NULL) {
  5094. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5095. }
  5096. if (ctx->s_cb.on_result != NULL) {
  5097. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5098. }
  5099. return EXPECT_RESULT();
  5100. }
  5101. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5102. {
  5103. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5104. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5105. if (ctx->c_cb.on_cleanup != NULL) {
  5106. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5107. }
  5108. if (ctx->s_cb.on_cleanup != NULL) {
  5109. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5110. }
  5111. wolfSSL_shutdown(ctx->s_ssl);
  5112. wolfSSL_shutdown(ctx->c_ssl);
  5113. wolfSSL_free(ctx->s_ssl);
  5114. wolfSSL_free(ctx->c_ssl);
  5115. if (!ctx->s_cb.isSharedCtx) {
  5116. wolfSSL_CTX_free(ctx->s_ctx);
  5117. ctx->s_ctx = NULL;
  5118. }
  5119. if (!ctx->c_cb.isSharedCtx) {
  5120. wolfSSL_CTX_free(ctx->c_ctx);
  5121. ctx->c_ctx = NULL;
  5122. }
  5123. if (!ctx->s_cb.ticNoInit) {
  5124. #if defined(HAVE_SESSION_TICKET) && \
  5125. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5126. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5127. OpenSSLTicketCleanup();
  5128. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5129. TicketCleanup();
  5130. #endif
  5131. #endif
  5132. }
  5133. }
  5134. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5135. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5136. {
  5137. EXPECT_DECLS;
  5138. struct test_ssl_memio_ctx test_ctx;
  5139. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5140. size_t msg_len;
  5141. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5142. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5143. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5144. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5145. test_ctx.c_ctx = client_cb->ctx;
  5146. test_ctx.s_ctx = server_cb->ctx;
  5147. test_ctx.c_cb.return_code = TEST_FAIL;
  5148. test_ctx.s_cb.return_code = TEST_FAIL;
  5149. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5150. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5151. if (client_on_handshake != NULL) {
  5152. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5153. TEST_SUCCESS);
  5154. }
  5155. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5156. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5157. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5158. MD_MAX_SIZE);
  5159. ExpectIntGE(msg_len, 0);
  5160. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5161. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5162. MD_MAX_SIZE);
  5163. ExpectIntGE(msg_len, 0);
  5164. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5165. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5166. test_ssl_memio_cleanup(&test_ctx);
  5167. client_cb->return_code = test_ctx.c_cb.return_code;
  5168. server_cb->return_code = test_ctx.s_cb.return_code;
  5169. return EXPECT_RESULT();
  5170. }
  5171. #endif
  5172. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5173. #ifdef WOLFSSL_SESSION_EXPORT
  5174. #ifdef WOLFSSL_DTLS
  5175. /* set up function for sending session information */
  5176. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5177. {
  5178. WOLFSSL_CTX* ctx = NULL;
  5179. WOLFSSL* ssl = NULL;
  5180. AssertNotNull(inSsl);
  5181. AssertNotNull(buf);
  5182. AssertIntNE(0, sz);
  5183. /* Set ctx to DTLS 1.2 */
  5184. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5185. AssertNotNull(ctx);
  5186. ssl = wolfSSL_new(ctx);
  5187. AssertNotNull(ssl);
  5188. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5189. wolfSSL_free(ssl);
  5190. wolfSSL_CTX_free(ctx);
  5191. (void)userCtx;
  5192. return 0;
  5193. }
  5194. #endif
  5195. /* returns negative value on fail and positive (including 0) on success */
  5196. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5197. {
  5198. int ret, err, loop_count, count, timeout = 10;
  5199. char msg[] = "I hear you fa shizzle!";
  5200. char input[1024];
  5201. loop_count = ((func_args*)args)->argc;
  5202. #ifdef WOLFSSL_ASYNC_CRYPT
  5203. err = 0; /* Reset error */
  5204. #endif
  5205. do {
  5206. #ifdef WOLFSSL_ASYNC_CRYPT
  5207. if (err == WC_PENDING_E) {
  5208. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5209. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5210. }
  5211. #endif
  5212. ret = wolfSSL_accept(ssl);
  5213. err = wolfSSL_get_error(ssl, 0);
  5214. if (err == WOLFSSL_ERROR_WANT_READ ||
  5215. err == WOLFSSL_ERROR_WANT_WRITE) {
  5216. int select_ret;
  5217. err = WC_PENDING_E;
  5218. select_ret = tcp_select(*sockfd, timeout);
  5219. if (select_ret == TEST_TIMEOUT) {
  5220. return WOLFSSL_FATAL_ERROR;
  5221. }
  5222. }
  5223. } while (err == WC_PENDING_E);
  5224. if (ret != WOLFSSL_SUCCESS) {
  5225. char buff[WOLFSSL_MAX_ERROR_SZ];
  5226. fprintf(stderr, "error = %d, %s\n", err,
  5227. wolfSSL_ERR_error_string(err, buff));
  5228. return ret;
  5229. }
  5230. for (count = 0; count < loop_count; count++) {
  5231. int select_ret;
  5232. select_ret = tcp_select(*sockfd, timeout);
  5233. if (select_ret == TEST_TIMEOUT) {
  5234. ret = WOLFSSL_FATAL_ERROR;
  5235. break;
  5236. }
  5237. do {
  5238. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5239. if (ret > 0) {
  5240. input[ret] = '\0';
  5241. fprintf(stderr, "Client message: %s\n", input);
  5242. }
  5243. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5244. do {
  5245. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5246. return WOLFSSL_FATAL_ERROR;
  5247. }
  5248. err = wolfSSL_get_error(ssl, ret);
  5249. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5250. }
  5251. return ret;
  5252. }
  5253. #endif /* WOLFSSL_SESSION_EXPORT */
  5254. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5255. {
  5256. SOCKET_T sockfd = 0;
  5257. SOCKET_T clientfd = 0;
  5258. word16 port;
  5259. callback_functions* cbf;
  5260. WOLFSSL_CTX* ctx = NULL;
  5261. WOLFSSL* ssl = NULL;
  5262. func_args* opts = (func_args*)args;
  5263. char msg[] = "I hear you fa shizzle!";
  5264. char input[1024];
  5265. int idx;
  5266. int ret, err = 0;
  5267. int sharedCtx = 0;
  5268. int doUdp = 0;
  5269. SOCKADDR_IN_T cliAddr;
  5270. socklen_t cliLen;
  5271. const char* certFile = svrCertFile;
  5272. const char* keyFile = svrKeyFile;
  5273. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5274. size_t msg_len = 0;
  5275. #endif
  5276. wolfSSL_SetLoggingPrefix("server");
  5277. #ifdef WOLFSSL_TIRTOS
  5278. fdOpenSession(Task_self());
  5279. #endif
  5280. opts->return_code = TEST_FAIL;
  5281. cbf = opts->callbacks;
  5282. if (cbf != NULL && cbf->ctx) {
  5283. ctx = cbf->ctx;
  5284. sharedCtx = 1;
  5285. }
  5286. else
  5287. {
  5288. WOLFSSL_METHOD* method = NULL;
  5289. if (cbf != NULL && cbf->method != NULL) {
  5290. method = cbf->method();
  5291. }
  5292. else {
  5293. method = wolfSSLv23_server_method();
  5294. }
  5295. ctx = wolfSSL_CTX_new(method);
  5296. }
  5297. if (ctx == NULL) {
  5298. /* Release the wait for TCP ready. */
  5299. signal_ready(opts->signal);
  5300. goto done;
  5301. }
  5302. if (cbf == NULL || !cbf->ticNoInit) {
  5303. #if defined(HAVE_SESSION_TICKET) && \
  5304. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5305. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5306. OpenSSLTicketInit();
  5307. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5308. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5309. TicketInit();
  5310. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5311. #endif
  5312. #endif
  5313. }
  5314. #if defined(USE_WINDOWS_API)
  5315. port = opts->signal->port;
  5316. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5317. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5318. /* Let tcp_listen assign port */
  5319. port = 0;
  5320. #else
  5321. /* Use default port */
  5322. port = wolfSSLPort;
  5323. #endif
  5324. if (cbf != NULL)
  5325. doUdp = cbf->doUdp;
  5326. /* do it here to detect failure */
  5327. tcp_accept(
  5328. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5329. if (doUdp) {
  5330. cliLen = sizeof(cliAddr);
  5331. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5332. (struct sockaddr*)&cliAddr, &cliLen);
  5333. AssertIntGT(idx, 0);
  5334. }
  5335. else {
  5336. CloseSocket(sockfd);
  5337. }
  5338. wolfSSL_CTX_set_verify(ctx,
  5339. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5340. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5341. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5342. #endif
  5343. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5344. != WOLFSSL_SUCCESS) {
  5345. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5346. goto done;
  5347. }
  5348. if (cbf != NULL && cbf->certPemFile != NULL)
  5349. certFile = cbf->certPemFile;
  5350. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5351. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5352. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5353. #else
  5354. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5355. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5356. #endif
  5357. /*err_sys("can't load server cert chain file, "
  5358. "Please run from wolfSSL home dir");*/
  5359. goto done;
  5360. }
  5361. if (cbf != NULL && cbf->keyPemFile != NULL)
  5362. keyFile = cbf->keyPemFile;
  5363. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5364. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5365. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5366. #else
  5367. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5368. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5369. #endif
  5370. /*err_sys("can't load server key file, "
  5371. "Please run from wolfSSL home dir");*/
  5372. goto done;
  5373. }
  5374. #ifdef HAVE_CRL
  5375. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5376. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5377. goto done;
  5378. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5379. != WOLFSSL_SUCCESS)
  5380. goto done;
  5381. }
  5382. #endif
  5383. /* call ctx setup callback */
  5384. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5385. cbf->ctx_ready(ctx);
  5386. }
  5387. ssl = wolfSSL_new(ctx);
  5388. if (ssl == NULL) {
  5389. goto done;
  5390. }
  5391. if (doUdp) {
  5392. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5393. if (err != WOLFSSL_SUCCESS)
  5394. goto done;
  5395. }
  5396. #ifdef WOLFSSL_SESSION_EXPORT
  5397. /* only add in more complex nonblocking case with session export tests */
  5398. if (args && opts->argc > 0) {
  5399. /* set as nonblock and time out for waiting on read/write */
  5400. tcp_set_nonblocking(&clientfd);
  5401. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5402. }
  5403. #endif
  5404. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5405. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5406. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5407. #else
  5408. if (wolfSSL_use_certificate_file(ssl, certFile,
  5409. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5410. #endif
  5411. /*err_sys("can't load server cert chain file, "
  5412. "Please run from wolfSSL home dir");*/
  5413. goto done;
  5414. }
  5415. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5416. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5417. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5418. #else
  5419. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5420. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5421. #endif
  5422. /*err_sys("can't load server key file, "
  5423. "Please run from wolfSSL home dir");*/
  5424. goto done;
  5425. }
  5426. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5427. /*err_sys("SSL_set_fd failed");*/
  5428. goto done;
  5429. }
  5430. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5431. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5432. #elif !defined(NO_DH)
  5433. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5434. #endif
  5435. /* call ssl setup callback */
  5436. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5437. cbf->ssl_ready(ssl);
  5438. }
  5439. #ifdef WOLFSSL_SESSION_EXPORT
  5440. /* only add in more complex nonblocking case with session export tests */
  5441. if (opts->argc > 0) {
  5442. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5443. if (ret >= 0) {
  5444. opts->return_code = TEST_SUCCESS;
  5445. }
  5446. #ifdef WOLFSSL_TIRTOS
  5447. Task_yield();
  5448. #endif
  5449. goto done;
  5450. }
  5451. #endif
  5452. #ifdef WOLFSSL_ASYNC_CRYPT
  5453. err = 0; /* Reset error */
  5454. #endif
  5455. do {
  5456. #ifdef WOLFSSL_ASYNC_CRYPT
  5457. if (err == WC_PENDING_E) {
  5458. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5459. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5460. }
  5461. #endif
  5462. ret = wolfSSL_negotiate(ssl);
  5463. err = wolfSSL_get_error(ssl, 0);
  5464. } while (err == WC_PENDING_E);
  5465. if (ret != WOLFSSL_SUCCESS) {
  5466. char buff[WOLFSSL_MAX_ERROR_SZ];
  5467. fprintf(stderr, "error = %d, %s\n", err,
  5468. wolfSSL_ERR_error_string(err, buff));
  5469. /*err_sys("SSL_accept failed");*/
  5470. goto done;
  5471. }
  5472. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5473. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5474. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5475. AssertIntGE(msg_len, 0);
  5476. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5477. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5478. AssertIntGE(msg_len, 0);
  5479. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5480. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5481. if (idx > 0) {
  5482. input[idx] = '\0';
  5483. fprintf(stderr, "Client message: %s\n", input);
  5484. }
  5485. else if (idx < 0) {
  5486. goto done;
  5487. }
  5488. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5489. /*err_sys("SSL_write failed");*/
  5490. goto done;
  5491. }
  5492. if (cbf != NULL && cbf->on_result != NULL)
  5493. cbf->on_result(ssl);
  5494. #ifdef WOLFSSL_TIRTOS
  5495. Task_yield();
  5496. #endif
  5497. opts->return_code = TEST_SUCCESS;
  5498. done:
  5499. if (cbf != NULL)
  5500. cbf->last_err = err;
  5501. if (cbf != NULL && cbf->on_cleanup != NULL)
  5502. cbf->on_cleanup(ssl);
  5503. wolfSSL_shutdown(ssl);
  5504. wolfSSL_free(ssl);
  5505. if (!sharedCtx)
  5506. wolfSSL_CTX_free(ctx);
  5507. CloseSocket(clientfd);
  5508. #ifdef WOLFSSL_TIRTOS
  5509. fdCloseSession(Task_self());
  5510. #endif
  5511. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5512. && defined(HAVE_THREAD_LS)
  5513. wc_ecc_fp_free(); /* free per thread cache */
  5514. #endif
  5515. if (cbf == NULL || !cbf->ticNoInit) {
  5516. #if defined(HAVE_SESSION_TICKET) && \
  5517. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5518. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5519. OpenSSLTicketCleanup();
  5520. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5521. TicketCleanup();
  5522. #endif
  5523. #endif
  5524. }
  5525. wolfSSL_SetLoggingPrefix(NULL);
  5526. WOLFSSL_RETURN_FROM_THREAD(0);
  5527. }
  5528. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5529. !defined(WOLFSSL_NO_TLS12)
  5530. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5531. {
  5532. SOCKET_T sockfd = 0;
  5533. SOCKET_T clientfd = 0;
  5534. word16 port;
  5535. callback_functions* cbf;
  5536. WOLFSSL_CTX* ctx = 0;
  5537. WOLFSSL* ssl = 0;
  5538. char msg[] = "I hear you fa shizzle!";
  5539. char input[1024];
  5540. int idx;
  5541. int ret, err = 0;
  5542. int sharedCtx = 0;
  5543. func_args* opts = (func_args*)args;
  5544. int loop_count = opts->argc;
  5545. int count = 0;
  5546. #ifdef WOLFSSL_TIRTOS
  5547. fdOpenSession(Task_self());
  5548. #endif
  5549. opts->return_code = TEST_FAIL;
  5550. cbf = opts->callbacks;
  5551. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5552. if (cbf != NULL && cbf->ctx) {
  5553. ctx = cbf->ctx;
  5554. sharedCtx = 1;
  5555. }
  5556. else
  5557. #endif
  5558. {
  5559. WOLFSSL_METHOD* method = NULL;
  5560. if (cbf != NULL && cbf->method != NULL) {
  5561. method = cbf->method();
  5562. }
  5563. else {
  5564. method = wolfSSLv23_server_method();
  5565. }
  5566. ctx = wolfSSL_CTX_new(method);
  5567. }
  5568. #if defined(USE_WINDOWS_API)
  5569. port = opts->signal->port;
  5570. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5571. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5572. /* Let tcp_listen assign port */
  5573. port = 0;
  5574. #else
  5575. /* Use default port */
  5576. port = wolfSSLPort;
  5577. #endif
  5578. wolfSSL_CTX_set_verify(ctx,
  5579. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5580. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5581. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5582. #endif
  5583. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5584. != WOLFSSL_SUCCESS) {
  5585. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5586. /* Release the wait for TCP ready. */
  5587. signal_ready(opts->signal);
  5588. goto done;
  5589. }
  5590. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  5591. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5592. /*err_sys("can't load server cert chain file, "
  5593. "Please run from wolfSSL home dir");*/
  5594. /* Release the wait for TCP ready. */
  5595. signal_ready(opts->signal);
  5596. goto done;
  5597. }
  5598. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5599. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5600. /*err_sys("can't load server key file, "
  5601. "Please run from wolfSSL home dir");*/
  5602. /* Release the wait for TCP ready. */
  5603. signal_ready(opts->signal);
  5604. goto done;
  5605. }
  5606. /* call ctx setup callback */
  5607. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5608. cbf->ctx_ready(ctx);
  5609. }
  5610. while (count != loop_count) {
  5611. ssl = wolfSSL_new(ctx);
  5612. if (ssl == NULL) {
  5613. signal_ready(opts->signal);
  5614. goto done;
  5615. }
  5616. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5617. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5618. /*err_sys("can't load server cert chain file, "
  5619. "Please run from wolfSSL home dir");*/
  5620. /* Release the wait for TCP ready. */
  5621. signal_ready(opts->signal);
  5622. goto done;
  5623. }
  5624. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5625. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5626. /*err_sys("can't load server key file, "
  5627. "Please run from wolfSSL home dir");*/
  5628. /* Release the wait for TCP ready. */
  5629. signal_ready(opts->signal);
  5630. goto done;
  5631. }
  5632. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5633. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5634. #elif !defined(NO_DH)
  5635. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5636. #endif
  5637. /* call ssl setup callback */
  5638. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5639. cbf->ssl_ready(ssl);
  5640. }
  5641. /* do it here to detect failure */
  5642. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5643. 0);
  5644. CloseSocket(sockfd);
  5645. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5646. /*err_sys("SSL_set_fd failed");*/
  5647. goto done;
  5648. }
  5649. #ifdef WOLFSSL_ASYNC_CRYPT
  5650. err = 0; /* Reset error */
  5651. #endif
  5652. do {
  5653. #ifdef WOLFSSL_ASYNC_CRYPT
  5654. if (err == WC_PENDING_E) {
  5655. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5656. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5657. }
  5658. #endif
  5659. ret = wolfSSL_accept(ssl);
  5660. err = wolfSSL_get_error(ssl, 0);
  5661. } while (err == WC_PENDING_E);
  5662. if (ret != WOLFSSL_SUCCESS) {
  5663. char buff[WOLFSSL_MAX_ERROR_SZ];
  5664. fprintf(stderr, "error = %d, %s\n", err,
  5665. wolfSSL_ERR_error_string(err, buff));
  5666. /*err_sys("SSL_accept failed");*/
  5667. goto done;
  5668. }
  5669. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5670. if (idx > 0) {
  5671. input[idx] = '\0';
  5672. fprintf(stderr, "Client message: %s\n", input);
  5673. }
  5674. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5675. /*err_sys("SSL_write failed");*/
  5676. goto done;
  5677. }
  5678. /* free ssl for this connection */
  5679. wolfSSL_shutdown(ssl);
  5680. wolfSSL_free(ssl); ssl = NULL;
  5681. CloseSocket(clientfd);
  5682. count++;
  5683. }
  5684. #ifdef WOLFSSL_TIRTOS
  5685. Task_yield();
  5686. #endif
  5687. opts->return_code = TEST_SUCCESS;
  5688. done:
  5689. if (ssl != NULL) {
  5690. wolfSSL_shutdown(ssl);
  5691. wolfSSL_free(ssl);
  5692. }
  5693. if (!sharedCtx)
  5694. wolfSSL_CTX_free(ctx);
  5695. CloseSocket(clientfd);
  5696. #ifdef WOLFSSL_TIRTOS
  5697. fdCloseSession(Task_self());
  5698. #endif
  5699. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5700. && defined(HAVE_THREAD_LS)
  5701. wc_ecc_fp_free(); /* free per thread cache */
  5702. #endif
  5703. WOLFSSL_RETURN_FROM_THREAD(0);
  5704. }
  5705. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5706. static int test_client_nofail(void* args, cbType cb)
  5707. {
  5708. #if !defined(NO_WOLFSSL_CLIENT)
  5709. SOCKET_T sockfd = 0;
  5710. callback_functions* cbf;
  5711. WOLFSSL_CTX* ctx = 0;
  5712. WOLFSSL* ssl = 0;
  5713. WOLFSSL_CIPHER* cipher;
  5714. char msg[64] = "hello wolfssl!";
  5715. char reply[1024];
  5716. int input;
  5717. int msgSz = (int)XSTRLEN(msg);
  5718. int ret, err = 0;
  5719. int cipherSuite;
  5720. int sharedCtx = 0;
  5721. int doUdp = 0;
  5722. const char* cipherName1, *cipherName2;
  5723. wolfSSL_SetLoggingPrefix("client");
  5724. #ifdef WOLFSSL_TIRTOS
  5725. fdOpenSession(Task_self());
  5726. #endif
  5727. ((func_args*)args)->return_code = TEST_FAIL;
  5728. cbf = ((func_args*)args)->callbacks;
  5729. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5730. if (cbf != NULL && cbf->ctx) {
  5731. ctx = cbf->ctx;
  5732. sharedCtx = cbf->isSharedCtx;
  5733. }
  5734. else
  5735. #endif
  5736. {
  5737. WOLFSSL_METHOD* method = NULL;
  5738. if (cbf != NULL && cbf->method != NULL) {
  5739. method = cbf->method();
  5740. }
  5741. else {
  5742. method = wolfSSLv23_client_method();
  5743. }
  5744. ctx = wolfSSL_CTX_new(method);
  5745. }
  5746. if (cbf != NULL)
  5747. doUdp = cbf->doUdp;
  5748. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5749. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5750. #endif
  5751. /* Do connect here so server detects failures */
  5752. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5753. doUdp, 0, NULL);
  5754. /* Connect the socket so that we don't have to set the peer later on */
  5755. if (doUdp)
  5756. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5757. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5758. {
  5759. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5760. goto done;
  5761. }
  5762. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5763. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5764. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5765. #else
  5766. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5767. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5768. #endif
  5769. /*err_sys("can't load client cert file, "
  5770. "Please run from wolfSSL home dir");*/
  5771. goto done;
  5772. }
  5773. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5774. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5775. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5776. #else
  5777. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5778. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5779. #endif
  5780. /*err_sys("can't load client key file, "
  5781. "Please run from wolfSSL home dir");*/
  5782. goto done;
  5783. }
  5784. #ifdef HAVE_CRL
  5785. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5786. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5787. goto done;
  5788. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5789. != WOLFSSL_SUCCESS)
  5790. goto done;
  5791. }
  5792. #endif
  5793. /* call ctx setup callback */
  5794. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5795. cbf->ctx_ready(ctx);
  5796. }
  5797. ssl = wolfSSL_new(ctx);
  5798. if (ssl == NULL) {
  5799. goto done;
  5800. }
  5801. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5802. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5803. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5804. #else
  5805. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5806. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5807. #endif
  5808. /*err_sys("can't load client cert file, "
  5809. "Please run from wolfSSL home dir");*/
  5810. goto done;
  5811. }
  5812. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5813. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5814. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5815. #else
  5816. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5817. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5818. #endif
  5819. /*err_sys("can't load client key file, "
  5820. "Please run from wolfSSL home dir");*/
  5821. goto done;
  5822. }
  5823. if (!doUdp) {
  5824. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5825. /*err_sys("SSL_set_fd failed");*/
  5826. goto done;
  5827. }
  5828. }
  5829. else {
  5830. #ifdef WOLFSSL_DTLS
  5831. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5832. /*err_sys("SSL_set_fd failed");*/
  5833. goto done;
  5834. }
  5835. #else
  5836. goto done;
  5837. #endif
  5838. }
  5839. /* call ssl setup callback */
  5840. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5841. cbf->ssl_ready(ssl);
  5842. }
  5843. #ifdef WOLFSSL_ASYNC_CRYPT
  5844. err = 0; /* Reset error */
  5845. #endif
  5846. do {
  5847. #ifdef WOLFSSL_ASYNC_CRYPT
  5848. if (err == WC_PENDING_E) {
  5849. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5850. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5851. }
  5852. #endif
  5853. ret = wolfSSL_negotiate(ssl);
  5854. err = wolfSSL_get_error(ssl, 0);
  5855. } while (err == WC_PENDING_E);
  5856. if (ret != WOLFSSL_SUCCESS) {
  5857. char buff[WOLFSSL_MAX_ERROR_SZ];
  5858. fprintf(stderr, "error = %d, %s\n", err,
  5859. wolfSSL_ERR_error_string(err, buff));
  5860. /*err_sys("SSL_connect failed");*/
  5861. goto done;
  5862. }
  5863. /* test the various get cipher methods */
  5864. /* Internal cipher suite names */
  5865. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5866. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5867. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5868. (cipherSuite >> 8), cipherSuite & 0xFF);
  5869. AssertStrEQ(cipherName1, cipherName2);
  5870. /* IANA Cipher Suites Names */
  5871. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5872. then it's the internal cipher suite name */
  5873. cipher = wolfSSL_get_current_cipher(ssl);
  5874. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  5875. cipherName2 = wolfSSL_get_cipher(ssl);
  5876. AssertStrEQ(cipherName1, cipherName2);
  5877. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  5878. !defined(WOLFSSL_QT)
  5879. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  5880. (cipherSuite >> 8), cipherSuite & 0xFF);
  5881. AssertStrEQ(cipherName1, cipherName2);
  5882. #endif
  5883. if (cb != NULL)
  5884. (cb)(ctx, ssl);
  5885. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5886. /*err_sys("SSL_write failed");*/
  5887. goto done;
  5888. }
  5889. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5890. if (input > 0) {
  5891. reply[input] = '\0';
  5892. fprintf(stderr, "Server response: %s\n", reply);
  5893. }
  5894. if (cbf != NULL && cbf->on_result != NULL)
  5895. cbf->on_result(ssl);
  5896. ((func_args*)args)->return_code = TEST_SUCCESS;
  5897. done:
  5898. if (cbf != NULL)
  5899. cbf->last_err = err;
  5900. if (cbf != NULL && cbf->on_cleanup != NULL)
  5901. cbf->on_cleanup(ssl);
  5902. wolfSSL_free(ssl);
  5903. if (!sharedCtx)
  5904. wolfSSL_CTX_free(ctx);
  5905. CloseSocket(sockfd);
  5906. #ifdef WOLFSSL_TIRTOS
  5907. fdCloseSession(Task_self());
  5908. #endif
  5909. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5910. && defined(HAVE_THREAD_LS)
  5911. wc_ecc_fp_free(); /* free per thread cache */
  5912. #endif
  5913. #else
  5914. (void)args;
  5915. (void)cb;
  5916. #endif /* !NO_WOLFSSL_CLIENT */
  5917. wolfSSL_SetLoggingPrefix(NULL);
  5918. return 0;
  5919. }
  5920. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  5921. callback_functions* server_cb, cbType client_on_handshake)
  5922. {
  5923. func_args client_args;
  5924. func_args server_args;
  5925. tcp_ready ready;
  5926. THREAD_TYPE serverThread;
  5927. XMEMSET(&client_args, 0, sizeof(func_args));
  5928. XMEMSET(&server_args, 0, sizeof(func_args));
  5929. #ifdef WOLFSSL_TIRTOS
  5930. fdOpenSession(Task_self());
  5931. #endif
  5932. StartTCP();
  5933. InitTcpReady(&ready);
  5934. #if defined(USE_WINDOWS_API)
  5935. /* use RNG to get random port if using windows */
  5936. ready.port = GetRandomPort();
  5937. #endif
  5938. server_args.signal = &ready;
  5939. server_args.callbacks = server_cb;
  5940. client_args.signal = &ready;
  5941. client_args.callbacks = client_cb;
  5942. start_thread(test_server_nofail, &server_args, &serverThread);
  5943. wait_tcp_ready(&server_args);
  5944. test_client_nofail(&client_args, client_on_handshake);
  5945. join_thread(serverThread);
  5946. client_cb->return_code = client_args.return_code;
  5947. server_cb->return_code = server_args.return_code;
  5948. FreeTcpReady(&ready);
  5949. #ifdef WOLFSSL_TIRTOS
  5950. fdOpenSession(Task_self());
  5951. #endif
  5952. }
  5953. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  5954. callback_functions* server_cb)
  5955. {
  5956. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  5957. }
  5958. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5959. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  5960. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  5961. void* server_args)
  5962. {
  5963. SOCKET_T sockfd = 0;
  5964. callback_functions* cbf;
  5965. WOLFSSL_CTX* ctx = 0;
  5966. WOLFSSL* ssl = 0;
  5967. WOLFSSL_SESSION* session = NULL;
  5968. char msg[64] = "hello wolfssl!";
  5969. char reply[1024];
  5970. int input;
  5971. int msgSz = (int)XSTRLEN(msg);
  5972. int ret, err = 0;
  5973. int sharedCtx = 0;
  5974. #ifdef WOLFSSL_TIRTOS
  5975. fdOpenSession(Task_self());
  5976. #endif
  5977. ((func_args*)args)->return_code = TEST_FAIL;
  5978. cbf = ((func_args*)args)->callbacks;
  5979. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5980. if (cbf != NULL && cbf->ctx) {
  5981. ctx = cbf->ctx;
  5982. sharedCtx = 1;
  5983. }
  5984. else
  5985. #endif
  5986. {
  5987. WOLFSSL_METHOD* method = NULL;
  5988. if (cbf != NULL && cbf->method != NULL) {
  5989. method = cbf->method();
  5990. }
  5991. else {
  5992. method = wolfSSLv23_client_method();
  5993. }
  5994. ctx = wolfSSL_CTX_new(method);
  5995. }
  5996. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5997. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5998. #endif
  5999. /* Do connect here so server detects failures */
  6000. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6001. 0, 0, NULL);
  6002. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6003. WOLFSSL_SUCCESS) {
  6004. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6005. goto done;
  6006. }
  6007. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6008. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6009. /*err_sys("can't load client cert file, "
  6010. "Please run from wolfSSL home dir");*/
  6011. goto done;
  6012. }
  6013. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6014. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6015. /*err_sys("can't load client key file, "
  6016. "Please run from wolfSSL home dir");*/
  6017. goto done;
  6018. }
  6019. /* call ctx setup callback */
  6020. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6021. cbf->ctx_ready(ctx);
  6022. }
  6023. ssl = wolfSSL_new(ctx);
  6024. if (ssl == NULL) {
  6025. goto done;
  6026. }
  6027. /* keep handshake resources for re-using WOLFSSL obj */
  6028. wolfSSL_KeepArrays(ssl);
  6029. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6030. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6031. goto done;
  6032. }
  6033. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6034. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6035. /*err_sys("can't load client cert file, "
  6036. "Please run from wolfSSL home dir");*/
  6037. goto done;
  6038. }
  6039. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6040. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6041. /*err_sys("can't load client key file, "
  6042. "Please run from wolfSSL home dir");*/
  6043. goto done;
  6044. }
  6045. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6046. /*err_sys("SSL_set_fd failed");*/
  6047. goto done;
  6048. }
  6049. /* call ssl setup callback */
  6050. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6051. cbf->ssl_ready(ssl);
  6052. }
  6053. #ifdef WOLFSSL_ASYNC_CRYPT
  6054. err = 0; /* Reset error */
  6055. #endif
  6056. do {
  6057. #ifdef WOLFSSL_ASYNC_CRYPT
  6058. if (err == WC_PENDING_E) {
  6059. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6060. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6061. }
  6062. #endif
  6063. ret = wolfSSL_connect(ssl);
  6064. err = wolfSSL_get_error(ssl, 0);
  6065. } while (err == WC_PENDING_E);
  6066. if (ret != WOLFSSL_SUCCESS) {
  6067. char buff[WOLFSSL_MAX_ERROR_SZ];
  6068. fprintf(stderr, "error = %d, %s\n", err,
  6069. wolfSSL_ERR_error_string(err, buff));
  6070. /*err_sys("SSL_connect failed");*/
  6071. goto done;
  6072. }
  6073. /* Build first session */
  6074. if (cb != NULL)
  6075. cb(ctx, ssl);
  6076. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6077. /*err_sys("SSL_write failed");*/
  6078. goto done;
  6079. }
  6080. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6081. if (input > 0) {
  6082. reply[input] = '\0';
  6083. fprintf(stderr, "Server response: %s\n", reply);
  6084. }
  6085. /* Session Resumption by re-using WOLFSSL object */
  6086. wolfSSL_set_quiet_shutdown(ssl, 1);
  6087. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6088. /* err_sys ("SSL shutdown failed"); */
  6089. goto done;
  6090. }
  6091. session = wolfSSL_get1_session(ssl);
  6092. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6093. wolfSSL_SESSION_free(session);
  6094. /* err_sys ("SSL_clear failed"); */
  6095. goto done;
  6096. }
  6097. wolfSSL_set_session(ssl, session);
  6098. wolfSSL_SESSION_free(session);
  6099. session = NULL;
  6100. /* close socket once */
  6101. CloseSocket(sockfd);
  6102. sockfd = 0;
  6103. /* wait until server ready */
  6104. wait_tcp_ready((func_args*)server_args);
  6105. fprintf(stderr, "session resumption\n");
  6106. /* Do re-connect */
  6107. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6108. 0, 0, NULL);
  6109. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6110. /*err_sys("SSL_set_fd failed");*/
  6111. goto done;
  6112. }
  6113. #ifdef WOLFSSL_ASYNC_CRYPT
  6114. err = 0; /* Reset error */
  6115. #endif
  6116. do {
  6117. #ifdef WOLFSSL_ASYNC_CRYPT
  6118. if (err == WC_PENDING_E) {
  6119. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6120. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6121. }
  6122. #endif
  6123. ret = wolfSSL_connect(ssl);
  6124. err = wolfSSL_get_error(ssl, 0);
  6125. } while (err == WC_PENDING_E);
  6126. if (ret != WOLFSSL_SUCCESS) {
  6127. char buff[WOLFSSL_MAX_ERROR_SZ];
  6128. fprintf(stderr, "error = %d, %s\n", err,
  6129. wolfSSL_ERR_error_string(err, buff));
  6130. /*err_sys("SSL_connect failed");*/
  6131. goto done;
  6132. }
  6133. /* Build first session */
  6134. if (cb != NULL)
  6135. cb(ctx, ssl);
  6136. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6137. /*err_sys("SSL_write failed");*/
  6138. goto done;
  6139. }
  6140. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6141. if (input > 0) {
  6142. reply[input] = '\0';
  6143. fprintf(stderr, "Server response: %s\n", reply);
  6144. }
  6145. ((func_args*)args)->return_code = TEST_SUCCESS;
  6146. done:
  6147. wolfSSL_free(ssl);
  6148. if (!sharedCtx)
  6149. wolfSSL_CTX_free(ctx);
  6150. CloseSocket(sockfd);
  6151. #ifdef WOLFSSL_TIRTOS
  6152. fdCloseSession(Task_self());
  6153. #endif
  6154. return;
  6155. }
  6156. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6157. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6158. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6159. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6160. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6161. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6162. #define HAVE_ALPN_PROTOS_SUPPORT
  6163. #endif
  6164. /* Generic TLS client / server with callbacks for API unit tests
  6165. * Used by SNI / ALPN / crypto callback helper functions */
  6166. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6167. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6168. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6169. #define ENABLE_TLS_CALLBACK_TEST
  6170. #endif
  6171. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6172. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6173. /* TLS server for API unit testing - generic */
  6174. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6175. {
  6176. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6177. WOLFSSL_CTX* ctx = NULL;
  6178. WOLFSSL* ssl = NULL;
  6179. SOCKET_T sfd = 0;
  6180. SOCKET_T cfd = 0;
  6181. word16 port;
  6182. char msg[] = "I hear you fa shizzle!";
  6183. int len = (int) XSTRLEN(msg);
  6184. char input[1024];
  6185. int idx;
  6186. int ret, err = 0;
  6187. ((func_args*)args)->return_code = TEST_FAIL;
  6188. #if defined(USE_WINDOWS_API)
  6189. port = ((func_args*)args)->signal->port;
  6190. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6191. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6192. /* Let tcp_listen assign port */
  6193. port = 0;
  6194. #else
  6195. /* Use default port */
  6196. port = wolfSSLPort;
  6197. #endif
  6198. #ifdef WOLFSSL_DTLS
  6199. if (callbacks->method == wolfDTLS_server_method
  6200. #ifdef WOLFSSL_STATIC_MEMORY
  6201. || callbacks->method_ex == wolfDTLS_server_method_ex
  6202. #endif
  6203. #ifndef NO_OLD_TLS
  6204. || callbacks->method == wolfDTLSv1_server_method
  6205. #ifdef WOLFSSL_STATIC_MEMORY
  6206. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6207. #endif
  6208. #endif
  6209. #ifndef WOLFSSL_NO_TLS12
  6210. || callbacks->method == wolfDTLSv1_2_server_method
  6211. #ifdef WOLFSSL_STATIC_MEMORY
  6212. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6213. #endif
  6214. #endif
  6215. #ifdef WOLFSSL_DTLS13
  6216. || callbacks->method == wolfDTLSv1_3_server_method
  6217. #ifdef WOLFSSL_STATIC_MEMORY
  6218. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6219. #endif
  6220. #endif
  6221. ) {
  6222. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6223. }
  6224. else
  6225. #endif
  6226. {
  6227. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6228. }
  6229. #ifdef WOLFSSL_STATIC_MEMORY
  6230. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6231. callbacks->memSz > 0) {
  6232. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6233. callbacks->mem, callbacks->memSz, 0, 1);
  6234. if (ret != WOLFSSL_SUCCESS) {
  6235. fprintf(stderr, "CTX static new failed %d\n", ret);
  6236. goto cleanup;
  6237. }
  6238. }
  6239. #else
  6240. ctx = wolfSSL_CTX_new(callbacks->method());
  6241. #endif
  6242. if (ctx == NULL) {
  6243. fprintf(stderr, "CTX new failed\n");
  6244. goto cleanup;
  6245. }
  6246. /* set defaults */
  6247. if (callbacks->caPemFile == NULL)
  6248. callbacks->caPemFile = cliCertFile;
  6249. if (callbacks->certPemFile == NULL)
  6250. callbacks->certPemFile = svrCertFile;
  6251. if (callbacks->keyPemFile == NULL)
  6252. callbacks->keyPemFile = svrKeyFile;
  6253. #ifdef WOLFSSL_TIRTOS
  6254. fdOpenSession(Task_self());
  6255. #endif
  6256. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6257. wolfSSL_CTX_set_verify(ctx,
  6258. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6259. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6260. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6261. #endif
  6262. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6263. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6264. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6265. goto cleanup;
  6266. }
  6267. #endif
  6268. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6269. WOLFSSL_SUCCESS) {
  6270. goto cleanup;
  6271. }
  6272. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6273. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6274. goto cleanup;
  6275. }
  6276. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6277. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6278. goto cleanup;
  6279. }
  6280. #ifdef HAVE_CRL
  6281. if (callbacks->crlPemFile != NULL) {
  6282. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6283. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6284. goto cleanup;
  6285. }
  6286. }
  6287. #endif
  6288. if (callbacks->ctx_ready)
  6289. callbacks->ctx_ready(ctx);
  6290. ssl = wolfSSL_new(ctx);
  6291. if (ssl == NULL) {
  6292. fprintf(stderr, "SSL new failed\n");
  6293. goto cleanup;
  6294. }
  6295. if (wolfSSL_dtls(ssl)) {
  6296. SOCKADDR_IN_T cliAddr;
  6297. socklen_t cliLen;
  6298. cliLen = sizeof(cliAddr);
  6299. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6300. (struct sockaddr*)&cliAddr, &cliLen);
  6301. if (idx <= 0) {
  6302. goto cleanup;
  6303. }
  6304. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6305. }
  6306. else {
  6307. CloseSocket(sfd);
  6308. }
  6309. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6310. goto cleanup;
  6311. }
  6312. if (callbacks->loadToSSL) {
  6313. wolfSSL_SetDevId(ssl, callbacks->devId);
  6314. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6315. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6316. goto cleanup;
  6317. }
  6318. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6319. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6320. goto cleanup;
  6321. }
  6322. }
  6323. #ifdef NO_PSK
  6324. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6325. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6326. #elif !defined(NO_DH)
  6327. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6328. #endif
  6329. #endif
  6330. if (callbacks->ssl_ready)
  6331. callbacks->ssl_ready(ssl);
  6332. #ifdef WOLFSSL_ASYNC_CRYPT
  6333. err = 0; /* Reset error */
  6334. #endif
  6335. do {
  6336. #ifdef WOLFSSL_ASYNC_CRYPT
  6337. if (err == WC_PENDING_E) {
  6338. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6339. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6340. }
  6341. #endif
  6342. ret = wolfSSL_accept(ssl);
  6343. err = wolfSSL_get_error(ssl, ret);
  6344. } while (err == WC_PENDING_E);
  6345. if (ret != WOLFSSL_SUCCESS) {
  6346. char buff[WOLFSSL_MAX_ERROR_SZ];
  6347. fprintf(stderr, "accept error = %d, %s\n", err,
  6348. wolfSSL_ERR_error_string(err, buff));
  6349. /*err_sys("SSL_accept failed");*/
  6350. }
  6351. else {
  6352. #ifdef WOLFSSL_ASYNC_CRYPT
  6353. err = 0; /* Reset error */
  6354. #endif
  6355. do {
  6356. #ifdef WOLFSSL_ASYNC_CRYPT
  6357. if (err == WC_PENDING_E) {
  6358. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6359. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6360. }
  6361. #endif
  6362. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6363. err = wolfSSL_get_error(ssl, idx);
  6364. } while (err == WC_PENDING_E);
  6365. if (idx > 0) {
  6366. input[idx] = 0;
  6367. fprintf(stderr, "Client message: %s\n", input);
  6368. }
  6369. #ifdef WOLFSSL_ASYNC_CRYPT
  6370. err = 0; /* Reset error */
  6371. #endif
  6372. do {
  6373. #ifdef WOLFSSL_ASYNC_CRYPT
  6374. if (err == WC_PENDING_E) {
  6375. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6376. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6377. }
  6378. #endif
  6379. ret = wolfSSL_write(ssl, msg, len);
  6380. err = wolfSSL_get_error(ssl, ret);
  6381. } while (err == WC_PENDING_E);
  6382. if (len != ret) {
  6383. goto cleanup;
  6384. }
  6385. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6386. defined(WOLFSSL_DTLS)
  6387. if (wolfSSL_dtls(ssl)) {
  6388. byte* import;
  6389. word32 sz;
  6390. wolfSSL_dtls_export(ssl, NULL, &sz);
  6391. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6392. if (import == NULL) {
  6393. goto cleanup;
  6394. }
  6395. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6396. if (idx < 0) {
  6397. goto cleanup;
  6398. }
  6399. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6400. goto cleanup;
  6401. }
  6402. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6403. }
  6404. #endif
  6405. #ifdef WOLFSSL_TIRTOS
  6406. Task_yield();
  6407. #endif
  6408. ((func_args*)args)->return_code = TEST_SUCCESS;
  6409. }
  6410. if (callbacks->on_result)
  6411. callbacks->on_result(ssl);
  6412. wolfSSL_shutdown(ssl);
  6413. cleanup:
  6414. wolfSSL_free(ssl);
  6415. wolfSSL_CTX_free(ctx);
  6416. CloseSocket(cfd);
  6417. #ifdef WOLFSSL_TIRTOS
  6418. fdCloseSession(Task_self());
  6419. #endif
  6420. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6421. && defined(HAVE_THREAD_LS)
  6422. wc_ecc_fp_free(); /* free per thread cache */
  6423. #endif
  6424. WOLFSSL_RETURN_FROM_THREAD(0);
  6425. }
  6426. /* TLS Client for API unit testing - generic */
  6427. static void run_wolfssl_client(void* args)
  6428. {
  6429. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6430. WOLFSSL_CTX* ctx = NULL;
  6431. WOLFSSL* ssl = NULL;
  6432. SOCKET_T sfd = 0;
  6433. char msg[] = "hello wolfssl server!";
  6434. int len = (int) XSTRLEN(msg);
  6435. char input[1024];
  6436. int ret, err = 0;
  6437. ((func_args*)args)->return_code = TEST_FAIL;
  6438. /* set defaults */
  6439. if (callbacks->caPemFile == NULL)
  6440. callbacks->caPemFile = caCertFile;
  6441. if (callbacks->certPemFile == NULL)
  6442. callbacks->certPemFile = cliCertFile;
  6443. if (callbacks->keyPemFile == NULL)
  6444. callbacks->keyPemFile = cliKeyFile;
  6445. #ifdef WOLFSSL_STATIC_MEMORY
  6446. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6447. callbacks->memSz > 0) {
  6448. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6449. callbacks->mem, callbacks->memSz, 0, 1);
  6450. if (ret != WOLFSSL_SUCCESS) {
  6451. fprintf(stderr, "CTX static new failed %d\n", ret);
  6452. goto cleanup;
  6453. }
  6454. }
  6455. #else
  6456. ctx = wolfSSL_CTX_new(callbacks->method());
  6457. #endif
  6458. if (ctx == NULL) {
  6459. fprintf(stderr, "CTX new failed\n");
  6460. goto cleanup;
  6461. }
  6462. #ifdef WOLFSSL_TIRTOS
  6463. fdOpenSession(Task_self());
  6464. #endif
  6465. if (!callbacks->loadToSSL) {
  6466. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6467. }
  6468. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6469. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6470. #endif
  6471. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6472. WOLFSSL_SUCCESS) {
  6473. goto cleanup;
  6474. }
  6475. if (!callbacks->loadToSSL) {
  6476. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6477. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6478. goto cleanup;
  6479. }
  6480. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6481. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6482. goto cleanup;
  6483. }
  6484. }
  6485. #ifdef HAVE_CRL
  6486. if (callbacks->crlPemFile != NULL) {
  6487. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6488. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6489. goto cleanup;
  6490. }
  6491. }
  6492. #endif
  6493. if (callbacks->ctx_ready)
  6494. callbacks->ctx_ready(ctx);
  6495. ssl = wolfSSL_new(ctx);
  6496. if (wolfSSL_dtls(ssl)) {
  6497. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6498. 1, 0, ssl);
  6499. }
  6500. else {
  6501. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6502. 0, 0, ssl);
  6503. }
  6504. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6505. goto cleanup;
  6506. }
  6507. if (callbacks->loadToSSL) {
  6508. wolfSSL_SetDevId(ssl, callbacks->devId);
  6509. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6510. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6511. goto cleanup;
  6512. }
  6513. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6514. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6515. goto cleanup;
  6516. }
  6517. }
  6518. if (callbacks->ssl_ready)
  6519. callbacks->ssl_ready(ssl);
  6520. #ifdef WOLFSSL_ASYNC_CRYPT
  6521. err = 0; /* Reset error */
  6522. #endif
  6523. do {
  6524. #ifdef WOLFSSL_ASYNC_CRYPT
  6525. if (err == WC_PENDING_E) {
  6526. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6527. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6528. }
  6529. #endif
  6530. ret = wolfSSL_connect(ssl);
  6531. err = wolfSSL_get_error(ssl, ret);
  6532. } while (err == WC_PENDING_E);
  6533. if (ret != WOLFSSL_SUCCESS) {
  6534. char buff[WOLFSSL_MAX_ERROR_SZ];
  6535. fprintf(stderr, "error = %d, %s\n", err,
  6536. wolfSSL_ERR_error_string(err, buff));
  6537. /*err_sys("SSL_connect failed");*/
  6538. }
  6539. else {
  6540. #ifdef WOLFSSL_ASYNC_CRYPT
  6541. err = 0; /* Reset error */
  6542. #endif
  6543. do {
  6544. #ifdef WOLFSSL_ASYNC_CRYPT
  6545. if (err == WC_PENDING_E) {
  6546. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6547. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6548. }
  6549. #endif
  6550. ret = wolfSSL_write(ssl, msg, len);
  6551. err = wolfSSL_get_error(ssl, ret);
  6552. } while (err == WC_PENDING_E);
  6553. if (len != ret)
  6554. goto cleanup;
  6555. #ifdef WOLFSSL_ASYNC_CRYPT
  6556. err = 0; /* Reset error */
  6557. #endif
  6558. do {
  6559. #ifdef WOLFSSL_ASYNC_CRYPT
  6560. if (err == WC_PENDING_E) {
  6561. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6562. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6563. }
  6564. #endif
  6565. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6566. err = wolfSSL_get_error(ssl, ret);
  6567. } while (err == WC_PENDING_E);
  6568. if (ret > 0) {
  6569. input[ret] = '\0'; /* null term */
  6570. fprintf(stderr, "Server response: %s\n", input);
  6571. }
  6572. ((func_args*)args)->return_code = TEST_SUCCESS;
  6573. }
  6574. if (callbacks->on_result)
  6575. callbacks->on_result(ssl);
  6576. cleanup:
  6577. wolfSSL_free(ssl);
  6578. wolfSSL_CTX_free(ctx);
  6579. CloseSocket(sfd);
  6580. #ifdef WOLFSSL_TIRTOS
  6581. fdCloseSession(Task_self());
  6582. #endif
  6583. }
  6584. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6585. static int test_wolfSSL_read_write(void)
  6586. {
  6587. /* The unit testing for read and write shall happen simultaneously, since
  6588. * one can't do anything with one without the other. (Except for a failure
  6589. * test case.) This function will call all the others that will set up,
  6590. * execute, and report their test findings.
  6591. *
  6592. * Set up the success case first. This function will become the template
  6593. * for the other tests. This should eventually be renamed
  6594. *
  6595. * The success case isn't interesting, how can this fail?
  6596. * - Do not give the client context a CA certificate. The connect should
  6597. * fail. Do not need server for this?
  6598. * - Using NULL for the ssl object on server. Do not need client for this.
  6599. * - Using NULL for the ssl object on client. Do not need server for this.
  6600. * - Good ssl objects for client and server. Client write() without server
  6601. * read().
  6602. * - Good ssl objects for client and server. Server write() without client
  6603. * read().
  6604. * - Forgetting the password callback?
  6605. */
  6606. tcp_ready ready;
  6607. func_args client_args;
  6608. func_args server_args;
  6609. THREAD_TYPE serverThread;
  6610. EXPECT_DECLS;
  6611. XMEMSET(&client_args, 0, sizeof(func_args));
  6612. XMEMSET(&server_args, 0, sizeof(func_args));
  6613. #ifdef WOLFSSL_TIRTOS
  6614. fdOpenSession(Task_self());
  6615. #endif
  6616. StartTCP();
  6617. InitTcpReady(&ready);
  6618. #if defined(USE_WINDOWS_API)
  6619. /* use RNG to get random port if using windows */
  6620. ready.port = GetRandomPort();
  6621. #endif
  6622. server_args.signal = &ready;
  6623. client_args.signal = &ready;
  6624. start_thread(test_server_nofail, &server_args, &serverThread);
  6625. wait_tcp_ready(&server_args);
  6626. test_client_nofail(&client_args, NULL);
  6627. join_thread(serverThread);
  6628. ExpectTrue(client_args.return_code);
  6629. ExpectTrue(server_args.return_code);
  6630. FreeTcpReady(&ready);
  6631. #ifdef WOLFSSL_TIRTOS
  6632. fdOpenSession(Task_self());
  6633. #endif
  6634. return EXPECT_RESULT();
  6635. }
  6636. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6637. {
  6638. EXPECT_DECLS;
  6639. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6640. !defined(WOLFSSL_NO_TLS12)
  6641. /* The unit test for session resumption by re-using WOLFSSL object.
  6642. * WOLFSSL object is not cleared after first session. It reuse the object
  6643. * for second connection.
  6644. */
  6645. tcp_ready ready;
  6646. func_args client_args;
  6647. func_args server_args;
  6648. THREAD_TYPE serverThread;
  6649. callback_functions client_cbf;
  6650. callback_functions server_cbf;
  6651. XMEMSET(&client_args, 0, sizeof(func_args));
  6652. XMEMSET(&server_args, 0, sizeof(func_args));
  6653. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6654. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6655. #ifdef WOLFSSL_TIRTOS
  6656. fdOpenSession(Task_self());
  6657. #endif
  6658. StartTCP();
  6659. InitTcpReady(&ready);
  6660. #if defined(USE_WINDOWS_API)
  6661. /* use RNG to get random port if using windows */
  6662. ready.port = GetRandomPort();
  6663. #endif
  6664. client_cbf.method = wolfTLSv1_2_client_method;
  6665. server_cbf.method = wolfTLSv1_2_server_method;
  6666. client_args.callbacks = &client_cbf;
  6667. server_args.callbacks = &server_cbf;
  6668. server_args.signal = &ready;
  6669. client_args.signal = &ready;
  6670. /* the var is used for loop number */
  6671. server_args.argc = 2;
  6672. start_thread(test_server_loop, &server_args, &serverThread);
  6673. wait_tcp_ready(&server_args);
  6674. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6675. join_thread(serverThread);
  6676. ExpectTrue(client_args.return_code);
  6677. ExpectTrue(server_args.return_code);
  6678. FreeTcpReady(&ready);
  6679. #ifdef WOLFSSL_TIRTOS
  6680. fdOpenSession(Task_self());
  6681. #endif
  6682. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6683. * !defined(WOLFSSL_TLS13) */
  6684. return EXPECT_RESULT();
  6685. }
  6686. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6687. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6688. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6689. WOLFSSL_CTX* ctx)
  6690. {
  6691. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6692. myVerifyAction = VERIFY_USE_PREVERFIY;
  6693. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6694. return TEST_SUCCESS;
  6695. }
  6696. #endif
  6697. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6698. {
  6699. EXPECT_DECLS;
  6700. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6701. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6702. test_ssl_cbf client_cbf;
  6703. test_ssl_cbf server_cbf;
  6704. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6705. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6706. #ifdef WOLFSSL_TLS13
  6707. client_cbf.method = wolfTLSv1_3_client_method;
  6708. #endif /* WOLFSSL_TLS13 */
  6709. client_cbf.ctx_ready =
  6710. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6711. /* test case 1 verify depth is equal to peer chain */
  6712. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6713. &server_cbf, NULL), TEST_SUCCESS);
  6714. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6715. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6716. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6717. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6718. return EXPECT_RESULT();
  6719. }
  6720. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6721. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6722. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6723. WOLFSSL_CTX* ctx)
  6724. {
  6725. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6726. myVerifyAction = VERIFY_USE_PREVERFIY;
  6727. wolfSSL_CTX_set_verify_depth(ctx, 1);
  6728. return TEST_SUCCESS;
  6729. }
  6730. #endif
  6731. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6732. {
  6733. EXPECT_DECLS;
  6734. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6735. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6736. test_ssl_cbf client_cbf;
  6737. test_ssl_cbf server_cbf;
  6738. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6739. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6740. #ifdef WOLFSSL_TLS13
  6741. client_cbf.method = wolfTLSv1_3_client_method;
  6742. #endif /* WOLFSSL_TLS13 */
  6743. client_cbf.ctx_ready =
  6744. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6745. /* test case 2
  6746. * verify depth is zero, number of peer's chain is 2.
  6747. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6748. * callback.
  6749. */
  6750. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6751. &server_cbf, NULL), TEST_SUCCESS);
  6752. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6753. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6754. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6755. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6756. return EXPECT_RESULT();
  6757. }
  6758. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6759. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6760. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6761. WOLFSSL_CTX* ctx)
  6762. {
  6763. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6764. myVerifyAction = VERIFY_USE_PREVERFIY;
  6765. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6766. return TEST_SUCCESS;
  6767. }
  6768. #endif
  6769. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6770. {
  6771. EXPECT_DECLS;
  6772. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6773. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6774. test_ssl_cbf client_cbf;
  6775. test_ssl_cbf server_cbf;
  6776. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6777. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6778. #ifdef WOLFSSL_TLS13
  6779. client_cbf.method = wolfTLSv1_3_client_method;
  6780. #endif /* WOLFSSL_TLS13 */
  6781. client_cbf.ctx_ready =
  6782. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6783. /* test case 3
  6784. * verify depth is zero, number of peer's chain is 2
  6785. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6786. * therefore, handshake becomes failure.
  6787. */
  6788. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6789. &server_cbf, NULL), TEST_SUCCESS);
  6790. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6791. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6792. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6793. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6794. return EXPECT_RESULT();
  6795. }
  6796. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6797. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6798. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6799. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6800. {
  6801. EXPECT_DECLS;
  6802. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6803. return EXPECT_RESULT();
  6804. }
  6805. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6806. {
  6807. EXPECT_DECLS;
  6808. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6809. return EXPECT_RESULT();
  6810. }
  6811. #endif
  6812. static int test_wolfSSL_CTX_set_cipher_list(void)
  6813. {
  6814. EXPECT_DECLS;
  6815. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6816. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6817. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6818. WOLFSSL_CTX* ctxClient = NULL;
  6819. WOLFSSL* sslClient = NULL;
  6820. test_ssl_cbf client_cbf;
  6821. test_ssl_cbf server_cbf;
  6822. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6823. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6824. server_cbf.method = wolfTLSv1_2_server_method;
  6825. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6826. client_cbf.method = wolfTLSv1_2_client_method;
  6827. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6828. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6829. &server_cbf, NULL), TEST_SUCCESS);
  6830. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6831. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6832. /* check with cipher string that has '+' */
  6833. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6834. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6835. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6836. /* check for the existence of an ECDHE ECDSA cipher suite */
  6837. if (EXPECT_SUCCESS()) {
  6838. int i = 0;
  6839. int found = 0;
  6840. const char* suite;
  6841. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6842. WOLFSSL_CIPHER* current;
  6843. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6844. do {
  6845. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6846. if (current) {
  6847. suite = wolfSSL_CIPHER_get_name(current);
  6848. if (suite && XSTRSTR(suite, "ECDSA")) {
  6849. found = 1;
  6850. break;
  6851. }
  6852. }
  6853. } while (current);
  6854. ExpectIntEQ(found, 1);
  6855. }
  6856. wolfSSL_free(sslClient);
  6857. wolfSSL_CTX_free(ctxClient);
  6858. #endif
  6859. return EXPECT_RESULT();
  6860. }
  6861. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6862. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6863. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  6864. WOLFSSL* ssl)
  6865. {
  6866. EXPECT_DECLS;
  6867. size_t msg_len;
  6868. (void)ctx;
  6869. /* get_finished test */
  6870. /* 1. get own sent message */
  6871. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6872. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6873. ExpectIntGE(msg_len, 0);
  6874. /* 2. get peer message */
  6875. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6876. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6877. ExpectIntGE(msg_len, 0);
  6878. return EXPECT_RESULT();
  6879. }
  6880. #endif
  6881. static int test_wolfSSL_get_finished(void)
  6882. {
  6883. EXPECT_DECLS;
  6884. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6885. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6886. test_ssl_cbf client_cbf;
  6887. test_ssl_cbf server_cbf;
  6888. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6889. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6890. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6891. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  6892. TEST_SUCCESS);
  6893. /* test received msg vs sent msg */
  6894. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6895. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6896. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  6897. return EXPECT_RESULT();
  6898. }
  6899. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6900. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6901. !defined(NO_SESSION_CACHE)
  6902. /* Sessions to restore/store */
  6903. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6904. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6905. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6906. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6907. {
  6908. /* Don't store sessions. Lookup is still enabled. */
  6909. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6910. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6911. #ifdef OPENSSL_EXTRA
  6912. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6913. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6914. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6915. #endif
  6916. /* Require both peers to provide certs */
  6917. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6918. }
  6919. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6920. {
  6921. WOLFSSL_SESSION** sess;
  6922. if (wolfSSL_is_server(ssl))
  6923. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6924. else
  6925. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6926. if (*sess == NULL) {
  6927. #ifdef NO_SESSION_CACHE_REF
  6928. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6929. #else
  6930. /* Test for backwards compatibility */
  6931. if (wolfSSL_is_server(ssl)) {
  6932. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6933. }
  6934. else {
  6935. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6936. }
  6937. #endif
  6938. /* Now save the session in the internal store to make it available
  6939. * for lookup. For TLS 1.3, we can't save the session without
  6940. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6941. * session from cache. */
  6942. if (wolfSSL_is_server(ssl)
  6943. #ifndef WOLFSSL_TICKET_HAVE_ID
  6944. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6945. #endif
  6946. )
  6947. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6948. *sess), WOLFSSL_SUCCESS);
  6949. }
  6950. else {
  6951. /* If we have a session retrieved then remaining connections should be
  6952. * resuming on that session */
  6953. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6954. }
  6955. /* Save CTX to be able to decrypt tickets */
  6956. if (wolfSSL_is_server(ssl) &&
  6957. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6958. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6959. = wolfSSL_get_SSL_CTX(ssl));
  6960. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6961. WOLFSSL_SUCCESS);
  6962. }
  6963. #ifdef SESSION_CERTS
  6964. #ifndef WOLFSSL_TICKET_HAVE_ID
  6965. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6966. wolfSSL_session_reused(ssl))
  6967. #endif
  6968. {
  6969. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6970. * for all connections. TLS 1.3 only has tickets so if we don't
  6971. * include the session id in the ticket then the certificates
  6972. * will not be available on resumption. */
  6973. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6974. AssertNotNull(peer);
  6975. wolfSSL_X509_free(peer);
  6976. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6977. #ifdef OPENSSL_EXTRA
  6978. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6979. #endif
  6980. }
  6981. #endif /* SESSION_CERTS */
  6982. }
  6983. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6984. {
  6985. /* Set the session to reuse for the client */
  6986. AssertIntEQ(wolfSSL_set_session(ssl,
  6987. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6988. }
  6989. #endif
  6990. static int test_wolfSSL_CTX_add_session(void)
  6991. {
  6992. EXPECT_DECLS;
  6993. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6994. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6995. !defined(NO_SESSION_CACHE)
  6996. tcp_ready ready;
  6997. func_args client_args;
  6998. func_args server_args;
  6999. THREAD_TYPE serverThread;
  7000. callback_functions client_cb;
  7001. callback_functions server_cb;
  7002. method_provider methods[][2] = {
  7003. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7004. !defined(NO_DES3))
  7005. /* Without AES there are almost no ciphersuites available. This leads
  7006. * to no ciphersuites being available and an error. */
  7007. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7008. #endif
  7009. #ifndef WOLFSSL_NO_TLS12
  7010. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7011. #endif
  7012. /* Needs the default ticket callback since it is tied to the
  7013. * connection context and this makes it easy to carry over the ticket
  7014. * crypto context between connections */
  7015. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7016. defined(HAVE_SESSION_TICKET)
  7017. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7018. #endif
  7019. };
  7020. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7021. size_t i, j;
  7022. for (i = 0; i < methodsLen; i++) {
  7023. /* First run creates a connection while the second+ run will attempt
  7024. * to resume the connection. The trick is that the internal cache
  7025. * is turned off. wolfSSL_CTX_add_session should put the session in
  7026. * the cache anyway. */
  7027. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7028. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7029. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7030. #ifdef NO_SESSION_CACHE_REF
  7031. for (j = 0; j < 4; j++) {
  7032. #else
  7033. /* The session may be overwritten in this case. Do only one resumption
  7034. * to stop this test from failing intermittently. */
  7035. for (j = 0; j < 2; j++) {
  7036. #endif
  7037. #ifdef WOLFSSL_TIRTOS
  7038. fdOpenSession(Task_self());
  7039. #endif
  7040. StartTCP();
  7041. InitTcpReady(&ready);
  7042. XMEMSET(&client_args, 0, sizeof(func_args));
  7043. XMEMSET(&server_args, 0, sizeof(func_args));
  7044. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7045. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7046. client_cb.method = methods[i][0];
  7047. server_cb.method = methods[i][1];
  7048. server_args.signal = &ready;
  7049. server_args.callbacks = &server_cb;
  7050. client_args.signal = &ready;
  7051. client_args.callbacks = &client_cb;
  7052. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7053. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7054. server_cb.isSharedCtx = 1;
  7055. }
  7056. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7057. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7058. if (j != 0)
  7059. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7060. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7061. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7062. server_cb.ticNoInit = 1; /* Use default builtin */
  7063. start_thread(test_server_nofail, &server_args, &serverThread);
  7064. wait_tcp_ready(&server_args);
  7065. test_client_nofail(&client_args, NULL);
  7066. join_thread(serverThread);
  7067. ExpectTrue(client_args.return_code);
  7068. ExpectTrue(server_args.return_code);
  7069. FreeTcpReady(&ready);
  7070. if (EXPECT_FAIL())
  7071. break;
  7072. }
  7073. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7074. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7075. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7076. if (EXPECT_FAIL())
  7077. break;
  7078. }
  7079. #endif
  7080. return EXPECT_RESULT();
  7081. }
  7082. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7083. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7084. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7085. defined(HAVE_SESSION_TICKET) && \
  7086. !defined(TITAN_SESSION_CACHE) && \
  7087. !defined(HUGE_SESSION_CACHE) && \
  7088. !defined(BIG_SESSION_CACHE) && \
  7089. !defined(MEDIUM_SESSION_CACHE)
  7090. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7091. /* Sessions to restore/store */
  7092. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7093. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7094. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7095. static int twcase_new_session_called = 0;
  7096. static int twcase_remove_session_called = 0;
  7097. static int twcase_get_session_called = 0;
  7098. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7099. #define SESSION_CACHE_SIZE 33
  7100. typedef struct {
  7101. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7102. WOLFSSL_SESSION* value;
  7103. } hashTable_entry;
  7104. typedef struct {
  7105. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7106. size_t capacity; /* size of entries */
  7107. size_t length; /* number of items in the hash table */
  7108. wolfSSL_Mutex htLock; /* lock */
  7109. }hashTable;
  7110. static hashTable server_sessionCache;
  7111. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7112. {
  7113. int i;
  7114. unsigned int len;
  7115. (void)ssl;
  7116. /*
  7117. * This example uses a hash table.
  7118. * Steps you should take for a non-demo code:
  7119. * - acquire a lock for the file named according to the session id
  7120. * - open the file
  7121. * - encrypt and write the SSL_SESSION object to the file
  7122. * - release the lock
  7123. *
  7124. * Return:
  7125. * 0: The callback does not wish to hold a reference of the sess
  7126. * 1: The callback wants to hold a reference of the sess. The callback is
  7127. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7128. */
  7129. if (sess == NULL)
  7130. return 0;
  7131. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7132. return 0;
  7133. }
  7134. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7135. if (server_sessionCache.entries[i].value == NULL) {
  7136. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7137. server_sessionCache.entries[i].value = sess;
  7138. server_sessionCache.length++;
  7139. break;
  7140. }
  7141. }
  7142. ++twcase_new_session_called;
  7143. wc_UnLockMutex(&server_sessionCache.htLock);
  7144. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7145. twcase_new_session_called);
  7146. return 1;
  7147. }
  7148. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7149. {
  7150. int i;
  7151. (void)ctx;
  7152. (void)sess;
  7153. if (sess == NULL)
  7154. return;
  7155. /*
  7156. * This example uses a hash table.
  7157. * Steps you should take for a non-demo code:
  7158. * - acquire a lock for the file named according to the session id
  7159. * - remove the file
  7160. * - release the lock
  7161. */
  7162. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7163. return;
  7164. }
  7165. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7166. if (server_sessionCache.entries[i].key != NULL &&
  7167. XMEMCMP(server_sessionCache.entries[i].key,
  7168. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7169. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7170. server_sessionCache.entries[i].value = NULL;
  7171. server_sessionCache.entries[i].key = NULL;
  7172. server_sessionCache.length--;
  7173. break;
  7174. }
  7175. }
  7176. ++twcase_remove_session_called;
  7177. wc_UnLockMutex(&server_sessionCache.htLock);
  7178. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7179. twcase_remove_session_called);
  7180. }
  7181. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7182. const unsigned char *id, int len, int *ref)
  7183. {
  7184. int i;
  7185. (void)ssl;
  7186. (void)id;
  7187. (void)len;
  7188. /*
  7189. * This example uses a hash table.
  7190. * Steps you should take for a non-demo code:
  7191. * - acquire a lock for the file named according to the session id in the
  7192. * 2nd arg
  7193. * - read and decrypt contents of file and create a new SSL_SESSION
  7194. * - object release the lock
  7195. * - return the new session object
  7196. */
  7197. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7198. ++twcase_get_session_called);
  7199. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7200. * be responsible for the pointer then set to 0. */
  7201. *ref = 1;
  7202. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7203. if (server_sessionCache.entries[i].key != NULL &&
  7204. XMEMCMP(server_sessionCache.entries[i].key, id,
  7205. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7206. return server_sessionCache.entries[i].value;
  7207. }
  7208. }
  7209. return NULL;
  7210. }
  7211. static int twcase_get_sessionCb_cleanup(void)
  7212. {
  7213. int i;
  7214. int cnt = 0;
  7215. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7216. * for freeing sessions */
  7217. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7218. if (server_sessionCache.entries[i].value != NULL) {
  7219. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7220. cnt++;
  7221. }
  7222. }
  7223. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7224. cnt);
  7225. return TEST_SUCCESS;
  7226. }
  7227. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7228. {
  7229. EXPECT_DECLS;
  7230. /* off - Disable internal cache */
  7231. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7232. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7233. #ifdef OPENSSL_EXTRA
  7234. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7235. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7236. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7237. #endif
  7238. /* off - Do not setup external cache */
  7239. /* Require both peers to provide certs */
  7240. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7241. return EXPECT_RESULT();
  7242. }
  7243. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7244. {
  7245. /* on - internal cache is on by default */
  7246. /* off - Do not setup external cache */
  7247. /* Require both peers to provide certs */
  7248. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7249. return TEST_SUCCESS;
  7250. }
  7251. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7252. {
  7253. EXPECT_DECLS;
  7254. /* off - Disable internal cache */
  7255. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7256. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7257. #ifdef OPENSSL_EXTRA
  7258. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7259. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7260. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7261. #endif
  7262. /* on - Enable external cache */
  7263. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7264. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7265. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7266. /* Require both peers to provide certs */
  7267. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7268. return EXPECT_RESULT();
  7269. }
  7270. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7271. {
  7272. /* on - internal cache is on by default */
  7273. /* on - Enable external cache */
  7274. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7275. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7276. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7277. /* Require both peers to provide certs */
  7278. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7279. return TEST_SUCCESS;
  7280. }
  7281. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7282. {
  7283. /* on - internal cache is on by default */
  7284. /* on - Enable external cache */
  7285. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7286. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7287. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7288. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7289. /* Require both peers to provide certs */
  7290. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7291. return TEST_SUCCESS;
  7292. }
  7293. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7294. {
  7295. EXPECT_DECLS;
  7296. WOLFSSL_SESSION** sess;
  7297. if (wolfSSL_is_server(ssl))
  7298. sess = &twcase_server_first_session_ptr;
  7299. else
  7300. return TEST_SUCCESS;
  7301. if (*sess == NULL) {
  7302. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7303. /* Now save the session in the internal store to make it available
  7304. * for lookup. For TLS 1.3, we can't save the session without
  7305. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7306. * session from cache. */
  7307. if (wolfSSL_is_server(ssl)
  7308. #ifndef WOLFSSL_TICKET_HAVE_ID
  7309. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7310. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7311. #endif
  7312. ) {
  7313. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7314. *sess), WOLFSSL_SUCCESS);
  7315. }
  7316. }
  7317. /* Save CTX to be able to decrypt tickets */
  7318. if (twcase_server_current_ctx_ptr == NULL) {
  7319. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7320. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7321. WOLFSSL_SUCCESS);
  7322. }
  7323. #ifdef SESSION_CERTS
  7324. #ifndef WOLFSSL_TICKET_HAVE_ID
  7325. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7326. wolfSSL_session_reused(ssl))
  7327. #endif
  7328. {
  7329. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7330. * for all connections. TLS 1.3 only has tickets so if we don't
  7331. * include the session id in the ticket then the certificates
  7332. * will not be available on resumption. */
  7333. WOLFSSL_X509* peer = NULL;
  7334. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7335. wolfSSL_X509_free(peer);
  7336. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7337. }
  7338. #endif
  7339. return EXPECT_RESULT();
  7340. }
  7341. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7342. {
  7343. EXPECT_DECLS;
  7344. WOLFSSL_SESSION** sess;
  7345. sess = &twcase_client_first_session_ptr;
  7346. if (*sess == NULL) {
  7347. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7348. }
  7349. else {
  7350. /* If we have a session retrieved then remaining connections should be
  7351. * resuming on that session */
  7352. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7353. }
  7354. #ifdef SESSION_CERTS
  7355. #ifndef WOLFSSL_TICKET_HAVE_ID
  7356. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7357. wolfSSL_session_reused(ssl))
  7358. #endif
  7359. {
  7360. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7361. ExpectNotNull(peer);
  7362. wolfSSL_X509_free(peer);
  7363. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7364. #ifdef OPENSSL_EXTRA
  7365. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7366. #endif
  7367. }
  7368. #endif
  7369. return EXPECT_RESULT();
  7370. }
  7371. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7372. {
  7373. EXPECT_DECLS;
  7374. /* Set the session to reuse for the client */
  7375. ExpectNotNull(ssl);
  7376. ExpectNotNull(twcase_client_first_session_ptr);
  7377. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7378. WOLFSSL_SUCCESS);
  7379. return EXPECT_RESULT();
  7380. }
  7381. struct test_add_session_ext_params {
  7382. method_provider client_meth;
  7383. method_provider server_meth;
  7384. const char* tls_version;
  7385. };
  7386. static int test_wolfSSL_CTX_add_session_ext(
  7387. struct test_add_session_ext_params* param)
  7388. {
  7389. EXPECT_DECLS;
  7390. /* Test the default 33 sessions */
  7391. int j;
  7392. /* Clear cache before starting */
  7393. wolfSSL_CTX_flush_sessions(NULL, -1);
  7394. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7395. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7396. return BAD_MUTEX_E;
  7397. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7398. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7399. for (j = 0; j < 5; j++) {
  7400. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7401. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7402. test_ssl_cbf client_cb;
  7403. test_ssl_cbf server_cb;
  7404. (void)dtls;
  7405. /* Test five cache configurations */
  7406. twcase_client_first_session_ptr = NULL;
  7407. twcase_server_first_session_ptr = NULL;
  7408. twcase_server_current_ctx_ptr = NULL;
  7409. twcase_new_session_called = 0;
  7410. twcase_remove_session_called = 0;
  7411. twcase_get_session_called = 0;
  7412. /* connection 1 - first connection */
  7413. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7414. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7415. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7416. client_cb.method = param->client_meth;
  7417. server_cb.method = param->server_meth;
  7418. if (dtls)
  7419. client_cb.doUdp = server_cb.doUdp = 1;
  7420. /* Setup internal and external cache */
  7421. switch (j) {
  7422. case 0:
  7423. /* SSL_OP_NO_TICKET stateful ticket case */
  7424. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7425. break;
  7426. case 1:
  7427. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7428. break;
  7429. case 2:
  7430. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7431. break;
  7432. case 3:
  7433. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7434. break;
  7435. case 4:
  7436. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7437. break;
  7438. }
  7439. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7440. /* Add session to internal cache and save SSL session for testing */
  7441. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7442. /* Save client SSL session for testing */
  7443. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7444. server_cb.ticNoInit = 1; /* Use default builtin */
  7445. /* Don't free/release ctx */
  7446. server_cb.ctx = twcase_server_current_ctx_ptr;
  7447. server_cb.isSharedCtx = 1;
  7448. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7449. &server_cb, NULL), TEST_SUCCESS);
  7450. ExpectIntEQ(twcase_get_session_called, 0);
  7451. if (EXPECT_FAIL()) {
  7452. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7453. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7454. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7455. break;
  7456. }
  7457. switch (j) {
  7458. case 0:
  7459. case 1:
  7460. case 2:
  7461. /* cache cannot be searched with out a connection */
  7462. /* Add a new session */
  7463. ExpectIntEQ(twcase_new_session_called, 1);
  7464. /* In twcase_server_sess_ctx_pre_shutdown
  7465. * wolfSSL_CTX_add_session which evicts the existing session
  7466. * in cache and adds it back in */
  7467. ExpectIntLE(twcase_remove_session_called, 1);
  7468. break;
  7469. case 3:
  7470. case 4:
  7471. /* no external cache */
  7472. ExpectIntEQ(twcase_new_session_called, 0);
  7473. ExpectIntEQ(twcase_remove_session_called, 0);
  7474. break;
  7475. }
  7476. /* connection 2 - session resume */
  7477. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7478. twcase_new_session_called = 0;
  7479. twcase_remove_session_called = 0;
  7480. twcase_get_session_called = 0;
  7481. server_cb.on_result = 0;
  7482. client_cb.on_result = 0;
  7483. server_cb.ticNoInit = 1; /* Use default builtin */
  7484. server_cb.ctx = twcase_server_current_ctx_ptr;
  7485. /* try session resumption */
  7486. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7487. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7488. &server_cb, NULL), TEST_SUCCESS);
  7489. /* Clear cache before checking */
  7490. wolfSSL_CTX_flush_sessions(NULL, -1);
  7491. switch (j) {
  7492. case 0:
  7493. if (tls13) {
  7494. /* (D)TLSv1.3 stateful case */
  7495. /* cache hit */
  7496. /* DTLS accesses cache once for stateless parsing and
  7497. * once for stateful parsing */
  7498. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7499. /* (D)TLSv1.3 creates a new ticket,
  7500. * updates both internal and external cache */
  7501. ExpectIntEQ(twcase_new_session_called, 1);
  7502. /* A new session ID is created for a new ticket */
  7503. ExpectIntEQ(twcase_remove_session_called, 2);
  7504. }
  7505. else {
  7506. /* non (D)TLSv1.3 case, no update */
  7507. /* DTLS accesses cache once for stateless parsing and
  7508. * once for stateful parsing */
  7509. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7510. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7511. #else
  7512. ExpectIntEQ(twcase_get_session_called, 1);
  7513. #endif
  7514. ExpectIntEQ(twcase_new_session_called, 0);
  7515. /* Called on session added in
  7516. * twcase_server_sess_ctx_pre_shutdown */
  7517. ExpectIntEQ(twcase_remove_session_called, 1);
  7518. }
  7519. break;
  7520. case 1:
  7521. if (tls13) {
  7522. /* (D)TLSv1.3 case */
  7523. /* cache hit */
  7524. ExpectIntEQ(twcase_get_session_called, 1);
  7525. /* (D)TLSv1.3 creates a new ticket,
  7526. * updates both internal and external cache */
  7527. ExpectIntEQ(twcase_new_session_called, 1);
  7528. /* Called on session added in
  7529. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7530. ExpectIntEQ(twcase_remove_session_called, 1);
  7531. }
  7532. else {
  7533. /* non (D)TLSv1.3 case */
  7534. /* cache hit */
  7535. /* DTLS accesses cache once for stateless parsing and
  7536. * once for stateful parsing */
  7537. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7538. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7539. #else
  7540. ExpectIntEQ(twcase_get_session_called, 1);
  7541. #endif
  7542. ExpectIntEQ(twcase_new_session_called, 0);
  7543. /* Called on session added in
  7544. * twcase_server_sess_ctx_pre_shutdown */
  7545. ExpectIntEQ(twcase_remove_session_called, 1);
  7546. }
  7547. break;
  7548. case 2:
  7549. if (tls13) {
  7550. /* (D)TLSv1.3 case */
  7551. /* cache hit */
  7552. ExpectIntEQ(twcase_get_session_called, 1);
  7553. /* (D)TLSv1.3 creates a new ticket,
  7554. * updates both internal and external cache */
  7555. ExpectIntEQ(twcase_new_session_called, 1);
  7556. /* Called on session added in
  7557. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7558. ExpectIntEQ(twcase_remove_session_called, 1);
  7559. }
  7560. else {
  7561. /* non (D)TLSv1.3 case */
  7562. /* cache hit */
  7563. /* DTLS accesses cache once for stateless parsing and
  7564. * once for stateful parsing */
  7565. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7566. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7567. #else
  7568. ExpectIntEQ(twcase_get_session_called, 1);
  7569. #endif
  7570. ExpectIntEQ(twcase_new_session_called, 0);
  7571. /* Called on session added in
  7572. * twcase_server_sess_ctx_pre_shutdown */
  7573. ExpectIntEQ(twcase_remove_session_called, 1);
  7574. }
  7575. break;
  7576. case 3:
  7577. case 4:
  7578. /* no external cache */
  7579. ExpectIntEQ(twcase_get_session_called, 0);
  7580. ExpectIntEQ(twcase_new_session_called, 0);
  7581. ExpectIntEQ(twcase_remove_session_called, 0);
  7582. break;
  7583. }
  7584. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7585. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7586. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7587. if (EXPECT_FAIL())
  7588. break;
  7589. }
  7590. twcase_get_sessionCb_cleanup();
  7591. XMEMSET(&server_sessionCache.entries, 0,
  7592. sizeof(server_sessionCache.entries));
  7593. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7594. wc_FreeMutex(&server_sessionCache.htLock);
  7595. return EXPECT_RESULT();
  7596. }
  7597. #endif
  7598. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7599. {
  7600. EXPECT_DECLS;
  7601. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7602. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7603. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7604. defined(HAVE_SESSION_TICKET) && \
  7605. !defined(TITAN_SESSION_CACHE) && \
  7606. !defined(HUGE_SESSION_CACHE) && \
  7607. !defined(BIG_SESSION_CACHE) && \
  7608. !defined(MEDIUM_SESSION_CACHE)
  7609. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7610. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7611. struct test_add_session_ext_params param[1] = {
  7612. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7613. };
  7614. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7615. #endif
  7616. #endif
  7617. return EXPECT_RESULT();
  7618. }
  7619. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7620. {
  7621. EXPECT_DECLS;
  7622. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7623. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7624. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7625. defined(HAVE_SESSION_TICKET) && \
  7626. !defined(TITAN_SESSION_CACHE) && \
  7627. !defined(HUGE_SESSION_CACHE) && \
  7628. !defined(BIG_SESSION_CACHE) && \
  7629. !defined(MEDIUM_SESSION_CACHE)
  7630. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7631. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7632. #ifdef WOLFSSL_DTLS13
  7633. struct test_add_session_ext_params param[1] = {
  7634. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7635. };
  7636. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7637. #endif
  7638. #endif
  7639. #endif
  7640. return EXPECT_RESULT();
  7641. }
  7642. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  7643. {
  7644. EXPECT_DECLS;
  7645. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7646. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7647. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7648. defined(HAVE_SESSION_TICKET) && \
  7649. !defined(TITAN_SESSION_CACHE) && \
  7650. !defined(HUGE_SESSION_CACHE) && \
  7651. !defined(BIG_SESSION_CACHE) && \
  7652. !defined(MEDIUM_SESSION_CACHE)
  7653. #ifndef WOLFSSL_NO_TLS12
  7654. struct test_add_session_ext_params param[1] = {
  7655. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7656. };
  7657. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7658. #endif
  7659. #endif
  7660. return EXPECT_RESULT();
  7661. }
  7662. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7663. {
  7664. EXPECT_DECLS;
  7665. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7666. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7667. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7668. defined(HAVE_SESSION_TICKET) && \
  7669. !defined(TITAN_SESSION_CACHE) && \
  7670. !defined(HUGE_SESSION_CACHE) && \
  7671. !defined(BIG_SESSION_CACHE) && \
  7672. !defined(MEDIUM_SESSION_CACHE)
  7673. #ifndef WOLFSSL_NO_TLS12
  7674. #ifdef WOLFSSL_DTLS
  7675. struct test_add_session_ext_params param[1] = {
  7676. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7677. };
  7678. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7679. #endif
  7680. #endif
  7681. #endif
  7682. return EXPECT_RESULT();
  7683. }
  7684. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7685. {
  7686. EXPECT_DECLS;
  7687. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7688. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7689. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7690. defined(HAVE_SESSION_TICKET) && \
  7691. !defined(TITAN_SESSION_CACHE) && \
  7692. !defined(HUGE_SESSION_CACHE) && \
  7693. !defined(BIG_SESSION_CACHE) && \
  7694. !defined(MEDIUM_SESSION_CACHE)
  7695. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7696. !defined(NO_DES3))
  7697. struct test_add_session_ext_params param[1] = {
  7698. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7699. };
  7700. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7701. #endif
  7702. #endif
  7703. return EXPECT_RESULT();
  7704. }
  7705. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7706. {
  7707. EXPECT_DECLS;
  7708. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7709. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7710. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7711. defined(HAVE_SESSION_TICKET) && \
  7712. !defined(TITAN_SESSION_CACHE) && \
  7713. !defined(HUGE_SESSION_CACHE) && \
  7714. !defined(BIG_SESSION_CACHE) && \
  7715. !defined(MEDIUM_SESSION_CACHE)
  7716. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7717. !defined(NO_DES3))
  7718. #ifdef WOLFSSL_DTLS
  7719. struct test_add_session_ext_params param[1] = {
  7720. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7721. };
  7722. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7723. #endif
  7724. #endif
  7725. #endif
  7726. return EXPECT_RESULT();
  7727. }
  7728. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7729. /* canned export of a session using older version 3 */
  7730. static unsigned char version_3[] = {
  7731. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7732. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7733. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7734. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7735. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7736. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7737. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7738. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7739. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7740. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7741. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7742. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7743. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7744. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7745. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7746. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7747. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7748. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7749. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7750. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7751. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7752. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7753. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7754. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7755. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7756. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7757. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7758. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7759. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7760. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7761. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7762. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7763. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7764. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7765. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7766. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7767. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7768. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7769. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7770. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7771. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7772. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7773. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7774. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7775. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7776. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7777. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7778. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7779. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7780. 0x2E, 0x31, 0xED, 0x4F
  7781. };
  7782. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7783. static int test_wolfSSL_dtls_export(void)
  7784. {
  7785. EXPECT_DECLS;
  7786. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7787. tcp_ready ready;
  7788. func_args client_args;
  7789. func_args server_args;
  7790. THREAD_TYPE serverThread;
  7791. callback_functions server_cbf;
  7792. callback_functions client_cbf;
  7793. #ifdef WOLFSSL_TIRTOS
  7794. fdOpenSession(Task_self());
  7795. #endif
  7796. InitTcpReady(&ready);
  7797. #if defined(USE_WINDOWS_API)
  7798. /* use RNG to get random port if using windows */
  7799. ready.port = GetRandomPort();
  7800. #endif
  7801. /* set using dtls */
  7802. XMEMSET(&client_args, 0, sizeof(func_args));
  7803. XMEMSET(&server_args, 0, sizeof(func_args));
  7804. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7805. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7806. server_cbf.method = wolfDTLSv1_2_server_method;
  7807. client_cbf.method = wolfDTLSv1_2_client_method;
  7808. server_args.callbacks = &server_cbf;
  7809. client_args.callbacks = &client_cbf;
  7810. server_args.signal = &ready;
  7811. client_args.signal = &ready;
  7812. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7813. wait_tcp_ready(&server_args);
  7814. run_wolfssl_client(&client_args);
  7815. join_thread(serverThread);
  7816. ExpectTrue(client_args.return_code);
  7817. ExpectTrue(server_args.return_code);
  7818. FreeTcpReady(&ready);
  7819. #ifdef WOLFSSL_TIRTOS
  7820. fdOpenSession(Task_self());
  7821. #endif
  7822. if (EXPECT_SUCCESS()) {
  7823. SOCKET_T sockfd = 0;
  7824. WOLFSSL_CTX* ctx = NULL;
  7825. WOLFSSL* ssl = NULL;
  7826. char msg[64] = "hello wolfssl!";
  7827. char reply[1024];
  7828. int msgSz = (int)XSTRLEN(msg);
  7829. byte *session, *window;
  7830. unsigned int sessionSz = 0;
  7831. unsigned int windowSz = 0;
  7832. #ifndef TEST_IPV6
  7833. struct sockaddr_in peerAddr;
  7834. #else
  7835. struct sockaddr_in6 peerAddr;
  7836. #endif /* TEST_IPV6 */
  7837. int i;
  7838. /* Set ctx to DTLS 1.2 */
  7839. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7840. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7841. /* test importing version 3 */
  7842. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7843. /* test importing bad length and bad version */
  7844. version_3[2] += 1;
  7845. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7846. version_3[2] -= 1; version_3[1] = 0XA0;
  7847. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7848. wolfSSL_free(ssl);
  7849. wolfSSL_CTX_free(ctx);
  7850. /* check storing client state after connection and storing window only */
  7851. #ifdef WOLFSSL_TIRTOS
  7852. fdOpenSession(Task_self());
  7853. #endif
  7854. InitTcpReady(&ready);
  7855. #if defined(USE_WINDOWS_API)
  7856. /* use RNG to get random port if using windows */
  7857. ready.port = GetRandomPort();
  7858. #endif
  7859. /* set using dtls */
  7860. XMEMSET(&server_args, 0, sizeof(func_args));
  7861. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7862. server_cbf.method = wolfDTLSv1_2_server_method;
  7863. server_cbf.doUdp = 1;
  7864. server_args.callbacks = &server_cbf;
  7865. server_args.argc = 3; /* set loop_count to 3 */
  7866. server_args.signal = &ready;
  7867. start_thread(test_server_nofail, &server_args, &serverThread);
  7868. wait_tcp_ready(&server_args);
  7869. /* create and connect with client */
  7870. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  7871. ExpectIntEQ(WOLFSSL_SUCCESS,
  7872. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  7873. ExpectIntEQ(WOLFSSL_SUCCESS,
  7874. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  7875. ExpectIntEQ(WOLFSSL_SUCCESS,
  7876. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  7877. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  7878. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7879. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7880. /* store server information connected too */
  7881. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  7882. #ifndef TEST_IPV6
  7883. peerAddr.sin_family = AF_INET;
  7884. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  7885. peerAddr.sin_port = XHTONS(server_args.signal->port);
  7886. #else
  7887. peerAddr.sin6_family = AF_INET6;
  7888. ExpectIntEQ(
  7889. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  7890. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  7891. #endif
  7892. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7893. WOLFSSL_SUCCESS);
  7894. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  7895. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  7896. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7897. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  7898. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7899. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7900. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  7901. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7902. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7903. wolfSSL_free(ssl);
  7904. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  7905. /* restore state */
  7906. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7907. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  7908. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  7909. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7910. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7911. WOLFSSL_SUCCESS);
  7912. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7913. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7914. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7915. wolfSSL_free(ssl);
  7916. }
  7917. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7918. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7919. wolfSSL_CTX_free(ctx);
  7920. fprintf(stderr, "done and waiting for server\n");
  7921. join_thread(serverThread);
  7922. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  7923. FreeTcpReady(&ready);
  7924. #ifdef WOLFSSL_TIRTOS
  7925. fdOpenSession(Task_self());
  7926. #endif
  7927. }
  7928. #endif
  7929. return EXPECT_RESULT();
  7930. }
  7931. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7932. #ifdef WOLFSSL_TLS13
  7933. static const byte canned_client_tls13_session[] = {
  7934. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  7935. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7936. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  7937. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7938. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7939. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7940. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7941. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  7942. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7943. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7944. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7945. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  7946. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7947. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7948. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7949. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7950. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7951. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7952. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7953. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7954. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7955. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7956. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7957. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7958. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7959. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7960. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7961. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7962. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7963. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  7964. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  7965. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  7966. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7967. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7968. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7969. 0x00, 0x03
  7970. };
  7971. static const byte canned_server_tls13_session[] = {
  7972. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  7973. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7974. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  7975. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7976. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7977. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7978. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7979. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  7980. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7981. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7982. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7983. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  7984. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7985. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7986. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7987. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7988. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7989. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7990. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7991. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7992. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7993. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7994. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7995. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7996. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7997. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7998. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7999. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8000. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8001. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8002. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8003. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8004. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8005. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8006. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8007. 0x00, 0x04
  8008. };
  8009. #endif /* WOLFSSL_TLS13 */
  8010. static const byte canned_client_session[] = {
  8011. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8012. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8013. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8014. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8015. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8016. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8018. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8019. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8020. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8021. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8022. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8023. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8024. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8025. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8026. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8027. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8028. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8029. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8030. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8031. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8032. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8033. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8034. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8035. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8036. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8037. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8038. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8039. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8040. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8041. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8042. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8043. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8044. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8045. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8046. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8047. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8048. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8049. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8050. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8051. 0x00, 0x03
  8052. };
  8053. static const byte canned_server_session[] = {
  8054. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8055. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8056. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8057. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8058. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8059. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8060. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8061. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8062. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8063. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8064. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8065. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8066. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8067. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8068. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8069. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8070. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8071. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8072. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8073. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8074. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8075. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8076. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8077. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8078. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8079. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8080. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8081. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8082. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8083. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8084. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8085. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8086. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8087. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8088. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8089. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8090. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8091. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8092. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8093. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8094. 0x00, 0x04
  8095. };
  8096. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8097. {
  8098. SOCKET_T sockfd = 0;
  8099. SOCKET_T clientfd = 0;
  8100. word16 port;
  8101. callback_functions* cbf;
  8102. WOLFSSL_CTX* ctx = 0;
  8103. WOLFSSL* ssl = 0;
  8104. char msg[] = "I hear you fa shizzle!";
  8105. char input[1024];
  8106. int idx;
  8107. #ifdef WOLFSSL_TIRTOS
  8108. fdOpenSession(Task_self());
  8109. #endif
  8110. ((func_args*)args)->return_code = TEST_FAIL;
  8111. cbf = ((func_args*)args)->callbacks;
  8112. #if defined(USE_WINDOWS_API)
  8113. port = ((func_args*)args)->signal->port;
  8114. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8115. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8116. /* Let tcp_listen assign port */
  8117. port = 0;
  8118. #else
  8119. /* Use default port */
  8120. port = wolfSSLPort;
  8121. #endif
  8122. /* do it here to detect failure */
  8123. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8124. CloseSocket(sockfd);
  8125. {
  8126. WOLFSSL_METHOD* method = NULL;
  8127. if (cbf != NULL && cbf->method != NULL) {
  8128. method = cbf->method();
  8129. }
  8130. else {
  8131. method = wolfTLSv1_2_server_method();
  8132. }
  8133. ctx = wolfSSL_CTX_new(method);
  8134. }
  8135. if (ctx == NULL) {
  8136. goto done;
  8137. }
  8138. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8139. /* call ctx setup callback */
  8140. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8141. cbf->ctx_ready(ctx);
  8142. }
  8143. ssl = wolfSSL_new(ctx);
  8144. if (ssl == NULL) {
  8145. goto done;
  8146. }
  8147. wolfSSL_set_fd(ssl, clientfd);
  8148. /* call ssl setup callback */
  8149. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8150. cbf->ssl_ready(ssl);
  8151. }
  8152. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8153. if (idx > 0) {
  8154. input[idx] = '\0';
  8155. fprintf(stderr, "Client message export/import: %s\n", input);
  8156. }
  8157. else {
  8158. fprintf(stderr, "ret = %d error = %d\n", idx,
  8159. wolfSSL_get_error(ssl, idx));
  8160. goto done;
  8161. }
  8162. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8163. /*err_sys("SSL_write failed");*/
  8164. WOLFSSL_RETURN_FROM_THREAD(0);
  8165. }
  8166. #ifdef WOLFSSL_TIRTOS
  8167. Task_yield();
  8168. #endif
  8169. ((func_args*)args)->return_code = TEST_SUCCESS;
  8170. done:
  8171. wolfSSL_shutdown(ssl);
  8172. wolfSSL_free(ssl);
  8173. wolfSSL_CTX_free(ctx);
  8174. CloseSocket(clientfd);
  8175. #ifdef WOLFSSL_TIRTOS
  8176. fdCloseSession(Task_self());
  8177. #endif
  8178. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8179. && defined(HAVE_THREAD_LS)
  8180. wc_ecc_fp_free(); /* free per thread cache */
  8181. #endif
  8182. #if defined(HAVE_SESSION_TICKET) && \
  8183. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8184. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8185. OpenSSLTicketCleanup();
  8186. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8187. TicketCleanup();
  8188. #endif
  8189. #endif
  8190. WOLFSSL_RETURN_FROM_THREAD(0);
  8191. }
  8192. static void load_tls12_canned_server(WOLFSSL* ssl)
  8193. {
  8194. int clientfd = wolfSSL_get_fd(ssl);
  8195. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8196. sizeof(canned_server_session)), sizeof(canned_server_session));
  8197. wolfSSL_set_fd(ssl, clientfd);
  8198. }
  8199. #ifdef WOLFSSL_TLS13
  8200. static void load_tls13_canned_server(WOLFSSL* ssl)
  8201. {
  8202. int clientfd = wolfSSL_get_fd(ssl);
  8203. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8204. sizeof(canned_server_tls13_session)),
  8205. sizeof(canned_server_tls13_session));
  8206. wolfSSL_set_fd(ssl, clientfd);
  8207. }
  8208. #endif
  8209. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8210. static int test_wolfSSL_tls_export_run(int v)
  8211. {
  8212. EXPECT_DECLS;
  8213. SOCKET_T sockfd = 0;
  8214. WOLFSSL_CTX* ctx = 0;
  8215. WOLFSSL* ssl = 0;
  8216. char msg[64] = "hello wolfssl!";
  8217. char reply[1024];
  8218. word32 replySz;
  8219. int msgSz = (int)XSTRLEN(msg);
  8220. const byte* clientSession = NULL;
  8221. int clientSessionSz = 0;
  8222. tcp_ready ready;
  8223. func_args server_args;
  8224. THREAD_TYPE serverThread;
  8225. callback_functions server_cbf;
  8226. #ifdef WOLFSSL_TIRTOS
  8227. fdOpenSession(Task_self());
  8228. #endif
  8229. InitTcpReady(&ready);
  8230. #if defined(USE_WINDOWS_API)
  8231. /* use RNG to get random port if using windows */
  8232. ready.port = GetRandomPort();
  8233. #endif
  8234. XMEMSET(&server_args, 0, sizeof(func_args));
  8235. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8236. switch (v) {
  8237. case WOLFSSL_TLSV1_2:
  8238. server_cbf.method = wolfTLSv1_2_server_method;
  8239. server_cbf.ssl_ready = load_tls12_canned_server;
  8240. /* setup the client side */
  8241. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8242. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8243. clientSession = canned_client_session;
  8244. clientSessionSz = sizeof(canned_client_session);
  8245. break;
  8246. #ifdef WOLFSSL_TLS13
  8247. case WOLFSSL_TLSV1_3:
  8248. server_cbf.method = wolfTLSv1_3_server_method;
  8249. server_cbf.ssl_ready = load_tls13_canned_server;
  8250. /* setup the client side */
  8251. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8252. clientSession = canned_client_tls13_session;
  8253. clientSessionSz = sizeof(canned_client_tls13_session);
  8254. break;
  8255. #endif
  8256. }
  8257. server_args.callbacks = &server_cbf;
  8258. server_args.signal = &ready;
  8259. start_thread(tls_export_server, &server_args, &serverThread);
  8260. wait_tcp_ready(&server_args);
  8261. #ifdef WOLFSSL_TIRTOS
  8262. fdOpenSession(Task_self());
  8263. #endif
  8264. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8265. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8266. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8267. clientSessionSz);
  8268. replySz = sizeof(reply);
  8269. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8270. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8271. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8272. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8273. #endif
  8274. wolfSSL_set_fd(ssl, sockfd);
  8275. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8276. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8277. wolfSSL_free(ssl);
  8278. wolfSSL_CTX_free(ctx);
  8279. CloseSocket(sockfd);
  8280. #ifdef WOLFSSL_TIRTOS
  8281. fdCloseSession(Task_self());
  8282. #endif
  8283. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8284. && defined(HAVE_THREAD_LS)
  8285. wc_ecc_fp_free(); /* free per thread cache */
  8286. #endif
  8287. join_thread(serverThread);
  8288. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8289. FreeTcpReady(&ready);
  8290. #ifdef WOLFSSL_TIRTOS
  8291. fdOpenSession(Task_self());
  8292. #endif
  8293. return EXPECT_RESULT();
  8294. }
  8295. #endif
  8296. static int test_wolfSSL_tls_export(void)
  8297. {
  8298. int res = TEST_SKIPPED;
  8299. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8300. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8301. #ifdef WOLFSSL_TLS13
  8302. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8303. #endif
  8304. res = TEST_RES_CHECK(1);
  8305. #endif
  8306. return res;
  8307. }
  8308. /*----------------------------------------------------------------------------*
  8309. | TLS extensions tests
  8310. *----------------------------------------------------------------------------*/
  8311. #ifdef ENABLE_TLS_CALLBACK_TEST
  8312. /* Connection test runner - generic */
  8313. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8314. callback_functions* server_callbacks)
  8315. {
  8316. tcp_ready ready;
  8317. func_args client_args;
  8318. func_args server_args;
  8319. THREAD_TYPE serverThread;
  8320. XMEMSET(&client_args, 0, sizeof(func_args));
  8321. XMEMSET(&server_args, 0, sizeof(func_args));
  8322. StartTCP();
  8323. client_args.callbacks = client_callbacks;
  8324. server_args.callbacks = server_callbacks;
  8325. #ifdef WOLFSSL_TIRTOS
  8326. fdOpenSession(Task_self());
  8327. #endif
  8328. /* RUN Server side */
  8329. InitTcpReady(&ready);
  8330. #if defined(USE_WINDOWS_API)
  8331. /* use RNG to get random port if using windows */
  8332. ready.port = GetRandomPort();
  8333. #endif
  8334. server_args.signal = &ready;
  8335. client_args.signal = &ready;
  8336. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8337. wait_tcp_ready(&server_args);
  8338. /* RUN Client side */
  8339. run_wolfssl_client(&client_args);
  8340. join_thread(serverThread);
  8341. FreeTcpReady(&ready);
  8342. #ifdef WOLFSSL_TIRTOS
  8343. fdCloseSession(Task_self());
  8344. #endif
  8345. client_callbacks->return_code = client_args.return_code;
  8346. server_callbacks->return_code = server_args.return_code;
  8347. }
  8348. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8349. #ifdef HAVE_SNI
  8350. static int test_wolfSSL_UseSNI_params(void)
  8351. {
  8352. EXPECT_DECLS;
  8353. #if !defined(NO_WOLFSSL_CLIENT)
  8354. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8355. WOLFSSL *ssl = wolfSSL_new(ctx);
  8356. ExpectNotNull(ctx);
  8357. ExpectNotNull(ssl);
  8358. /* invalid [ctx|ssl] */
  8359. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8361. /* invalid type */
  8362. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8363. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8364. /* invalid data */
  8365. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8366. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8367. /* success case */
  8368. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8369. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8370. wolfSSL_free(ssl);
  8371. wolfSSL_CTX_free(ctx);
  8372. #endif /* !NO_WOLFSSL_CLIENT */
  8373. return EXPECT_RESULT();
  8374. }
  8375. /* BEGIN of connection tests callbacks */
  8376. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8377. {
  8378. AssertIntEQ(WOLFSSL_SUCCESS,
  8379. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8380. }
  8381. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8382. {
  8383. AssertIntEQ(WOLFSSL_SUCCESS,
  8384. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8385. }
  8386. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8387. {
  8388. AssertIntEQ(WOLFSSL_SUCCESS,
  8389. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8390. }
  8391. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8392. {
  8393. use_SNI_at_ssl(ssl);
  8394. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8395. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8396. }
  8397. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8398. {
  8399. use_SNI_at_ssl(ssl);
  8400. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8401. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8402. }
  8403. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8404. {
  8405. use_SNI_at_ctx(ctx);
  8406. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8407. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8408. }
  8409. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8410. {
  8411. use_SNI_at_ssl(ssl);
  8412. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8413. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8414. }
  8415. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8416. {
  8417. use_SNI_at_ctx(ctx);
  8418. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8419. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8420. }
  8421. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8422. {
  8423. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8424. }
  8425. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8426. {
  8427. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8428. }
  8429. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8430. {
  8431. byte type = WOLFSSL_SNI_HOST_NAME;
  8432. void* request = (void*) &type; /* to be overwritten */
  8433. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8434. AssertNotNull(request);
  8435. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8436. AssertNull(request);
  8437. }
  8438. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8439. {
  8440. byte type = WOLFSSL_SNI_HOST_NAME;
  8441. void* request = NULL;
  8442. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8443. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8444. AssertNotNull(request);
  8445. AssertStrEQ("www.wolfssl.com", (char*)request);
  8446. }
  8447. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8448. {
  8449. byte type = WOLFSSL_SNI_HOST_NAME;
  8450. void* request = NULL;
  8451. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8452. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8453. AssertNotNull(request);
  8454. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8455. }
  8456. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8457. {
  8458. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8459. }
  8460. /* END of connection tests callbacks */
  8461. static int test_wolfSSL_UseSNI_connection(void)
  8462. {
  8463. int res = TEST_SKIPPED;
  8464. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8465. callback_functions client_cb;
  8466. callback_functions server_cb;
  8467. size_t i;
  8468. #ifdef WOLFSSL_STATIC_MEMORY
  8469. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8470. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8471. #endif
  8472. struct {
  8473. method_provider client_meth;
  8474. method_provider server_meth;
  8475. #ifdef WOLFSSL_STATIC_MEMORY
  8476. wolfSSL_method_func client_meth_ex;
  8477. wolfSSL_method_func server_meth_ex;
  8478. #endif
  8479. } methods[] = {
  8480. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8481. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8482. #ifdef WOLFSSL_STATIC_MEMORY
  8483. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8484. #endif
  8485. },
  8486. #endif
  8487. #ifndef WOLFSSL_NO_TLS12
  8488. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8489. #ifdef WOLFSSL_STATIC_MEMORY
  8490. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8491. #endif
  8492. },
  8493. #endif
  8494. #ifdef WOLFSSL_TLS13
  8495. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8496. #ifdef WOLFSSL_STATIC_MEMORY
  8497. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8498. #endif
  8499. },
  8500. #endif
  8501. };
  8502. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8503. for (i = 0; i < methodsSz; i++) {
  8504. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8505. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8506. client_cb.method = methods[i].client_meth;
  8507. server_cb.method = methods[i].server_meth;
  8508. client_cb.devId = testDevId;
  8509. server_cb.devId = testDevId;
  8510. #ifdef WOLFSSL_STATIC_MEMORY
  8511. client_cb.method_ex = methods[i].client_meth_ex;
  8512. server_cb.method_ex = methods[i].server_meth_ex;
  8513. client_cb.mem = cliMem;
  8514. client_cb.memSz = (word32)sizeof(cliMem);
  8515. server_cb.mem = svrMem;
  8516. server_cb.memSz = (word32)sizeof(svrMem);;
  8517. #endif
  8518. /* success case at ctx */
  8519. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8520. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8521. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8522. test_wolfSSL_client_server(&client_cb, &server_cb);
  8523. /* success case at ssl */
  8524. fprintf(stderr, "\tsuccess case at ssl\n");
  8525. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8526. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8527. test_wolfSSL_client_server(&client_cb, &server_cb);
  8528. /* default mismatch behavior */
  8529. fprintf(stderr, "\tdefault mismatch behavior\n");
  8530. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8531. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8532. test_wolfSSL_client_server(&client_cb, &server_cb);
  8533. /* continue on mismatch */
  8534. fprintf(stderr, "\tcontinue on mismatch\n");
  8535. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8536. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8537. test_wolfSSL_client_server(&client_cb, &server_cb);
  8538. /* fake answer on mismatch */
  8539. fprintf(stderr, "\tfake answer on mismatch\n");
  8540. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8541. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8542. test_wolfSSL_client_server(&client_cb, &server_cb);
  8543. /* sni abort - success */
  8544. fprintf(stderr, "\tsni abort - success\n");
  8545. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8546. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8547. test_wolfSSL_client_server(&client_cb, &server_cb);
  8548. /* sni abort - abort when absent (ctx) */
  8549. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8550. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8551. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8552. test_wolfSSL_client_server(&client_cb, &server_cb);
  8553. /* sni abort - abort when absent (ssl) */
  8554. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8555. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8556. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8557. test_wolfSSL_client_server(&client_cb, &server_cb);
  8558. /* sni abort - success when overwritten */
  8559. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8560. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8561. 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;
  8562. test_wolfSSL_client_server(&client_cb, &server_cb);
  8563. /* sni abort - success when allowing mismatches */
  8564. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8565. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8566. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8567. test_wolfSSL_client_server(&client_cb, &server_cb);
  8568. }
  8569. res = TEST_RES_CHECK(1);
  8570. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8571. return res;
  8572. }
  8573. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8574. {
  8575. EXPECT_DECLS;
  8576. byte buff[] = { /* www.paypal.com */
  8577. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8578. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8579. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8580. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8581. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8582. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8583. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8584. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8585. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8586. };
  8587. byte buff2[] = { /* api.textmate.org */
  8588. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8589. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8590. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8591. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8592. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8593. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8594. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8595. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8596. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8597. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8598. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8599. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8600. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8601. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8602. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8603. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8604. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8605. };
  8606. byte buff3[] = { /* no sni extension */
  8607. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8608. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8609. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8610. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8611. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8612. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8613. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8614. };
  8615. byte buff4[] = { /* last extension has zero size */
  8616. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8617. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8618. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8619. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8620. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8621. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8622. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8623. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8624. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8625. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8626. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8627. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8628. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8629. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8630. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8631. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8632. 0x12, 0x00, 0x00
  8633. };
  8634. byte buff5[] = { /* SSL v2.0 client hello */
  8635. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8636. /* dummy bytes below, just to pass size check */
  8637. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8638. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8639. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8640. };
  8641. byte result[32] = {0};
  8642. word32 length = 32;
  8643. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8644. 0, result, &length));
  8645. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8646. 0, result, &length));
  8647. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8648. 1, result, &length));
  8649. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8650. 0, result, &length));
  8651. buff[0] = 0x16;
  8652. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8653. 0, result, &length));
  8654. buff[1] = 0x03;
  8655. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8656. sizeof(buff), 0, result, &length));
  8657. buff[2] = 0x03;
  8658. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8659. sizeof(buff), 0, result, &length));
  8660. buff[4] = 0x64;
  8661. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8662. 0, result, &length));
  8663. if (EXPECT_SUCCESS())
  8664. result[length] = 0;
  8665. ExpectStrEQ("www.paypal.com", (const char*) result);
  8666. length = 32;
  8667. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8668. 0, result, &length));
  8669. if (EXPECT_SUCCESS())
  8670. result[length] = 0;
  8671. ExpectStrEQ("api.textmate.org", (const char*) result);
  8672. /* SSL v2.0 tests */
  8673. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8674. sizeof(buff5), 0, result, &length));
  8675. buff5[2] = 0x02;
  8676. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8677. sizeof(buff5), 0, result, &length));
  8678. buff5[2] = 0x01; buff5[6] = 0x08;
  8679. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8680. sizeof(buff5), 0, result, &length));
  8681. buff5[6] = 0x09; buff5[8] = 0x01;
  8682. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8683. sizeof(buff5), 0, result, &length));
  8684. return EXPECT_RESULT();
  8685. }
  8686. #endif /* HAVE_SNI */
  8687. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8688. static int test_wolfSSL_UseTrustedCA(void)
  8689. {
  8690. EXPECT_DECLS;
  8691. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8692. && !defined(NO_RSA)
  8693. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8694. WOLFSSL_CTX *ctx = NULL;
  8695. WOLFSSL *ssl = NULL;
  8696. byte id[20];
  8697. #ifndef NO_WOLFSSL_SERVER
  8698. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8699. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8700. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8701. #else
  8702. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8703. #endif
  8704. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8705. XMEMSET(id, 0, sizeof(id));
  8706. /* error cases */
  8707. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8708. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8709. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8710. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8711. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8712. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8713. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8714. #ifdef NO_SHA
  8715. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8716. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8717. #endif
  8718. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8719. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8720. /* success cases */
  8721. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8722. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8723. #ifndef NO_SHA
  8724. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8725. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8726. #endif
  8727. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8728. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8729. wolfSSL_free(ssl);
  8730. wolfSSL_CTX_free(ctx);
  8731. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8732. #endif /* HAVE_TRUSTED_CA */
  8733. return EXPECT_RESULT();
  8734. }
  8735. static int test_wolfSSL_UseMaxFragment(void)
  8736. {
  8737. EXPECT_DECLS;
  8738. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8739. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8740. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8741. #ifndef NO_WOLFSSL_SERVER
  8742. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8743. #else
  8744. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8745. #endif
  8746. WOLFSSL *ssl = NULL;
  8747. #ifdef OPENSSL_EXTRA
  8748. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8749. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8750. #else
  8751. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8752. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8753. #endif
  8754. #ifndef NO_WOLFSSL_SERVER
  8755. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8756. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8757. #endif
  8758. ExpectNotNull(ctx);
  8759. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8760. #ifdef OPENSSL_EXTRA
  8761. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8762. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8763. #else
  8764. UseMaxFragment = wolfSSL_UseMaxFragment;
  8765. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8766. #endif
  8767. /* error cases */
  8768. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8769. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8770. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8771. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8772. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8773. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8774. /* success case */
  8775. #ifdef OPENSSL_EXTRA
  8776. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8777. #else
  8778. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8779. #endif
  8780. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8781. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8782. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8783. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8784. #ifdef OPENSSL_EXTRA
  8785. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8786. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8787. #else
  8788. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8789. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8790. #endif
  8791. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8792. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8793. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8794. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8795. #ifdef OPENSSL_EXTRA
  8796. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8797. #else
  8798. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8799. #endif
  8800. wolfSSL_free(ssl);
  8801. wolfSSL_CTX_free(ctx);
  8802. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8803. #endif
  8804. return EXPECT_RESULT();
  8805. }
  8806. static int test_wolfSSL_UseTruncatedHMAC(void)
  8807. {
  8808. EXPECT_DECLS;
  8809. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8810. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8811. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8812. #ifndef NO_WOLFSSL_SERVER
  8813. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8814. #else
  8815. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8816. #endif
  8817. WOLFSSL *ssl = NULL;
  8818. ExpectNotNull(ctx);
  8819. #ifndef NO_WOLFSSL_SERVER
  8820. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8821. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8822. #endif
  8823. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8824. /* error cases */
  8825. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8826. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8827. /* success case */
  8828. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8829. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8830. wolfSSL_free(ssl);
  8831. wolfSSL_CTX_free(ctx);
  8832. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8833. #endif
  8834. return EXPECT_RESULT();
  8835. }
  8836. static int test_wolfSSL_UseSupportedCurve(void)
  8837. {
  8838. EXPECT_DECLS;
  8839. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8840. !defined(NO_TLS)
  8841. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8842. WOLFSSL *ssl = wolfSSL_new(ctx);
  8843. ExpectNotNull(ctx);
  8844. ExpectNotNull(ssl);
  8845. /* error cases */
  8846. ExpectIntNE(WOLFSSL_SUCCESS,
  8847. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8848. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8849. ExpectIntNE(WOLFSSL_SUCCESS,
  8850. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8851. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8852. /* success case */
  8853. ExpectIntEQ(WOLFSSL_SUCCESS,
  8854. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8855. ExpectIntEQ(WOLFSSL_SUCCESS,
  8856. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8857. wolfSSL_free(ssl);
  8858. wolfSSL_CTX_free(ctx);
  8859. #endif
  8860. return EXPECT_RESULT();
  8861. }
  8862. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  8863. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8864. {
  8865. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  8866. }
  8867. static void use_ALPN_all(WOLFSSL* ssl)
  8868. {
  8869. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8870. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8871. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8872. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8873. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8874. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8875. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8876. }
  8877. static void use_ALPN_all_continue(WOLFSSL* ssl)
  8878. {
  8879. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8880. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8881. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8882. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8883. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8884. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8885. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8886. }
  8887. static void use_ALPN_one(WOLFSSL* ssl)
  8888. {
  8889. /* spdy/2 */
  8890. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8891. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8892. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8893. }
  8894. static void use_ALPN_unknown(WOLFSSL* ssl)
  8895. {
  8896. /* http/2.0 */
  8897. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8898. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8899. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8900. }
  8901. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  8902. {
  8903. /* http/2.0 */
  8904. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8905. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8906. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8907. }
  8908. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  8909. {
  8910. /* spdy/3 */
  8911. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8912. char *proto = NULL;
  8913. word16 protoSz = 0;
  8914. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8915. /* check value */
  8916. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  8917. if (proto) {
  8918. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  8919. }
  8920. }
  8921. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  8922. {
  8923. char *proto = NULL;
  8924. word16 protoSz = 0;
  8925. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  8926. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8927. /* check value */
  8928. AssertIntEQ(1, (0 == protoSz));
  8929. AssertIntEQ(1, (NULL == proto));
  8930. }
  8931. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  8932. {
  8933. /* http/1.1 */
  8934. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8935. char *proto;
  8936. word16 protoSz = 0;
  8937. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8938. /* check value */
  8939. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8940. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8941. }
  8942. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  8943. {
  8944. /* spdy/2 */
  8945. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8946. char *proto;
  8947. word16 protoSz = 0;
  8948. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8949. /* check value */
  8950. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8951. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8952. }
  8953. static void verify_ALPN_client_list(WOLFSSL* ssl)
  8954. {
  8955. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8956. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8957. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8958. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8959. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8960. char *clist = NULL;
  8961. word16 clistSz = 0;
  8962. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  8963. &clistSz));
  8964. /* check value */
  8965. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  8966. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  8967. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  8968. }
  8969. static int test_wolfSSL_UseALPN_connection(void)
  8970. {
  8971. int res = TEST_SKIPPED;
  8972. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8973. callback_functions client_cb;
  8974. callback_functions server_cb;
  8975. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8976. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8977. client_cb.method = wolfSSLv23_client_method;
  8978. server_cb.method = wolfSSLv23_server_method;
  8979. client_cb.devId = testDevId;
  8980. server_cb.devId = testDevId;
  8981. /* success case same list */
  8982. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8983. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  8984. test_wolfSSL_client_server(&client_cb, &server_cb);
  8985. /* success case only one for server */
  8986. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  8987. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  8988. test_wolfSSL_client_server(&client_cb, &server_cb);
  8989. /* success case only one for client */
  8990. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  8991. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  8992. test_wolfSSL_client_server(&client_cb, &server_cb);
  8993. /* success case none for client */
  8994. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8995. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  8996. test_wolfSSL_client_server(&client_cb, &server_cb);
  8997. /* success case mismatch behavior but option 'continue' set */
  8998. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  8999. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9000. test_wolfSSL_client_server(&client_cb, &server_cb);
  9001. /* success case read protocol send by client */
  9002. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9003. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9004. test_wolfSSL_client_server(&client_cb, &server_cb);
  9005. /* mismatch behavior with same list
  9006. * the first and only this one must be taken */
  9007. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9008. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9009. test_wolfSSL_client_server(&client_cb, &server_cb);
  9010. /* default mismatch behavior */
  9011. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9012. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9013. test_wolfSSL_client_server(&client_cb, &server_cb);
  9014. res = TEST_RES_CHECK(1);
  9015. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9016. return res;
  9017. }
  9018. static int test_wolfSSL_UseALPN_params(void)
  9019. {
  9020. EXPECT_DECLS;
  9021. #ifndef NO_WOLFSSL_CLIENT
  9022. /* "http/1.1" */
  9023. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9024. /* "spdy/1" */
  9025. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9026. /* "spdy/2" */
  9027. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9028. /* "spdy/3" */
  9029. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9030. char buff[256];
  9031. word32 idx;
  9032. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9033. WOLFSSL *ssl = wolfSSL_new(ctx);
  9034. ExpectNotNull(ctx);
  9035. ExpectNotNull(ssl);
  9036. /* error cases */
  9037. ExpectIntNE(WOLFSSL_SUCCESS,
  9038. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9039. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9040. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9041. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9042. /* success case */
  9043. /* http1 only */
  9044. ExpectIntEQ(WOLFSSL_SUCCESS,
  9045. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9046. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9047. /* http1, spdy1 */
  9048. XMEMCPY(buff, http1, sizeof(http1));
  9049. idx = sizeof(http1);
  9050. buff[idx++] = ',';
  9051. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9052. idx += sizeof(spdy1);
  9053. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9054. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9055. /* http1, spdy2, spdy1 */
  9056. XMEMCPY(buff, http1, sizeof(http1));
  9057. idx = sizeof(http1);
  9058. buff[idx++] = ',';
  9059. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9060. idx += sizeof(spdy2);
  9061. buff[idx++] = ',';
  9062. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9063. idx += sizeof(spdy1);
  9064. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9065. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9066. /* spdy3, http1, spdy2, spdy1 */
  9067. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9068. idx = sizeof(spdy3);
  9069. buff[idx++] = ',';
  9070. XMEMCPY(buff+idx, http1, sizeof(http1));
  9071. idx += sizeof(http1);
  9072. buff[idx++] = ',';
  9073. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9074. idx += sizeof(spdy2);
  9075. buff[idx++] = ',';
  9076. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9077. idx += sizeof(spdy1);
  9078. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9079. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9080. wolfSSL_free(ssl);
  9081. wolfSSL_CTX_free(ctx);
  9082. #endif
  9083. return EXPECT_RESULT();
  9084. }
  9085. #endif /* HAVE_ALPN */
  9086. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9087. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9088. {
  9089. unsigned char p[] = {
  9090. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9091. 6, 's', 'p', 'd', 'y', '/', '2',
  9092. 6, 's', 'p', 'd', 'y', '/', '1',
  9093. };
  9094. unsigned char p_len = sizeof(p);
  9095. int ret;
  9096. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9097. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9098. AssertIntEQ(ret, 0);
  9099. #else
  9100. AssertIntEQ(ret, SSL_SUCCESS);
  9101. #endif
  9102. }
  9103. static void set_alpn_protos(SSL* ssl)
  9104. {
  9105. unsigned char p[] = {
  9106. 6, 's', 'p', 'd', 'y', '/', '3',
  9107. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9108. 6, 's', 'p', 'd', 'y', '/', '2',
  9109. 6, 's', 'p', 'd', 'y', '/', '1',
  9110. };
  9111. unsigned char p_len = sizeof(p);
  9112. int ret;
  9113. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9114. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9115. AssertIntEQ(ret, 0);
  9116. #else
  9117. AssertIntEQ(ret, SSL_SUCCESS);
  9118. #endif
  9119. }
  9120. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9121. {
  9122. /* "spdy/3" */
  9123. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9124. const unsigned char *proto;
  9125. unsigned int protoSz = 0;
  9126. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9127. /* check value */
  9128. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9129. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9130. }
  9131. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9132. {
  9133. /* "http/1.1" */
  9134. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9135. const unsigned char *proto;
  9136. unsigned int protoSz = 0;
  9137. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9138. /* check value */
  9139. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9140. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9141. }
  9142. static int test_wolfSSL_set_alpn_protos(void)
  9143. {
  9144. int res = TEST_SKIPPED;
  9145. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9146. callback_functions client_cb;
  9147. callback_functions server_cb;
  9148. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9149. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9150. client_cb.method = wolfSSLv23_client_method;
  9151. server_cb.method = wolfSSLv23_server_method;
  9152. client_cb.devId = testDevId;
  9153. server_cb.devId = testDevId;
  9154. /* use CTX_alpn_protos */
  9155. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9156. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9157. test_wolfSSL_client_server(&client_cb, &server_cb);
  9158. /* use set_alpn_protos */
  9159. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9160. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9161. test_wolfSSL_client_server(&client_cb, &server_cb);
  9162. res = TEST_SUCCESS;
  9163. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9164. return res;
  9165. }
  9166. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9167. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9168. {
  9169. EXPECT_DECLS;
  9170. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9171. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9172. WOLFSSL *ssl = wolfSSL_new(ctx);
  9173. ExpectNotNull(ctx);
  9174. ExpectNotNull(ssl);
  9175. /* error cases */
  9176. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9177. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9178. /* success cases */
  9179. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9180. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9181. wolfSSL_free(ssl);
  9182. wolfSSL_CTX_free(ctx);
  9183. #endif
  9184. return EXPECT_RESULT();
  9185. }
  9186. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9187. {
  9188. EXPECT_DECLS;
  9189. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9190. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9191. WOLFSSL *ssl = wolfSSL_new(ctx);
  9192. ExpectNotNull(ctx);
  9193. ExpectNotNull(ssl);
  9194. /* error cases */
  9195. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9196. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9197. /* success cases */
  9198. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9199. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9200. wolfSSL_free(ssl);
  9201. wolfSSL_CTX_free(ctx);
  9202. #endif
  9203. return EXPECT_RESULT();
  9204. }
  9205. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9206. static int test_wolfSSL_SCR_Reconnect(void)
  9207. {
  9208. EXPECT_DECLS;
  9209. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9210. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9211. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9212. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9213. struct test_memio_ctx test_ctx;
  9214. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9215. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9216. byte data;
  9217. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9218. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9219. test_ctx.s_ciphers =
  9220. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9221. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9222. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9223. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9224. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9225. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9226. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9227. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9228. /* WOLFSSL_FATAL_ERROR since it will block */
  9229. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9230. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9231. WOLFSSL_ERROR_WANT_READ);
  9232. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9233. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9234. WOLFSSL_ERROR_WANT_READ);
  9235. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9236. wolfSSL_free(ssl_c);
  9237. ssl_c = NULL;
  9238. wolfSSL_free(ssl_s);
  9239. ssl_s = NULL;
  9240. wolfSSL_CTX_free(ctx_c);
  9241. ctx_c = NULL;
  9242. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9243. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9244. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9245. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9246. wolfSSL_free(ssl_s);
  9247. wolfSSL_free(ssl_c);
  9248. wolfSSL_CTX_free(ctx_s);
  9249. wolfSSL_CTX_free(ctx_c);
  9250. #endif
  9251. return EXPECT_RESULT();
  9252. }
  9253. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9254. (!defined(NO_RSA) || defined(HAVE_ECC))
  9255. /* Called when writing. */
  9256. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9257. {
  9258. (void)ssl;
  9259. (void)buf;
  9260. (void)sz;
  9261. (void)ctx;
  9262. /* Force error return from wolfSSL_accept_TLSv13(). */
  9263. return WANT_WRITE;
  9264. }
  9265. /* Called when reading. */
  9266. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9267. {
  9268. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9269. int len = (int)msg->length;
  9270. (void)ssl;
  9271. (void)sz;
  9272. /* Pass back as much of message as will fit in buffer. */
  9273. if (len > sz)
  9274. len = sz;
  9275. XMEMCPY(buf, msg->buffer, len);
  9276. /* Move over returned data. */
  9277. msg->buffer += len;
  9278. msg->length -= len;
  9279. /* Amount actually copied. */
  9280. return len;
  9281. }
  9282. #endif
  9283. /* Test the detection of duplicate known TLS extensions.
  9284. * Specifically in a ClientHello.
  9285. */
  9286. static int test_tls_ext_duplicate(void)
  9287. {
  9288. EXPECT_DECLS;
  9289. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9290. !defined(NO_FILESYSTEM)
  9291. const unsigned char clientHelloDupTlsExt[] = {
  9292. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9293. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9294. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9295. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9296. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9297. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9298. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9299. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9300. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9301. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9302. 0x00, 0x9e, 0x01, 0x00,
  9303. /* Extensions - duplicate signature algorithms. */
  9304. 0x00, 0x19, 0x00, 0x0d,
  9305. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9306. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9307. /* Supported Versions extension for TLS 1.3. */
  9308. 0x00, 0x2b,
  9309. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9310. };
  9311. WOLFSSL_BUFFER_INFO msg;
  9312. const char* testCertFile;
  9313. const char* testKeyFile;
  9314. WOLFSSL_CTX *ctx = NULL;
  9315. WOLFSSL *ssl = NULL;
  9316. #ifndef NO_RSA
  9317. testCertFile = svrCertFile;
  9318. testKeyFile = svrKeyFile;
  9319. #elif defined(HAVE_ECC)
  9320. testCertFile = eccCertFile;
  9321. testKeyFile = eccKeyFile;
  9322. #endif
  9323. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9324. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9325. WOLFSSL_FILETYPE_PEM));
  9326. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9327. WOLFSSL_FILETYPE_PEM));
  9328. /* Read from 'msg'. */
  9329. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9330. /* No where to send to - dummy sender. */
  9331. wolfSSL_SetIOSend(ctx, DummySend);
  9332. ssl = wolfSSL_new(ctx);
  9333. ExpectNotNull(ssl);
  9334. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9335. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9336. wolfSSL_SetIOReadCtx(ssl, &msg);
  9337. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9338. /* can return duplicate ext error or socket error if the peer closed down
  9339. * while sending alert */
  9340. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9341. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9342. }
  9343. wolfSSL_free(ssl);
  9344. wolfSSL_CTX_free(ctx);
  9345. #endif
  9346. return EXPECT_RESULT();
  9347. }
  9348. /*----------------------------------------------------------------------------*
  9349. | X509 Tests
  9350. *----------------------------------------------------------------------------*/
  9351. static int test_wolfSSL_X509_NAME_get_entry(void)
  9352. {
  9353. EXPECT_DECLS;
  9354. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9355. #if defined(OPENSSL_ALL) || \
  9356. (defined(OPENSSL_EXTRA) && \
  9357. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9358. /* use openssl like name to test mapping */
  9359. X509_NAME_ENTRY* ne;
  9360. X509_NAME* name;
  9361. X509* x509 = NULL;
  9362. #ifndef NO_FILESYSTEM
  9363. ASN1_STRING* asn;
  9364. char* subCN = NULL;
  9365. #endif
  9366. int idx;
  9367. ASN1_OBJECT *object = NULL;
  9368. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9369. defined(WOLFSSL_NGINX)
  9370. #ifndef NO_BIO
  9371. BIO* bio = NULL;
  9372. #endif
  9373. #endif
  9374. #ifndef NO_FILESYSTEM
  9375. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9376. WOLFSSL_FILETYPE_PEM));
  9377. ExpectNotNull(name = X509_get_subject_name(x509));
  9378. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9379. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9380. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9381. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9382. wolfSSL_FreeX509(x509);
  9383. x509 = NULL;
  9384. #endif
  9385. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9386. WOLFSSL_FILETYPE_PEM));
  9387. ExpectNotNull(name = X509_get_subject_name(x509));
  9388. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9389. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9390. defined(WOLFSSL_NGINX)
  9391. #ifndef NO_BIO
  9392. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9393. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9394. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9395. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9396. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9397. BIO_free(bio);
  9398. #endif
  9399. #endif
  9400. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9401. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9402. wolfSSL_FreeX509(x509);
  9403. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9404. #endif /* !NO_CERTS && !NO_RSA */
  9405. return EXPECT_RESULT();
  9406. }
  9407. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9408. static int test_wolfSSL_PKCS12(void)
  9409. {
  9410. EXPECT_DECLS;
  9411. /* .p12 file is encrypted with DES3 */
  9412. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9413. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9414. * Password Key
  9415. */
  9416. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9417. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9418. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9419. byte buf[6000];
  9420. char file[] = "./certs/test-servercert.p12";
  9421. char order[] = "./certs/ecc-rsa-server.p12";
  9422. #ifdef WC_RC2
  9423. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9424. #endif
  9425. char pass[] = "a password";
  9426. const char goodPsw[] = "wolfSSL test";
  9427. const char badPsw[] = "bad";
  9428. #ifdef HAVE_ECC
  9429. WOLFSSL_X509_NAME *subject = NULL;
  9430. WOLFSSL_X509 *x509 = NULL;
  9431. #endif
  9432. XFILE f = XBADFILE;
  9433. int bytes, ret, goodPswLen, badPswLen;
  9434. WOLFSSL_BIO *bio = NULL;
  9435. WOLFSSL_EVP_PKEY *pkey = NULL;
  9436. WC_PKCS12 *pkcs12 = NULL;
  9437. WC_PKCS12 *pkcs12_2 = NULL;
  9438. WOLFSSL_X509 *cert = NULL;
  9439. WOLFSSL_X509 *tmp = NULL;
  9440. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9441. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9442. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9443. WOLFSSL_CTX *ctx = NULL;
  9444. WOLFSSL *ssl = NULL;
  9445. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9446. #endif
  9447. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9448. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9449. if (f != XBADFILE) {
  9450. XFCLOSE(f);
  9451. f = XBADFILE;
  9452. }
  9453. goodPswLen = (int)XSTRLEN(goodPsw);
  9454. badPswLen = (int)XSTRLEN(badPsw);
  9455. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9456. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9457. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9458. ExpectNotNull(pkcs12);
  9459. BIO_free(bio);
  9460. bio = NULL;
  9461. /* check verify MAC directly */
  9462. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9463. /* check verify MAC fail case directly */
  9464. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9465. /* check verify MAC fail case */
  9466. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9467. ExpectNull(pkey);
  9468. ExpectNull(cert);
  9469. /* check parse with no extra certs kept */
  9470. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9471. 1);
  9472. ExpectNotNull(pkey);
  9473. ExpectNotNull(cert);
  9474. wolfSSL_EVP_PKEY_free(pkey);
  9475. pkey = NULL;
  9476. wolfSSL_X509_free(cert);
  9477. cert = NULL;
  9478. /* check parse with extra certs kept */
  9479. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9480. 1);
  9481. ExpectNotNull(pkey);
  9482. ExpectNotNull(cert);
  9483. ExpectNotNull(ca);
  9484. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9485. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9486. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9487. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9488. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9489. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9490. #else
  9491. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9492. #endif
  9493. /* Copy stack structure */
  9494. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9495. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9496. /* CTX now owns the tmp_ca stack structure */
  9497. tmp_ca = NULL;
  9498. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9499. ExpectNotNull(tmp_ca);
  9500. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9501. /* Check that the main cert is also set */
  9502. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9503. ExpectNotNull(ssl = SSL_new(ctx));
  9504. ExpectNotNull(SSL_get_certificate(ssl));
  9505. SSL_free(ssl);
  9506. SSL_CTX_free(ctx);
  9507. ctx = NULL;
  9508. #endif
  9509. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9510. /* should be 2 other certs on stack */
  9511. ExpectNotNull(tmp = sk_X509_pop(ca));
  9512. X509_free(tmp);
  9513. ExpectNotNull(tmp = sk_X509_pop(ca));
  9514. X509_free(tmp);
  9515. ExpectNull(sk_X509_pop(ca));
  9516. EVP_PKEY_free(pkey);
  9517. pkey = NULL;
  9518. X509_free(cert);
  9519. cert = NULL;
  9520. sk_X509_pop_free(ca, X509_free);
  9521. ca = NULL;
  9522. /* check PKCS12_create */
  9523. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9524. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9525. SSL_SUCCESS);
  9526. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9527. -1, -1, 100, -1, 0)));
  9528. EVP_PKEY_free(pkey);
  9529. pkey = NULL;
  9530. X509_free(cert);
  9531. cert = NULL;
  9532. sk_X509_pop_free(ca, NULL);
  9533. ca = NULL;
  9534. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9535. SSL_SUCCESS);
  9536. PKCS12_free(pkcs12_2);
  9537. pkcs12_2 = NULL;
  9538. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9539. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9540. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9541. 2000, 1, 0)));
  9542. EVP_PKEY_free(pkey);
  9543. pkey = NULL;
  9544. X509_free(cert);
  9545. cert = NULL;
  9546. sk_X509_pop_free(ca, NULL);
  9547. ca = NULL;
  9548. /* convert to DER then back and parse */
  9549. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9550. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9551. PKCS12_free(pkcs12_2);
  9552. pkcs12_2 = NULL;
  9553. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9554. BIO_free(bio);
  9555. bio = NULL;
  9556. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9557. SSL_SUCCESS);
  9558. /* should be 2 other certs on stack */
  9559. ExpectNotNull(tmp = sk_X509_pop(ca));
  9560. X509_free(tmp);
  9561. ExpectNotNull(tmp = sk_X509_pop(ca));
  9562. X509_free(tmp);
  9563. ExpectNull(sk_X509_pop(ca));
  9564. #ifndef NO_RC4
  9565. PKCS12_free(pkcs12_2);
  9566. pkcs12_2 = NULL;
  9567. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9568. NID_pbe_WithSHA1And128BitRC4,
  9569. NID_pbe_WithSHA1And128BitRC4,
  9570. 2000, 1, 0)));
  9571. EVP_PKEY_free(pkey);
  9572. pkey = NULL;
  9573. X509_free(cert);
  9574. cert = NULL;
  9575. sk_X509_pop_free(ca, NULL);
  9576. ca = NULL;
  9577. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9578. SSL_SUCCESS);
  9579. #endif /* NO_RC4 */
  9580. EVP_PKEY_free(pkey);
  9581. pkey = NULL;
  9582. X509_free(cert);
  9583. cert = NULL;
  9584. PKCS12_free(pkcs12);
  9585. pkcs12 = NULL;
  9586. PKCS12_free(pkcs12_2);
  9587. pkcs12_2 = NULL;
  9588. sk_X509_pop_free(ca, NULL);
  9589. ca = NULL;
  9590. #ifdef HAVE_ECC
  9591. /* test order of parsing */
  9592. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9593. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9594. if (f != XBADFILE) {
  9595. XFCLOSE(f);
  9596. f = XBADFILE;
  9597. }
  9598. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9599. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9600. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9601. WOLFSSL_SUCCESS);
  9602. /* check use of pkey after parse */
  9603. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9604. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9605. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9606. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9607. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9608. #else
  9609. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9610. #endif
  9611. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9612. SSL_CTX_free(ctx);
  9613. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9614. #endif
  9615. ExpectNotNull(pkey);
  9616. ExpectNotNull(cert);
  9617. ExpectNotNull(ca);
  9618. /* compare subject lines of certificates */
  9619. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9620. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9621. SSL_FILETYPE_PEM));
  9622. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9623. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9624. X509_free(x509);
  9625. x509 = NULL;
  9626. /* test expected fail case */
  9627. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9628. SSL_FILETYPE_PEM));
  9629. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9630. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9631. X509_free(x509);
  9632. x509 = NULL;
  9633. X509_free(cert);
  9634. cert = NULL;
  9635. /* get subject line from ca stack */
  9636. ExpectNotNull(cert = sk_X509_pop(ca));
  9637. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9638. /* compare subject from certificate in ca to expected */
  9639. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9640. SSL_FILETYPE_PEM));
  9641. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9642. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9643. EVP_PKEY_free(pkey);
  9644. pkey = NULL;
  9645. X509_free(x509);
  9646. x509 = NULL;
  9647. X509_free(cert);
  9648. cert = NULL;
  9649. BIO_free(bio);
  9650. bio = NULL;
  9651. PKCS12_free(pkcs12);
  9652. pkcs12 = NULL;
  9653. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9654. ca = NULL;
  9655. /* test order of parsing */
  9656. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9657. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9658. if (f != XBADFILE) {
  9659. XFCLOSE(f);
  9660. f = XBADFILE;
  9661. }
  9662. /* check verify MAC fail case */
  9663. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9664. ExpectNull(pkey);
  9665. ExpectNull(cert);
  9666. /* check parse with no extra certs kept */
  9667. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9668. 1);
  9669. ExpectNotNull(pkey);
  9670. ExpectNotNull(cert);
  9671. wolfSSL_EVP_PKEY_free(pkey);
  9672. pkey = NULL;
  9673. wolfSSL_X509_free(cert);
  9674. cert = NULL;
  9675. /* check parse with extra certs kept */
  9676. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9677. 1);
  9678. ExpectNotNull(pkey);
  9679. ExpectNotNull(cert);
  9680. ExpectNotNull(ca);
  9681. wolfSSL_EVP_PKEY_free(pkey);
  9682. pkey = NULL;
  9683. wolfSSL_X509_free(cert);
  9684. cert = NULL;
  9685. sk_X509_pop_free(ca, NULL);
  9686. ca = NULL;
  9687. PKCS12_free(pkcs12);
  9688. pkcs12 = NULL;
  9689. #endif /* HAVE_ECC */
  9690. #ifdef WC_RC2
  9691. /* test PKCS#12 with RC2 encryption */
  9692. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9693. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9694. if (f != XBADFILE) {
  9695. XFCLOSE(f);
  9696. f = XBADFILE;
  9697. }
  9698. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9699. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9700. /* check verify MAC fail case */
  9701. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9702. ExpectNull(pkey);
  9703. ExpectNull(cert);
  9704. /* check parse with not extra certs kept */
  9705. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9706. WOLFSSL_SUCCESS);
  9707. ExpectNotNull(pkey);
  9708. ExpectNotNull(cert);
  9709. wolfSSL_EVP_PKEY_free(pkey);
  9710. pkey = NULL;
  9711. wolfSSL_X509_free(cert);
  9712. cert = NULL;
  9713. /* check parse with extra certs kept */
  9714. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9715. WOLFSSL_SUCCESS);
  9716. ExpectNotNull(pkey);
  9717. ExpectNotNull(cert);
  9718. ExpectNotNull(ca);
  9719. wolfSSL_EVP_PKEY_free(pkey);
  9720. wolfSSL_X509_free(cert);
  9721. sk_X509_pop_free(ca, NULL);
  9722. BIO_free(bio);
  9723. bio = NULL;
  9724. PKCS12_free(pkcs12);
  9725. pkcs12 = NULL;
  9726. #endif /* WC_RC2 */
  9727. /* Test i2d_PKCS12_bio */
  9728. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9729. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9730. if (f != XBADFILE)
  9731. XFCLOSE(f);
  9732. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9733. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9734. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9735. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9736. PKCS12_free(pkcs12);
  9737. BIO_free(bio);
  9738. (void)order;
  9739. #endif /* OPENSSL_EXTRA */
  9740. #endif /* HAVE_FIPS */
  9741. return EXPECT_RESULT();
  9742. }
  9743. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9744. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9745. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9746. #define TEST_PKCS8_ENC
  9747. #endif
  9748. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9749. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9750. /* used to keep track if FailTestCallback was called */
  9751. static int failTestCallbackCalled = 0;
  9752. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9753. {
  9754. (void)passwd;
  9755. (void)sz;
  9756. (void)rw;
  9757. (void)userdata;
  9758. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9759. failTestCallbackCalled = 1;
  9760. return -1;
  9761. }
  9762. #endif
  9763. static int test_wolfSSL_no_password_cb(void)
  9764. {
  9765. EXPECT_DECLS;
  9766. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9767. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9768. WOLFSSL_CTX* ctx = NULL;
  9769. byte buff[FOURK_BUF];
  9770. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9771. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9772. XFILE f = XBADFILE;
  9773. int bytes = 0;
  9774. #ifndef NO_WOLFSSL_CLIENT
  9775. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9776. #else
  9777. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9778. #endif
  9779. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9780. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9781. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9782. if (f != XBADFILE) {
  9783. XFCLOSE(f);
  9784. f = XBADFILE;
  9785. }
  9786. ExpectIntLE(bytes, sizeof(buff));
  9787. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9788. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9789. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9790. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9791. if (f != XBADFILE)
  9792. XFCLOSE(f);
  9793. ExpectIntLE(bytes, sizeof(buff));
  9794. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9795. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9796. wolfSSL_CTX_free(ctx);
  9797. /* Password callback should not be called by default */
  9798. ExpectIntEQ(failTestCallbackCalled, 0);
  9799. #endif
  9800. return EXPECT_RESULT();
  9801. }
  9802. #ifdef TEST_PKCS8_ENC
  9803. /* for PKCS8 test case */
  9804. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9805. {
  9806. int flag = 0;
  9807. (void)rw;
  9808. if (userdata != NULL) {
  9809. flag = *((int*)userdata); /* user set data */
  9810. }
  9811. switch (flag) {
  9812. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9813. * can be anything the user wishes to be passed to the callback
  9814. * associated with the WOLFSSL_CTX */
  9815. XSTRNCPY(passwd, "yassl123", sz);
  9816. return 8;
  9817. default:
  9818. return BAD_FUNC_ARG;
  9819. }
  9820. }
  9821. #endif /* TEST_PKCS8_ENC */
  9822. /* Testing functions dealing with PKCS8 */
  9823. static int test_wolfSSL_PKCS8(void)
  9824. {
  9825. EXPECT_DECLS;
  9826. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9827. !defined(WOLFCRYPT_ONLY)
  9828. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9829. byte buff[FOURK_BUF];
  9830. byte der[FOURK_BUF];
  9831. #ifndef NO_RSA
  9832. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9833. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9834. #endif
  9835. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9836. #ifdef HAVE_ECC
  9837. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9838. #endif
  9839. XFILE f = XBADFILE;
  9840. int bytes = 0;
  9841. WOLFSSL_CTX* ctx = NULL;
  9842. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9843. int ret;
  9844. ecc_key key;
  9845. word32 x = 0;
  9846. #endif
  9847. #ifdef TEST_PKCS8_ENC
  9848. #if !defined(NO_RSA) && !defined(NO_SHA)
  9849. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9850. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9851. #endif
  9852. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9853. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9854. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9855. #endif
  9856. int flag;
  9857. #endif
  9858. (void)der;
  9859. #ifndef NO_WOLFSSL_CLIENT
  9860. #ifndef WOLFSSL_NO_TLS12
  9861. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9862. #else
  9863. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9864. #endif
  9865. #else
  9866. #ifndef WOLFSSL_NO_TLS12
  9867. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  9868. #else
  9869. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  9870. #endif
  9871. #endif
  9872. #ifdef TEST_PKCS8_ENC
  9873. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  9874. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  9875. flag = 1; /* used by password callback as return code */
  9876. #if !defined(NO_RSA) && !defined(NO_SHA)
  9877. /* test loading PEM PKCS8 encrypted file */
  9878. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  9879. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9880. if (f != XBADFILE) {
  9881. XFCLOSE(f);
  9882. f = XBADFILE;
  9883. }
  9884. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9885. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9886. /* this next case should fail because of password callback return code */
  9887. flag = 0; /* used by password callback as return code */
  9888. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9889. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9890. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9891. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9892. "yassl123"), 0);
  9893. /* test that error value is returned with a bad password */
  9894. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9895. "bad"), 0);
  9896. /* test loading PEM PKCS8 encrypted file */
  9897. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  9898. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9899. if (f != XBADFILE) {
  9900. XFCLOSE(f);
  9901. f = XBADFILE;
  9902. }
  9903. flag = 1; /* used by password callback as return code */
  9904. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9905. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9906. /* this next case should fail because of password callback return code */
  9907. flag = 0; /* used by password callback as return code */
  9908. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9909. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9910. #endif /* !NO_RSA && !NO_SHA */
  9911. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9912. /* test loading PEM PKCS8 encrypted ECC Key file */
  9913. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  9914. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9915. if (f != XBADFILE) {
  9916. XFCLOSE(f);
  9917. f = XBADFILE;
  9918. }
  9919. flag = 1; /* used by password callback as return code */
  9920. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9921. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9922. /* this next case should fail because of password callback return code */
  9923. flag = 0; /* used by password callback as return code */
  9924. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9925. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9926. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9927. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9928. "yassl123"), 0);
  9929. /* test that error value is returned with a bad password */
  9930. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9931. "bad"), 0);
  9932. /* test loading DER PKCS8 encrypted ECC Key file */
  9933. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  9934. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9935. if (f != XBADFILE) {
  9936. XFCLOSE(f);
  9937. f = XBADFILE;
  9938. }
  9939. flag = 1; /* used by password callback as return code */
  9940. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9941. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9942. /* this next case should fail because of password callback return code */
  9943. flag = 0; /* used by password callback as return code */
  9944. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9945. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9946. /* leave flag as "okay" */
  9947. flag = 1;
  9948. #endif /* HAVE_ECC && !NO_SHA */
  9949. #endif /* TEST_PKCS8_ENC */
  9950. #ifndef NO_RSA
  9951. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  9952. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  9953. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9954. if (f != XBADFILE) {
  9955. XFCLOSE(f);
  9956. f = XBADFILE;
  9957. }
  9958. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9959. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9960. /* test loading PEM PKCS8 private key file (not encrypted) */
  9961. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  9962. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9963. if (f != XBADFILE) {
  9964. XFCLOSE(f);
  9965. f = XBADFILE;
  9966. }
  9967. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9968. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9969. #endif /* !NO_RSA */
  9970. /* Test PKCS8 PEM ECC key no crypt */
  9971. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9972. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9973. if (f != XBADFILE) {
  9974. XFCLOSE(f);
  9975. f = XBADFILE;
  9976. }
  9977. #ifdef HAVE_ECC
  9978. /* Test PKCS8 PEM ECC key no crypt */
  9979. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9980. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9981. #ifndef NO_CODING
  9982. /* decrypt PKCS8 PEM to key in DER format */
  9983. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  9984. (word32)sizeof(der), NULL)), 0);
  9985. ret = wc_ecc_init(&key);
  9986. if (ret == 0) {
  9987. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  9988. wc_ecc_free(&key);
  9989. }
  9990. ExpectIntEQ(ret, 0);
  9991. #endif
  9992. /* Test PKCS8 DER ECC key no crypt */
  9993. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9994. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9995. if (f != XBADFILE)
  9996. XFCLOSE(f);
  9997. /* Test using a PKCS8 ECC PEM */
  9998. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9999. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10000. #else
  10001. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10002. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10003. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10004. #endif /* HAVE_ECC */
  10005. wolfSSL_CTX_free(ctx);
  10006. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10007. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10008. return EXPECT_RESULT();
  10009. }
  10010. static int test_wolfSSL_PKCS8_ED25519(void)
  10011. {
  10012. EXPECT_DECLS;
  10013. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10014. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10015. defined(HAVE_ED25519_KEY_IMPORT)
  10016. const byte encPrivKey[] = \
  10017. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10018. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10019. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10020. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10021. "2L9W4v7swXkV+X+a1ww=\n"
  10022. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10023. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10024. byte der[FOURK_BUF];
  10025. WOLFSSL_CTX* ctx = NULL;
  10026. int bytes;
  10027. XMEMSET(der, 0, sizeof(der));
  10028. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10029. (word32)sizeof(der), password)), 0);
  10030. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10031. #ifndef NO_WOLFSSL_SERVER
  10032. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10033. #else
  10034. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10035. #endif
  10036. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10037. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10038. wolfSSL_CTX_free(ctx);
  10039. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10040. #endif
  10041. return EXPECT_RESULT();
  10042. }
  10043. static int test_wolfSSL_PKCS8_ED448(void)
  10044. {
  10045. EXPECT_DECLS;
  10046. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10047. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10048. defined(HAVE_ED448_KEY_IMPORT)
  10049. const byte encPrivKey[] = \
  10050. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10051. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10052. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10053. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10054. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10055. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10056. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10057. byte der[FOURK_BUF];
  10058. WOLFSSL_CTX* ctx = NULL;
  10059. int bytes;
  10060. XMEMSET(der, 0, sizeof(der));
  10061. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10062. (word32)sizeof(der), password)), 0);
  10063. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10064. #ifndef NO_WOLFSSL_SERVER
  10065. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10066. #else
  10067. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10068. #endif
  10069. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10071. wolfSSL_CTX_free(ctx);
  10072. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10073. #endif
  10074. return EXPECT_RESULT();
  10075. }
  10076. /* Testing functions dealing with PKCS5 */
  10077. static int test_wolfSSL_PKCS5(void)
  10078. {
  10079. EXPECT_DECLS;
  10080. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10081. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10082. const char* passwd = "myfipsPa$$W0rd";
  10083. #else
  10084. const char *passwd = "pass1234";
  10085. #endif
  10086. const unsigned char *salt = (unsigned char *)"salt1234";
  10087. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10088. DYNAMIC_TYPE_TMP_BUFFER);
  10089. int ret = 0;
  10090. ExpectNotNull(out);
  10091. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10092. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10093. WOLFSSL_SUCCESS);
  10094. #ifdef WOLFSSL_SHA512
  10095. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10096. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10097. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10098. #endif
  10099. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10100. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10101. return EXPECT_RESULT();
  10102. }
  10103. /* test parsing URI from certificate */
  10104. static int test_wolfSSL_URI(void)
  10105. {
  10106. EXPECT_DECLS;
  10107. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10108. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10109. defined(OPENSSL_EXTRA))
  10110. WOLFSSL_X509* x509 = NULL;
  10111. const char uri[] = "./certs/client-uri-cert.pem";
  10112. const char urn[] = "./certs/client-absolute-urn.pem";
  10113. const char badUri[] = "./certs/client-relative-uri.pem";
  10114. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10115. WOLFSSL_FILETYPE_PEM));
  10116. wolfSSL_FreeX509(x509);
  10117. x509 = NULL;
  10118. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10119. WOLFSSL_FILETYPE_PEM));
  10120. wolfSSL_FreeX509(x509);
  10121. x509 = NULL;
  10122. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10123. && !defined(WOLFSSL_FPKI)
  10124. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10125. WOLFSSL_FILETYPE_PEM));
  10126. #else
  10127. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10128. WOLFSSL_FILETYPE_PEM));
  10129. #endif
  10130. wolfSSL_FreeX509(x509);
  10131. #endif
  10132. return EXPECT_RESULT();
  10133. }
  10134. static int test_wolfSSL_TBS(void)
  10135. {
  10136. EXPECT_DECLS;
  10137. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10138. && defined(OPENSSL_EXTRA)
  10139. WOLFSSL_X509* x509 = NULL;
  10140. const unsigned char* tbs;
  10141. int tbsSz;
  10142. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10143. WOLFSSL_FILETYPE_PEM));
  10144. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10145. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10146. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10147. ExpectIntEQ(tbsSz, 1003);
  10148. wolfSSL_FreeX509(x509);
  10149. #endif
  10150. return EXPECT_RESULT();
  10151. }
  10152. static int test_wolfSSL_X509_verify(void)
  10153. {
  10154. EXPECT_DECLS;
  10155. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10156. defined(OPENSSL_EXTRA)
  10157. WOLFSSL_X509* ca = NULL;
  10158. WOLFSSL_X509* serv = NULL;
  10159. WOLFSSL_EVP_PKEY* pkey = NULL;
  10160. unsigned char buf[2048];
  10161. const unsigned char* pt = NULL;
  10162. int bufSz;
  10163. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10164. WOLFSSL_FILETYPE_PEM));
  10165. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10166. WOLFSSL_SUCCESS);
  10167. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10168. WOLFSSL_SUCCESS);
  10169. ExpectIntEQ(bufSz, 294);
  10170. bufSz = 2048;
  10171. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10172. WOLFSSL_SUCCESS);
  10173. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10174. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10175. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10176. WOLFSSL_FILETYPE_PEM));
  10177. /* success case */
  10178. pt = buf;
  10179. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10180. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10181. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10182. wolfSSL_EVP_PKEY_free(pkey);
  10183. pkey = NULL;
  10184. /* fail case */
  10185. bufSz = 2048;
  10186. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10187. WOLFSSL_SUCCESS);
  10188. pt = buf;
  10189. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10190. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10191. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10192. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10193. wolfSSL_EVP_PKEY_free(pkey);
  10194. wolfSSL_FreeX509(ca);
  10195. wolfSSL_FreeX509(serv);
  10196. #endif
  10197. return EXPECT_RESULT();
  10198. }
  10199. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10200. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10201. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10202. /* create certificate with version 2 */
  10203. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10204. {
  10205. EXPECT_DECLS;
  10206. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10207. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10208. unsigned char *der = NULL, *key = NULL, *pt;
  10209. char *header = NULL, *name = NULL;
  10210. int derSz;
  10211. long keySz;
  10212. XFILE fp = XBADFILE;
  10213. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10214. time_t t;
  10215. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10216. WOLFSSL_FILETYPE_PEM));
  10217. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10218. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10219. WOLFSSL_SUCCESS);
  10220. if (fp != XBADFILE)
  10221. XFCLOSE(fp);
  10222. pt = key;
  10223. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10224. (const unsigned char**)&pt, keySz));
  10225. /* create the version 2 certificate */
  10226. ExpectNotNull(x509v2 = X509_new());
  10227. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10228. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10229. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10230. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10231. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10232. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10233. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10234. t = time(NULL);
  10235. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10236. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10237. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10238. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10239. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10240. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10241. ExpectIntGT(derSz, 0);
  10242. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10243. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10244. /* TODO: Replace with API call */
  10245. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10246. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10247. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10248. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10249. wolfSSL_X509_free(x509);
  10250. wolfSSL_X509_free(x509v2);
  10251. wolfSSL_EVP_PKEY_free(priv);
  10252. wolfSSL_EVP_PKEY_free(pub);
  10253. wolfSSL_ASN1_TIME_free(notBefore);
  10254. wolfSSL_ASN1_TIME_free(notAfter);
  10255. return EXPECT_RESULT();
  10256. }
  10257. /* override certificate version error */
  10258. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10259. {
  10260. EXPECT_DECLS;
  10261. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10262. ExpectIntEQ(store->error, ASN_VERSION_E);
  10263. #else
  10264. ExpectIntEQ(store->error, 0);
  10265. #endif
  10266. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10267. (void)preverify;
  10268. return EXPECT_RESULT() == TEST_SUCCESS;
  10269. }
  10270. /* set verify callback that will override bad certificate version */
  10271. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10272. {
  10273. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10274. return TEST_SUCCESS;
  10275. }
  10276. #endif
  10277. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10278. {
  10279. EXPECT_DECLS;
  10280. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10281. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10282. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10283. test_ssl_cbf func_cb_client;
  10284. test_ssl_cbf func_cb_server;
  10285. /* test server rejects a client certificate that is not version 3 */
  10286. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10287. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10288. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10289. #ifndef WOLFSSL_NO_TLS12
  10290. func_cb_client.method = wolfTLSv1_2_client_method;
  10291. #else
  10292. func_cb_client.method = wolfTLSv1_3_client_method;
  10293. #endif
  10294. #ifndef WOLFSSL_NO_TLS12
  10295. func_cb_server.method = wolfTLSv1_2_server_method;
  10296. #else
  10297. func_cb_server.method = wolfTLSv1_3_server_method;
  10298. #endif
  10299. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10300. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10301. &func_cb_server, NULL), TEST_FAIL);
  10302. #else
  10303. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10304. &func_cb_server, NULL), TEST_SUCCESS);
  10305. #endif
  10306. #endif
  10307. return EXPECT_RESULT();
  10308. }
  10309. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10310. {
  10311. EXPECT_DECLS;
  10312. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10313. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10314. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10315. test_ssl_cbf func_cb_client;
  10316. test_ssl_cbf func_cb_server;
  10317. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10318. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10319. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10320. func_cb_server.ctx_ready = &test_set_override_x509;
  10321. #ifndef WOLFSSL_NO_TLS12
  10322. func_cb_client.method = wolfTLSv1_2_client_method;
  10323. #else
  10324. func_cb_client.method = wolfTLSv1_3_client_method;
  10325. #endif
  10326. #ifndef WOLFSSL_NO_TLS12
  10327. func_cb_server.method = wolfTLSv1_2_server_method;
  10328. #else
  10329. func_cb_server.method = wolfTLSv1_3_server_method;
  10330. #endif
  10331. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10332. &func_cb_server, NULL), TEST_SUCCESS);
  10333. #endif
  10334. return EXPECT_RESULT();
  10335. }
  10336. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10337. * version allowed.
  10338. * POST: 1 on success.
  10339. */
  10340. static int test_wolfSSL_CTX_SetMinVersion(void)
  10341. {
  10342. int res = TEST_SKIPPED;
  10343. #ifndef NO_WOLFSSL_CLIENT
  10344. int failFlag = WOLFSSL_SUCCESS;
  10345. WOLFSSL_CTX* ctx;
  10346. int itr;
  10347. #ifndef NO_OLD_TLS
  10348. const int versions[] = {
  10349. #ifdef WOLFSSL_ALLOW_TLSV10
  10350. WOLFSSL_TLSV1,
  10351. #endif
  10352. WOLFSSL_TLSV1_1,
  10353. WOLFSSL_TLSV1_2 };
  10354. #elif !defined(WOLFSSL_NO_TLS12)
  10355. const int versions[] = { WOLFSSL_TLSV1_2 };
  10356. #elif defined(WOLFSSL_TLS13)
  10357. const int versions[] = { WOLFSSL_TLSV1_3 };
  10358. #else
  10359. const int versions[0];
  10360. #endif
  10361. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10362. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10363. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10364. != WOLFSSL_SUCCESS) {
  10365. failFlag = WOLFSSL_FAILURE;
  10366. }
  10367. }
  10368. wolfSSL_CTX_free(ctx);
  10369. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10370. #endif
  10371. return res;
  10372. } /* END test_wolfSSL_CTX_SetMinVersion */
  10373. /*----------------------------------------------------------------------------*
  10374. | OCSP Stapling
  10375. *----------------------------------------------------------------------------*/
  10376. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10377. * need to contact the CA, lowering the cost of cert revocation checking.
  10378. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10379. * POST: 1 returned for success.
  10380. */
  10381. static int test_wolfSSL_UseOCSPStapling(void)
  10382. {
  10383. EXPECT_DECLS;
  10384. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10385. !defined(NO_WOLFSSL_CLIENT)
  10386. WOLFSSL_CTX* ctx = NULL;
  10387. WOLFSSL* ssl = NULL;
  10388. #ifndef NO_WOLFSSL_CLIENT
  10389. #ifndef WOLFSSL_NO_TLS12
  10390. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10391. #else
  10392. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10393. #endif
  10394. #else
  10395. #ifndef WOLFSSL_NO_TLS12
  10396. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10397. #else
  10398. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10399. #endif
  10400. #endif
  10401. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10402. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10403. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10404. #ifndef NO_WOLFSSL_CLIENT
  10405. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10406. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10407. #else
  10408. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10409. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10410. #endif
  10411. wolfSSL_free(ssl);
  10412. wolfSSL_CTX_free(ctx);
  10413. #endif
  10414. return EXPECT_RESULT();
  10415. } /* END test_wolfSSL_UseOCSPStapling */
  10416. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10417. * stapling eliminates the need to contact the CA and lowers cert revocation
  10418. * check.
  10419. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10420. */
  10421. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10422. {
  10423. EXPECT_DECLS;
  10424. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10425. !defined(NO_WOLFSSL_CLIENT)
  10426. WOLFSSL_CTX* ctx = NULL;
  10427. WOLFSSL* ssl = NULL;
  10428. #ifndef NO_WOLFSSL_CLIENT
  10429. #ifndef WOLFSSL_NO_TLS12
  10430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10431. #else
  10432. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10433. #endif
  10434. #else
  10435. #ifndef WOLFSSL_NO_TLS12
  10436. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10437. #else
  10438. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10439. #endif
  10440. #endif
  10441. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10442. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10443. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10444. #ifndef NO_WOLFSSL_CLIENT
  10445. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10446. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10447. #else
  10448. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10449. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10450. #endif
  10451. wolfSSL_free(ssl);
  10452. wolfSSL_CTX_free(ctx);
  10453. #endif
  10454. return EXPECT_RESULT();
  10455. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10456. /*----------------------------------------------------------------------------*
  10457. | Multicast Tests
  10458. *----------------------------------------------------------------------------*/
  10459. static int test_wolfSSL_mcast(void)
  10460. {
  10461. EXPECT_DECLS;
  10462. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10463. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10464. WOLFSSL_CTX* ctx = NULL;
  10465. WOLFSSL* ssl = NULL;
  10466. byte preMasterSecret[512];
  10467. byte clientRandom[32];
  10468. byte serverRandom[32];
  10469. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10470. byte buf[256];
  10471. word16 newId;
  10472. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10473. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10474. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10475. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10476. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10477. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10478. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10479. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10480. WOLFSSL_SUCCESS);
  10481. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10482. ExpectIntLE(newId, 100);
  10483. wolfSSL_free(ssl);
  10484. wolfSSL_CTX_free(ctx);
  10485. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10486. * WOLFSSL_SNIFFER) */
  10487. return EXPECT_RESULT();
  10488. }
  10489. /*----------------------------------------------------------------------------*
  10490. | Wolfcrypt
  10491. *----------------------------------------------------------------------------*/
  10492. /*
  10493. * Unit test for the wc_InitBlake2b()
  10494. */
  10495. static int test_wc_InitBlake2b(void)
  10496. {
  10497. EXPECT_DECLS;
  10498. #ifdef HAVE_BLAKE2
  10499. Blake2b blake;
  10500. /* Test good arg. */
  10501. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10502. /* Test bad arg. */
  10503. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10504. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10505. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10506. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10507. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10508. #endif
  10509. return EXPECT_RESULT();
  10510. } /* END test_wc_InitBlake2b*/
  10511. /*
  10512. * Unit test for the wc_InitBlake2b_WithKey()
  10513. */
  10514. static int test_wc_InitBlake2b_WithKey(void)
  10515. {
  10516. EXPECT_DECLS;
  10517. #ifdef HAVE_BLAKE2
  10518. Blake2b blake;
  10519. word32 digestSz = BLAKE2B_KEYBYTES;
  10520. byte key[BLAKE2B_KEYBYTES];
  10521. word32 keylen = BLAKE2B_KEYBYTES;
  10522. XMEMSET(key, 0, sizeof(key));
  10523. /* Test good arg. */
  10524. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10525. /* Test bad args. */
  10526. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10527. BAD_FUNC_ARG);
  10528. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10529. BAD_FUNC_ARG);
  10530. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10531. #endif
  10532. return EXPECT_RESULT();
  10533. } /* END wc_InitBlake2b_WithKey*/
  10534. /*
  10535. * Unit test for the wc_InitBlake2s_WithKey()
  10536. */
  10537. static int test_wc_InitBlake2s_WithKey(void)
  10538. {
  10539. EXPECT_DECLS;
  10540. #ifdef HAVE_BLAKE2S
  10541. Blake2s blake;
  10542. word32 digestSz = BLAKE2S_KEYBYTES;
  10543. byte *key = (byte*)"01234567890123456789012345678901";
  10544. word32 keylen = BLAKE2S_KEYBYTES;
  10545. /* Test good arg. */
  10546. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10547. /* Test bad args. */
  10548. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10549. BAD_FUNC_ARG);
  10550. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10551. BAD_FUNC_ARG);
  10552. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10553. #endif
  10554. return EXPECT_RESULT();
  10555. } /* END wc_InitBlake2s_WithKey*/
  10556. /*
  10557. * Unit test for the wc_InitMd5()
  10558. */
  10559. static int test_wc_InitMd5(void)
  10560. {
  10561. EXPECT_DECLS;
  10562. #ifndef NO_MD5
  10563. wc_Md5 md5;
  10564. /* Test good arg. */
  10565. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10566. /* Test bad arg. */
  10567. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10568. wc_Md5Free(&md5);
  10569. #endif
  10570. return EXPECT_RESULT();
  10571. } /* END test_wc_InitMd5 */
  10572. /*
  10573. * Testing wc_UpdateMd5()
  10574. */
  10575. static int test_wc_Md5Update(void)
  10576. {
  10577. EXPECT_DECLS;
  10578. #ifndef NO_MD5
  10579. wc_Md5 md5;
  10580. byte hash[WC_MD5_DIGEST_SIZE];
  10581. testVector a, b, c;
  10582. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10583. /* Input */
  10584. a.input = "a";
  10585. a.inLen = XSTRLEN(a.input);
  10586. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10587. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10588. /* Update input. */
  10589. a.input = "abc";
  10590. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10591. "\x72";
  10592. a.inLen = XSTRLEN(a.input);
  10593. a.outLen = XSTRLEN(a.output);
  10594. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10595. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10596. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10597. /* Pass in bad values. */
  10598. b.input = NULL;
  10599. b.inLen = 0;
  10600. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10601. c.input = NULL;
  10602. c.inLen = WC_MD5_DIGEST_SIZE;
  10603. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10604. BAD_FUNC_ARG);
  10605. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10606. BAD_FUNC_ARG);
  10607. wc_Md5Free(&md5);
  10608. #endif
  10609. return EXPECT_RESULT();
  10610. } /* END test_wc_Md5Update() */
  10611. /*
  10612. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10613. */
  10614. static int test_wc_Md5Final(void)
  10615. {
  10616. EXPECT_DECLS;
  10617. #ifndef NO_MD5
  10618. /* Instantiate */
  10619. wc_Md5 md5;
  10620. byte* hash_test[3];
  10621. byte hash1[WC_MD5_DIGEST_SIZE];
  10622. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10623. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10624. int times, i;
  10625. /* Initialize */
  10626. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10627. hash_test[0] = hash1;
  10628. hash_test[1] = hash2;
  10629. hash_test[2] = hash3;
  10630. times = sizeof(hash_test)/sizeof(byte*);
  10631. for (i = 0; i < times; i++) {
  10632. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10633. }
  10634. /* Test bad args. */
  10635. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10636. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10637. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10638. wc_Md5Free(&md5);
  10639. #endif
  10640. return EXPECT_RESULT();
  10641. }
  10642. /*
  10643. * Unit test for the wc_InitSha()
  10644. */
  10645. static int test_wc_InitSha(void)
  10646. {
  10647. EXPECT_DECLS;
  10648. #ifndef NO_SHA
  10649. wc_Sha sha;
  10650. /* Test good arg. */
  10651. ExpectIntEQ(wc_InitSha(&sha), 0);
  10652. /* Test bad arg. */
  10653. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10654. wc_ShaFree(&sha);
  10655. #endif
  10656. return EXPECT_RESULT();
  10657. } /* END test_wc_InitSha */
  10658. /*
  10659. * Tesing wc_ShaUpdate()
  10660. */
  10661. static int test_wc_ShaUpdate(void)
  10662. {
  10663. EXPECT_DECLS;
  10664. #ifndef NO_SHA
  10665. wc_Sha sha;
  10666. byte hash[WC_SHA_DIGEST_SIZE];
  10667. testVector a, b, c;
  10668. ExpectIntEQ(wc_InitSha(&sha), 0);
  10669. /* Input. */
  10670. a.input = "a";
  10671. a.inLen = XSTRLEN(a.input);
  10672. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10673. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10674. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10675. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10676. /* Update input. */
  10677. a.input = "abc";
  10678. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10679. "\x6C\x9C\xD0\xD8\x9D";
  10680. a.inLen = XSTRLEN(a.input);
  10681. a.outLen = XSTRLEN(a.output);
  10682. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10683. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10684. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10685. /* Try passing in bad values. */
  10686. b.input = NULL;
  10687. b.inLen = 0;
  10688. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  10689. c.input = NULL;
  10690. c.inLen = WC_SHA_DIGEST_SIZE;
  10691. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  10692. BAD_FUNC_ARG);
  10693. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  10694. BAD_FUNC_ARG);
  10695. wc_ShaFree(&sha);
  10696. #endif
  10697. return EXPECT_RESULT();
  10698. } /* END test_wc_ShaUpdate() */
  10699. /*
  10700. * Unit test on wc_ShaFinal
  10701. */
  10702. static int test_wc_ShaFinal(void)
  10703. {
  10704. EXPECT_DECLS;
  10705. #ifndef NO_SHA
  10706. wc_Sha sha;
  10707. byte* hash_test[3];
  10708. byte hash1[WC_SHA_DIGEST_SIZE];
  10709. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10710. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10711. int times, i;
  10712. /* Initialize*/
  10713. ExpectIntEQ(wc_InitSha(&sha), 0);
  10714. hash_test[0] = hash1;
  10715. hash_test[1] = hash2;
  10716. hash_test[2] = hash3;
  10717. times = sizeof(hash_test)/sizeof(byte*);
  10718. for (i = 0; i < times; i++) {
  10719. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  10720. }
  10721. /* Test bad args. */
  10722. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  10723. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  10724. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  10725. wc_ShaFree(&sha);
  10726. #endif
  10727. return EXPECT_RESULT();
  10728. } /* END test_wc_ShaFinal */
  10729. /*
  10730. * Unit test for wc_InitSha256()
  10731. */
  10732. static int test_wc_InitSha256(void)
  10733. {
  10734. EXPECT_DECLS;
  10735. #ifndef NO_SHA256
  10736. wc_Sha256 sha256;
  10737. /* Test good arg. */
  10738. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10739. /* Test bad arg. */
  10740. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  10741. wc_Sha256Free(&sha256);
  10742. #endif
  10743. return EXPECT_RESULT();
  10744. } /* END test_wc_InitSha256 */
  10745. /*
  10746. * Unit test for wc_Sha256Update()
  10747. */
  10748. static int test_wc_Sha256Update(void)
  10749. {
  10750. EXPECT_DECLS;
  10751. #ifndef NO_SHA256
  10752. wc_Sha256 sha256;
  10753. byte hash[WC_SHA256_DIGEST_SIZE];
  10754. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  10755. testVector a, b, c;
  10756. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10757. /* Input. */
  10758. a.input = "a";
  10759. a.inLen = XSTRLEN(a.input);
  10760. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  10761. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  10762. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10763. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10764. /* Update input. */
  10765. a.input = "abc";
  10766. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10767. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10768. "\x15\xAD";
  10769. a.inLen = XSTRLEN(a.input);
  10770. a.outLen = XSTRLEN(a.output);
  10771. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10772. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10773. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  10774. /* Unaligned check. */
  10775. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  10776. 0);
  10777. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  10778. /* Try passing in bad values */
  10779. b.input = NULL;
  10780. b.inLen = 0;
  10781. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  10782. c.input = NULL;
  10783. c.inLen = WC_SHA256_DIGEST_SIZE;
  10784. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  10785. BAD_FUNC_ARG);
  10786. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  10787. BAD_FUNC_ARG);
  10788. wc_Sha256Free(&sha256);
  10789. #endif
  10790. return EXPECT_RESULT();
  10791. } /* END test_wc_Sha256Update */
  10792. /*
  10793. * Unit test function for wc_Sha256Final()
  10794. */
  10795. static int test_wc_Sha256Final(void)
  10796. {
  10797. EXPECT_DECLS;
  10798. #ifndef NO_SHA256
  10799. wc_Sha256 sha256;
  10800. byte* hash_test[3];
  10801. byte hash1[WC_SHA256_DIGEST_SIZE];
  10802. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10803. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10804. int times, i;
  10805. /* Initialize */
  10806. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10807. hash_test[0] = hash1;
  10808. hash_test[1] = hash2;
  10809. hash_test[2] = hash3;
  10810. times = sizeof(hash_test) / sizeof(byte*);
  10811. for (i = 0; i < times; i++) {
  10812. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  10813. }
  10814. /* Test bad args. */
  10815. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  10816. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  10817. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  10818. wc_Sha256Free(&sha256);
  10819. #endif
  10820. return EXPECT_RESULT();
  10821. } /* END test_wc_Sha256Final */
  10822. /*
  10823. * Unit test function for wc_Sha256FinalRaw()
  10824. */
  10825. static int test_wc_Sha256FinalRaw(void)
  10826. {
  10827. EXPECT_DECLS;
  10828. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10829. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10830. !defined(WOLFSSL_NO_HASH_RAW)
  10831. wc_Sha256 sha256;
  10832. byte* hash_test[3];
  10833. byte hash1[WC_SHA256_DIGEST_SIZE];
  10834. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10835. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10836. int times, i;
  10837. /* Initialize */
  10838. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10839. hash_test[0] = hash1;
  10840. hash_test[1] = hash2;
  10841. hash_test[2] = hash3;
  10842. times = sizeof(hash_test) / sizeof(byte*);
  10843. for (i = 0; i < times; i++) {
  10844. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  10845. }
  10846. /* Test bad args. */
  10847. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  10848. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  10849. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  10850. wc_Sha256Free(&sha256);
  10851. #endif
  10852. return EXPECT_RESULT();
  10853. } /* END test_wc_Sha256FinalRaw */
  10854. /*
  10855. * Unit test function for wc_Sha256GetFlags()
  10856. */
  10857. static int test_wc_Sha256GetFlags(void)
  10858. {
  10859. EXPECT_DECLS;
  10860. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10861. wc_Sha256 sha256;
  10862. word32 flags = 0;
  10863. /* Initialize */
  10864. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10865. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  10866. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  10867. wc_Sha256Free(&sha256);
  10868. #endif
  10869. return EXPECT_RESULT();
  10870. } /* END test_wc_Sha256GetFlags */
  10871. /*
  10872. * Unit test function for wc_Sha256Free()
  10873. */
  10874. static int test_wc_Sha256Free(void)
  10875. {
  10876. EXPECT_DECLS;
  10877. #ifndef NO_SHA256
  10878. wc_Sha256Free(NULL);
  10879. /* Set result to SUCCESS. */
  10880. ExpectTrue(1);
  10881. #endif
  10882. return EXPECT_RESULT();
  10883. } /* END test_wc_Sha256Free */
  10884. /*
  10885. * Unit test function for wc_Sha256GetHash()
  10886. */
  10887. static int test_wc_Sha256GetHash(void)
  10888. {
  10889. EXPECT_DECLS;
  10890. #ifndef NO_SHA256
  10891. wc_Sha256 sha256;
  10892. byte hash1[WC_SHA256_DIGEST_SIZE];
  10893. /* Initialize */
  10894. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10895. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  10896. /* test bad arguments*/
  10897. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  10898. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  10899. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  10900. wc_Sha256Free(&sha256);
  10901. #endif
  10902. return EXPECT_RESULT();
  10903. } /* END test_wc_Sha256GetHash */
  10904. /*
  10905. * Unit test function for wc_Sha256Copy()
  10906. */
  10907. static int test_wc_Sha256Copy(void)
  10908. {
  10909. EXPECT_DECLS;
  10910. #ifndef NO_SHA256
  10911. wc_Sha256 sha256;
  10912. wc_Sha256 temp;
  10913. XMEMSET(&sha256, 0, sizeof(sha256));
  10914. XMEMSET(&temp, 0, sizeof(temp));
  10915. /* Initialize */
  10916. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10917. ExpectIntEQ(wc_InitSha256(&temp), 0);
  10918. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  10919. /* test bad arguments*/
  10920. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  10921. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  10922. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  10923. wc_Sha256Free(&sha256);
  10924. wc_Sha256Free(&temp);
  10925. #endif
  10926. return EXPECT_RESULT();
  10927. } /* END test_wc_Sha256Copy */
  10928. /*
  10929. * Testing wc_InitSha512()
  10930. */
  10931. static int test_wc_InitSha512(void)
  10932. {
  10933. EXPECT_DECLS;
  10934. #ifdef WOLFSSL_SHA512
  10935. wc_Sha512 sha512;
  10936. /* Test good arg. */
  10937. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  10938. /* Test bad arg. */
  10939. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  10940. wc_Sha512Free(&sha512);
  10941. #endif
  10942. return EXPECT_RESULT();
  10943. } /* END test_wc_InitSha512 */
  10944. /*
  10945. * wc_Sha512Update() test.
  10946. */
  10947. static int test_wc_Sha512Update(void)
  10948. {
  10949. EXPECT_DECLS;
  10950. #ifdef WOLFSSL_SHA512
  10951. wc_Sha512 sha512;
  10952. byte hash[WC_SHA512_DIGEST_SIZE];
  10953. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  10954. testVector a, b, c;
  10955. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  10956. /* Input. */
  10957. a.input = "a";
  10958. a.inLen = XSTRLEN(a.input);
  10959. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  10960. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  10961. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  10962. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  10963. /* Update input. */
  10964. a.input = "abc";
  10965. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  10966. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  10967. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  10968. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  10969. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  10970. a.inLen = XSTRLEN(a.input);
  10971. a.outLen = XSTRLEN(a.output);
  10972. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  10973. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  10974. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  10975. /* Unaligned check. */
  10976. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  10977. 0);
  10978. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  10979. /* Try passing in bad values */
  10980. b.input = NULL;
  10981. b.inLen = 0;
  10982. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  10983. c.input = NULL;
  10984. c.inLen = WC_SHA512_DIGEST_SIZE;
  10985. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  10986. BAD_FUNC_ARG);
  10987. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  10988. BAD_FUNC_ARG);
  10989. wc_Sha512Free(&sha512);
  10990. #endif
  10991. return EXPECT_RESULT();
  10992. } /* END test_wc_Sha512Update */
  10993. #ifdef WOLFSSL_SHA512
  10994. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10995. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  10996. /* Performs test for
  10997. * - wc_Sha512Final/wc_Sha512FinalRaw
  10998. * - wc_Sha512_224Final/wc_Sha512_224Final
  10999. * - wc_Sha512_256Final/wc_Sha512_256Final
  11000. * parameter:
  11001. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11002. * WC_HASH_TYPE_SHA512_256
  11003. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11004. *return 0 on success
  11005. */
  11006. static int test_Sha512_Family_Final(int type, int isRaw)
  11007. {
  11008. EXPECT_DECLS;
  11009. wc_Sha512 sha512;
  11010. byte* hash_test[3];
  11011. byte hash1[WC_SHA512_DIGEST_SIZE];
  11012. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11013. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11014. int times, i;
  11015. int(*initFp)(wc_Sha512*);
  11016. int(*finalFp)(wc_Sha512*, byte*);
  11017. void(*freeFp)(wc_Sha512*);
  11018. if (type == WC_HASH_TYPE_SHA512) {
  11019. initFp = wc_InitSha512;
  11020. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11021. !defined(WOLFSSL_NO_HASH_RAW)
  11022. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11023. #else
  11024. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11025. #endif
  11026. freeFp = wc_Sha512Free;
  11027. }
  11028. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11029. #if !defined(WOLFSSL_NOSHA512_224)
  11030. else if (type == WC_HASH_TYPE_SHA512_224) {
  11031. initFp = wc_InitSha512_224;
  11032. #if !defined(WOLFSSL_NO_HASH_RAW)
  11033. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11034. #else
  11035. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11036. #endif
  11037. freeFp = wc_Sha512_224Free;
  11038. }
  11039. #endif
  11040. #if !defined(WOLFSSL_NOSHA512_256)
  11041. else if (type == WC_HASH_TYPE_SHA512_256) {
  11042. initFp = wc_InitSha512_256;
  11043. #if !defined(WOLFSSL_NO_HASH_RAW)
  11044. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11045. #else
  11046. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11047. #endif
  11048. freeFp = wc_Sha512_256Free;
  11049. }
  11050. #endif
  11051. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11052. else
  11053. return TEST_FAIL;
  11054. /* Initialize */
  11055. ExpectIntEQ(initFp(&sha512), 0);
  11056. hash_test[0] = hash1;
  11057. hash_test[1] = hash2;
  11058. hash_test[2] = hash3;
  11059. times = sizeof(hash_test) / sizeof(byte *);
  11060. /* Good test args. */
  11061. for (i = 0; i < times; i++) {
  11062. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11063. }
  11064. /* Test bad args. */
  11065. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11066. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11067. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11068. freeFp(&sha512);
  11069. return EXPECT_RESULT();
  11070. }
  11071. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11072. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11073. #endif /* WOLFSSL_SHA512 */
  11074. /*
  11075. * Unit test function for wc_Sha512Final()
  11076. */
  11077. static int test_wc_Sha512Final(void)
  11078. {
  11079. EXPECT_DECLS;
  11080. #ifdef WOLFSSL_SHA512
  11081. wc_Sha512 sha512;
  11082. byte* hash_test[3];
  11083. byte hash1[WC_SHA512_DIGEST_SIZE];
  11084. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11085. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11086. int times, i;
  11087. /* Initialize */
  11088. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11089. hash_test[0] = hash1;
  11090. hash_test[1] = hash2;
  11091. hash_test[2] = hash3;
  11092. times = sizeof(hash_test) / sizeof(byte *);
  11093. for (i = 0; i < times; i++) {
  11094. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11095. }
  11096. /* Test bad args. */
  11097. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11098. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11099. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11100. wc_Sha512Free(&sha512);
  11101. #endif
  11102. return EXPECT_RESULT();
  11103. } /* END test_wc_Sha512Final */
  11104. /*
  11105. * Unit test function for wc_Sha512GetFlags()
  11106. */
  11107. static int test_wc_Sha512GetFlags(void)
  11108. {
  11109. EXPECT_DECLS;
  11110. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11111. wc_Sha512 sha512;
  11112. word32 flags = 0;
  11113. /* Initialize */
  11114. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11115. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11116. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11117. wc_Sha512Free(&sha512);
  11118. #endif
  11119. return EXPECT_RESULT();
  11120. } /* END test_wc_Sha512GetFlags */
  11121. /*
  11122. * Unit test function for wc_Sha512FinalRaw()
  11123. */
  11124. static int test_wc_Sha512FinalRaw(void)
  11125. {
  11126. EXPECT_DECLS;
  11127. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11128. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11129. !defined(WOLFSSL_NO_HASH_RAW)
  11130. wc_Sha512 sha512;
  11131. byte* hash_test[3];
  11132. byte hash1[WC_SHA512_DIGEST_SIZE];
  11133. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11134. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11135. int times, i;
  11136. /* Initialize */
  11137. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11138. hash_test[0] = hash1;
  11139. hash_test[1] = hash2;
  11140. hash_test[2] = hash3;
  11141. times = sizeof(hash_test) / sizeof(byte*);
  11142. /* Good test args. */
  11143. for (i = 0; i < times; i++) {
  11144. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11145. }
  11146. /* Test bad args. */
  11147. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11148. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11149. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11150. wc_Sha512Free(&sha512);
  11151. #endif
  11152. return EXPECT_RESULT();
  11153. } /* END test_wc_Sha512FinalRaw */
  11154. /*
  11155. * Unit test function for wc_Sha512Free()
  11156. */
  11157. static int test_wc_Sha512Free(void)
  11158. {
  11159. EXPECT_DECLS;
  11160. #ifdef WOLFSSL_SHA512
  11161. wc_Sha512Free(NULL);
  11162. /* Set result to SUCCESS. */
  11163. ExpectTrue(1);
  11164. #endif
  11165. return EXPECT_RESULT();
  11166. } /* END test_wc_Sha512Free */
  11167. #ifdef WOLFSSL_SHA512
  11168. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11169. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11170. static int test_Sha512_Family_GetHash(int type )
  11171. {
  11172. EXPECT_DECLS;
  11173. int(*initFp)(wc_Sha512*);
  11174. int(*ghashFp)(wc_Sha512*, byte*);
  11175. wc_Sha512 sha512;
  11176. byte hash1[WC_SHA512_DIGEST_SIZE];
  11177. if (type == WC_HASH_TYPE_SHA512) {
  11178. initFp = wc_InitSha512;
  11179. ghashFp = wc_Sha512GetHash;
  11180. }
  11181. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11182. #if !defined(WOLFSSL_NOSHA512_224)
  11183. else if (type == WC_HASH_TYPE_SHA512_224) {
  11184. initFp = wc_InitSha512_224;
  11185. ghashFp = wc_Sha512_224GetHash;
  11186. }
  11187. #endif
  11188. #if !defined(WOLFSSL_NOSHA512_256)
  11189. else if (type == WC_HASH_TYPE_SHA512_256) {
  11190. initFp = wc_InitSha512_256;
  11191. ghashFp = wc_Sha512_256GetHash;
  11192. }
  11193. #endif
  11194. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11195. else {
  11196. initFp = NULL;
  11197. ghashFp = NULL;
  11198. }
  11199. if (initFp == NULL || ghashFp == NULL)
  11200. return TEST_FAIL;
  11201. ExpectIntEQ(initFp(&sha512), 0);
  11202. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11203. /* test bad arguments*/
  11204. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11205. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11206. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11207. wc_Sha512Free(&sha512);
  11208. return EXPECT_RESULT();
  11209. }
  11210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11211. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11212. #endif /* WOLFSSL_SHA512 */
  11213. /*
  11214. * Unit test function for wc_Sha512GetHash()
  11215. */
  11216. static int test_wc_Sha512GetHash(void)
  11217. {
  11218. EXPECT_DECLS;
  11219. #ifdef WOLFSSL_SHA512
  11220. wc_Sha512 sha512;
  11221. byte hash1[WC_SHA512_DIGEST_SIZE];
  11222. /* Initialize */
  11223. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11224. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11225. /* test bad arguments*/
  11226. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11227. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11228. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11229. wc_Sha512Free(&sha512);
  11230. #endif
  11231. return EXPECT_RESULT();
  11232. } /* END test_wc_Sha512GetHash */
  11233. /*
  11234. * Unit test function for wc_Sha512Copy()
  11235. */
  11236. static int test_wc_Sha512Copy(void)
  11237. {
  11238. EXPECT_DECLS;
  11239. #ifdef WOLFSSL_SHA512
  11240. wc_Sha512 sha512;
  11241. wc_Sha512 temp;
  11242. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11243. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11244. /* Initialize */
  11245. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11246. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11247. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11248. /* test bad arguments*/
  11249. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11250. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11251. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11252. wc_Sha512Free(&sha512);
  11253. wc_Sha512Free(&temp);
  11254. #endif
  11255. return EXPECT_RESULT();
  11256. } /* END test_wc_Sha512Copy */
  11257. static int test_wc_InitSha512_224(void)
  11258. {
  11259. EXPECT_DECLS;
  11260. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11261. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11262. wc_Sha512 sha512;
  11263. /* Test good arg. */
  11264. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11265. /* Test bad arg. */
  11266. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11267. wc_Sha512_224Free(&sha512);
  11268. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11269. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11270. return EXPECT_RESULT();
  11271. }
  11272. static int test_wc_Sha512_224Update(void)
  11273. {
  11274. EXPECT_DECLS;
  11275. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11276. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11277. wc_Sha512 sha512;
  11278. byte hash[WC_SHA512_DIGEST_SIZE];
  11279. testVector a, c;
  11280. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11281. /* Input. */
  11282. a.input = "a";
  11283. a.inLen = XSTRLEN(a.input);
  11284. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11285. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11286. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11287. 0);
  11288. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11289. /* Update input. */
  11290. a.input = "abc";
  11291. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11292. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11293. a.inLen = XSTRLEN(a.input);
  11294. a.outLen = XSTRLEN(a.output);
  11295. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11296. 0);
  11297. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11298. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11299. c.input = NULL;
  11300. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11301. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11302. BAD_FUNC_ARG);
  11303. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11304. BAD_FUNC_ARG);
  11305. wc_Sha512_224Free(&sha512);
  11306. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11307. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11308. return EXPECT_RESULT();
  11309. }
  11310. static int test_wc_Sha512_224Final(void)
  11311. {
  11312. EXPECT_DECLS;
  11313. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11314. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11315. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11316. TEST_SUCCESS);
  11317. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11318. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11319. return EXPECT_RESULT();
  11320. }
  11321. static int test_wc_Sha512_224GetFlags(void)
  11322. {
  11323. EXPECT_DECLS;
  11324. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11325. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11326. wc_Sha512 sha512;
  11327. wc_Sha512 copy;
  11328. word32 flags = 0;
  11329. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11330. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11331. /* Initialize */
  11332. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11333. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11334. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11335. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11336. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11337. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11338. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11339. wc_Sha512_224Free(&copy);
  11340. wc_Sha512_224Free(&sha512);
  11341. #endif
  11342. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11343. return EXPECT_RESULT();
  11344. }
  11345. static int test_wc_Sha512_224FinalRaw(void)
  11346. {
  11347. EXPECT_DECLS;
  11348. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11349. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11350. !defined(WOLFSSL_NO_HASH_RAW)
  11351. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11352. TEST_SUCCESS);
  11353. #endif
  11354. return EXPECT_RESULT();
  11355. }
  11356. static int test_wc_Sha512_224Free(void)
  11357. {
  11358. EXPECT_DECLS;
  11359. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11360. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11361. wc_Sha512_224Free(NULL);
  11362. /* Set result to SUCCESS. */
  11363. ExpectTrue(1);
  11364. #endif
  11365. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11366. return EXPECT_RESULT();
  11367. }
  11368. static int test_wc_Sha512_224GetHash(void)
  11369. {
  11370. EXPECT_DECLS;
  11371. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11372. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11373. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11374. TEST_SUCCESS);
  11375. #endif
  11376. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11377. return EXPECT_RESULT();
  11378. }
  11379. static int test_wc_Sha512_224Copy(void)
  11380. {
  11381. EXPECT_DECLS;
  11382. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11383. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11384. wc_Sha512 sha512;
  11385. wc_Sha512 temp;
  11386. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11387. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11388. /* Initialize */
  11389. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11390. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11391. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11392. /* test bad arguments*/
  11393. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11394. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11395. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11396. wc_Sha512_224Free(&sha512);
  11397. wc_Sha512_224Free(&temp);
  11398. #endif
  11399. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11400. return EXPECT_RESULT();
  11401. }
  11402. static int test_wc_InitSha512_256(void)
  11403. {
  11404. EXPECT_DECLS;
  11405. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11406. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11407. wc_Sha512 sha512;
  11408. /* Test good arg. */
  11409. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11410. /* Test bad arg. */
  11411. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11412. wc_Sha512_256Free(&sha512);
  11413. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11414. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11415. return EXPECT_RESULT();
  11416. }
  11417. static int test_wc_Sha512_256Update(void)
  11418. {
  11419. EXPECT_DECLS;
  11420. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11421. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11422. wc_Sha512 sha512;
  11423. byte hash[WC_SHA512_DIGEST_SIZE];
  11424. testVector a, c;
  11425. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11426. /* Input. */
  11427. a.input = "a";
  11428. a.inLen = XSTRLEN(a.input);
  11429. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11430. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11431. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11432. 0);
  11433. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11434. /* Update input. */
  11435. a.input = "abc";
  11436. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11437. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11438. "\x07\xe7\xaf\x23";
  11439. a.inLen = XSTRLEN(a.input);
  11440. a.outLen = XSTRLEN(a.output);
  11441. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11442. 0);
  11443. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11444. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11445. c.input = NULL;
  11446. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11447. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11448. BAD_FUNC_ARG);
  11449. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11450. BAD_FUNC_ARG);
  11451. wc_Sha512_256Free(&sha512);
  11452. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11453. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11454. return EXPECT_RESULT();
  11455. }
  11456. static int test_wc_Sha512_256Final(void)
  11457. {
  11458. EXPECT_DECLS;
  11459. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11460. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11461. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11462. TEST_SUCCESS);
  11463. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11464. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11465. return EXPECT_RESULT();
  11466. }
  11467. static int test_wc_Sha512_256GetFlags(void)
  11468. {
  11469. EXPECT_DECLS;
  11470. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11471. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11472. wc_Sha512 sha512, copy;
  11473. word32 flags = 0;
  11474. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11475. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11476. /* Initialize */
  11477. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11478. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11479. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11480. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11481. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11482. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11483. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11484. wc_Sha512_256Free(&sha512);
  11485. #endif
  11486. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11487. return EXPECT_RESULT();
  11488. }
  11489. static int test_wc_Sha512_256FinalRaw(void)
  11490. {
  11491. EXPECT_DECLS;
  11492. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11493. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11494. !defined(WOLFSSL_NO_HASH_RAW)
  11495. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11496. TEST_SUCCESS);
  11497. #endif
  11498. return EXPECT_RESULT();
  11499. }
  11500. static int test_wc_Sha512_256Free(void)
  11501. {
  11502. EXPECT_DECLS;
  11503. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11504. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11505. wc_Sha512_256Free(NULL);
  11506. /* Set result to SUCCESS. */
  11507. ExpectTrue(1);
  11508. #endif
  11509. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11510. return EXPECT_RESULT();
  11511. }
  11512. static int test_wc_Sha512_256GetHash(void)
  11513. {
  11514. EXPECT_DECLS;
  11515. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11516. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11517. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11518. TEST_SUCCESS);
  11519. #endif
  11520. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11521. return EXPECT_RESULT();
  11522. }
  11523. static int test_wc_Sha512_256Copy(void)
  11524. {
  11525. EXPECT_DECLS;
  11526. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11527. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11528. wc_Sha512 sha512;
  11529. wc_Sha512 temp;
  11530. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11531. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11532. /* Initialize */
  11533. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11534. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11535. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11536. /* test bad arguments*/
  11537. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11538. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11539. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11540. wc_Sha512_256Free(&sha512);
  11541. wc_Sha512_256Free(&temp);
  11542. #endif
  11543. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11544. return EXPECT_RESULT();
  11545. }
  11546. /*
  11547. * Testing wc_InitSha384()
  11548. */
  11549. static int test_wc_InitSha384(void)
  11550. {
  11551. EXPECT_DECLS;
  11552. #ifdef WOLFSSL_SHA384
  11553. wc_Sha384 sha384;
  11554. /* Test good arg. */
  11555. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11556. /* Test bad arg. */
  11557. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11558. wc_Sha384Free(&sha384);
  11559. #endif
  11560. return EXPECT_RESULT();
  11561. } /* END test_wc_InitSha384 */
  11562. /*
  11563. * test wc_Sha384Update()
  11564. */
  11565. static int test_wc_Sha384Update(void)
  11566. {
  11567. EXPECT_DECLS;
  11568. #ifdef WOLFSSL_SHA384
  11569. wc_Sha384 sha384;
  11570. byte hash[WC_SHA384_DIGEST_SIZE];
  11571. testVector a, b, c;
  11572. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11573. /* Input */
  11574. a.input = "a";
  11575. a.inLen = XSTRLEN(a.input);
  11576. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11577. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11578. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11579. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11580. /* Update input. */
  11581. a.input = "abc";
  11582. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11583. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11584. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11585. "\xc8\x25\xa7";
  11586. a.inLen = XSTRLEN(a.input);
  11587. a.outLen = XSTRLEN(a.output);
  11588. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11589. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11590. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11591. /* Pass in bad values. */
  11592. b.input = NULL;
  11593. b.inLen = 0;
  11594. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11595. c.input = NULL;
  11596. c.inLen = WC_SHA384_DIGEST_SIZE;
  11597. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11598. BAD_FUNC_ARG);
  11599. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11600. BAD_FUNC_ARG);
  11601. wc_Sha384Free(&sha384);
  11602. #endif
  11603. return EXPECT_RESULT();
  11604. } /* END test_wc_Sha384Update */
  11605. /*
  11606. * Unit test function for wc_Sha384Final();
  11607. */
  11608. static int test_wc_Sha384Final(void)
  11609. {
  11610. EXPECT_DECLS;
  11611. #ifdef WOLFSSL_SHA384
  11612. wc_Sha384 sha384;
  11613. byte* hash_test[3];
  11614. byte hash1[WC_SHA384_DIGEST_SIZE];
  11615. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11616. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11617. int times, i;
  11618. /* Initialize */
  11619. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11620. hash_test[0] = hash1;
  11621. hash_test[1] = hash2;
  11622. hash_test[2] = hash3;
  11623. times = sizeof(hash_test) / sizeof(byte*);
  11624. /* Good test args. */
  11625. for (i = 0; i < times; i++) {
  11626. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11627. }
  11628. /* Test bad args. */
  11629. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11630. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11631. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11632. wc_Sha384Free(&sha384);
  11633. #endif
  11634. return EXPECT_RESULT();
  11635. } /* END test_wc_Sha384Final */
  11636. /*
  11637. * Unit test function for wc_Sha384GetFlags()
  11638. */
  11639. static int test_wc_Sha384GetFlags(void)
  11640. {
  11641. EXPECT_DECLS;
  11642. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11643. wc_Sha384 sha384;
  11644. word32 flags = 0;
  11645. /* Initialize */
  11646. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11647. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11648. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11649. wc_Sha384Free(&sha384);
  11650. #endif
  11651. return EXPECT_RESULT();
  11652. } /* END test_wc_Sha384GetFlags */
  11653. /*
  11654. * Unit test function for wc_Sha384FinalRaw()
  11655. */
  11656. static int test_wc_Sha384FinalRaw(void)
  11657. {
  11658. EXPECT_DECLS;
  11659. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11660. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11661. !defined(WOLFSSL_NO_HASH_RAW)
  11662. wc_Sha384 sha384;
  11663. byte* hash_test[3];
  11664. byte hash1[WC_SHA384_DIGEST_SIZE];
  11665. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11666. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11667. int times, i;
  11668. /* Initialize */
  11669. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11670. hash_test[0] = hash1;
  11671. hash_test[1] = hash2;
  11672. hash_test[2] = hash3;
  11673. times = sizeof(hash_test) / sizeof(byte*);
  11674. /* Good test args. */
  11675. for (i = 0; i < times; i++) {
  11676. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11677. }
  11678. /* Test bad args. */
  11679. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11680. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11681. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11682. wc_Sha384Free(&sha384);
  11683. #endif
  11684. return EXPECT_RESULT();
  11685. } /* END test_wc_Sha384FinalRaw */
  11686. /*
  11687. * Unit test function for wc_Sha384Free()
  11688. */
  11689. static int test_wc_Sha384Free(void)
  11690. {
  11691. EXPECT_DECLS;
  11692. #ifdef WOLFSSL_SHA384
  11693. wc_Sha384Free(NULL);
  11694. /* Set result to SUCCESS. */
  11695. ExpectTrue(1);
  11696. #endif
  11697. return EXPECT_RESULT();
  11698. } /* END test_wc_Sha384Free */
  11699. /*
  11700. * Unit test function for wc_Sha384GetHash()
  11701. */
  11702. static int test_wc_Sha384GetHash(void)
  11703. {
  11704. EXPECT_DECLS;
  11705. #ifdef WOLFSSL_SHA384
  11706. wc_Sha384 sha384;
  11707. byte hash1[WC_SHA384_DIGEST_SIZE];
  11708. /* Initialize */
  11709. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11710. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  11711. /* test bad arguments*/
  11712. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  11713. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  11714. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  11715. wc_Sha384Free(&sha384);
  11716. #endif
  11717. return EXPECT_RESULT();
  11718. } /* END test_wc_Sha384GetHash */
  11719. /*
  11720. * Unit test function for wc_Sha384Copy()
  11721. */
  11722. static int test_wc_Sha384Copy(void)
  11723. {
  11724. EXPECT_DECLS;
  11725. #ifdef WOLFSSL_SHA384
  11726. wc_Sha384 sha384;
  11727. wc_Sha384 temp;
  11728. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  11729. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  11730. /* Initialize */
  11731. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11732. ExpectIntEQ(wc_InitSha384(&temp), 0);
  11733. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  11734. /* test bad arguments*/
  11735. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  11736. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  11737. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  11738. wc_Sha384Free(&sha384);
  11739. wc_Sha384Free(&temp);
  11740. #endif
  11741. return EXPECT_RESULT();
  11742. } /* END test_wc_Sha384Copy */
  11743. /*
  11744. * Testing wc_InitSha224();
  11745. */
  11746. static int test_wc_InitSha224(void)
  11747. {
  11748. EXPECT_DECLS;
  11749. #ifdef WOLFSSL_SHA224
  11750. wc_Sha224 sha224;
  11751. /* Test good arg. */
  11752. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11753. /* Test bad arg. */
  11754. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  11755. wc_Sha224Free(&sha224);
  11756. #endif
  11757. return EXPECT_RESULT();
  11758. } /* END test_wc_InitSha224 */
  11759. /*
  11760. * Unit test on wc_Sha224Update
  11761. */
  11762. static int test_wc_Sha224Update(void)
  11763. {
  11764. EXPECT_DECLS;
  11765. #ifdef WOLFSSL_SHA224
  11766. wc_Sha224 sha224;
  11767. byte hash[WC_SHA224_DIGEST_SIZE];
  11768. testVector a, b, c;
  11769. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11770. /* Input. */
  11771. a.input = "a";
  11772. a.inLen = XSTRLEN(a.input);
  11773. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  11774. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  11775. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11776. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11777. /* Update input. */
  11778. a.input = "abc";
  11779. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11780. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11781. a.inLen = XSTRLEN(a.input);
  11782. a.outLen = XSTRLEN(a.output);
  11783. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11784. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11785. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  11786. /* Pass in bad values. */
  11787. b.input = NULL;
  11788. b.inLen = 0;
  11789. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  11790. c.input = NULL;
  11791. c.inLen = WC_SHA224_DIGEST_SIZE;
  11792. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  11793. BAD_FUNC_ARG);
  11794. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11795. BAD_FUNC_ARG);
  11796. wc_Sha224Free(&sha224);
  11797. #endif
  11798. return EXPECT_RESULT();
  11799. } /* END test_wc_Sha224Update */
  11800. /*
  11801. * Unit test for wc_Sha224Final();
  11802. */
  11803. static int test_wc_Sha224Final(void)
  11804. {
  11805. EXPECT_DECLS;
  11806. #ifdef WOLFSSL_SHA224
  11807. wc_Sha224 sha224;
  11808. byte* hash_test[3];
  11809. byte hash1[WC_SHA224_DIGEST_SIZE];
  11810. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11811. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11812. int times, i;
  11813. /* Initialize */
  11814. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11815. hash_test[0] = hash1;
  11816. hash_test[1] = hash2;
  11817. hash_test[2] = hash3;
  11818. times = sizeof(hash_test) / sizeof(byte*);
  11819. /* Good test args. */
  11820. /* Testing oversized buffers. */
  11821. for (i = 0; i < times; i++) {
  11822. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  11823. }
  11824. /* Test bad args. */
  11825. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  11826. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  11827. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  11828. wc_Sha224Free(&sha224);
  11829. #endif
  11830. return EXPECT_RESULT();
  11831. } /* END test_wc_Sha224Final */
  11832. /*
  11833. * Unit test function for wc_Sha224SetFlags()
  11834. */
  11835. static int test_wc_Sha224SetFlags(void)
  11836. {
  11837. EXPECT_DECLS;
  11838. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11839. wc_Sha224 sha224;
  11840. word32 flags = WC_HASH_FLAG_WILLCOPY;
  11841. /* Initialize */
  11842. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11843. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  11844. flags = 0;
  11845. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11846. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  11847. wc_Sha224Free(&sha224);
  11848. #endif
  11849. return EXPECT_RESULT();
  11850. } /* END test_wc_Sha224SetFlags */
  11851. /*
  11852. * Unit test function for wc_Sha224GetFlags()
  11853. */
  11854. static int test_wc_Sha224GetFlags(void)
  11855. {
  11856. EXPECT_DECLS;
  11857. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11858. wc_Sha224 sha224;
  11859. word32 flags = 0;
  11860. /* Initialize */
  11861. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11862. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11863. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11864. wc_Sha224Free(&sha224);
  11865. #endif
  11866. return EXPECT_RESULT();
  11867. } /* END test_wc_Sha224GetFlags */
  11868. /*
  11869. * Unit test function for wc_Sha224Free()
  11870. */
  11871. static int test_wc_Sha224Free(void)
  11872. {
  11873. EXPECT_DECLS;
  11874. #ifdef WOLFSSL_SHA224
  11875. wc_Sha224Free(NULL);
  11876. /* Set result to SUCCESS. */
  11877. ExpectTrue(1);
  11878. #endif
  11879. return EXPECT_RESULT();
  11880. } /* END test_wc_Sha224Free */
  11881. /*
  11882. * Unit test function for wc_Sha224GetHash()
  11883. */
  11884. static int test_wc_Sha224GetHash(void)
  11885. {
  11886. EXPECT_DECLS;
  11887. #ifdef WOLFSSL_SHA224
  11888. wc_Sha224 sha224;
  11889. byte hash1[WC_SHA224_DIGEST_SIZE];
  11890. /* Initialize */
  11891. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11892. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  11893. /* test bad arguments*/
  11894. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  11895. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  11896. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  11897. wc_Sha224Free(&sha224);
  11898. #endif
  11899. return EXPECT_RESULT();
  11900. } /* END test_wc_Sha224GetHash */
  11901. /*
  11902. * Unit test function for wc_Sha224Copy()
  11903. */
  11904. static int test_wc_Sha224Copy(void)
  11905. {
  11906. EXPECT_DECLS;
  11907. #ifdef WOLFSSL_SHA224
  11908. wc_Sha224 sha224;
  11909. wc_Sha224 temp;
  11910. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  11911. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  11912. /* Initialize */
  11913. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11914. ExpectIntEQ(wc_InitSha224(&temp), 0);
  11915. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  11916. /* test bad arguments*/
  11917. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  11918. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  11919. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  11920. wc_Sha224Free(&sha224);
  11921. wc_Sha224Free(&temp);
  11922. #endif
  11923. return EXPECT_RESULT();
  11924. } /* END test_wc_Sha224Copy */
  11925. /*
  11926. * Testing wc_InitRipeMd()
  11927. */
  11928. static int test_wc_InitRipeMd(void)
  11929. {
  11930. EXPECT_DECLS;
  11931. #ifdef WOLFSSL_RIPEMD
  11932. RipeMd ripemd;
  11933. /* Test good arg. */
  11934. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  11935. /* Test bad arg. */
  11936. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  11937. #endif
  11938. return EXPECT_RESULT();
  11939. } /* END test_wc_InitRipeMd */
  11940. /*
  11941. * Testing wc_RipeMdUpdate()
  11942. */
  11943. static int test_wc_RipeMdUpdate(void)
  11944. {
  11945. EXPECT_DECLS;
  11946. #ifdef WOLFSSL_RIPEMD
  11947. RipeMd ripemd;
  11948. byte hash[RIPEMD_DIGEST_SIZE];
  11949. testVector a, b, c;
  11950. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  11951. /* Input */
  11952. a.input = "a";
  11953. a.inLen = XSTRLEN(a.input);
  11954. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  11955. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  11956. /* Update input. */
  11957. a.input = "abc";
  11958. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11959. "\xb0\x87\xf1\x5a\x0b\xfc";
  11960. a.inLen = XSTRLEN(a.input);
  11961. a.outLen = XSTRLEN(a.output);
  11962. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  11963. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  11964. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  11965. /* Pass in bad values. */
  11966. b.input = NULL;
  11967. b.inLen = 0;
  11968. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  11969. c.input = NULL;
  11970. c.inLen = RIPEMD_DIGEST_SIZE;
  11971. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  11972. BAD_FUNC_ARG);
  11973. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11974. BAD_FUNC_ARG);
  11975. #endif
  11976. return EXPECT_RESULT();
  11977. } /* END test_wc_RipeMdUdpate */
  11978. /*
  11979. * Unit test function for wc_RipeMdFinal()
  11980. */
  11981. static int test_wc_RipeMdFinal(void)
  11982. {
  11983. EXPECT_DECLS;
  11984. #ifdef WOLFSSL_RIPEMD
  11985. RipeMd ripemd;
  11986. byte* hash_test[3];
  11987. byte hash1[RIPEMD_DIGEST_SIZE];
  11988. byte hash2[2*RIPEMD_DIGEST_SIZE];
  11989. byte hash3[5*RIPEMD_DIGEST_SIZE];
  11990. int times, i;
  11991. /* Initialize */
  11992. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  11993. hash_test[0] = hash1;
  11994. hash_test[1] = hash2;
  11995. hash_test[2] = hash3;
  11996. times = sizeof(hash_test) / sizeof(byte*);
  11997. /* Testing oversized buffers. */
  11998. for (i = 0; i < times; i++) {
  11999. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12000. }
  12001. /* Test bad args. */
  12002. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12003. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12004. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12005. #endif
  12006. return EXPECT_RESULT();
  12007. } /* END test_wc_RipeMdFinal */
  12008. /*
  12009. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12010. * wc_InitSha3_512
  12011. */
  12012. static int test_wc_InitSha3(void)
  12013. {
  12014. EXPECT_DECLS;
  12015. #if defined(WOLFSSL_SHA3)
  12016. wc_Sha3 sha3;
  12017. (void)sha3;
  12018. #if !defined(WOLFSSL_NOSHA3_224)
  12019. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12020. /* Test bad args. */
  12021. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12022. wc_Sha3_224_Free(&sha3);
  12023. #endif /* NOSHA3_224 */
  12024. #if !defined(WOLFSSL_NOSHA3_256)
  12025. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12026. /* Test bad args. */
  12027. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12028. wc_Sha3_256_Free(&sha3);
  12029. #endif /* NOSHA3_256 */
  12030. #if !defined(WOLFSSL_NOSHA3_384)
  12031. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12032. /* Test bad args. */
  12033. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12034. wc_Sha3_384_Free(&sha3);
  12035. #endif /* NOSHA3_384 */
  12036. #if !defined(WOLFSSL_NOSHA3_512)
  12037. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12038. /* Test bad args. */
  12039. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12040. wc_Sha3_512_Free(&sha3);
  12041. #endif /* NOSHA3_512 */
  12042. #endif
  12043. return EXPECT_RESULT();
  12044. } /* END test_wc_InitSha3 */
  12045. /*
  12046. * Testing wc_Sha3_Update()
  12047. */
  12048. static int testing_wc_Sha3_Update(void)
  12049. {
  12050. EXPECT_DECLS;
  12051. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12052. !defined(WOLFSSL_AFALG_XILINX)
  12053. wc_Sha3 sha3;
  12054. byte msg[] = "Everybody's working for the weekend.";
  12055. byte msg2[] = "Everybody gets Friday off.";
  12056. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12057. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12058. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12059. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12060. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12061. word32 msglen = sizeof(msg) - 1;
  12062. word32 msg2len = sizeof(msg2);
  12063. word32 msgCmplen = sizeof(msgCmp);
  12064. #if !defined(WOLFSSL_NOSHA3_224)
  12065. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12066. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12067. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12068. ExpectTrue(sha3.i == msglen);
  12069. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12070. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12071. /* Pass bad args. */
  12072. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12073. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12074. wc_Sha3_224_Free(&sha3);
  12075. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12076. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12077. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12078. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12079. wc_Sha3_224_Free(&sha3);
  12080. #endif /* SHA3_224 */
  12081. #if !defined(WOLFSSL_NOSHA3_256)
  12082. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12083. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12084. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12085. ExpectTrue(sha3.i == msglen);
  12086. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12087. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12088. /* Pass bad args. */
  12089. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12090. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12091. wc_Sha3_256_Free(&sha3);
  12092. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12093. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12094. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12095. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12096. wc_Sha3_256_Free(&sha3);
  12097. #endif /* SHA3_256 */
  12098. #if !defined(WOLFSSL_NOSHA3_384)
  12099. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12100. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12101. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12102. ExpectTrue(sha3.i == msglen);
  12103. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12104. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12105. /* Pass bad args. */
  12106. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12107. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12108. wc_Sha3_384_Free(&sha3);
  12109. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12110. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12111. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12112. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12113. wc_Sha3_384_Free(&sha3);
  12114. #endif /* SHA3_384 */
  12115. #if !defined(WOLFSSL_NOSHA3_512)
  12116. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12117. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12118. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12119. ExpectTrue(sha3.i == msglen);
  12120. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12121. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12122. /* Pass bad args. */
  12123. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12124. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12125. wc_Sha3_512_Free(&sha3);
  12126. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12127. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12128. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12129. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12130. wc_Sha3_512_Free(&sha3);
  12131. #endif /* SHA3_512 */
  12132. #endif /* WOLFSSL_SHA3 */
  12133. return EXPECT_RESULT();
  12134. } /* END testing_wc_Sha3_Update */
  12135. /*
  12136. * Testing wc_Sha3_224_Final()
  12137. */
  12138. static int test_wc_Sha3_224_Final(void)
  12139. {
  12140. EXPECT_DECLS;
  12141. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12142. wc_Sha3 sha3;
  12143. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12144. "nopnopq";
  12145. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12146. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12147. "\x64\xea\xd0\xfc\xce\x33";
  12148. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12149. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12150. /* Init stack variables. */
  12151. XMEMSET(hash, 0, sizeof(hash));
  12152. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12153. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12154. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12155. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12156. /* Test bad args. */
  12157. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12158. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12159. wc_Sha3_224_Free(&sha3);
  12160. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12161. /* Init stack variables. */
  12162. XMEMSET(hash, 0, sizeof(hash));
  12163. XMEMSET(hashRet, 0, sizeof(hashRet));
  12164. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12165. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12166. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12167. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12168. /* Test bad args. */
  12169. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12170. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12171. wc_Sha3_224_Free(&sha3);
  12172. #endif
  12173. return EXPECT_RESULT();
  12174. } /* END test_wc_Sha3_224_Final */
  12175. /*
  12176. * Testing wc_Sha3_256_Final()
  12177. */
  12178. static int test_wc_Sha3_256_Final(void)
  12179. {
  12180. EXPECT_DECLS;
  12181. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12182. wc_Sha3 sha3;
  12183. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12184. "nopnopq";
  12185. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12186. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12187. "\xdd\x97\x49\x6d\x33\x76";
  12188. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12189. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12190. /* Init stack variables. */
  12191. XMEMSET(hash, 0, sizeof(hash));
  12192. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12193. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12194. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12195. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12196. /* Test bad args. */
  12197. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12198. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12199. wc_Sha3_256_Free(&sha3);
  12200. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12201. /* Init stack variables. */
  12202. XMEMSET(hash, 0, sizeof(hash));
  12203. XMEMSET(hashRet, 0, sizeof(hashRet));
  12204. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12205. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12206. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12207. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12208. /* Test bad args. */
  12209. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12210. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12211. wc_Sha3_256_Free(&sha3);
  12212. #endif
  12213. return EXPECT_RESULT();
  12214. } /* END test_wc_Sha3_256_Final */
  12215. /*
  12216. * Testing wc_Sha3_384_Final()
  12217. */
  12218. static int test_wc_Sha3_384_Final(void)
  12219. {
  12220. EXPECT_DECLS;
  12221. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12222. wc_Sha3 sha3;
  12223. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12224. "nopnopq";
  12225. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12226. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12227. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12228. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12229. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12230. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12231. /* Init stack variables. */
  12232. XMEMSET(hash, 0, sizeof(hash));
  12233. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12234. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12235. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12236. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12237. /* Test bad args. */
  12238. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12239. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12240. wc_Sha3_384_Free(&sha3);
  12241. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12242. /* Init stack variables. */
  12243. XMEMSET(hash, 0, sizeof(hash));
  12244. XMEMSET(hashRet, 0, sizeof(hashRet));
  12245. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12246. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12247. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12248. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12249. /* Test bad args. */
  12250. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12251. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12252. wc_Sha3_384_Free(&sha3);
  12253. #endif
  12254. return EXPECT_RESULT();
  12255. } /* END test_wc_Sha3_384_Final */
  12256. /*
  12257. * Testing wc_Sha3_512_Final()
  12258. */
  12259. static int test_wc_Sha3_512_Final(void)
  12260. {
  12261. EXPECT_DECLS;
  12262. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12263. !defined(WOLFSSL_NOSHA3_384)
  12264. wc_Sha3 sha3;
  12265. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12266. "nopnopq";
  12267. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12268. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12269. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12270. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12271. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12272. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12273. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12274. /* Init stack variables. */
  12275. XMEMSET(hash, 0, sizeof(hash));
  12276. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12277. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12278. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12279. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12280. /* Test bad args. */
  12281. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12282. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12283. wc_Sha3_512_Free(&sha3);
  12284. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12285. /* Init stack variables. */
  12286. XMEMSET(hash, 0, sizeof(hash));
  12287. XMEMSET(hashRet, 0, sizeof(hashRet));
  12288. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12289. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12290. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12291. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12292. /* Test bad args. */
  12293. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12294. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12295. wc_Sha3_512_Free(&sha3);
  12296. #endif
  12297. return EXPECT_RESULT();
  12298. } /* END test_wc_Sha3_512_Final */
  12299. /*
  12300. * Testing wc_Sha3_224_Copy()
  12301. */
  12302. static int test_wc_Sha3_224_Copy(void)
  12303. {
  12304. EXPECT_DECLS;
  12305. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12306. wc_Sha3 sha3, sha3Cpy;
  12307. const char* msg = TEST_STRING;
  12308. word32 msglen = (word32)TEST_STRING_SZ;
  12309. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12310. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12311. XMEMSET(hash, 0, sizeof(hash));
  12312. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12313. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12314. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12315. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12316. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12317. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12318. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12319. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12320. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12321. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12322. /* Test bad args. */
  12323. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12324. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12325. wc_Sha3_224_Free(&sha3);
  12326. wc_Sha3_224_Free(&sha3Cpy);
  12327. #endif
  12328. return EXPECT_RESULT();
  12329. } /* END test_wc_Sha3_224_Copy */
  12330. /*
  12331. * Testing wc_Sha3_256_Copy()
  12332. */
  12333. static int test_wc_Sha3_256_Copy(void)
  12334. {
  12335. EXPECT_DECLS;
  12336. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12337. wc_Sha3 sha3, sha3Cpy;
  12338. const char* msg = TEST_STRING;
  12339. word32 msglen = (word32)TEST_STRING_SZ;
  12340. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12341. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12342. XMEMSET(hash, 0, sizeof(hash));
  12343. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12344. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12345. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12346. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12347. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12348. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12349. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12350. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12351. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12352. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12353. /* Test bad args. */
  12354. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12355. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12356. wc_Sha3_256_Free(&sha3);
  12357. wc_Sha3_256_Free(&sha3Cpy);
  12358. #endif
  12359. return EXPECT_RESULT();
  12360. } /* END test_wc_Sha3_256_Copy */
  12361. /*
  12362. * Testing wc_Sha3_384_Copy()
  12363. */
  12364. static int test_wc_Sha3_384_Copy(void)
  12365. {
  12366. EXPECT_DECLS;
  12367. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12368. wc_Sha3 sha3, sha3Cpy;
  12369. const char* msg = TEST_STRING;
  12370. word32 msglen = (word32)TEST_STRING_SZ;
  12371. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12372. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12373. XMEMSET(hash, 0, sizeof(hash));
  12374. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12375. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12376. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12377. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12378. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12379. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12380. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12381. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12382. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12383. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12384. /* Test bad args. */
  12385. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12386. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12387. wc_Sha3_384_Free(&sha3);
  12388. wc_Sha3_384_Free(&sha3Cpy);
  12389. #endif
  12390. return EXPECT_RESULT();
  12391. } /* END test_wc_Sha3_384_Copy */
  12392. /*
  12393. * Testing wc_Sha3_512_Copy()
  12394. */
  12395. static int test_wc_Sha3_512_Copy(void)
  12396. {
  12397. EXPECT_DECLS;
  12398. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12399. wc_Sha3 sha3, sha3Cpy;
  12400. const char* msg = TEST_STRING;
  12401. word32 msglen = (word32)TEST_STRING_SZ;
  12402. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12403. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12404. XMEMSET(hash, 0, sizeof(hash));
  12405. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12406. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12407. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12408. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12409. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12410. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12411. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12412. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12413. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12414. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12415. /* Test bad args. */
  12416. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12417. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12418. wc_Sha3_512_Free(&sha3);
  12419. wc_Sha3_512_Free(&sha3Cpy);
  12420. #endif
  12421. return EXPECT_RESULT();
  12422. } /* END test_wc_Sha3_512_Copy */
  12423. /*
  12424. * Unit test function for wc_Sha3_GetFlags()
  12425. */
  12426. static int test_wc_Sha3_GetFlags(void)
  12427. {
  12428. EXPECT_DECLS;
  12429. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12430. wc_Sha3 sha3;
  12431. word32 flags = 0;
  12432. /* Initialize */
  12433. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12434. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12435. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12436. wc_Sha3_224_Free(&sha3);
  12437. #endif
  12438. return EXPECT_RESULT();
  12439. } /* END test_wc_Sha3_GetFlags */
  12440. static int test_wc_InitShake256(void)
  12441. {
  12442. EXPECT_DECLS;
  12443. #ifdef WOLFSSL_SHAKE256
  12444. wc_Shake shake;
  12445. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12446. /* Test bad args. */
  12447. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12448. wc_Shake256_Free(&shake);
  12449. #endif
  12450. return EXPECT_RESULT();
  12451. }
  12452. static int testing_wc_Shake256_Update(void)
  12453. {
  12454. EXPECT_DECLS;
  12455. #ifdef WOLFSSL_SHAKE256
  12456. wc_Shake shake;
  12457. byte msg[] = "Everybody's working for the weekend.";
  12458. byte msg2[] = "Everybody gets Friday off.";
  12459. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12460. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12461. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12462. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12463. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12464. word32 msglen = sizeof(msg) - 1;
  12465. word32 msg2len = sizeof(msg2);
  12466. word32 msgCmplen = sizeof(msgCmp);
  12467. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12468. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12469. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12470. ExpectTrue(shake.i == msglen);
  12471. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12472. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12473. /* Pass bad args. */
  12474. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12475. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12476. wc_Shake256_Free(&shake);
  12477. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12478. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12479. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12480. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12481. wc_Shake256_Free(&shake);
  12482. #endif /* WOLFSSL_SHAKE256 */
  12483. return EXPECT_RESULT();
  12484. }
  12485. static int test_wc_Shake256_Final(void)
  12486. {
  12487. EXPECT_DECLS;
  12488. #ifdef WOLFSSL_SHAKE256
  12489. wc_Shake shake;
  12490. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12491. "nopnopq";
  12492. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12493. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12494. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12495. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12496. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12497. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12498. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12499. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12500. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12501. byte hash[114];
  12502. /* Init stack variables. */
  12503. XMEMSET(hash, 0, sizeof(hash));
  12504. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12505. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12506. 0);
  12507. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12508. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12509. /* Test bad args. */
  12510. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12511. BAD_FUNC_ARG);
  12512. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12513. BAD_FUNC_ARG);
  12514. wc_Shake256_Free(&shake);
  12515. #endif
  12516. return EXPECT_RESULT();
  12517. }
  12518. /*
  12519. * Testing wc_Shake256_Copy()
  12520. */
  12521. static int test_wc_Shake256_Copy(void)
  12522. {
  12523. EXPECT_DECLS;
  12524. #ifdef WOLFSSL_SHAKE256
  12525. wc_Shake shake, shakeCpy;
  12526. const char* msg = TEST_STRING;
  12527. word32 msglen = (word32)TEST_STRING_SZ;
  12528. byte hash[144];
  12529. byte hashCpy[144];
  12530. word32 hashLen = sizeof(hash);
  12531. word32 hashLenCpy = sizeof(hashCpy);
  12532. XMEMSET(hash, 0, sizeof(hash));
  12533. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12534. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12535. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12536. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12537. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12538. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12539. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12540. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12541. /* Test bad args. */
  12542. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12543. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12544. wc_Shake256_Free(&shake);
  12545. wc_Shake256_Free(&shakeCpy);
  12546. #endif
  12547. return EXPECT_RESULT();
  12548. } /* END test_wc_Shake256_Copy */
  12549. /*
  12550. * Unit test function for wc_Shake256Hash()
  12551. */
  12552. static int test_wc_Shake256Hash(void)
  12553. {
  12554. EXPECT_DECLS;
  12555. #ifdef WOLFSSL_SHAKE256
  12556. const byte data[] = { /* Hello World */
  12557. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12558. 0x72,0x6c,0x64
  12559. };
  12560. word32 len = sizeof(data);
  12561. byte hash[144];
  12562. word32 hashLen = sizeof(hash);
  12563. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12564. #endif
  12565. return EXPECT_RESULT();
  12566. } /* END test_wc_Shake256Hash */
  12567. /*
  12568. * Testing wc_InitSm3(), wc_Sm3Free()
  12569. */
  12570. static int test_wc_InitSm3Free(void)
  12571. {
  12572. EXPECT_DECLS;
  12573. #ifdef WOLFSSL_SM3
  12574. wc_Sm3 sm3;
  12575. /* Invalid Parameters */
  12576. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12577. /* Valid Parameters */
  12578. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12579. wc_Sm3Free(NULL);
  12580. wc_Sm3Free(&sm3);
  12581. #endif
  12582. return EXPECT_RESULT();
  12583. } /* END test_wc_InitSm3 */
  12584. /*
  12585. * Testing wc_Sm3Update(), wc_Sm3Final()
  12586. */
  12587. static int test_wc_Sm3UpdateFinal(void)
  12588. {
  12589. EXPECT_DECLS;
  12590. #ifdef WOLFSSL_SM3
  12591. wc_Sm3 sm3;
  12592. byte data[WC_SM3_BLOCK_SIZE * 4];
  12593. byte hash[WC_SM3_DIGEST_SIZE];
  12594. byte calcHash[WC_SM3_DIGEST_SIZE];
  12595. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12596. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12597. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12598. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12599. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12600. };
  12601. word32 chunk;
  12602. word32 i;
  12603. XMEMSET(data, 0, sizeof(data));
  12604. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12605. /* Invalid Parameters */
  12606. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12607. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12608. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12609. /* Valid Parameters */
  12610. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12611. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12612. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12613. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12614. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12615. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12616. /* Ensure too many bytes for lengths. */
  12617. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12618. /* Invalid Parameters */
  12619. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12620. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12621. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12622. /* Valid Parameters */
  12623. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12624. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12625. /* Chunk tests. */
  12626. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12627. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12628. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12629. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12630. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12631. }
  12632. if (i < (word32)sizeof(data)) {
  12633. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12634. 0);
  12635. }
  12636. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12637. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12638. }
  12639. /* Not testing when the low 32-bit length overflows. */
  12640. wc_Sm3Free(&sm3);
  12641. #endif
  12642. return EXPECT_RESULT();
  12643. } /* END test_wc_Sm3Update */
  12644. /*
  12645. * Testing wc_Sm3GetHash()
  12646. */
  12647. static int test_wc_Sm3GetHash(void)
  12648. {
  12649. EXPECT_DECLS;
  12650. #ifdef WOLFSSL_SM3
  12651. wc_Sm3 sm3;
  12652. byte hash[WC_SM3_DIGEST_SIZE];
  12653. byte calcHash[WC_SM3_DIGEST_SIZE];
  12654. byte data[WC_SM3_BLOCK_SIZE];
  12655. XMEMSET(data, 0, sizeof(data));
  12656. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12657. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12658. /* Invalid Parameters */
  12659. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12660. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12661. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12662. /* Valid Parameters */
  12663. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12664. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12665. /* With update. */
  12666. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12667. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12668. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12669. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12670. wc_Sm3Free(&sm3);
  12671. #endif
  12672. return EXPECT_RESULT();
  12673. } /* END test_wc_Sm3Update */
  12674. /*
  12675. * Testing wc_Sm3Copy()
  12676. */
  12677. static int test_wc_Sm3Copy(void)
  12678. {
  12679. EXPECT_DECLS;
  12680. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12681. wc_Sm3 sm3;
  12682. wc_Sm3 sm3Copy;
  12683. byte hash[WC_SM3_DIGEST_SIZE];
  12684. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12685. byte data[WC_SM3_BLOCK_SIZE + 1];
  12686. int i;
  12687. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12688. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12689. /* Invalid Parameters */
  12690. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  12691. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  12692. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  12693. /* Valid Parameters */
  12694. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12695. /* Ensure all parts of data updated during hashing are copied. */
  12696. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  12697. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  12698. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12699. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12700. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  12701. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12702. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  12703. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  12704. }
  12705. wc_Sm3Free(&sm3Copy);
  12706. wc_Sm3Free(&sm3);
  12707. #endif
  12708. return EXPECT_RESULT();
  12709. } /* END test_wc_Sm3Copy */
  12710. /*
  12711. * Testing wc_Sm3FinalRaw()
  12712. */
  12713. static int test_wc_Sm3FinalRaw(void)
  12714. {
  12715. EXPECT_DECLS;
  12716. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  12717. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12718. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12719. !defined(WOLFSSL_NO_HASH_RAW)
  12720. wc_Sm3 sm3;
  12721. byte hash1[WC_SM3_DIGEST_SIZE];
  12722. byte hash2[WC_SM3_DIGEST_SIZE];
  12723. byte hash3[WC_SM3_DIGEST_SIZE];
  12724. byte* hash_test[3] = { hash1, hash2, hash3 };
  12725. int times;
  12726. int i;
  12727. XMEMSET(&sm3, 0, sizeof(sm3));
  12728. /* Initialize */
  12729. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12730. /* Invalid Parameters */
  12731. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12732. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  12733. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12734. times = sizeof(hash_test) / sizeof(byte*);
  12735. for (i = 0; i < times; i++) {
  12736. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  12737. }
  12738. wc_Sm3Free(&sm3);
  12739. #endif
  12740. return EXPECT_RESULT();
  12741. } /* END test_wc_Sm3FinalRaw */
  12742. /*
  12743. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  12744. */
  12745. static int test_wc_Sm3GetSetFlags(void)
  12746. {
  12747. EXPECT_DECLS;
  12748. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12749. wc_Sm3 sm3;
  12750. wc_Sm3 sm3Copy;
  12751. word32 flags = 0;
  12752. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12753. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12754. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12755. ExpectIntEQ(flags, 0);
  12756. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  12757. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12758. ExpectIntEQ(flags, 0);
  12759. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12760. ExpectIntEQ(flags, 0);
  12761. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  12762. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12763. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  12764. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12765. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  12766. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  12767. wc_Sm3Free(&sm3Copy);
  12768. wc_Sm3Free(&sm3);
  12769. #endif
  12770. return EXPECT_RESULT();
  12771. } /* END test_wc_Sm3Update */
  12772. /*
  12773. * Testing wc_Sm3Hash()
  12774. */
  12775. static int test_wc_Sm3Hash(void)
  12776. {
  12777. EXPECT_DECLS;
  12778. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12779. byte data[WC_SM3_BLOCK_SIZE];
  12780. byte hash[WC_SM3_DIGEST_SIZE];
  12781. /* Invalid parameters. */
  12782. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  12783. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  12784. /* Valid parameters. */
  12785. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  12786. #endif
  12787. return EXPECT_RESULT();
  12788. } /* END test_wc_Sm3Hash */
  12789. /*
  12790. * Test function for wc_HmacSetKey
  12791. */
  12792. static int test_wc_Md5HmacSetKey(void)
  12793. {
  12794. EXPECT_DECLS;
  12795. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12796. Hmac hmac;
  12797. int ret, times, itr;
  12798. const char* keys[]=
  12799. {
  12800. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12801. #ifndef HAVE_FIPS
  12802. "Jefe", /* smaller than minimum FIPS key size */
  12803. #endif
  12804. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12805. };
  12806. times = sizeof(keys) / sizeof(char*);
  12807. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12808. for (itr = 0; itr < times; itr++) {
  12809. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12810. (word32)XSTRLEN(keys[itr]));
  12811. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12812. wc_HmacFree(&hmac);
  12813. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12814. #else
  12815. ExpectIntEQ(ret, 0);
  12816. #endif
  12817. }
  12818. /* Bad args. */
  12819. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12820. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12821. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  12822. BAD_FUNC_ARG);
  12823. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12824. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12825. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12826. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12827. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12828. #elif defined(HAVE_FIPS)
  12829. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12830. #else
  12831. ExpectIntEQ(ret, 0);
  12832. #endif
  12833. wc_HmacFree(&hmac);
  12834. #endif
  12835. return EXPECT_RESULT();
  12836. } /* END test_wc_Md5HmacSetKey */
  12837. /*
  12838. * testing wc_HmacSetKey() on wc_Sha hash.
  12839. */
  12840. static int test_wc_ShaHmacSetKey(void)
  12841. {
  12842. EXPECT_DECLS;
  12843. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12844. Hmac hmac;
  12845. int ret, times, itr;
  12846. const char* keys[]=
  12847. {
  12848. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12849. "\x0b\x0b\x0b",
  12850. #ifndef HAVE_FIPS
  12851. "Jefe", /* smaller than minimum FIPS key size */
  12852. #endif
  12853. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12854. "\xAA\xAA\xAA"
  12855. };
  12856. times = sizeof(keys) / sizeof(char*);
  12857. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12858. for (itr = 0; itr < times; itr++) {
  12859. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12860. (word32)XSTRLEN(keys[itr])), 0);
  12861. }
  12862. /* Bad args. */
  12863. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12864. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12865. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  12866. BAD_FUNC_ARG);
  12867. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12868. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12869. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12870. #ifdef HAVE_FIPS
  12871. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12872. #else
  12873. ExpectIntEQ(ret, 0);
  12874. #endif
  12875. wc_HmacFree(&hmac);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_ShaHmacSetKey() */
  12879. /*
  12880. * testing wc_HmacSetKey() on Sha224 hash.
  12881. */
  12882. static int test_wc_Sha224HmacSetKey(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12886. Hmac hmac;
  12887. int ret, times, itr;
  12888. const char* keys[]=
  12889. {
  12890. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12891. "\x0b\x0b\x0b",
  12892. #ifndef HAVE_FIPS
  12893. "Jefe", /* smaller than minimum FIPS key size */
  12894. #endif
  12895. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12896. "\xAA\xAA\xAA"
  12897. };
  12898. times = sizeof(keys) / sizeof(char*);
  12899. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12900. for (itr = 0; itr < times; itr++) {
  12901. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12902. (word32)XSTRLEN(keys[itr])), 0);
  12903. }
  12904. /* Bad args. */
  12905. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12906. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12907. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  12908. BAD_FUNC_ARG);
  12909. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12910. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12911. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12912. #ifdef HAVE_FIPS
  12913. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12914. #else
  12915. ExpectIntEQ(ret, 0);
  12916. #endif
  12917. wc_HmacFree(&hmac);
  12918. #endif
  12919. return EXPECT_RESULT();
  12920. } /* END test_wc_Sha224HmacSetKey() */
  12921. /*
  12922. * testing wc_HmacSetKey() on Sha256 hash
  12923. */
  12924. static int test_wc_Sha256HmacSetKey(void)
  12925. {
  12926. EXPECT_DECLS;
  12927. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12928. Hmac hmac;
  12929. int ret, times, itr;
  12930. const char* keys[]=
  12931. {
  12932. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12933. "\x0b\x0b\x0b",
  12934. #ifndef HAVE_FIPS
  12935. "Jefe", /* smaller than minimum FIPS key size */
  12936. #endif
  12937. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12938. "\xAA\xAA\xAA"
  12939. };
  12940. times = sizeof(keys) / sizeof(char*);
  12941. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12942. for (itr = 0; itr < times; itr++) {
  12943. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12944. (word32)XSTRLEN(keys[itr])), 0);
  12945. }
  12946. /* Bad args. */
  12947. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12948. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12949. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  12950. BAD_FUNC_ARG);
  12951. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12952. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12953. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12954. #ifdef HAVE_FIPS
  12955. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12956. #else
  12957. ExpectIntEQ(ret, 0);
  12958. #endif
  12959. wc_HmacFree(&hmac);
  12960. #endif
  12961. return EXPECT_RESULT();
  12962. } /* END test_wc_Sha256HmacSetKey() */
  12963. /*
  12964. * testing wc_HmacSetKey on Sha384 hash.
  12965. */
  12966. static int test_wc_Sha384HmacSetKey(void)
  12967. {
  12968. EXPECT_DECLS;
  12969. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12970. Hmac hmac;
  12971. int ret, times, itr;
  12972. const char* keys[]=
  12973. {
  12974. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12975. "\x0b\x0b\x0b",
  12976. #ifndef HAVE_FIPS
  12977. "Jefe", /* smaller than minimum FIPS key size */
  12978. #endif
  12979. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12980. "\xAA\xAA\xAA"
  12981. };
  12982. times = sizeof(keys) / sizeof(char*);
  12983. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12984. for (itr = 0; itr < times; itr++) {
  12985. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12986. (word32)XSTRLEN(keys[itr])), 0);
  12987. }
  12988. /* Bad args. */
  12989. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12990. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12991. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  12992. BAD_FUNC_ARG);
  12993. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12994. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12995. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12996. #ifdef HAVE_FIPS
  12997. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12998. #else
  12999. ExpectIntEQ(ret, 0);
  13000. #endif
  13001. wc_HmacFree(&hmac);
  13002. #endif
  13003. return EXPECT_RESULT();
  13004. } /* END test_wc_Sha384HmacSetKey() */
  13005. /*
  13006. * testing wc_HmacUpdate on wc_Md5 hash.
  13007. */
  13008. static int test_wc_Md5HmacUpdate(void)
  13009. {
  13010. EXPECT_DECLS;
  13011. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13012. Hmac hmac;
  13013. testVector a, b;
  13014. #ifdef HAVE_FIPS
  13015. const char* keys =
  13016. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13017. #else
  13018. const char* keys = "Jefe";
  13019. #endif
  13020. a.input = "what do ya want for nothing?";
  13021. a.inLen = XSTRLEN(a.input);
  13022. b.input = "Hi There";
  13023. b.inLen = XSTRLEN(b.input);
  13024. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13025. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13026. (word32)XSTRLEN(keys)), 0);
  13027. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13028. /* Update Hmac. */
  13029. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13030. /* Test bad args. */
  13031. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13032. BAD_FUNC_ARG);
  13033. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13034. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13035. wc_HmacFree(&hmac);
  13036. #endif
  13037. return EXPECT_RESULT();
  13038. } /* END test_wc_Md5HmacUpdate */
  13039. /*
  13040. * testing wc_HmacUpdate on SHA hash.
  13041. */
  13042. static int test_wc_ShaHmacUpdate(void)
  13043. {
  13044. EXPECT_DECLS;
  13045. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13046. Hmac hmac;
  13047. testVector a, b;
  13048. #ifdef HAVE_FIPS
  13049. const char* keys =
  13050. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13051. #else
  13052. const char* keys = "Jefe";
  13053. #endif
  13054. a.input = "what do ya want for nothing?";
  13055. a.inLen = XSTRLEN(a.input);
  13056. b.input = "Hi There";
  13057. b.inLen = XSTRLEN(b.input);
  13058. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13059. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13060. (word32)XSTRLEN(keys)), 0);
  13061. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13062. /* Update Hmac. */
  13063. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13064. /* Test bad args. */
  13065. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13066. BAD_FUNC_ARG);
  13067. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13068. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13069. wc_HmacFree(&hmac);
  13070. #endif
  13071. return EXPECT_RESULT();
  13072. } /* END test_wc_ShaHmacUpdate */
  13073. /*
  13074. * testing wc_HmacUpdate on SHA224 hash.
  13075. */
  13076. static int test_wc_Sha224HmacUpdate(void)
  13077. {
  13078. EXPECT_DECLS;
  13079. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13080. Hmac hmac;
  13081. testVector a, b;
  13082. #ifdef HAVE_FIPS
  13083. const char* keys =
  13084. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13085. #else
  13086. const char* keys = "Jefe";
  13087. #endif
  13088. a.input = "what do ya want for nothing?";
  13089. a.inLen = XSTRLEN(a.input);
  13090. b.input = "Hi There";
  13091. b.inLen = XSTRLEN(b.input);
  13092. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13093. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13094. (word32)XSTRLEN(keys)), 0);
  13095. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13096. /* Update Hmac. */
  13097. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13098. /* Test bad args. */
  13099. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13100. BAD_FUNC_ARG);
  13101. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13102. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13103. wc_HmacFree(&hmac);
  13104. #endif
  13105. return EXPECT_RESULT();
  13106. } /* END test_wc_Sha224HmacUpdate */
  13107. /*
  13108. * testing wc_HmacUpdate on SHA256 hash.
  13109. */
  13110. static int test_wc_Sha256HmacUpdate(void)
  13111. {
  13112. EXPECT_DECLS;
  13113. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13114. Hmac hmac;
  13115. testVector a, b;
  13116. #ifdef HAVE_FIPS
  13117. const char* keys =
  13118. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13119. #else
  13120. const char* keys = "Jefe";
  13121. #endif
  13122. a.input = "what do ya want for nothing?";
  13123. a.inLen = XSTRLEN(a.input);
  13124. b.input = "Hi There";
  13125. b.inLen = XSTRLEN(b.input);
  13126. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13127. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13128. (word32)XSTRLEN(keys)), 0);
  13129. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13130. /* Update Hmac. */
  13131. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13132. /* Test bad args. */
  13133. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13134. BAD_FUNC_ARG);
  13135. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13136. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13137. wc_HmacFree(&hmac);
  13138. #endif
  13139. return EXPECT_RESULT();
  13140. } /* END test_wc_Sha256HmacUpdate */
  13141. /*
  13142. * testing wc_HmacUpdate on SHA384 hash.
  13143. */
  13144. static int test_wc_Sha384HmacUpdate(void)
  13145. {
  13146. EXPECT_DECLS;
  13147. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13148. Hmac hmac;
  13149. testVector a, b;
  13150. #ifdef HAVE_FIPS
  13151. const char* keys =
  13152. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13153. #else
  13154. const char* keys = "Jefe";
  13155. #endif
  13156. a.input = "what do ya want for nothing?";
  13157. a.inLen = XSTRLEN(a.input);
  13158. b.input = "Hi There";
  13159. b.inLen = XSTRLEN(b.input);
  13160. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13161. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13162. (word32)XSTRLEN(keys)), 0);
  13163. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13164. /* Update Hmac. */
  13165. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13166. /* Test bad args. */
  13167. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13168. BAD_FUNC_ARG);
  13169. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13170. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13171. wc_HmacFree(&hmac);
  13172. #endif
  13173. return EXPECT_RESULT();
  13174. } /* END test_wc_Sha384HmacUpdate */
  13175. /*
  13176. * Testing wc_HmacFinal() with MD5
  13177. */
  13178. static int test_wc_Md5HmacFinal(void)
  13179. {
  13180. EXPECT_DECLS;
  13181. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13182. Hmac hmac;
  13183. byte hash[WC_MD5_DIGEST_SIZE];
  13184. testVector a;
  13185. const char* key;
  13186. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13187. a.input = "Hi There";
  13188. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13189. "\x9d";
  13190. a.inLen = XSTRLEN(a.input);
  13191. a.outLen = XSTRLEN(a.output);
  13192. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13193. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13194. 0);
  13195. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13196. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13197. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13198. /* Try bad parameters. */
  13199. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13200. #ifndef HAVE_FIPS
  13201. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13202. #endif
  13203. wc_HmacFree(&hmac);
  13204. #endif
  13205. return EXPECT_RESULT();
  13206. } /* END test_wc_Md5HmacFinal */
  13207. /*
  13208. * Testing wc_HmacFinal() with SHA
  13209. */
  13210. static int test_wc_ShaHmacFinal(void)
  13211. {
  13212. EXPECT_DECLS;
  13213. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13214. Hmac hmac;
  13215. byte hash[WC_SHA_DIGEST_SIZE];
  13216. testVector a;
  13217. const char* key;
  13218. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13219. "\x0b\x0b\x0b";
  13220. a.input = "Hi There";
  13221. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13222. "\x8e\xf1\x46\xbe\x00";
  13223. a.inLen = XSTRLEN(a.input);
  13224. a.outLen = XSTRLEN(a.output);
  13225. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13226. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13227. 0);
  13228. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13229. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13230. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13231. /* Try bad parameters. */
  13232. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13233. #ifndef HAVE_FIPS
  13234. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13235. #endif
  13236. wc_HmacFree(&hmac);
  13237. #endif
  13238. return EXPECT_RESULT();
  13239. } /* END test_wc_ShaHmacFinal */
  13240. /*
  13241. * Testing wc_HmacFinal() with SHA224
  13242. */
  13243. static int test_wc_Sha224HmacFinal(void)
  13244. {
  13245. EXPECT_DECLS;
  13246. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13247. Hmac hmac;
  13248. byte hash[WC_SHA224_DIGEST_SIZE];
  13249. testVector a;
  13250. const char* key;
  13251. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13252. "\x0b\x0b\x0b";
  13253. a.input = "Hi There";
  13254. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13255. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13256. a.inLen = XSTRLEN(a.input);
  13257. a.outLen = XSTRLEN(a.output);
  13258. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13259. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13260. (word32)XSTRLEN(key)), 0);
  13261. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13262. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13263. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13264. /* Try bad parameters. */
  13265. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13266. #ifndef HAVE_FIPS
  13267. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13268. #endif
  13269. wc_HmacFree(&hmac);
  13270. #endif
  13271. return EXPECT_RESULT();
  13272. } /* END test_wc_Sha224HmacFinal */
  13273. /*
  13274. * Testing wc_HmacFinal() with SHA256
  13275. */
  13276. static int test_wc_Sha256HmacFinal(void)
  13277. {
  13278. EXPECT_DECLS;
  13279. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13280. Hmac hmac;
  13281. byte hash[WC_SHA256_DIGEST_SIZE];
  13282. testVector a;
  13283. const char* key;
  13284. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13285. "\x0b\x0b\x0b";
  13286. a.input = "Hi There";
  13287. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13288. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13289. "\xcf\xf7";
  13290. a.inLen = XSTRLEN(a.input);
  13291. a.outLen = XSTRLEN(a.output);
  13292. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13293. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13294. (word32)XSTRLEN(key)), 0);
  13295. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13296. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13297. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13298. /* Try bad parameters. */
  13299. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13300. #ifndef HAVE_FIPS
  13301. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13302. #endif
  13303. wc_HmacFree(&hmac);
  13304. #endif
  13305. return EXPECT_RESULT();
  13306. } /* END test_wc_Sha256HmacFinal */
  13307. /*
  13308. * Testing wc_HmacFinal() with SHA384
  13309. */
  13310. static int test_wc_Sha384HmacFinal(void)
  13311. {
  13312. EXPECT_DECLS;
  13313. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13314. Hmac hmac;
  13315. byte hash[WC_SHA384_DIGEST_SIZE];
  13316. testVector a;
  13317. const char* key;
  13318. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13319. "\x0b\x0b\x0b";
  13320. a.input = "Hi There";
  13321. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13322. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13323. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13324. "\xfa\x9c\xb6";
  13325. a.inLen = XSTRLEN(a.input);
  13326. a.outLen = XSTRLEN(a.output);
  13327. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13328. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13329. (word32)XSTRLEN(key)), 0);
  13330. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13331. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13332. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13333. /* Try bad parameters. */
  13334. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13335. #ifndef HAVE_FIPS
  13336. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13337. #endif
  13338. wc_HmacFree(&hmac);
  13339. #endif
  13340. return EXPECT_RESULT();
  13341. } /* END test_wc_Sha384HmacFinal */
  13342. /*
  13343. * Testing wc_InitCmac()
  13344. */
  13345. static int test_wc_InitCmac(void)
  13346. {
  13347. EXPECT_DECLS;
  13348. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13349. Cmac cmac1;
  13350. Cmac cmac2;
  13351. Cmac cmac3;
  13352. /* AES 128 key. */
  13353. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13354. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13355. /* AES 192 key. */
  13356. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13357. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13358. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13359. /* AES 256 key. */
  13360. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13361. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13362. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13363. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13364. word32 key1Sz = (word32)sizeof(key1) - 1;
  13365. word32 key2Sz = (word32)sizeof(key2) - 1;
  13366. word32 key3Sz = (word32)sizeof(key3) - 1;
  13367. int type = WC_CMAC_AES;
  13368. (void)key1;
  13369. (void)key1Sz;
  13370. (void)key2;
  13371. (void)key2Sz;
  13372. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13373. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13374. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13375. #ifdef WOLFSSL_AES_128
  13376. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13377. #endif
  13378. #ifdef WOLFSSL_AES_192
  13379. wc_AesFree(&cmac1.aes);
  13380. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13381. #endif
  13382. #ifdef WOLFSSL_AES_256
  13383. wc_AesFree(&cmac2.aes);
  13384. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13385. #endif
  13386. wc_AesFree(&cmac3.aes);
  13387. /* Test bad args. */
  13388. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13389. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13390. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13391. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13392. #endif
  13393. return EXPECT_RESULT();
  13394. } /* END test_wc_InitCmac */
  13395. /*
  13396. * Testing wc_CmacUpdate()
  13397. */
  13398. static int test_wc_CmacUpdate(void)
  13399. {
  13400. EXPECT_DECLS;
  13401. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13402. Cmac cmac;
  13403. byte key[] = {
  13404. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13405. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13406. };
  13407. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13408. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13409. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13410. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13411. "\xf4";
  13412. word32 inSz = (word32)sizeof(in) - 1;
  13413. word32 keySz = (word32)sizeof(key);
  13414. int type = WC_CMAC_AES;
  13415. XMEMSET(&cmac, 0, sizeof(Cmac));
  13416. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13417. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13418. /* Test bad args. */
  13419. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13420. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13421. wc_AesFree(&cmac.aes);
  13422. #endif
  13423. return EXPECT_RESULT();
  13424. } /* END test_wc_CmacUpdate */
  13425. /*
  13426. * Testing wc_CmacFinal()
  13427. */
  13428. static int test_wc_CmacFinal(void)
  13429. {
  13430. EXPECT_DECLS;
  13431. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13432. Cmac cmac;
  13433. byte key[] = {
  13434. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13435. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13436. };
  13437. byte msg[] = {
  13438. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13439. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13440. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13441. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13442. 0xf4
  13443. };
  13444. /* Test vectors from CMACGenAES128.rsp from
  13445. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13446. * Per RFC4493 truncation of lsb is possible.
  13447. */
  13448. byte expMac[] = {
  13449. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13450. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13451. };
  13452. byte mac[AES_BLOCK_SIZE];
  13453. word32 msgSz = (word32)sizeof(msg);
  13454. word32 keySz = (word32)sizeof(key);
  13455. word32 macSz = sizeof(mac);
  13456. word32 badMacSz = 17;
  13457. int expMacSz = sizeof(expMac);
  13458. int type = WC_CMAC_AES;
  13459. XMEMSET(&cmac, 0, sizeof(Cmac));
  13460. XMEMSET(mac, 0, macSz);
  13461. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13462. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13463. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13464. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13465. /* Pass in bad args. */
  13466. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13467. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13468. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13469. #endif
  13470. return EXPECT_RESULT();
  13471. } /* END test_wc_CmacFinal */
  13472. /*
  13473. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13474. */
  13475. static int test_wc_AesCmacGenerate(void)
  13476. {
  13477. EXPECT_DECLS;
  13478. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13479. byte key[] = {
  13480. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13481. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13482. };
  13483. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13484. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13485. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13486. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13487. byte mac[AES_BLOCK_SIZE];
  13488. word32 keySz = sizeof(key);
  13489. word32 macSz = sizeof(mac);
  13490. word32 msgSz = sizeof(msg) - 1;
  13491. word32 expMacSz = sizeof(expMac) - 1;
  13492. XMEMSET(mac, 0, macSz);
  13493. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13494. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13495. /* Pass in bad args. */
  13496. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13497. BAD_FUNC_ARG);
  13498. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13499. BAD_FUNC_ARG);
  13500. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13501. BAD_FUNC_ARG);
  13502. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13503. BAD_FUNC_ARG);
  13504. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13505. /* Test bad args. */
  13506. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13507. BAD_FUNC_ARG);
  13508. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13509. BAD_FUNC_ARG);
  13510. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13511. BAD_FUNC_ARG);
  13512. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13513. BAD_FUNC_ARG);
  13514. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13515. BAD_FUNC_ARG);
  13516. #endif
  13517. return EXPECT_RESULT();
  13518. } /* END test_wc_AesCmacGenerate */
  13519. /*
  13520. * Testing streaming AES-GCM API.
  13521. */
  13522. static int test_wc_AesGcmStream(void)
  13523. {
  13524. EXPECT_DECLS;
  13525. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13526. defined(WOLFSSL_AESGCM_STREAM)
  13527. int i;
  13528. WC_RNG rng[1];
  13529. Aes aesEnc[1];
  13530. Aes aesDec[1];
  13531. byte tag[AES_BLOCK_SIZE];
  13532. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13533. byte out[AES_BLOCK_SIZE * 3 + 2];
  13534. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13535. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13536. byte key[AES_128_KEY_SIZE] = { 0, };
  13537. byte iv[AES_IV_SIZE] = { 1, };
  13538. byte ivOut[AES_IV_SIZE];
  13539. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13540. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13541. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13542. };
  13543. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13544. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13545. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13546. };
  13547. static const byte expTag[AES_BLOCK_SIZE] = {
  13548. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13549. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13550. };
  13551. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13552. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13553. XMEMSET(&aesDec, 0, sizeof(Aes));
  13554. /* Create a random for generating IV/nonce. */
  13555. ExpectIntEQ(wc_InitRng(rng), 0);
  13556. /* Initialize data structures. */
  13557. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13558. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13559. /* BadParameters to streaming init. */
  13560. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13561. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13562. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13563. BAD_FUNC_ARG);
  13564. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13565. BAD_FUNC_ARG);
  13566. /* Bad parameters to encrypt update. */
  13567. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13568. BAD_FUNC_ARG);
  13569. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13570. BAD_FUNC_ARG);
  13571. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13572. BAD_FUNC_ARG);
  13573. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13574. BAD_FUNC_ARG);
  13575. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13576. BAD_FUNC_ARG);
  13577. /* Bad parameters to decrypt update. */
  13578. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13579. BAD_FUNC_ARG);
  13580. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13581. BAD_FUNC_ARG);
  13582. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13583. BAD_FUNC_ARG);
  13584. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13585. BAD_FUNC_ARG);
  13586. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13587. BAD_FUNC_ARG);
  13588. /* Bad parameters to encrypt final. */
  13589. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13590. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13591. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13592. BAD_FUNC_ARG);
  13593. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13594. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13595. BAD_FUNC_ARG);
  13596. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13597. BAD_FUNC_ARG);
  13598. /* Bad parameters to decrypt final. */
  13599. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13600. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13601. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13602. BAD_FUNC_ARG);
  13603. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13604. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13605. BAD_FUNC_ARG);
  13606. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13607. BAD_FUNC_ARG);
  13608. /* Check calling final before setting key fails. */
  13609. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13610. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13611. /* Check calling update before setting key else fails. */
  13612. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13613. MISSING_KEY);
  13614. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13615. MISSING_KEY);
  13616. /* Set key but not IV. */
  13617. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13618. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13619. /* Check calling final before setting IV fails. */
  13620. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13621. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13622. /* Check calling update before setting IV else fails. */
  13623. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13624. MISSING_IV);
  13625. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13626. MISSING_IV);
  13627. /* Set IV using fixed part IV and external IV APIs. */
  13628. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13629. rng), 0);
  13630. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13631. GCM_NONCE_MID_SZ), 0);
  13632. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13633. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13634. /* Encrypt and decrypt data. */
  13635. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13636. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13637. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13638. /* Finalize and check tag matches. */
  13639. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13640. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13641. /* Set key and IV through streaming init API. */
  13642. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13643. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13644. /* Encrypt/decrypt one block and AAD of one block. */
  13645. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13646. AES_BLOCK_SIZE), 0);
  13647. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13648. AES_BLOCK_SIZE), 0);
  13649. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13650. /* Finalize and check tag matches. */
  13651. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13652. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13653. /* Set key and IV through streaming init API. */
  13654. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13655. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13656. /* No data to encrypt/decrypt one byte of AAD. */
  13657. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13658. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13659. /* Finalize and check tag matches. */
  13660. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13661. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13662. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13663. /* Set key and IV through streaming init API. */
  13664. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13665. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13666. /* Encrypt/decrypt one byte and no AAD. */
  13667. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13668. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13669. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13670. /* Finalize and check tag matches. */
  13671. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13672. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13673. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13674. /* Set key and IV through streaming init API. */
  13675. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13676. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13677. /* Encryption AES is one byte at a time */
  13678. for (i = 0; i < (int)sizeof(aad); i++) {
  13679. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13680. 0);
  13681. }
  13682. for (i = 0; i < (int)sizeof(in); i++) {
  13683. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13684. 0);
  13685. }
  13686. /* Decryption AES is two bytes at a time */
  13687. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13688. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13689. 0);
  13690. }
  13691. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13692. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13693. 0), 0);
  13694. }
  13695. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13696. /* Finalize and check tag matches. */
  13697. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13698. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13699. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13700. /* Check streaming encryption can be decrypted with one shot. */
  13701. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13702. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13703. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13704. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13705. wc_AesFree(aesEnc);
  13706. wc_AesFree(aesDec);
  13707. wc_FreeRng(rng);
  13708. #endif
  13709. return EXPECT_RESULT();
  13710. } /* END test_wc_AesGcmStream */
  13711. /*
  13712. * Testing streaming SM4 API.
  13713. */
  13714. static int test_wc_Sm4(void)
  13715. {
  13716. int res = TEST_SKIPPED;
  13717. #ifdef WOLFSSL_SM4
  13718. EXPECT_DECLS;
  13719. wc_Sm4 sm4;
  13720. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13721. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13722. unsigned char key[SM4_KEY_SIZE];
  13723. #endif
  13724. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13725. unsigned char iv[SM4_IV_SIZE];
  13726. #endif
  13727. /* Invalid parameters - wc_Sm4Init */
  13728. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13729. /* Valid cases - wc_Sm4Init */
  13730. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13731. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13732. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13733. XMEMSET(key, 0, sizeof(key));
  13734. /* Invalid parameters - wc_Sm4SetKey. */
  13735. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  13736. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  13737. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  13738. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13739. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13742. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  13743. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  13744. /* Valid cases - wc_Sm4SetKey. */
  13745. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13746. #endif
  13747. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13748. XMEMSET(iv, 0, sizeof(iv));
  13749. /* Invalid parameters - wc_Sm4SetIV. */
  13750. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  13751. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  13752. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  13753. /* Valid cases - wc_Sm4SetIV. */
  13754. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13755. #endif
  13756. /* Valid cases - wc_Sm4Free */
  13757. wc_Sm4Free(NULL);
  13758. wc_Sm4Free(&sm4);
  13759. res = EXPECT_RESULT();
  13760. #endif
  13761. return res;
  13762. } /* END test_wc_Sm4 */
  13763. /*
  13764. * Testing block based SM4-ECB API.
  13765. */
  13766. static int test_wc_Sm4Ecb(void)
  13767. {
  13768. int res = TEST_SKIPPED;
  13769. #ifdef WOLFSSL_SM4_ECB
  13770. EXPECT_DECLS;
  13771. wc_Sm4 sm4;
  13772. unsigned char key[SM4_KEY_SIZE];
  13773. unsigned char in[SM4_BLOCK_SIZE * 2];
  13774. unsigned char out[SM4_BLOCK_SIZE * 2];
  13775. unsigned char out2[SM4_BLOCK_SIZE];
  13776. XMEMSET(key, 0, sizeof(key));
  13777. XMEMSET(in, 0, sizeof(in));
  13778. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13779. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13780. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13781. /* Tested in test_wc_Sm4. */
  13782. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13783. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  13784. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13785. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13786. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13787. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13788. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13789. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13790. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13791. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13792. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13793. /* Valid cases - wc_Sm4EcbEncrypt. */
  13794. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  13795. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13796. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13797. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13798. /* In and out are same pointer. */
  13799. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13800. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13801. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  13802. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13803. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13804. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13805. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13806. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13807. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13808. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13809. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13810. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13811. /* Valid cases - wc_Sm4EcbDecrypt. */
  13812. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  13813. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13814. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13815. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13816. /* In and out are same pointer. */
  13817. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13818. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13819. wc_Sm4Free(&sm4);
  13820. res = EXPECT_RESULT();
  13821. #endif
  13822. return res;
  13823. } /* END test_wc_Sm4Ecb */
  13824. /*
  13825. * Testing block based SM4-CBC API.
  13826. */
  13827. static int test_wc_Sm4Cbc(void)
  13828. {
  13829. int res = TEST_SKIPPED;
  13830. #ifdef WOLFSSL_SM4_CBC
  13831. EXPECT_DECLS;
  13832. wc_Sm4 sm4;
  13833. unsigned char key[SM4_KEY_SIZE];
  13834. unsigned char iv[SM4_IV_SIZE];
  13835. unsigned char in[SM4_BLOCK_SIZE * 2];
  13836. unsigned char out[SM4_BLOCK_SIZE * 2];
  13837. unsigned char out2[SM4_BLOCK_SIZE];
  13838. XMEMSET(key, 0, sizeof(key));
  13839. XMEMSET(iv, 0, sizeof(iv));
  13840. XMEMSET(in, 0, sizeof(in));
  13841. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13842. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13843. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13844. /* Tested in test_wc_Sm4. */
  13845. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13846. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  13847. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  13848. /* Tested in test_wc_Sm4. */
  13849. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13850. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  13851. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13852. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13853. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13854. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13855. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13856. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13857. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13858. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13859. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13860. /* Valid cases - wc_Sm4CbcEncrypt. */
  13861. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  13862. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13863. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13864. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13865. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13866. /* In and out are same pointer. */
  13867. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13868. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13869. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13870. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  13871. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13872. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13873. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13874. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13875. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13876. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13877. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13878. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13879. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13880. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13881. /* Valid cases - wc_Sm4CbcDecrypt. */
  13882. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  13883. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13884. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13885. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13886. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13887. /* In and out are same pointer. */
  13888. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13889. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13890. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13891. wc_Sm4Free(&sm4);
  13892. res = EXPECT_RESULT();
  13893. #endif
  13894. return res;
  13895. } /* END test_wc_Sm4Cbc */
  13896. /*
  13897. * Testing streaming SM4-CTR API.
  13898. */
  13899. static int test_wc_Sm4Ctr(void)
  13900. {
  13901. int res = TEST_SKIPPED;
  13902. #ifdef WOLFSSL_SM4_CTR
  13903. EXPECT_DECLS;
  13904. wc_Sm4 sm4;
  13905. unsigned char key[SM4_KEY_SIZE];
  13906. unsigned char iv[SM4_IV_SIZE];
  13907. unsigned char in[SM4_BLOCK_SIZE * 4];
  13908. unsigned char out[SM4_BLOCK_SIZE * 4];
  13909. unsigned char out2[SM4_BLOCK_SIZE * 4];
  13910. word32 chunk;
  13911. word32 i;
  13912. XMEMSET(key, 0, sizeof(key));
  13913. XMEMSET(iv, 0, sizeof(iv));
  13914. XMEMSET(in, 0, sizeof(in));
  13915. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13916. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13917. /* Tested in test_wc_Sm4. */
  13918. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13919. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  13920. /* Tested in test_wc_Sm4. */
  13921. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13922. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  13923. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13924. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  13925. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  13926. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  13927. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13928. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13929. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13930. /* Valid cases - wc_Sm4CtrEncrypt. */
  13931. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  13932. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  13933. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13934. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  13935. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  13936. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13937. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13938. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  13939. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13940. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13941. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13942. /* In and out are same pointer. Also check encrypt of cipher text produces
  13943. * plaintext.
  13944. */
  13945. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13946. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  13947. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13948. /* Chunking tests. */
  13949. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13950. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  13951. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  13952. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13953. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  13954. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  13955. }
  13956. if (i < (word32)sizeof(in)) {
  13957. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  13958. (word32)sizeof(in) - i), 0);
  13959. }
  13960. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  13961. }
  13962. for (i = 0; i < (word32)sizeof(iv); i++) {
  13963. iv[i] = 0xff;
  13964. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13965. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13966. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13967. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  13968. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  13969. }
  13970. wc_Sm4Free(&sm4);
  13971. res = EXPECT_RESULT();
  13972. #endif
  13973. return res;
  13974. } /* END test_wc_Sm4Ctr */
  13975. /*
  13976. * Testing stream SM4-GCM API.
  13977. */
  13978. static int test_wc_Sm4Gcm(void)
  13979. {
  13980. int res = TEST_SKIPPED;
  13981. #ifdef WOLFSSL_SM4_GCM
  13982. EXPECT_DECLS;
  13983. wc_Sm4 sm4;
  13984. unsigned char key[SM4_KEY_SIZE];
  13985. unsigned char nonce[GCM_NONCE_MAX_SZ];
  13986. unsigned char in[SM4_BLOCK_SIZE * 2];
  13987. unsigned char in2[SM4_BLOCK_SIZE * 2];
  13988. unsigned char out[SM4_BLOCK_SIZE * 2];
  13989. unsigned char out2[SM4_BLOCK_SIZE * 2];
  13990. unsigned char dec[SM4_BLOCK_SIZE * 2];
  13991. unsigned char tag[SM4_BLOCK_SIZE];
  13992. unsigned char aad[SM4_BLOCK_SIZE * 2];
  13993. word32 i;
  13994. XMEMSET(key, 0, sizeof(key));
  13995. XMEMSET(nonce, 0, sizeof(nonce));
  13996. XMEMSET(in, 0, sizeof(in));
  13997. XMEMSET(in2, 0, sizeof(in2));
  13998. XMEMSET(aad, 0, sizeof(aad));
  13999. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14000. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14001. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14002. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14003. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14004. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14005. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14006. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14007. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14008. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14009. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14010. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14011. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14012. /* Valid parameters - wc_Sm4GcmSetKey. */
  14013. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14014. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14015. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14016. 0), BAD_FUNC_ARG);
  14017. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14018. 0), BAD_FUNC_ARG);
  14019. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14020. 0), BAD_FUNC_ARG);
  14021. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14022. 0), BAD_FUNC_ARG);
  14023. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14024. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14025. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14026. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14027. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14028. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14029. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14030. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14031. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14032. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14033. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14034. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14035. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14036. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14037. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14038. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14039. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14040. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14041. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14042. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14043. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14044. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14045. 0), BAD_FUNC_ARG);
  14046. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14047. 0), BAD_FUNC_ARG);
  14048. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14049. 0), BAD_FUNC_ARG);
  14050. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14051. 0), BAD_FUNC_ARG);
  14052. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14053. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14054. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14055. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14056. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14057. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14058. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14059. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14060. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14061. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14062. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14063. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14064. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14065. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14066. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14067. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14068. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14069. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14070. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14071. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14072. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14073. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14074. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14075. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14076. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14077. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14078. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14079. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14080. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14081. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14082. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14083. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14084. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14085. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14086. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14087. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14088. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14089. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14090. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14091. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14092. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14093. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14094. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14095. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14096. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14097. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14098. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14099. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14100. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14101. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14102. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14103. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14104. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14105. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14106. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14107. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14108. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14109. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14110. SM4_GCM_AUTH_E);
  14111. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14112. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14113. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14114. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14115. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14116. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14117. }
  14118. /* Check different in/out sizes. */
  14119. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14120. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14121. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14122. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14123. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14124. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14125. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14126. XMEMCPY(out2, out, i - 1);
  14127. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14128. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14129. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14130. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14131. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14132. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14133. }
  14134. /* Force the counter to roll over in first byte. */
  14135. {
  14136. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14137. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14138. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14139. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14140. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14141. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14142. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14143. }
  14144. wc_Sm4Free(&sm4);
  14145. res = EXPECT_RESULT();
  14146. #endif
  14147. return res;
  14148. } /* END test_wc_Sm4Gcm */
  14149. /*
  14150. * Testing stream SM4-CCM API.
  14151. */
  14152. static int test_wc_Sm4Ccm(void)
  14153. {
  14154. int res = TEST_SKIPPED;
  14155. #ifdef WOLFSSL_SM4_CCM
  14156. EXPECT_DECLS;
  14157. wc_Sm4 sm4;
  14158. unsigned char key[SM4_KEY_SIZE];
  14159. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14160. unsigned char in[SM4_BLOCK_SIZE * 2];
  14161. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14162. unsigned char out[SM4_BLOCK_SIZE * 2];
  14163. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14164. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14165. unsigned char tag[SM4_BLOCK_SIZE];
  14166. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14167. word32 i;
  14168. XMEMSET(key, 0, sizeof(key));
  14169. XMEMSET(nonce, 0, sizeof(nonce));
  14170. XMEMSET(in, 0, sizeof(in));
  14171. XMEMSET(in2, 0, sizeof(in2));
  14172. XMEMSET(aad, 0, sizeof(aad));
  14173. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14174. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14175. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14176. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14177. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14178. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14179. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14180. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14181. 0), BAD_FUNC_ARG);
  14182. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14183. 0), BAD_FUNC_ARG);
  14184. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14185. 0), BAD_FUNC_ARG);
  14186. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14187. 0), BAD_FUNC_ARG);
  14188. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14189. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14190. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14191. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14192. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14193. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14194. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14195. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14196. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14197. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14198. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14199. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14200. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14201. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14202. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14203. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14204. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14205. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14206. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14207. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14208. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14209. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14210. 0), BAD_FUNC_ARG);
  14211. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14212. 0), BAD_FUNC_ARG);
  14213. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14214. 0), BAD_FUNC_ARG);
  14215. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14216. 0), BAD_FUNC_ARG);
  14217. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14218. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14219. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14220. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14221. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14222. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14223. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14224. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14225. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14226. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14227. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14228. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14229. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14230. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14231. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14232. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14233. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14234. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14235. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14236. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14237. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14238. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14239. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14240. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14241. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14242. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14243. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14244. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14245. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14246. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14247. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14248. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14249. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14250. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14251. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14252. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14253. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14254. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14255. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14256. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14257. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14258. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14259. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14260. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14261. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14262. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14263. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14264. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14265. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14266. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14267. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14268. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14269. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14270. }
  14271. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14272. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14273. SM4_CCM_AUTH_E);
  14274. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14275. for (i = 0; i < 4; i++) {
  14276. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14277. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14278. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14279. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14280. }
  14281. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14282. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14283. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14284. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14285. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14286. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14287. }
  14288. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14289. * Even values in range 4..SM4_BLOCK_SIZE.
  14290. */
  14291. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14292. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14293. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14294. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14295. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14296. }
  14297. /* Check different in/out sizes. */
  14298. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14299. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14300. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14301. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14302. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14303. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14304. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14305. XMEMCPY(out2, out, i - 1);
  14306. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14307. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14308. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14309. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14310. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14311. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14312. }
  14313. /* Force the counter to roll over in first byte. */
  14314. {
  14315. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14316. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14317. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14318. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14319. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14320. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14321. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14322. }
  14323. wc_Sm4Free(&sm4);
  14324. res = EXPECT_RESULT();
  14325. #endif
  14326. return res;
  14327. } /* END test_wc_Sm4Ccm */
  14328. /*
  14329. * unit test for wc_Des3_SetIV()
  14330. */
  14331. static int test_wc_Des3_SetIV(void)
  14332. {
  14333. EXPECT_DECLS;
  14334. #ifndef NO_DES3
  14335. Des3 des;
  14336. const byte key[] = {
  14337. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14338. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14339. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14340. };
  14341. const byte iv[] = {
  14342. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14343. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14344. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14345. };
  14346. XMEMSET(&des, 0, sizeof(Des3));
  14347. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14348. /* DES_ENCRYPTION or DES_DECRYPTION */
  14349. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14350. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14351. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14352. /* Test explicitly wc_Des3_SetIV() */
  14353. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14354. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14355. #endif
  14356. wc_Des3Free(&des);
  14357. #endif
  14358. return EXPECT_RESULT();
  14359. } /* END test_wc_Des3_SetIV */
  14360. /*
  14361. * unit test for wc_Des3_SetKey()
  14362. */
  14363. static int test_wc_Des3_SetKey(void)
  14364. {
  14365. EXPECT_DECLS;
  14366. #ifndef NO_DES3
  14367. Des3 des;
  14368. const byte key[] = {
  14369. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14370. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14371. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14372. };
  14373. const byte iv[] = {
  14374. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14375. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14376. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14377. };
  14378. XMEMSET(&des, 0, sizeof(Des3));
  14379. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14380. /* DES_ENCRYPTION or DES_DECRYPTION */
  14381. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14382. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14383. /* Test bad args. */
  14384. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14385. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14386. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14387. /* Default case. Should return 0. */
  14388. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14389. wc_Des3Free(&des);
  14390. #endif
  14391. return EXPECT_RESULT();
  14392. } /* END test_wc_Des3_SetKey */
  14393. /*
  14394. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14395. */
  14396. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14397. {
  14398. EXPECT_DECLS;
  14399. #ifndef NO_DES3
  14400. Des3 des;
  14401. byte cipher[24];
  14402. byte plain[24];
  14403. const byte key[] = {
  14404. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14405. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14406. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14407. };
  14408. const byte iv[] = {
  14409. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14410. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14411. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14412. };
  14413. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14414. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14415. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14416. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14417. };
  14418. XMEMSET(&des, 0, sizeof(Des3));
  14419. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14420. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14421. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14422. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14423. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14424. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14425. /* Pass in bad args. */
  14426. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14427. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14428. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14429. BAD_FUNC_ARG);
  14430. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14431. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14432. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14433. wc_Des3Free(&des);
  14434. #endif
  14435. return EXPECT_RESULT();
  14436. } /* END wc_Des3_CbcEncrypt */
  14437. /*
  14438. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14439. */
  14440. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14441. {
  14442. EXPECT_DECLS;
  14443. #ifndef NO_DES3
  14444. word32 vectorSz, cipherSz;
  14445. byte cipher[24];
  14446. byte plain[24];
  14447. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14448. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14449. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14450. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14451. };
  14452. byte key[] = {
  14453. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14454. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14455. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14456. };
  14457. byte iv[] = {
  14458. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14459. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14460. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14461. };
  14462. vectorSz = sizeof(byte) * 24;
  14463. cipherSz = sizeof(byte) * 24;
  14464. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14465. 0);
  14466. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14467. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14468. /* pass in bad args. */
  14469. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14470. BAD_FUNC_ARG);
  14471. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14472. BAD_FUNC_ARG);
  14473. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14474. BAD_FUNC_ARG);
  14475. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14476. 0);
  14477. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14478. BAD_FUNC_ARG);
  14479. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14480. BAD_FUNC_ARG);
  14481. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14482. BAD_FUNC_ARG);
  14483. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14484. 0);
  14485. #endif
  14486. return EXPECT_RESULT();
  14487. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14488. /*
  14489. * Unit test for wc_Des3_EcbEncrypt
  14490. */
  14491. static int test_wc_Des3_EcbEncrypt(void)
  14492. {
  14493. EXPECT_DECLS;
  14494. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14495. Des3 des;
  14496. byte cipher[24];
  14497. word32 cipherSz = sizeof(cipher);
  14498. const byte key[] = {
  14499. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14500. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14501. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14502. };
  14503. const byte iv[] = {
  14504. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14505. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14506. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14507. };
  14508. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14509. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14510. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14511. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14512. };
  14513. XMEMSET(&des, 0, sizeof(Des3));
  14514. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14515. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14516. /* Bad Cases */
  14517. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14518. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14519. BAD_FUNC_ARG);
  14520. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14521. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14522. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14523. /* Good Cases */
  14524. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14525. wc_Des3Free(&des);
  14526. #endif
  14527. return EXPECT_RESULT();
  14528. } /* END test_wc_Des3_EcbEncrypt */
  14529. /*
  14530. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14531. */
  14532. static int test_wc_Chacha_SetKey(void)
  14533. {
  14534. EXPECT_DECLS;
  14535. #ifdef HAVE_CHACHA
  14536. ChaCha ctx;
  14537. const byte key[] = {
  14538. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14539. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14540. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14541. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14542. };
  14543. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14544. byte cipher[128];
  14545. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14546. /* Test bad args. */
  14547. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14548. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14549. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14550. /* Test bad args. */
  14551. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14552. #endif
  14553. return EXPECT_RESULT();
  14554. } /* END test_wc_Chacha_SetKey */
  14555. /*
  14556. * unit test for wc_Poly1305SetKey()
  14557. */
  14558. static int test_wc_Poly1305SetKey(void)
  14559. {
  14560. EXPECT_DECLS;
  14561. #ifdef HAVE_POLY1305
  14562. Poly1305 ctx;
  14563. const byte key[] =
  14564. {
  14565. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14567. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14568. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14569. };
  14570. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14571. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14572. /* Test bad args. */
  14573. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14574. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14575. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14576. #endif
  14577. return EXPECT_RESULT();
  14578. } /* END test_wc_Poly1305_SetKey() */
  14579. /*
  14580. * Testing wc_Chacha_Process()
  14581. */
  14582. static int test_wc_Chacha_Process(void)
  14583. {
  14584. EXPECT_DECLS;
  14585. #ifdef HAVE_CHACHA
  14586. ChaCha enc, dec;
  14587. byte cipher[128];
  14588. byte plain[128];
  14589. const byte key[] =
  14590. {
  14591. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14592. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14593. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14594. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14595. };
  14596. const char* input = "Everybody gets Friday off.";
  14597. word32 keySz = sizeof(key)/sizeof(byte);
  14598. unsigned long int inlen = XSTRLEN(input);
  14599. /* Initialize stack variables. */
  14600. XMEMSET(cipher, 0, 128);
  14601. XMEMSET(plain, 0, 128);
  14602. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14603. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14604. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14605. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14606. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14607. 0);
  14608. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14609. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14610. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14611. /* test checking and using leftovers, currently just in C code */
  14612. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14613. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14614. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14615. (word32)inlen - 2), 0);
  14616. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14617. (byte*)input + (inlen - 2), 2), 0);
  14618. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14619. (word32)inlen - 2), 0);
  14620. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14621. (byte*)input + (inlen - 2), 2), 0);
  14622. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14623. /* check edge cases with counter increment */
  14624. {
  14625. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14626. const byte expected[] = {
  14627. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14628. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14629. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14630. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14631. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14632. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14633. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14634. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14635. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14636. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14637. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14638. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14639. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14640. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14641. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14642. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14643. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14644. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14645. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14646. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14647. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14648. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14649. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14650. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14651. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14652. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14653. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14654. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14655. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14656. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14657. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14658. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14659. };
  14660. const byte iv2[] = {
  14661. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14662. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14663. };
  14664. byte input2[256];
  14665. int i;
  14666. for (i = 0; i < 256; i++)
  14667. input2[i] = i;
  14668. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  14669. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  14670. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14671. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  14672. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14673. /* partial */
  14674. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  14675. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14676. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  14677. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14678. }
  14679. #endif
  14680. /* Test bad args. */
  14681. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  14682. BAD_FUNC_ARG);
  14683. #endif
  14684. return EXPECT_RESULT();
  14685. } /* END test_wc_Chacha_Process */
  14686. /*
  14687. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14688. */
  14689. static int test_wc_ChaCha20Poly1305_aead(void)
  14690. {
  14691. EXPECT_DECLS;
  14692. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14693. const byte key[] = {
  14694. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14695. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14696. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14697. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14698. };
  14699. const byte plaintext[] = {
  14700. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14701. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14702. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14703. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14704. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14705. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14706. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14707. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14708. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14709. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14710. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14711. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14712. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14713. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14714. 0x74, 0x2e
  14715. };
  14716. const byte iv[] = {
  14717. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14718. 0x44, 0x45, 0x46, 0x47
  14719. };
  14720. const byte aad[] = { /* additional data */
  14721. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14722. 0xc4, 0xc5, 0xc6, 0xc7
  14723. };
  14724. const byte cipher[] = { /* expected output from operation */
  14725. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14726. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14727. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14728. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14729. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14730. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14731. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14732. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14733. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14734. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14735. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14736. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14737. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14738. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14739. 0x61, 0x16
  14740. };
  14741. const byte authTag[] = { /* expected output from operation */
  14742. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14743. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14744. };
  14745. byte generatedCiphertext[272];
  14746. byte generatedPlaintext[272];
  14747. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14748. /* Initialize stack variables. */
  14749. XMEMSET(generatedCiphertext, 0, 272);
  14750. XMEMSET(generatedPlaintext, 0, 272);
  14751. /* Test Encrypt */
  14752. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14753. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14754. 0);
  14755. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  14756. sizeof(cipher)/sizeof(byte)), 0);
  14757. /* Test bad args. */
  14758. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  14759. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14760. BAD_FUNC_ARG);
  14761. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14762. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14763. BAD_FUNC_ARG);
  14764. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14765. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14766. BAD_FUNC_ARG);
  14767. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14768. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14769. BAD_FUNC_ARG);
  14770. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14771. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  14772. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14773. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  14774. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14775. sizeof(cipher), authTag, generatedPlaintext), 0);
  14776. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  14777. sizeof(plaintext)/sizeof(byte)), 0);
  14778. /* Test bad args. */
  14779. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14780. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14781. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14782. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14783. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14784. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14785. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14786. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  14787. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14788. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  14789. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14790. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14791. #endif
  14792. return EXPECT_RESULT();
  14793. } /* END test_wc_ChaCha20Poly1305_aead */
  14794. /*
  14795. * Testing function for wc_Rc2SetKey().
  14796. */
  14797. static int test_wc_Rc2SetKey(void)
  14798. {
  14799. EXPECT_DECLS;
  14800. #ifdef WC_RC2
  14801. Rc2 rc2;
  14802. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14803. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14804. /* valid key and IV */
  14805. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14806. iv, 40), 0);
  14807. /* valid key, no IV */
  14808. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14809. NULL, 40), 0);
  14810. /* bad arguments */
  14811. /* null Rc2 struct */
  14812. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14813. iv, 40), BAD_FUNC_ARG);
  14814. /* null key */
  14815. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14816. iv, 40), BAD_FUNC_ARG);
  14817. /* key size == 0 */
  14818. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  14819. /* key size > 128 */
  14820. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  14821. /* effective bits == 0 */
  14822. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14823. iv, 0), WC_KEY_SIZE_E);
  14824. /* effective bits > 1024 */
  14825. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14826. iv, 1025), WC_KEY_SIZE_E);
  14827. #endif
  14828. return EXPECT_RESULT();
  14829. } /* END test_wc_Rc2SetKey */
  14830. /*
  14831. * Testing function for wc_Rc2SetIV().
  14832. */
  14833. static int test_wc_Rc2SetIV(void)
  14834. {
  14835. EXPECT_DECLS;
  14836. #ifdef WC_RC2
  14837. Rc2 rc2;
  14838. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14839. /* valid IV */
  14840. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14841. /* valid NULL IV */
  14842. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  14843. /* bad arguments */
  14844. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  14845. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  14846. #endif
  14847. return EXPECT_RESULT();
  14848. } /* END test_wc_Rc2SetIV */
  14849. /*
  14850. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  14851. */
  14852. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14853. {
  14854. EXPECT_DECLS;
  14855. #ifdef WC_RC2
  14856. Rc2 rc2;
  14857. int effectiveKeyBits = 63;
  14858. byte cipher[RC2_BLOCK_SIZE];
  14859. byte plain[RC2_BLOCK_SIZE];
  14860. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14861. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14862. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14863. XMEMSET(cipher, 0, sizeof(cipher));
  14864. XMEMSET(plain, 0, sizeof(plain));
  14865. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14866. NULL, effectiveKeyBits), 0);
  14867. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  14868. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  14869. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  14870. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  14871. /* Rc2EcbEncrypt bad arguments */
  14872. /* null Rc2 struct */
  14873. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  14874. BAD_FUNC_ARG);
  14875. /* null out buffer */
  14876. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  14877. BAD_FUNC_ARG);
  14878. /* null input buffer */
  14879. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  14880. BAD_FUNC_ARG);
  14881. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14882. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  14883. /* Rc2EcbDecrypt bad arguments */
  14884. /* null Rc2 struct */
  14885. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  14886. BAD_FUNC_ARG);
  14887. /* null out buffer */
  14888. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  14889. BAD_FUNC_ARG);
  14890. /* null input buffer */
  14891. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  14892. BAD_FUNC_ARG);
  14893. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14894. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  14895. #endif
  14896. return EXPECT_RESULT();
  14897. } /* END test_wc_Rc2EcbEncryptDecrypt */
  14898. /*
  14899. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  14900. */
  14901. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14902. {
  14903. EXPECT_DECLS;
  14904. #ifdef WC_RC2
  14905. Rc2 rc2;
  14906. int effectiveKeyBits = 63;
  14907. byte cipher[RC2_BLOCK_SIZE*2];
  14908. byte plain[RC2_BLOCK_SIZE*2];
  14909. /* vector taken from test.c */
  14910. byte key[] = {
  14911. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14912. };
  14913. byte iv[] = {
  14914. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14915. };
  14916. byte input[] = {
  14917. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14918. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14919. };
  14920. byte output[] = {
  14921. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14922. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14923. };
  14924. XMEMSET(cipher, 0, sizeof(cipher));
  14925. XMEMSET(plain, 0, sizeof(plain));
  14926. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14927. iv, effectiveKeyBits), 0);
  14928. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  14929. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  14930. /* reset IV for decrypt */
  14931. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14932. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  14933. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  14934. /* Rc2CbcEncrypt bad arguments */
  14935. /* null Rc2 struct */
  14936. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  14937. BAD_FUNC_ARG);
  14938. /* null out buffer */
  14939. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  14940. BAD_FUNC_ARG);
  14941. /* null input buffer */
  14942. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  14943. BAD_FUNC_ARG);
  14944. /* Rc2CbcDecrypt bad arguments */
  14945. /* in size is 0 */
  14946. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  14947. /* null Rc2 struct */
  14948. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  14949. BAD_FUNC_ARG);
  14950. /* null out buffer */
  14951. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  14952. BAD_FUNC_ARG);
  14953. /* null input buffer */
  14954. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  14955. BAD_FUNC_ARG);
  14956. #endif
  14957. return EXPECT_RESULT();
  14958. } /* END test_wc_Rc2CbcEncryptDecrypt */
  14959. /*
  14960. * Testing function for wc_AesSetIV
  14961. */
  14962. static int test_wc_AesSetIV(void)
  14963. {
  14964. int res = TEST_SKIPPED;
  14965. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14966. Aes aes;
  14967. int ret = 0;
  14968. byte key16[] =
  14969. {
  14970. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14971. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14972. };
  14973. byte iv1[] = "1234567890abcdef";
  14974. byte iv2[] = "0987654321fedcba";
  14975. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14976. if (ret != 0)
  14977. return ret;
  14978. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14979. iv1, AES_ENCRYPTION);
  14980. if (ret == 0) {
  14981. ret = wc_AesSetIV(&aes, iv2);
  14982. }
  14983. /* Test bad args. */
  14984. if (ret == 0) {
  14985. ret = wc_AesSetIV(NULL, iv1);
  14986. if (ret == BAD_FUNC_ARG) {
  14987. /* NULL iv should return 0. */
  14988. ret = wc_AesSetIV(&aes, NULL);
  14989. }
  14990. else {
  14991. ret = WOLFSSL_FATAL_ERROR;
  14992. }
  14993. }
  14994. wc_AesFree(&aes);
  14995. res = TEST_RES_CHECK(ret == 0);
  14996. #endif
  14997. return res;
  14998. } /* test_wc_AesSetIV */
  14999. /*
  15000. * Testing function for wc_AesSetKey().
  15001. */
  15002. static int test_wc_AesSetKey(void)
  15003. {
  15004. EXPECT_DECLS;
  15005. #ifndef NO_AES
  15006. Aes aes;
  15007. byte key16[] = {
  15008. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15009. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15010. };
  15011. #ifdef WOLFSSL_AES_192
  15012. byte key24[] = {
  15013. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15014. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15015. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15016. };
  15017. #endif
  15018. #ifdef WOLFSSL_AES_256
  15019. byte key32[] = {
  15020. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15021. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15022. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15023. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15024. };
  15025. #endif
  15026. byte badKey16[] = {
  15027. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15028. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15029. };
  15030. byte iv[] = "1234567890abcdef";
  15031. XMEMSET(&aes, 0, sizeof(Aes));
  15032. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15033. #ifdef WOLFSSL_AES_128
  15034. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15035. iv, AES_ENCRYPTION), 0);
  15036. #endif
  15037. #ifdef WOLFSSL_AES_192
  15038. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15039. iv, AES_ENCRYPTION), 0);
  15040. #endif
  15041. #ifdef WOLFSSL_AES_256
  15042. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15043. iv, AES_ENCRYPTION), 0);
  15044. #endif
  15045. /* Pass in bad args. */
  15046. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15047. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15048. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15049. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15050. BAD_FUNC_ARG);
  15051. wc_AesFree(&aes);
  15052. #endif
  15053. return EXPECT_RESULT();
  15054. } /* END test_wc_AesSetKey */
  15055. /*
  15056. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15057. * and wc_AesCbcDecryptWithKey()
  15058. */
  15059. static int test_wc_AesCbcEncryptDecrypt(void)
  15060. {
  15061. EXPECT_DECLS;
  15062. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15063. defined(WOLFSSL_AES_256)
  15064. Aes aes;
  15065. byte key32[] = {
  15066. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15067. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15068. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15069. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15070. };
  15071. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15072. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15073. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15074. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15075. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15076. };
  15077. byte iv[] = "1234567890abcdef";
  15078. byte enc[sizeof(vector)];
  15079. byte dec[sizeof(vector)];
  15080. byte dec2[sizeof(vector)];
  15081. /* Init stack variables. */
  15082. XMEMSET(&aes, 0, sizeof(Aes));
  15083. XMEMSET(enc, 0, sizeof(enc));
  15084. XMEMSET(dec, 0, sizeof(vector));
  15085. XMEMSET(dec2, 0, sizeof(vector));
  15086. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15087. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15088. AES_ENCRYPTION), 0);
  15089. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15090. wc_AesFree(&aes);
  15091. /* Re init for decrypt and set flag. */
  15092. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15093. AES_DECRYPTION), 0);
  15094. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15095. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15096. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15097. sizeof(key32)/sizeof(byte), iv), 0);
  15098. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15099. /* Pass in bad args */
  15100. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15101. BAD_FUNC_ARG);
  15102. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15103. BAD_FUNC_ARG);
  15104. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15105. BAD_FUNC_ARG);
  15106. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15107. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15108. BAD_LENGTH_E);
  15109. #endif
  15110. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15111. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15112. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15113. "mode (v2), skip test");
  15114. #else
  15115. /* Test passing in size of 0 */
  15116. XMEMSET(enc, 0, sizeof(enc));
  15117. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15118. /* Check enc was not modified */
  15119. {
  15120. int i;
  15121. for (i = 0; i < (int)sizeof(enc); i++)
  15122. ExpectIntEQ(enc[i], 0);
  15123. }
  15124. #endif
  15125. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15126. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15127. BAD_FUNC_ARG);
  15128. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15129. BAD_FUNC_ARG);
  15130. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15131. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15132. BAD_LENGTH_E);
  15133. #else
  15134. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15135. BAD_FUNC_ARG);
  15136. #endif
  15137. /* Test passing in size of 0 */
  15138. XMEMSET(dec, 0, sizeof(dec));
  15139. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15140. /* Check dec was not modified */
  15141. {
  15142. int i;
  15143. for (i = 0; i < (int)sizeof(dec); i++)
  15144. ExpectIntEQ(dec[i], 0);
  15145. }
  15146. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15147. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15148. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15149. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15150. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15151. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15152. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15153. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15154. wc_AesFree(&aes);
  15155. #endif
  15156. return EXPECT_RESULT();
  15157. } /* END test_wc_AesCbcEncryptDecrypt */
  15158. /*
  15159. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15160. */
  15161. static int test_wc_AesCtrEncryptDecrypt(void)
  15162. {
  15163. EXPECT_DECLS;
  15164. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15165. Aes aesEnc;
  15166. Aes aesDec;
  15167. byte key32[] = {
  15168. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15169. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15170. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15171. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15172. };
  15173. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15174. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15175. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15176. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15177. };
  15178. byte iv[] = "1234567890abcdef";
  15179. byte enc[AES_BLOCK_SIZE * 2];
  15180. byte dec[AES_BLOCK_SIZE * 2];
  15181. /* Init stack variables. */
  15182. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15183. XMEMSET(&aesDec, 0, sizeof(Aes));
  15184. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15185. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15186. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15187. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15188. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15189. AES_ENCRYPTION), 0);
  15190. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15191. sizeof(vector)/sizeof(byte)), 0);
  15192. /* Decrypt with wc_AesCtrEncrypt() */
  15193. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15194. AES_ENCRYPTION), 0);
  15195. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15196. 0);
  15197. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15198. /* Test bad args. */
  15199. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15200. BAD_FUNC_ARG);
  15201. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15202. BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15204. BAD_FUNC_ARG);
  15205. wc_AesFree(&aesEnc);
  15206. wc_AesFree(&aesDec);
  15207. #endif
  15208. return EXPECT_RESULT();
  15209. } /* END test_wc_AesCtrEncryptDecrypt */
  15210. /*
  15211. * test function for wc_AesGcmSetKey()
  15212. */
  15213. static int test_wc_AesGcmSetKey(void)
  15214. {
  15215. EXPECT_DECLS;
  15216. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15217. Aes aes;
  15218. #ifdef WOLFSSL_AES_128
  15219. byte key16[] = {
  15220. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15221. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15222. };
  15223. #endif
  15224. #ifdef WOLFSSL_AES_192
  15225. byte key24[] = {
  15226. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15227. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15228. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15229. };
  15230. #endif
  15231. #ifdef WOLFSSL_AES_256
  15232. byte key32[] = {
  15233. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15234. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15235. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15236. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15237. };
  15238. #endif
  15239. byte badKey16[] = {
  15240. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15241. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15242. };
  15243. byte badKey24[] = {
  15244. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15245. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15246. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15247. };
  15248. byte badKey32[] = {
  15249. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15250. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15251. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15252. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15253. };
  15254. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15255. #ifdef WOLFSSL_AES_128
  15256. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15257. #endif
  15258. #ifdef WOLFSSL_AES_192
  15259. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15260. #endif
  15261. #ifdef WOLFSSL_AES_256
  15262. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15263. #endif
  15264. /* Pass in bad args. */
  15265. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15266. BAD_FUNC_ARG);
  15267. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15268. BAD_FUNC_ARG);
  15269. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15270. BAD_FUNC_ARG);
  15271. wc_AesFree(&aes);
  15272. #endif
  15273. return EXPECT_RESULT();
  15274. } /* END test_wc_AesGcmSetKey */
  15275. /*
  15276. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15277. */
  15278. static int test_wc_AesGcmEncryptDecrypt(void)
  15279. {
  15280. EXPECT_DECLS;
  15281. /* WOLFSSL_AFALG requires 12 byte IV */
  15282. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15283. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15284. Aes aes;
  15285. byte key32[] = {
  15286. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15287. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15288. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15289. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15290. };
  15291. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15292. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15293. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15294. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15295. };
  15296. const byte a[] = {
  15297. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15298. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15299. 0xab, 0xad, 0xda, 0xd2
  15300. };
  15301. byte iv[] = "1234567890a";
  15302. byte longIV[] = "1234567890abcdefghij";
  15303. byte enc[sizeof(vector)];
  15304. byte resultT[AES_BLOCK_SIZE];
  15305. byte dec[sizeof(vector)];
  15306. /* Init stack variables. */
  15307. XMEMSET(&aes, 0, sizeof(Aes));
  15308. XMEMSET(enc, 0, sizeof(vector));
  15309. XMEMSET(dec, 0, sizeof(vector));
  15310. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15311. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15312. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15313. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15314. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15315. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15316. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15317. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15318. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15319. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15320. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15321. BAD_FUNC_ARG);
  15322. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15323. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15324. BAD_FUNC_ARG);
  15325. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15326. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15327. BAD_FUNC_ARG);
  15328. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15329. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15330. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15331. /* FIPS does not check the lower bound of ivSz */
  15332. #else
  15333. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15334. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15335. #endif
  15336. /* This case is now considered good. Long IVs are now allowed.
  15337. * Except for the original FIPS release, it still has an upper
  15338. * bound on the IV length. */
  15339. #if (!defined(HAVE_FIPS) || \
  15340. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15341. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15342. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15343. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15344. 0);
  15345. #else
  15346. (void)longIV;
  15347. #endif /* Old FIPS */
  15348. /* END wc_AesGcmEncrypt */
  15349. #ifdef HAVE_AES_DECRYPT
  15350. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15351. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15352. BAD_FUNC_ARG);
  15353. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15354. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15355. BAD_FUNC_ARG);
  15356. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15357. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15358. BAD_FUNC_ARG);
  15359. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15360. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15361. BAD_FUNC_ARG);
  15362. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15363. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15364. BAD_FUNC_ARG);
  15365. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15366. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15367. BAD_FUNC_ARG);
  15368. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15369. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15370. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15371. /* FIPS does not check the lower bound of ivSz */
  15372. #else
  15373. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15374. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15375. #endif
  15376. #endif /* HAVE_AES_DECRYPT */
  15377. wc_AesFree(&aes);
  15378. #endif
  15379. return EXPECT_RESULT();
  15380. } /* END test_wc_AesGcmEncryptDecrypt */
  15381. /*
  15382. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15383. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15384. */
  15385. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15386. {
  15387. EXPECT_DECLS;
  15388. #if (!defined(HAVE_FIPS) || \
  15389. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15390. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15391. const byte key[] = {
  15392. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15393. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15394. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15395. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15396. };
  15397. const byte in[] = {
  15398. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15399. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15400. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15401. };
  15402. const byte aad[] = {
  15403. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15404. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15405. 0xab, 0xad, 0xda, 0xd2
  15406. };
  15407. Aes aesEnc;
  15408. Aes aesDec;
  15409. byte iv[] = "1234567890abcdefghij";
  15410. byte out[sizeof(in)];
  15411. byte plain[sizeof(in)];
  15412. byte tag[AES_BLOCK_SIZE];
  15413. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15414. XMEMSET(&aesDec, 0, sizeof(Aes));
  15415. XMEMSET(out, 0, sizeof(out));
  15416. XMEMSET(plain, 0, sizeof(plain));
  15417. XMEMSET(tag, 0, sizeof(tag));
  15418. /* Perform one-shot encryption using long IV */
  15419. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15420. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15421. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15422. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15423. /* Perform streaming decryption using long IV */
  15424. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15425. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15426. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15427. sizeof(aad)), 0);
  15428. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15429. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15430. /* Free resources */
  15431. wc_AesFree(&aesEnc);
  15432. wc_AesFree(&aesDec);
  15433. #endif
  15434. return EXPECT_RESULT();
  15435. } /* END wc_AesGcmMixedEncDecLongIV */
  15436. /*
  15437. * unit test for wc_GmacSetKey()
  15438. */
  15439. static int test_wc_GmacSetKey(void)
  15440. {
  15441. EXPECT_DECLS;
  15442. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15443. Gmac gmac;
  15444. byte key16[] = {
  15445. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15446. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15447. };
  15448. #ifdef WOLFSSL_AES_192
  15449. byte key24[] = {
  15450. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15451. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15452. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15453. };
  15454. #endif
  15455. #ifdef WOLFSSL_AES_256
  15456. byte key32[] = {
  15457. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15458. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15459. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15460. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15461. };
  15462. #endif
  15463. byte badKey16[] = {
  15464. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15465. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15466. };
  15467. byte badKey24[] = {
  15468. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15469. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15470. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15471. };
  15472. byte badKey32[] = {
  15473. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15474. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15475. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15476. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15477. };
  15478. XMEMSET(&gmac, 0, sizeof(Gmac));
  15479. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15480. #ifdef WOLFSSL_AES_128
  15481. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15482. #endif
  15483. #ifdef WOLFSSL_AES_192
  15484. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15485. #endif
  15486. #ifdef WOLFSSL_AES_256
  15487. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15488. #endif
  15489. /* Pass in bad args. */
  15490. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15491. BAD_FUNC_ARG);
  15492. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15493. BAD_FUNC_ARG);
  15494. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15495. BAD_FUNC_ARG);
  15496. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15497. BAD_FUNC_ARG);
  15498. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15499. BAD_FUNC_ARG);
  15500. wc_AesFree(&gmac.aes);
  15501. #endif
  15502. return EXPECT_RESULT();
  15503. } /* END test_wc_GmacSetKey */
  15504. /*
  15505. * unit test for wc_GmacUpdate
  15506. */
  15507. static int test_wc_GmacUpdate(void)
  15508. {
  15509. EXPECT_DECLS;
  15510. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15511. Gmac gmac;
  15512. #ifdef WOLFSSL_AES_128
  15513. const byte key16[] = {
  15514. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15515. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15516. };
  15517. #endif
  15518. #ifdef WOLFSSL_AES_192
  15519. byte key24[] = {
  15520. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15521. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15522. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15523. };
  15524. #endif
  15525. #ifdef WOLFSSL_AES_256
  15526. byte key32[] = {
  15527. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15528. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15529. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15530. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15531. };
  15532. #endif
  15533. #ifdef WOLFSSL_AES_128
  15534. const byte authIn[] = {
  15535. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15536. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15537. };
  15538. #endif
  15539. #ifdef WOLFSSL_AES_192
  15540. const byte authIn2[] = {
  15541. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15542. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15543. };
  15544. #endif
  15545. const byte authIn3[] = {
  15546. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15547. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15548. };
  15549. #ifdef WOLFSSL_AES_128
  15550. const byte tag1[] = { /* Known. */
  15551. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15552. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15553. };
  15554. #endif
  15555. #ifdef WOLFSSL_AES_192
  15556. const byte tag2[] = { /* Known */
  15557. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15558. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15559. };
  15560. #endif
  15561. const byte tag3[] = { /* Known */
  15562. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15563. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15564. };
  15565. #ifdef WOLFSSL_AES_128
  15566. const byte iv[] = {
  15567. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15568. 0xe2, 0x8c, 0x8f, 0x16
  15569. };
  15570. #endif
  15571. #ifdef WOLFSSL_AES_192
  15572. const byte iv2[] = {
  15573. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15574. 0x7e, 0x1a, 0x6f, 0xbc
  15575. };
  15576. #endif
  15577. const byte iv3[] = {
  15578. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15579. 0xc3, 0xfb, 0x6c, 0x8a
  15580. };
  15581. byte tagOut[16];
  15582. byte tagOut2[24];
  15583. byte tagOut3[32];
  15584. /* Init stack variables. */
  15585. XMEMSET(&gmac, 0, sizeof(Gmac));
  15586. XMEMSET(tagOut, 0, sizeof(tagOut));
  15587. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15588. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15589. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15590. #ifdef WOLFSSL_AES_128
  15591. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15592. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15593. tagOut, sizeof(tag1)), 0);
  15594. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15595. #endif
  15596. #ifdef WOLFSSL_AES_192
  15597. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15598. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15599. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15600. tagOut2, sizeof(tag2)), 0);
  15601. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15602. #endif
  15603. #ifdef WOLFSSL_AES_256
  15604. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15605. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15606. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15607. tagOut3, sizeof(tag3)), 0);
  15608. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15609. #endif
  15610. /* Pass bad args. */
  15611. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15612. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15613. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15614. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15615. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15616. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15617. wc_AesFree(&gmac.aes);
  15618. #endif
  15619. return EXPECT_RESULT();
  15620. } /* END test_wc_GmacUpdate */
  15621. /*
  15622. * testing wc_CamelliaSetKey
  15623. */
  15624. static int test_wc_CamelliaSetKey(void)
  15625. {
  15626. EXPECT_DECLS;
  15627. #ifdef HAVE_CAMELLIA
  15628. Camellia camellia;
  15629. /*128-bit key*/
  15630. static const byte key16[] = {
  15631. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15632. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15633. };
  15634. /* 192-bit key */
  15635. static const byte key24[] = {
  15636. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15637. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15638. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15639. };
  15640. /* 256-bit key */
  15641. static const byte key32[] = {
  15642. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15643. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15644. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15645. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15646. };
  15647. static const byte iv[] = {
  15648. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15649. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15650. };
  15651. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  15652. 0);
  15653. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  15654. NULL), 0);
  15655. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15656. 0);
  15657. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15658. NULL), 0);
  15659. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  15660. 0);
  15661. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  15662. NULL), 0);
  15663. /* Bad args. */
  15664. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  15665. BAD_FUNC_ARG);
  15666. #endif
  15667. return EXPECT_RESULT();
  15668. } /* END test_wc_CammeliaSetKey */
  15669. /*
  15670. * Testing wc_CamelliaSetIV()
  15671. */
  15672. static int test_wc_CamelliaSetIV(void)
  15673. {
  15674. EXPECT_DECLS;
  15675. #ifdef HAVE_CAMELLIA
  15676. Camellia camellia;
  15677. static const byte iv[] = {
  15678. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15679. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15680. };
  15681. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  15682. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  15683. /* Bad args. */
  15684. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  15685. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  15686. #endif
  15687. return EXPECT_RESULT();
  15688. } /* END test_wc_CamelliaSetIV*/
  15689. /*
  15690. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15691. */
  15692. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15693. {
  15694. EXPECT_DECLS;
  15695. #ifdef HAVE_CAMELLIA
  15696. Camellia camellia;
  15697. static const byte key24[] = {
  15698. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15699. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15700. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15701. };
  15702. static const byte iv[] = {
  15703. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15704. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15705. };
  15706. static const byte plainT[] = {
  15707. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15708. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15709. };
  15710. byte enc[sizeof(plainT)];
  15711. byte dec[sizeof(enc)];
  15712. /* Init stack variables.*/
  15713. XMEMSET(enc, 0, 16);
  15714. XMEMSET(enc, 0, 16);
  15715. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15716. 0);
  15717. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  15718. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  15719. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15720. /* Pass bad args. */
  15721. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  15722. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  15723. BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  15725. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  15726. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  15727. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  15728. #endif
  15729. return EXPECT_RESULT();
  15730. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15731. /*
  15732. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15733. */
  15734. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15735. {
  15736. EXPECT_DECLS;
  15737. #ifdef HAVE_CAMELLIA
  15738. Camellia camellia;
  15739. static const byte key24[] = {
  15740. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15741. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15742. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15743. };
  15744. static const byte plainT[] = {
  15745. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15746. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15747. };
  15748. byte enc[CAMELLIA_BLOCK_SIZE];
  15749. byte dec[CAMELLIA_BLOCK_SIZE];
  15750. /* Init stack variables. */
  15751. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15752. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15753. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15754. NULL), 0);
  15755. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  15756. CAMELLIA_BLOCK_SIZE), 0);
  15757. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15758. NULL), 0);
  15759. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  15760. 0);
  15761. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15762. /* Pass in bad args. */
  15763. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  15764. BAD_FUNC_ARG);
  15765. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15766. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15767. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15768. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15769. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  15770. BAD_FUNC_ARG);
  15771. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15772. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15773. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15774. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15775. #endif
  15776. return EXPECT_RESULT();
  15777. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15778. /*
  15779. * Testing wc_Arc4SetKey()
  15780. */
  15781. static int test_wc_Arc4SetKey(void)
  15782. {
  15783. EXPECT_DECLS;
  15784. #ifndef NO_RC4
  15785. Arc4 arc;
  15786. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15787. int keyLen = 8;
  15788. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  15789. /* Test bad args. */
  15790. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  15791. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  15792. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  15793. #endif
  15794. return EXPECT_RESULT();
  15795. } /* END test_wc_Arc4SetKey */
  15796. /*
  15797. * Testing wc_Arc4Process for ENC/DEC.
  15798. */
  15799. static int test_wc_Arc4Process(void)
  15800. {
  15801. EXPECT_DECLS;
  15802. #ifndef NO_RC4
  15803. Arc4 enc;
  15804. Arc4 dec;
  15805. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15806. int keyLen = 8;
  15807. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15808. byte cipher[8];
  15809. byte plain[8];
  15810. /* Init stack variables */
  15811. XMEMSET(&enc, 0, sizeof(Arc4));
  15812. XMEMSET(&dec, 0, sizeof(Arc4));
  15813. XMEMSET(cipher, 0, sizeof(cipher));
  15814. XMEMSET(plain, 0, sizeof(plain));
  15815. /* Use for async. */
  15816. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  15817. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  15818. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  15819. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  15820. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  15821. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  15822. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  15823. /* Bad args. */
  15824. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  15825. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  15826. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  15827. wc_Arc4Free(&enc);
  15828. wc_Arc4Free(&dec);
  15829. #endif
  15830. return EXPECT_RESULT();
  15831. } /* END test_wc_Arc4Process */
  15832. /*
  15833. * Testing wc_Init RsaKey()
  15834. */
  15835. static int test_wc_InitRsaKey(void)
  15836. {
  15837. EXPECT_DECLS;
  15838. #ifndef NO_RSA
  15839. RsaKey key;
  15840. XMEMSET(&key, 0, sizeof(RsaKey));
  15841. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15842. /* Test bad args. */
  15843. #ifndef HAVE_USER_RSA
  15844. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  15845. #else
  15846. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  15847. #endif
  15848. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15849. #endif
  15850. return EXPECT_RESULT();
  15851. } /* END test_wc_InitRsaKey */
  15852. /*
  15853. * Testing wc_RsaPrivateKeyDecode()
  15854. */
  15855. static int test_wc_RsaPrivateKeyDecode(void)
  15856. {
  15857. EXPECT_DECLS;
  15858. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15859. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15860. RsaKey key;
  15861. byte* tmp = NULL;
  15862. word32 idx = 0;
  15863. int bytes = 0;
  15864. XMEMSET(&key, 0, sizeof(RsaKey));
  15865. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  15866. DYNAMIC_TYPE_TMP_BUFFER));
  15867. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15868. if (tmp != NULL) {
  15869. #ifdef USE_CERT_BUFFERS_1024
  15870. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15871. bytes = sizeof_client_key_der_1024;
  15872. #else
  15873. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15874. bytes = sizeof_client_key_der_2048;
  15875. #endif /* Use cert buffers. */
  15876. }
  15877. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  15878. #ifndef HAVE_USER_RSA
  15879. /* Test bad args. */
  15880. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  15881. BAD_FUNC_ARG);
  15882. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  15883. BAD_FUNC_ARG);
  15884. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15885. BAD_FUNC_ARG);
  15886. #else
  15887. /* Test bad args. User RSA. */
  15888. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  15889. USER_CRYPTO_ERROR);
  15890. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  15891. USER_CRYPTO_ERROR);
  15892. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15893. USER_CRYPTO_ERROR);
  15894. #endif
  15895. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15896. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15897. #endif
  15898. return EXPECT_RESULT();
  15899. } /* END test_wc_RsaPrivateKeyDecode */
  15900. /*
  15901. * Testing wc_RsaPublicKeyDecode()
  15902. */
  15903. static int test_wc_RsaPublicKeyDecode(void)
  15904. {
  15905. EXPECT_DECLS;
  15906. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15907. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15908. RsaKey keyPub;
  15909. byte* tmp = NULL;
  15910. word32 idx = 0;
  15911. int bytes = 0;
  15912. word32 keySz = 0;
  15913. word32 tstKeySz = 0;
  15914. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15915. XFILE f = XBADFILE;
  15916. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  15917. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  15918. byte buf[4096];
  15919. #endif
  15920. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  15921. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15922. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  15923. if (tmp != NULL) {
  15924. #ifdef USE_CERT_BUFFERS_1024
  15925. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15926. bytes = sizeof_client_keypub_der_1024;
  15927. keySz = 1024;
  15928. #else
  15929. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15930. bytes = sizeof_client_keypub_der_2048;
  15931. keySz = 2048;
  15932. #endif
  15933. }
  15934. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  15935. #ifndef HAVE_USER_RSA
  15936. /* Pass in bad args. */
  15937. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  15938. BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  15940. BAD_FUNC_ARG);
  15941. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15942. BAD_FUNC_ARG);
  15943. #else
  15944. /* Pass in bad args. */
  15945. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  15946. USER_CRYPTO_ERROR);
  15947. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  15948. USER_CRYPTO_ERROR);
  15949. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15950. USER_CRYPTO_ERROR);
  15951. #endif
  15952. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  15953. /* Test for getting modulus key size */
  15954. idx = 0;
  15955. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15956. &tstKeySz, NULL, NULL), 0);
  15957. ExpectIntEQ(tstKeySz, keySz/8);
  15958. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15959. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  15960. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  15961. if (f != XBADFILE) {
  15962. XFCLOSE(f);
  15963. f = XBADFILE;
  15964. }
  15965. idx = 0;
  15966. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15967. NULL), 0);
  15968. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  15969. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  15970. if (f != XBADFILE)
  15971. XFCLOSE(f);
  15972. idx = 0;
  15973. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15974. NULL), 0);
  15975. #endif
  15976. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15977. #endif
  15978. return EXPECT_RESULT();
  15979. } /* END test_wc_RsaPublicKeyDecode */
  15980. /*
  15981. * Testing wc_RsaPublicKeyDecodeRaw()
  15982. */
  15983. static int test_wc_RsaPublicKeyDecodeRaw(void)
  15984. {
  15985. EXPECT_DECLS;
  15986. #if !defined(NO_RSA)
  15987. RsaKey key;
  15988. const byte n = 0x23;
  15989. const byte e = 0x03;
  15990. int nSz = sizeof(n);
  15991. int eSz = sizeof(e);
  15992. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15993. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  15994. #ifndef HAVE_USER_RSA
  15995. /* Pass in bad args. */
  15996. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  15997. BAD_FUNC_ARG);
  15998. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  15999. BAD_FUNC_ARG);
  16000. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16001. BAD_FUNC_ARG);
  16002. #else
  16003. /* Pass in bad args. User RSA. */
  16004. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16005. USER_CRYPTO_ERROR);
  16006. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16007. USER_CRYPTO_ERROR);
  16008. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16009. USER_CRYPTO_ERROR);
  16010. #endif
  16011. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16012. #endif
  16013. return EXPECT_RESULT();
  16014. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16015. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16016. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16017. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16018. * trying until it gets a probable prime. */
  16019. #ifdef HAVE_FIPS
  16020. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16021. {
  16022. int ret;
  16023. for (;;) {
  16024. ret = wc_MakeRsaKey(key, size, e, rng);
  16025. if (ret != PRIME_GEN_E) break;
  16026. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16027. "trying again.\n");
  16028. }
  16029. return ret;
  16030. }
  16031. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16032. #else
  16033. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16034. #endif
  16035. #endif
  16036. /*
  16037. * Testing wc_MakeRsaKey()
  16038. */
  16039. static int test_wc_MakeRsaKey(void)
  16040. {
  16041. EXPECT_DECLS;
  16042. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16043. RsaKey genKey;
  16044. WC_RNG rng;
  16045. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16046. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16047. int bits = 1024;
  16048. #else
  16049. int bits = 2048;
  16050. #endif
  16051. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16052. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16053. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16054. ExpectIntEQ(wc_InitRng(&rng), 0);
  16055. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16056. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16057. #ifndef HAVE_USER_RSA
  16058. /* Test bad args. */
  16059. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16060. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16061. BAD_FUNC_ARG);
  16062. /* e < 3 */
  16063. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16064. /* e & 1 == 0 */
  16065. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16066. #else
  16067. /* Test bad args. */
  16068. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16069. USER_CRYPTO_ERROR);
  16070. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16071. USER_CRYPTO_ERROR);
  16072. /* e < 3 */
  16073. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16074. /* e & 1 == 0 */
  16075. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16076. #endif /* HAVE_USER_RSA */
  16077. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16078. #endif
  16079. return EXPECT_RESULT();
  16080. } /* END test_wc_MakeRsaKey */
  16081. /*
  16082. * Test the bounds checking on the cipher text versus the key modulus.
  16083. * 1. Make a new RSA key.
  16084. * 2. Set c to 1.
  16085. * 3. Decrypt c into k. (error)
  16086. * 4. Copy the key modulus to c and sub 1 from the copy.
  16087. * 5. Decrypt c into k. (error)
  16088. * Valid bounds test cases are covered by all the other RSA tests.
  16089. */
  16090. static int test_RsaDecryptBoundsCheck(void)
  16091. {
  16092. EXPECT_DECLS;
  16093. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16094. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16095. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16096. WC_RNG rng;
  16097. RsaKey key;
  16098. byte flatC[256];
  16099. word32 flatCSz;
  16100. byte out[256];
  16101. word32 outSz = sizeof(out);
  16102. XMEMSET(&key, 0, sizeof(RsaKey));
  16103. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16104. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16105. ExpectIntEQ(wc_InitRng(&rng), 0);
  16106. if (EXPECT_SUCCESS()) {
  16107. const byte* derKey;
  16108. word32 derKeySz;
  16109. word32 idx = 0;
  16110. #ifdef USE_CERT_BUFFERS_1024
  16111. derKey = server_key_der_1024;
  16112. derKeySz = (word32)sizeof_server_key_der_1024;
  16113. flatCSz = 128;
  16114. #else
  16115. derKey = server_key_der_2048;
  16116. derKeySz = (word32)sizeof_server_key_der_2048;
  16117. flatCSz = 256;
  16118. #endif
  16119. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16120. }
  16121. if (EXPECT_SUCCESS()) {
  16122. XMEMSET(flatC, 0, flatCSz);
  16123. flatC[flatCSz-1] = 1;
  16124. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16125. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16126. if (EXPECT_SUCCESS()) {
  16127. mp_int c;
  16128. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16129. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16130. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16131. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16132. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16133. mp_clear(&c);
  16134. }
  16135. }
  16136. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16137. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16138. #endif
  16139. return EXPECT_RESULT();
  16140. } /* END test_wc_RsaDecryptBoundsCheck */
  16141. /*
  16142. * Testing wc_SetKeyUsage()
  16143. */
  16144. static int test_wc_SetKeyUsage(void)
  16145. {
  16146. EXPECT_DECLS;
  16147. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16148. Cert myCert;
  16149. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16150. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16151. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16152. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16153. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16154. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16155. /* Test bad args. */
  16156. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16157. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16158. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16159. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16160. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16161. KEYUSAGE_E);
  16162. #endif
  16163. return EXPECT_RESULT();
  16164. } /* END test_wc_SetKeyUsage */
  16165. /*
  16166. * Testing wc_CheckProbablePrime()
  16167. */
  16168. static int test_wc_CheckProbablePrime(void)
  16169. {
  16170. EXPECT_DECLS;
  16171. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16172. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16173. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16174. RsaKey key;
  16175. WC_RNG rng;
  16176. byte e[3];
  16177. word32 eSz = (word32)sizeof(e);
  16178. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16179. word32 nSz = (word32)sizeof(n);
  16180. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16181. word32 dSz = (word32)sizeof(d);
  16182. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16183. word32 pSz = (word32)sizeof(p);
  16184. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16185. word32 qSz = (word32)sizeof(q);
  16186. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16187. int* isPrime;
  16188. int test[5];
  16189. isPrime = test;
  16190. XMEMSET(&key, 0, sizeof(RsaKey));
  16191. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16192. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16193. ExpectIntEQ(wc_InitRng(&rng), 0);
  16194. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16195. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16196. WC_RSA_EXPONENT, &rng), 0);
  16197. PRIVATE_KEY_UNLOCK();
  16198. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16199. &qSz), 0);
  16200. PRIVATE_KEY_LOCK();
  16201. /* Bad cases */
  16202. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16203. BAD_FUNC_ARG);
  16204. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16205. BAD_FUNC_ARG);
  16206. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16207. BAD_FUNC_ARG);
  16208. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16209. BAD_FUNC_ARG);
  16210. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16211. BAD_FUNC_ARG);
  16212. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16213. BAD_FUNC_ARG);
  16214. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16215. BAD_FUNC_ARG);
  16216. /* Good case */
  16217. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16218. 0);
  16219. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16220. wc_FreeRng(&rng);
  16221. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16222. #endif
  16223. return EXPECT_RESULT();
  16224. } /* END test_wc_CheckProbablePrime */
  16225. /*
  16226. * Testing wc_RsaPSS_Verify()
  16227. */
  16228. static int test_wc_RsaPSS_Verify(void)
  16229. {
  16230. EXPECT_DECLS;
  16231. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16232. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16233. RsaKey key;
  16234. WC_RNG rng;
  16235. int sz = 256;
  16236. const char* szMessage = "This is the string to be signed";
  16237. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16238. unsigned char pDecrypted[2048/8];
  16239. byte* pt = pDecrypted;
  16240. word32 outLen = sizeof(pDecrypted);
  16241. XMEMSET(&key, 0, sizeof(RsaKey));
  16242. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16243. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16244. ExpectIntEQ(wc_InitRng(&rng), 0);
  16245. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16246. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16247. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16248. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16249. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16250. /* Bad cases */
  16251. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16252. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16253. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16254. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16255. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16256. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16257. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16258. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16259. /* Good case */
  16260. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16261. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16262. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16263. wc_FreeRng(&rng);
  16264. #endif
  16265. return EXPECT_RESULT();
  16266. } /* END test_wc_RsaPSS_Verify */
  16267. /*
  16268. * Testing wc_RsaPSS_VerifyCheck()
  16269. */
  16270. static int test_wc_RsaPSS_VerifyCheck(void)
  16271. {
  16272. EXPECT_DECLS;
  16273. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16274. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16275. RsaKey key;
  16276. WC_RNG rng;
  16277. int sz = 256; /* 2048/8 */
  16278. byte digest[32];
  16279. word32 digestSz = sizeof(digest);
  16280. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16281. word32 pSignatureSz = sizeof(pSignature);
  16282. unsigned char pDecrypted[2048/8];
  16283. byte* pt = pDecrypted;
  16284. word32 outLen = sizeof(pDecrypted);
  16285. XMEMSET(&key, 0, sizeof(RsaKey));
  16286. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16287. XMEMSET(digest, 0, sizeof(digest));
  16288. XMEMSET(pSignature, 0, sizeof(pSignature));
  16289. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16290. ExpectIntEQ(wc_InitRng(&rng), 0);
  16291. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16292. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16293. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16294. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16295. 0);
  16296. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16297. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16298. /* Bad cases */
  16299. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16300. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16301. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16302. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16303. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16304. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16305. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16306. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16307. /* Good case */
  16308. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16309. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16310. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16311. wc_FreeRng(&rng);
  16312. #endif
  16313. return EXPECT_RESULT();
  16314. } /* END test_wc_RsaPSS_VerifyCheck */
  16315. /*
  16316. * Testing wc_RsaPSS_VerifyCheckInline()
  16317. */
  16318. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16319. {
  16320. EXPECT_DECLS;
  16321. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16322. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16323. RsaKey key;
  16324. WC_RNG rng;
  16325. int sz = 256;
  16326. byte digest[32];
  16327. word32 digestSz = sizeof(digest);
  16328. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16329. unsigned char pDecrypted[2048/8];
  16330. byte* pt = pDecrypted;
  16331. XMEMSET(&key, 0, sizeof(RsaKey));
  16332. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16333. XMEMSET(digest, 0, sizeof(digest));
  16334. XMEMSET(pSignature, 0, sizeof(pSignature));
  16335. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16336. ExpectIntEQ(wc_InitRng(&rng), 0);
  16337. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16338. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16339. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16340. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16341. 0);
  16342. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16343. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16344. /* Bad Cases */
  16345. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16346. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16347. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16348. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16349. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16350. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16351. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16352. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16353. /* Good case */
  16354. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16355. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16356. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16357. wc_FreeRng(&rng);
  16358. #endif
  16359. return EXPECT_RESULT();
  16360. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16361. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16362. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16363. {
  16364. (void)flag;
  16365. (void)type;
  16366. (void)file;
  16367. (void)line;
  16368. }
  16369. #endif
  16370. /*
  16371. * Testing wc_LockMutex_ex
  16372. */
  16373. static int test_wc_LockMutex_ex(void)
  16374. {
  16375. EXPECT_DECLS;
  16376. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16377. int flag = CRYPTO_LOCK;
  16378. int type = 0;
  16379. const char* file = "./test-LockMutex_ex.txt";
  16380. int line = 0;
  16381. /* without SetMutexCb */
  16382. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16383. /* with SetMutexCb */
  16384. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16385. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16386. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16387. #endif
  16388. return EXPECT_RESULT();
  16389. } /* End test_wc_LockMutex_ex*/
  16390. /*
  16391. * Testing wc_SetMutexCb
  16392. */
  16393. static int test_wc_SetMutexCb(void)
  16394. {
  16395. EXPECT_DECLS;
  16396. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16397. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16398. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16399. #endif
  16400. return EXPECT_RESULT();
  16401. } /* End test_wc_SetMutexCb*/
  16402. /*
  16403. * Testing wc_RsaKeyToDer()
  16404. */
  16405. static int test_wc_RsaKeyToDer(void)
  16406. {
  16407. EXPECT_DECLS;
  16408. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16409. RsaKey genKey;
  16410. WC_RNG rng;
  16411. byte* der = NULL;
  16412. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16413. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16414. int bits = 1024;
  16415. word32 derSz = 611;
  16416. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16417. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16418. #else
  16419. int bits = 2048;
  16420. word32 derSz = 1196;
  16421. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16422. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16423. #endif
  16424. XMEMSET(&rng, 0, sizeof(rng));
  16425. XMEMSET(&genKey, 0, sizeof(genKey));
  16426. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16427. /* Init structures. */
  16428. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16429. ExpectIntEQ(wc_InitRng(&rng), 0);
  16430. /* Make key. */
  16431. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16432. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16433. #ifndef HAVE_USER_RSA
  16434. /* Pass good/bad args. */
  16435. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16436. /* Get just the output length */
  16437. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16438. /* Try Public Key. */
  16439. genKey.type = 0;
  16440. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16441. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16442. /* Put back to Private Key */
  16443. genKey.type = 1;
  16444. #endif
  16445. #else
  16446. /* Pass good/bad args. */
  16447. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16448. /* Get just the output length */
  16449. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16450. /* Try Public Key. */
  16451. genKey.type = 0;
  16452. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16453. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16454. /* Put back to Private Key */
  16455. genKey.type = 1;
  16456. #endif
  16457. #endif
  16458. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16459. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16460. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16461. #endif
  16462. return EXPECT_RESULT();
  16463. } /* END test_wc_RsaKeyToDer */
  16464. /*
  16465. * Testing wc_RsaKeyToPublicDer()
  16466. */
  16467. static int test_wc_RsaKeyToPublicDer(void)
  16468. {
  16469. EXPECT_DECLS;
  16470. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16471. RsaKey key;
  16472. WC_RNG rng;
  16473. byte* der = NULL;
  16474. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16475. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16476. int bits = 1024;
  16477. word32 derLen = 162;
  16478. #else
  16479. int bits = 2048;
  16480. word32 derLen = 294;
  16481. #endif
  16482. #ifndef HAVE_USER_RSA
  16483. int ret;
  16484. #endif
  16485. XMEMSET(&rng, 0, sizeof(rng));
  16486. XMEMSET(&key, 0, sizeof(key));
  16487. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16488. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16489. ExpectIntEQ(wc_InitRng(&rng), 0);
  16490. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16491. /* test getting size only */
  16492. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16493. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16494. /* test getting size only */
  16495. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16496. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16497. #ifndef HAVE_USER_RSA
  16498. /* Pass in bad args. */
  16499. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16500. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16501. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16502. #else
  16503. /* Pass in bad args. */
  16504. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16505. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16506. #endif
  16507. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16508. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16509. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16510. #endif
  16511. return EXPECT_RESULT();
  16512. } /* END test_wc_RsaKeyToPublicDer */
  16513. /*
  16514. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16515. */
  16516. static int test_wc_RsaPublicEncryptDecrypt(void)
  16517. {
  16518. EXPECT_DECLS;
  16519. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16520. RsaKey key;
  16521. WC_RNG rng;
  16522. const char inStr[] = TEST_STRING;
  16523. const word32 plainLen = (word32)TEST_STRING_SZ;
  16524. const word32 inLen = (word32)TEST_STRING_SZ;
  16525. int bits = TEST_RSA_BITS;
  16526. const word32 cipherLen = TEST_RSA_BYTES;
  16527. word32 cipherLenResult = cipherLen;
  16528. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16529. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16530. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16531. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16532. ExpectNotNull(in);
  16533. ExpectNotNull(plain);
  16534. ExpectNotNull(cipher);
  16535. #endif
  16536. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16537. /* Initialize stack structures. */
  16538. XMEMSET(&key, 0, sizeof(RsaKey));
  16539. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16540. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16541. ExpectIntEQ(wc_InitRng(&rng), 0);
  16542. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16543. /* Encrypt. */
  16544. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16545. cipherLen, &key, &rng), 0);
  16546. /* Pass bad args - tested in another testing function.*/
  16547. /* Decrypt */
  16548. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16549. /* Bind rng */
  16550. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16551. #endif
  16552. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16553. &key), 0);
  16554. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16555. /* Pass bad args - tested in another testing function.*/
  16556. WC_FREE_VAR(in, NULL);
  16557. WC_FREE_VAR(plain, NULL);
  16558. WC_FREE_VAR(cipher, NULL);
  16559. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16560. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16561. #endif
  16562. return EXPECT_RESULT();
  16563. } /* END test_wc_RsaPublicEncryptDecrypt */
  16564. /*
  16565. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16566. */
  16567. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16568. {
  16569. EXPECT_DECLS;
  16570. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16571. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16572. && !defined(NO_SHA256)
  16573. RsaKey key;
  16574. WC_RNG rng;
  16575. const char inStr[] = TEST_STRING;
  16576. const word32 inLen = (word32)TEST_STRING_SZ;
  16577. const word32 plainSz = (word32)TEST_STRING_SZ;
  16578. byte* res = NULL;
  16579. int idx = 0;
  16580. int bits = TEST_RSA_BITS;
  16581. const word32 cipherSz = TEST_RSA_BYTES;
  16582. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16583. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16584. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16585. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16586. ExpectNotNull(in);
  16587. ExpectNotNull(plain);
  16588. ExpectNotNull(cipher);
  16589. #endif
  16590. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16591. /* Initialize stack structures. */
  16592. XMEMSET(&key, 0, sizeof(RsaKey));
  16593. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16594. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16595. ExpectIntEQ(wc_InitRng(&rng), 0);
  16596. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16597. /* Encrypt */
  16598. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16599. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16600. /* Pass bad args - tested in another testing function.*/
  16601. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16602. /* Decrypt */
  16603. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16604. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16605. #endif
  16606. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16607. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16608. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16609. /* Pass bad args - tested in another testing function.*/
  16610. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16611. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16612. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16613. #endif
  16614. WC_FREE_VAR(in, NULL);
  16615. WC_FREE_VAR(plain, NULL);
  16616. WC_FREE_VAR(cipher, NULL);
  16617. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16618. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16619. #endif
  16620. return EXPECT_RESULT();
  16621. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16622. /*
  16623. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16624. */
  16625. static int test_wc_RsaSSL_SignVerify(void)
  16626. {
  16627. EXPECT_DECLS;
  16628. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16629. RsaKey key;
  16630. WC_RNG rng;
  16631. const char inStr[] = TEST_STRING;
  16632. const word32 plainSz = (word32)TEST_STRING_SZ;
  16633. const word32 inLen = (word32)TEST_STRING_SZ;
  16634. word32 idx = 0;
  16635. int bits = TEST_RSA_BITS;
  16636. const word32 outSz = TEST_RSA_BYTES;
  16637. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16638. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16639. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16640. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16641. ExpectNotNull(in);
  16642. ExpectNotNull(out);
  16643. ExpectNotNull(plain);
  16644. #endif
  16645. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16646. XMEMSET(&key, 0, sizeof(RsaKey));
  16647. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16648. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16649. ExpectIntEQ(wc_InitRng(&rng), 0);
  16650. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16651. /* Sign. */
  16652. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  16653. idx = (int)outSz;
  16654. #ifndef HAVE_USER_RSA
  16655. /* Test bad args. */
  16656. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16657. BAD_FUNC_ARG);
  16658. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16659. BAD_FUNC_ARG);
  16660. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16661. BAD_FUNC_ARG);
  16662. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16663. BAD_FUNC_ARG);
  16664. #else
  16665. /* Test bad args. */
  16666. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16667. USER_CRYPTO_ERROR);
  16668. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16669. USER_CRYPTO_ERROR);
  16670. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16671. USER_CRYPTO_ERROR);
  16672. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16673. USER_CRYPTO_ERROR);
  16674. #endif
  16675. /* Verify. */
  16676. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  16677. #ifndef HAVE_USER_RSA
  16678. /* Pass bad args. */
  16679. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16680. BAD_FUNC_ARG);
  16681. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16682. BAD_FUNC_ARG);
  16683. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16684. BAD_FUNC_ARG);
  16685. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16686. BAD_FUNC_ARG);
  16687. #else
  16688. /* Pass bad args. */
  16689. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16690. USER_CRYPTO_ERROR);
  16691. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16692. USER_CRYPTO_ERROR);
  16693. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16694. USER_CRYPTO_ERROR);
  16695. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16696. USER_CRYPTO_ERROR);
  16697. #endif
  16698. WC_FREE_VAR(in, NULL);
  16699. WC_FREE_VAR(out, NULL);
  16700. WC_FREE_VAR(plain, NULL);
  16701. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16702. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16703. #endif
  16704. return EXPECT_RESULT();
  16705. } /* END test_wc_RsaSSL_SignVerify */
  16706. /*
  16707. * Testing wc_RsaEncryptSize()
  16708. */
  16709. static int test_wc_RsaEncryptSize(void)
  16710. {
  16711. EXPECT_DECLS;
  16712. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16713. RsaKey key;
  16714. WC_RNG rng;
  16715. XMEMSET(&key, 0, sizeof(RsaKey));
  16716. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16717. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16718. ExpectIntEQ(wc_InitRng(&rng), 0);
  16719. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16720. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16721. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  16722. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  16723. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16724. #endif
  16725. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16726. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  16727. /* Pass in bad arg. */
  16728. #ifndef HAVE_USER_RSA
  16729. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  16730. #else
  16731. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  16732. #endif
  16733. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16734. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16735. #endif
  16736. return EXPECT_RESULT();
  16737. } /* END test_wc_RsaEncryptSize*/
  16738. /*
  16739. * Testing wc_RsaFlattenPublicKey()
  16740. */
  16741. static int test_wc_RsaFlattenPublicKey(void)
  16742. {
  16743. EXPECT_DECLS;
  16744. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16745. RsaKey key;
  16746. WC_RNG rng;
  16747. byte e[256];
  16748. byte n[256];
  16749. word32 eSz = sizeof(e);
  16750. word32 nSz = sizeof(n);
  16751. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16752. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16753. int bits = 1024;
  16754. #else
  16755. int bits = 2048;
  16756. #endif
  16757. XMEMSET(&key, 0, sizeof(RsaKey));
  16758. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16759. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16760. ExpectIntEQ(wc_InitRng(&rng), 0);
  16761. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16762. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  16763. #ifndef HAVE_USER_RSA
  16764. /* Pass bad args. */
  16765. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16766. BAD_FUNC_ARG);
  16767. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16768. BAD_FUNC_ARG);
  16769. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16770. BAD_FUNC_ARG);
  16771. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16772. BAD_FUNC_ARG);
  16773. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16774. BAD_FUNC_ARG);
  16775. #else
  16776. /* Pass bad args. */
  16777. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16778. USER_CRYPTO_ERROR);
  16779. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16780. USER_CRYPTO_ERROR);
  16781. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16782. USER_CRYPTO_ERROR);
  16783. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16784. USER_CRYPTO_ERROR);
  16785. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16786. USER_CRYPTO_ERROR);
  16787. #endif
  16788. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16789. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16790. #endif
  16791. return EXPECT_RESULT();
  16792. } /* END test_wc_RsaFlattenPublicKey */
  16793. /*
  16794. * unit test for wc_AesCcmSetKey
  16795. */
  16796. static int test_wc_AesCcmSetKey(void)
  16797. {
  16798. EXPECT_DECLS;
  16799. #ifdef HAVE_AESCCM
  16800. Aes aes;
  16801. const byte key16[] = {
  16802. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16803. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16804. };
  16805. const byte key24[] = {
  16806. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16807. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16808. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16809. };
  16810. const byte key32[] = {
  16811. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16812. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16813. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16814. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16815. };
  16816. XMEMSET(&aes, 0, sizeof(Aes));
  16817. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16818. #ifdef WOLFSSL_AES_128
  16819. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16820. #endif
  16821. #ifdef WOLFSSL_AES_192
  16822. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  16823. #endif
  16824. #ifdef WOLFSSL_AES_256
  16825. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  16826. #endif
  16827. /* Test bad args. */
  16828. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  16829. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  16830. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  16831. wc_AesFree(&aes);
  16832. #endif
  16833. return EXPECT_RESULT();
  16834. } /* END test_wc_AesCcmSetKey */
  16835. /*
  16836. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16837. */
  16838. static int test_wc_AesCcmEncryptDecrypt(void)
  16839. {
  16840. EXPECT_DECLS;
  16841. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16842. Aes aes;
  16843. const byte key16[] = {
  16844. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16845. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16846. };
  16847. /* plaintext */
  16848. const byte plainT[] = {
  16849. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16850. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16851. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16852. };
  16853. /* nonce */
  16854. const byte iv[] = {
  16855. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16856. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16857. };
  16858. const byte c[] = { /* cipher text. */
  16859. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  16860. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  16861. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  16862. };
  16863. const byte t[] = { /* Auth tag */
  16864. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  16865. };
  16866. const byte authIn[] = {
  16867. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  16868. };
  16869. byte cipherOut[sizeof(plainT)];
  16870. byte authTag[sizeof(t)];
  16871. #ifdef HAVE_AES_DECRYPT
  16872. byte plainOut[sizeof(cipherOut)];
  16873. #endif
  16874. XMEMSET(&aes, 0, sizeof(Aes));
  16875. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16876. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16877. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16878. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  16879. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  16880. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  16881. #ifdef HAVE_AES_DECRYPT
  16882. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16883. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  16884. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  16885. #endif
  16886. /* Pass in bad args. Encrypt*/
  16887. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  16888. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16889. BAD_FUNC_ARG);
  16890. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  16891. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16892. BAD_FUNC_ARG);
  16893. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  16894. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16895. BAD_FUNC_ARG);
  16896. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16897. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16898. BAD_FUNC_ARG);
  16899. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16900. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  16901. BAD_FUNC_ARG);
  16902. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16903. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16904. BAD_FUNC_ARG);
  16905. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16906. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16907. BAD_FUNC_ARG);
  16908. #ifdef HAVE_AES_DECRYPT
  16909. /* Pass in bad args. Decrypt*/
  16910. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  16911. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16912. BAD_FUNC_ARG);
  16913. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  16914. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16915. BAD_FUNC_ARG);
  16916. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  16917. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16918. BAD_FUNC_ARG);
  16919. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16920. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16921. BAD_FUNC_ARG);
  16922. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16923. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  16924. BAD_FUNC_ARG);
  16925. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16926. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16927. BAD_FUNC_ARG);
  16928. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16929. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16930. BAD_FUNC_ARG);
  16931. #endif
  16932. wc_AesFree(&aes);
  16933. #endif /* HAVE_AESCCM */
  16934. return EXPECT_RESULT();
  16935. } /* END test_wc_AesCcmEncryptDecrypt */
  16936. /*
  16937. * Testing wc_InitDsaKey()
  16938. */
  16939. static int test_wc_InitDsaKey(void)
  16940. {
  16941. EXPECT_DECLS;
  16942. #ifndef NO_DSA
  16943. DsaKey key;
  16944. XMEMSET(&key, 0, sizeof(DsaKey));
  16945. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  16946. /* Pass in bad args. */
  16947. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  16948. wc_FreeDsaKey(&key);
  16949. #endif
  16950. return EXPECT_RESULT();
  16951. } /* END test_wc_InitDsaKey */
  16952. /*
  16953. * Testing wc_DsaSign() and wc_DsaVerify()
  16954. */
  16955. static int test_wc_DsaSignVerify(void)
  16956. {
  16957. EXPECT_DECLS;
  16958. #if !defined(NO_DSA)
  16959. DsaKey key;
  16960. WC_RNG rng;
  16961. wc_Sha sha;
  16962. byte signature[DSA_SIG_SIZE];
  16963. byte hash[WC_SHA_DIGEST_SIZE];
  16964. word32 idx = 0;
  16965. word32 bytes;
  16966. int answer;
  16967. #ifdef USE_CERT_BUFFERS_1024
  16968. byte tmp[ONEK_BUF];
  16969. XMEMSET(tmp, 0, sizeof(tmp));
  16970. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  16971. bytes = sizeof_dsa_key_der_1024;
  16972. #elif defined(USE_CERT_BUFFERS_2048)
  16973. byte tmp[TWOK_BUF];
  16974. XMEMSET(tmp, 0, sizeof(tmp));
  16975. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  16976. bytes = sizeof_dsa_key_der_2048;
  16977. #else
  16978. byte tmp[TWOK_BUF];
  16979. XFILE fp = XBADFILE;
  16980. XMEMSET(tmp, 0, sizeof(tmp));
  16981. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  16982. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  16983. if (fp != XBADFILE)
  16984. XFCLOSE(fp);
  16985. #endif /* END USE_CERT_BUFFERS_1024 */
  16986. ExpectIntEQ(wc_InitSha(&sha), 0);
  16987. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  16988. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  16989. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  16990. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  16991. ExpectIntEQ(wc_InitRng(&rng), 0);
  16992. /* Sign. */
  16993. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  16994. /* Test bad args. */
  16995. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  16996. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  16997. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  16998. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  16999. /* Verify. */
  17000. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  17001. ExpectIntEQ(answer, 1);
  17002. /* Pass in bad args. */
  17003. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  17004. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  17005. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  17006. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  17007. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17008. /* hard set q to 0 and test fail case */
  17009. mp_free(&key.q);
  17010. mp_init(&key.q);
  17011. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17012. mp_set(&key.q, 1);
  17013. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17014. #endif
  17015. DoExpectIntEQ(wc_FreeRng(&rng),0);
  17016. wc_FreeDsaKey(&key);
  17017. wc_ShaFree(&sha);
  17018. #endif
  17019. return EXPECT_RESULT();
  17020. } /* END test_wc_DsaSign */
  17021. /*
  17022. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17023. */
  17024. static int test_wc_DsaPublicPrivateKeyDecode(void)
  17025. {
  17026. EXPECT_DECLS;
  17027. #if !defined(NO_DSA)
  17028. DsaKey key;
  17029. word32 bytes;
  17030. word32 idx = 0;
  17031. int ret;
  17032. #ifdef USE_CERT_BUFFERS_1024
  17033. byte tmp[ONEK_BUF];
  17034. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17035. bytes = sizeof_dsa_key_der_1024;
  17036. #elif defined(USE_CERT_BUFFERS_2048)
  17037. byte tmp[TWOK_BUF];
  17038. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17039. bytes = sizeof_dsa_key_der_2048;
  17040. #else
  17041. byte tmp[TWOK_BUF];
  17042. XFILE fp = XBADFILE;
  17043. XMEMSET(tmp, 0, sizeof(tmp));
  17044. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  17045. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  17046. if (fp != XBADFILE)
  17047. XFCLOSE(fp);
  17048. #endif /* END USE_CERT_BUFFERS_1024 */
  17049. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17050. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17051. /* Test bad args. */
  17052. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  17053. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  17054. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  17055. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17056. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  17057. wc_FreeDsaKey(&key);
  17058. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17059. idx = 0; /* Reset */
  17060. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  17061. /* Test bad args. */
  17062. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  17063. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  17064. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  17065. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  17066. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  17067. wc_FreeDsaKey(&key);
  17068. #endif /* !NO_DSA */
  17069. return EXPECT_RESULT();
  17070. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17071. /*
  17072. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17073. */
  17074. static int test_wc_MakeDsaKey(void)
  17075. {
  17076. EXPECT_DECLS;
  17077. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17078. DsaKey genKey;
  17079. WC_RNG rng;
  17080. XMEMSET(&genKey, 0, sizeof(genKey));
  17081. XMEMSET(&rng, 0, sizeof(rng));
  17082. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  17083. ExpectIntEQ(wc_InitRng(&rng), 0);
  17084. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  17085. /* Test bad args. */
  17086. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  17087. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  17088. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  17089. BAD_FUNC_ARG);
  17090. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  17091. /* Test bad args. */
  17092. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  17093. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  17094. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17095. wc_FreeDsaKey(&genKey);
  17096. #endif
  17097. return EXPECT_RESULT();
  17098. } /* END test_wc_MakeDsaKey */
  17099. /*
  17100. * Testing wc_DsaKeyToDer()
  17101. */
  17102. static int test_wc_DsaKeyToDer(void)
  17103. {
  17104. EXPECT_DECLS;
  17105. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17106. DsaKey key;
  17107. word32 bytes;
  17108. word32 idx = 0;
  17109. #ifdef USE_CERT_BUFFERS_1024
  17110. byte tmp[ONEK_BUF];
  17111. byte der[ONEK_BUF];
  17112. XMEMSET(tmp, 0, sizeof(tmp));
  17113. XMEMSET(der, 0, sizeof(der));
  17114. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17115. bytes = sizeof_dsa_key_der_1024;
  17116. #elif defined(USE_CERT_BUFFERS_2048)
  17117. byte tmp[TWOK_BUF];
  17118. byte der[TWOK_BUF];
  17119. XMEMSET(tmp, 0, sizeof(tmp));
  17120. XMEMSET(der, 0, sizeof(der));
  17121. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17122. bytes = sizeof_dsa_key_der_2048;
  17123. #else
  17124. byte tmp[TWOK_BUF];
  17125. byte der[TWOK_BUF];
  17126. XFILE fp = XBADFILE;
  17127. XMEMSET(tmp, 0, sizeof(tmp));
  17128. XMEMSET(der, 0, sizeof(der));
  17129. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  17130. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  17131. if (fp != XBADFILE)
  17132. XFCLOSE(fp);
  17133. #endif /* END USE_CERT_BUFFERS_1024 */
  17134. XMEMSET(&key, 0, sizeof(DsaKey));
  17135. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17136. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17137. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  17138. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  17139. /* Test bad args. */
  17140. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17141. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  17142. wc_FreeDsaKey(&key);
  17143. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17144. return EXPECT_RESULT();
  17145. } /* END test_wc_DsaKeyToDer */
  17146. /*
  17147. * Testing wc_DsaKeyToPublicDer()
  17148. * (indirectly testing setDsaPublicKey())
  17149. */
  17150. static int test_wc_DsaKeyToPublicDer(void)
  17151. {
  17152. EXPECT_DECLS;
  17153. #ifndef HAVE_SELFTEST
  17154. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17155. DsaKey key;
  17156. WC_RNG rng;
  17157. byte* der = NULL;
  17158. word32 sz = 0;
  17159. word32 idx = 0;
  17160. XMEMSET(&key, 0, sizeof(DsaKey));
  17161. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17162. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  17163. DYNAMIC_TYPE_TMP_BUFFER));
  17164. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17165. ExpectIntEQ(wc_InitRng(&rng), 0);
  17166. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  17167. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  17168. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  17169. wc_FreeDsaKey(&key);
  17170. idx = 0;
  17171. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  17172. /* Test without the SubjectPublicKeyInfo header */
  17173. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  17174. wc_FreeDsaKey(&key);
  17175. idx = 0;
  17176. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  17177. /* Test bad args. */
  17178. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17179. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  17180. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17181. wc_FreeDsaKey(&key);
  17182. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17183. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17184. #endif /* !HAVE_SELFTEST */
  17185. return EXPECT_RESULT();
  17186. } /* END test_wc_DsaKeyToPublicDer */
  17187. /*
  17188. * Testing wc_DsaImportParamsRaw()
  17189. */
  17190. static int test_wc_DsaImportParamsRaw(void)
  17191. {
  17192. EXPECT_DECLS;
  17193. #if !defined(NO_DSA)
  17194. DsaKey key;
  17195. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17196. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17197. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17198. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17199. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17200. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17201. "80a0093113a8bd73";
  17202. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17203. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17204. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17205. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17206. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17207. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17208. "76341a7e7d9";
  17209. /* invalid p and q parameters */
  17210. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17211. const char* invalidQ = "96c5390a";
  17212. XMEMSET(&key, 0, sizeof(DsaKey));
  17213. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17214. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  17215. /* test bad args */
  17216. /* null key struct */
  17217. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  17218. /* null param pointers */
  17219. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  17220. /* illegal p length */
  17221. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  17222. /* illegal q length */
  17223. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  17224. wc_FreeDsaKey(&key);
  17225. #endif
  17226. return EXPECT_RESULT();
  17227. } /* END test_wc_DsaImportParamsRaw */
  17228. /*
  17229. * Testing wc_DsaImportParamsRawCheck()
  17230. */
  17231. static int test_wc_DsaImportParamsRawCheck(void)
  17232. {
  17233. EXPECT_DECLS;
  17234. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17235. DsaKey key;
  17236. int trusted = 0;
  17237. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17238. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17239. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17240. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17241. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17242. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17243. "80a0093113a8bd73";
  17244. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17245. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17246. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17247. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17248. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17249. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17250. "76341a7e7d9";
  17251. /* invalid p and q parameters */
  17252. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17253. const char* invalidQ = "96c5390a";
  17254. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17255. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  17256. /* test bad args */
  17257. /* null key struct */
  17258. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  17259. BAD_FUNC_ARG);
  17260. /* null param pointers */
  17261. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  17262. NULL), BAD_FUNC_ARG);
  17263. /* illegal p length */
  17264. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  17265. BAD_FUNC_ARG);
  17266. /* illegal q length */
  17267. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  17268. BAD_FUNC_ARG);
  17269. wc_FreeDsaKey(&key);
  17270. #endif
  17271. return EXPECT_RESULT();
  17272. } /* END test_wc_DsaImportParamsRawCheck */
  17273. /*
  17274. * Testing wc_DsaExportParamsRaw()
  17275. */
  17276. static int test_wc_DsaExportParamsRaw(void)
  17277. {
  17278. EXPECT_DECLS;
  17279. #if !defined(NO_DSA)
  17280. DsaKey key;
  17281. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17282. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17283. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17284. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17285. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17286. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17287. "80a0093113a8bd73";
  17288. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17289. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17290. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17291. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17292. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17293. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17294. "76341a7e7d9";
  17295. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  17296. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  17297. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  17298. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  17299. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  17300. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  17301. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  17302. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  17303. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  17304. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  17305. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  17306. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  17307. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  17308. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  17309. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  17310. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  17311. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  17312. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  17313. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  17314. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  17315. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  17316. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  17317. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  17318. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  17319. byte pOut[MAX_DSA_PARAM_SIZE];
  17320. byte qOut[MAX_DSA_PARAM_SIZE];
  17321. byte gOut[MAX_DSA_PARAM_SIZE];
  17322. word32 pOutSz;
  17323. word32 qOutSz;
  17324. word32 gOutSz;
  17325. XMEMSET(&key, 0, sizeof(DsaKey));
  17326. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17327. /* first test using imported raw parameters, for expected */
  17328. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  17329. pOutSz = sizeof(pOut);
  17330. qOutSz = sizeof(qOut);
  17331. gOutSz = sizeof(gOut);
  17332. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17333. &gOutSz), 0);
  17334. /* validate exported parameters are correct */
  17335. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  17336. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  17337. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  17338. /* test bad args */
  17339. /* null key struct */
  17340. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17341. &gOutSz), BAD_FUNC_ARG);
  17342. /* null output pointers */
  17343. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  17344. &gOutSz), LENGTH_ONLY_E);
  17345. /* null output size pointers */
  17346. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  17347. NULL), BAD_FUNC_ARG);
  17348. /* p output buffer size too small */
  17349. pOutSz = 1;
  17350. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17351. &gOutSz), BUFFER_E);
  17352. pOutSz = sizeof(pOut);
  17353. /* q output buffer size too small */
  17354. qOutSz = 1;
  17355. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17356. &gOutSz), BUFFER_E);
  17357. qOutSz = sizeof(qOut);
  17358. /* g output buffer size too small */
  17359. gOutSz = 1;
  17360. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17361. &gOutSz), BUFFER_E);
  17362. wc_FreeDsaKey(&key);
  17363. #endif
  17364. return EXPECT_RESULT();
  17365. } /* END test_wc_DsaExportParamsRaw */
  17366. /*
  17367. * Testing wc_DsaExportKeyRaw()
  17368. */
  17369. static int test_wc_DsaExportKeyRaw(void)
  17370. {
  17371. EXPECT_DECLS;
  17372. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17373. DsaKey key;
  17374. WC_RNG rng;
  17375. byte xOut[MAX_DSA_PARAM_SIZE];
  17376. byte yOut[MAX_DSA_PARAM_SIZE];
  17377. word32 xOutSz, yOutSz;
  17378. XMEMSET(&key, 0, sizeof(key));
  17379. XMEMSET(&rng, 0, sizeof(rng));
  17380. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17381. ExpectIntEQ(wc_InitRng(&rng), 0);
  17382. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  17383. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  17384. /* try successful export */
  17385. xOutSz = sizeof(xOut);
  17386. yOutSz = sizeof(yOut);
  17387. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  17388. /* test bad args */
  17389. /* null key struct */
  17390. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  17391. BAD_FUNC_ARG);
  17392. /* null output pointers */
  17393. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  17394. LENGTH_ONLY_E);
  17395. /* null output size pointers */
  17396. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  17397. BAD_FUNC_ARG);
  17398. /* x output buffer size too small */
  17399. xOutSz = 1;
  17400. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  17401. BUFFER_E);
  17402. xOutSz = sizeof(xOut);
  17403. /* y output buffer size too small */
  17404. yOutSz = 1;
  17405. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  17406. BUFFER_E);
  17407. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17408. wc_FreeDsaKey(&key);
  17409. #endif
  17410. return EXPECT_RESULT();
  17411. } /* END test_wc_DsaExportParamsRaw */
  17412. /*
  17413. * Testing wc_ed25519_make_key().
  17414. */
  17415. static int test_wc_ed25519_make_key(void)
  17416. {
  17417. EXPECT_DECLS;
  17418. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  17419. ed25519_key key;
  17420. WC_RNG rng;
  17421. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  17422. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  17423. XMEMSET(&key, 0, sizeof(ed25519_key));
  17424. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17425. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17426. ExpectIntEQ(wc_InitRng(&rng), 0);
  17427. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  17428. ECC_PRIV_KEY_E);
  17429. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  17430. ECC_PRIV_KEY_E);
  17431. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17432. /* Test bad args. */
  17433. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  17434. BAD_FUNC_ARG);
  17435. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  17436. BAD_FUNC_ARG);
  17437. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  17438. BAD_FUNC_ARG);
  17439. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  17440. BAD_FUNC_ARG);
  17441. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17442. wc_ed25519_free(&key);
  17443. #endif
  17444. return EXPECT_RESULT();
  17445. } /* END test_wc_ed25519_make_key */
  17446. /*
  17447. * Testing wc_ed25519_init()
  17448. */
  17449. static int test_wc_ed25519_init(void)
  17450. {
  17451. EXPECT_DECLS;
  17452. #if defined(HAVE_ED25519)
  17453. ed25519_key key;
  17454. XMEMSET(&key, 0, sizeof(ed25519_key));
  17455. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17456. /* Test bad args. */
  17457. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  17458. wc_ed25519_free(&key);
  17459. #endif
  17460. return EXPECT_RESULT();
  17461. } /* END test_wc_ed25519_init */
  17462. /*
  17463. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  17464. */
  17465. static int test_wc_ed25519_sign_msg(void)
  17466. {
  17467. EXPECT_DECLS;
  17468. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  17469. WC_RNG rng;
  17470. ed25519_key key;
  17471. byte msg[] = "Everybody gets Friday off.\n";
  17472. byte sig[ED25519_SIG_SIZE+1];
  17473. word32 msglen = sizeof(msg);
  17474. word32 siglen = ED25519_SIG_SIZE;
  17475. word32 badSigLen = ED25519_SIG_SIZE - 1;
  17476. #ifdef HAVE_ED25519_VERIFY
  17477. int verify_ok = 0; /*1 = Verify success.*/
  17478. #endif
  17479. /* Initialize stack variables. */
  17480. XMEMSET(&key, 0, sizeof(ed25519_key));
  17481. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17482. XMEMSET(sig, 0, sizeof(sig));
  17483. /* Initialize key. */
  17484. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17485. ExpectIntEQ(wc_InitRng(&rng), 0);
  17486. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17487. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  17488. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  17489. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  17490. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  17491. /* Test bad args. */
  17492. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  17493. BAD_FUNC_ARG);
  17494. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  17495. BAD_FUNC_ARG);
  17496. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  17497. BAD_FUNC_ARG);
  17498. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  17499. BAD_FUNC_ARG);
  17500. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  17501. BUFFER_E);
  17502. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  17503. badSigLen -= 1;
  17504. #ifdef HAVE_ED25519_VERIFY
  17505. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  17506. &key), 0);
  17507. ExpectIntEQ(verify_ok, 1);
  17508. /* Test bad args. */
  17509. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  17510. &verify_ok, &key), BAD_FUNC_ARG);
  17511. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  17512. &verify_ok, &key), BAD_FUNC_ARG);
  17513. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  17514. &key), BAD_FUNC_ARG);
  17515. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  17516. &key), BAD_FUNC_ARG);
  17517. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  17518. BAD_FUNC_ARG);
  17519. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  17520. NULL), BAD_FUNC_ARG);
  17521. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  17522. &key), BAD_FUNC_ARG);
  17523. #endif /* Verify. */
  17524. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17525. wc_ed25519_free(&key);
  17526. #endif
  17527. return EXPECT_RESULT();
  17528. } /* END test_wc_ed25519_sign_msg */
  17529. /*
  17530. * Testing wc_ed25519_import_public()
  17531. */
  17532. static int test_wc_ed25519_import_public(void)
  17533. {
  17534. EXPECT_DECLS;
  17535. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  17536. ed25519_key pubKey;
  17537. WC_RNG rng;
  17538. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  17539. word32 inlen = sizeof(in);
  17540. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  17541. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17542. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  17543. ExpectIntEQ(wc_InitRng(&rng), 0);
  17544. #ifdef HAVE_ED25519_MAKE_KEY
  17545. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  17546. #endif
  17547. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  17548. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  17549. /* Test bad args. */
  17550. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  17551. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  17552. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  17553. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17554. wc_ed25519_free(&pubKey);
  17555. #endif
  17556. return EXPECT_RESULT();
  17557. } /* END wc_ed25519_import_public */
  17558. /*
  17559. * Testing wc_ed25519_import_private_key()
  17560. */
  17561. static int test_wc_ed25519_import_private_key(void)
  17562. {
  17563. EXPECT_DECLS;
  17564. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  17565. ed25519_key key;
  17566. WC_RNG rng;
  17567. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  17568. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  17569. word32 privKeySz = sizeof(privKey);
  17570. word32 pubKeySz = sizeof(pubKey);
  17571. #ifdef HAVE_ED25519_KEY_EXPORT
  17572. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  17573. word32 bothKeysSz = sizeof(bothKeys);
  17574. #endif
  17575. XMEMSET(&key, 0, sizeof(ed25519_key));
  17576. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17577. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17578. ExpectIntEQ(wc_InitRng(&rng), 0);
  17579. #ifdef HAVE_ED25519_MAKE_KEY
  17580. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17581. #endif
  17582. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  17583. pubKeySz, &key, 1), 0);
  17584. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  17585. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  17586. #ifdef HAVE_ED25519_KEY_EXPORT
  17587. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  17588. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  17589. &key, 1), 0);
  17590. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  17591. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  17592. #endif
  17593. /* Test bad args. */
  17594. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  17595. &key), BAD_FUNC_ARG);
  17596. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  17597. pubKeySz, &key), BAD_FUNC_ARG);
  17598. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  17599. pubKeySz, NULL), BAD_FUNC_ARG);
  17600. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  17601. pubKeySz, &key), BAD_FUNC_ARG);
  17602. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  17603. pubKeySz - 1, &key), BAD_FUNC_ARG);
  17604. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  17605. &key), BAD_FUNC_ARG);
  17606. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17607. wc_ed25519_free(&key);
  17608. #endif
  17609. return EXPECT_RESULT();
  17610. } /* END test_wc_ed25519_import_private_key */
  17611. /*
  17612. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  17613. */
  17614. static int test_wc_ed25519_export(void)
  17615. {
  17616. EXPECT_DECLS;
  17617. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  17618. ed25519_key key;
  17619. WC_RNG rng;
  17620. byte priv[ED25519_PRV_KEY_SIZE];
  17621. byte pub[ED25519_PUB_KEY_SIZE];
  17622. word32 privSz = sizeof(priv);
  17623. word32 pubSz = sizeof(pub);
  17624. #ifndef HAVE_ED25519_MAKE_KEY
  17625. const byte privKey[] = {
  17626. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  17627. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  17628. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  17629. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  17630. };
  17631. const byte pubKey[] = {
  17632. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  17633. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  17634. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  17635. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  17636. };
  17637. #endif
  17638. XMEMSET(&key, 0, sizeof(ed25519_key));
  17639. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17640. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17641. ExpectIntEQ(wc_InitRng(&rng), 0);
  17642. #ifdef HAVE_ED25519_MAKE_KEY
  17643. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17644. #else
  17645. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  17646. pubKey, sizeof(pubKey), &key, 1), 0);
  17647. #endif
  17648. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  17649. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  17650. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  17651. /* Test bad args. */
  17652. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  17653. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  17654. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  17655. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  17656. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  17657. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  17658. /* Test bad args. */
  17659. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  17660. BAD_FUNC_ARG);
  17661. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  17662. BAD_FUNC_ARG);
  17663. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  17664. BAD_FUNC_ARG);
  17665. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17666. wc_ed25519_free(&key);
  17667. #endif
  17668. return EXPECT_RESULT();
  17669. } /* END test_wc_ed25519_export */
  17670. /*
  17671. * Testing wc_ed25519_size()
  17672. */
  17673. static int test_wc_ed25519_size(void)
  17674. {
  17675. EXPECT_DECLS;
  17676. #if defined(HAVE_ED25519)
  17677. ed25519_key key;
  17678. WC_RNG rng;
  17679. #ifndef HAVE_ED25519_MAKE_KEY
  17680. const byte privKey[] = {
  17681. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  17682. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  17683. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  17684. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  17685. };
  17686. const byte pubKey[] = {
  17687. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  17688. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  17689. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  17690. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  17691. };
  17692. #endif
  17693. XMEMSET(&key, 0, sizeof(ed25519_key));
  17694. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17695. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17696. ExpectIntEQ(wc_InitRng(&rng), 0);
  17697. #ifdef HAVE_ED25519_MAKE_KEY
  17698. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17699. #else
  17700. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  17701. pubKey, sizeof(pubKey), &key, 1), 0);
  17702. #endif
  17703. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  17704. /* Test bad args. */
  17705. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  17706. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  17707. /* Test bad args. */
  17708. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  17709. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  17710. /* Test bad args. */
  17711. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  17712. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  17713. /* Test bad args. */
  17714. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  17715. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17716. wc_ed25519_free(&key);
  17717. #endif
  17718. return EXPECT_RESULT();
  17719. } /* END test_wc_ed25519_size */
  17720. /*
  17721. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  17722. */
  17723. static int test_wc_ed25519_exportKey(void)
  17724. {
  17725. EXPECT_DECLS;
  17726. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  17727. WC_RNG rng;
  17728. ed25519_key key;
  17729. byte priv[ED25519_PRV_KEY_SIZE];
  17730. byte pub[ED25519_PUB_KEY_SIZE];
  17731. byte privOnly[ED25519_PRV_KEY_SIZE];
  17732. word32 privSz = sizeof(priv);
  17733. word32 pubSz = sizeof(pub);
  17734. word32 privOnlySz = sizeof(privOnly);
  17735. #ifndef HAVE_ED25519_MAKE_KEY
  17736. const byte privKey[] = {
  17737. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  17738. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  17739. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  17740. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  17741. };
  17742. const byte pubKey[] = {
  17743. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  17744. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  17745. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  17746. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  17747. };
  17748. #endif
  17749. XMEMSET(&key, 0, sizeof(ed25519_key));
  17750. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17751. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17752. ExpectIntEQ(wc_InitRng(&rng), 0);
  17753. #ifdef HAVE_ED25519_MAKE_KEY
  17754. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17755. #else
  17756. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  17757. pubKey, sizeof(pubKey), &key, 1), 0);
  17758. #endif
  17759. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  17760. /* Test bad args. */
  17761. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  17762. BAD_FUNC_ARG);
  17763. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  17764. BAD_FUNC_ARG);
  17765. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  17766. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  17767. /* Test bad args. */
  17768. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  17769. BAD_FUNC_ARG);
  17770. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  17771. BAD_FUNC_ARG);
  17772. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  17773. BAD_FUNC_ARG);
  17774. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  17775. BAD_FUNC_ARG);
  17776. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  17777. BAD_FUNC_ARG);
  17778. /* Cross check output. */
  17779. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  17780. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17781. wc_ed25519_free(&key);
  17782. #endif
  17783. return EXPECT_RESULT();
  17784. } /* END test_wc_ed25519_exportKey */
  17785. /*
  17786. * Testing wc_Ed25519PublicKeyToDer
  17787. */
  17788. static int test_wc_Ed25519PublicKeyToDer(void)
  17789. {
  17790. EXPECT_DECLS;
  17791. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  17792. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  17793. ed25519_key key;
  17794. byte derBuf[1024];
  17795. XMEMSET(&key, 0, sizeof(ed25519_key));
  17796. /* Test bad args */
  17797. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  17798. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17799. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  17800. wc_ed25519_free(&key);
  17801. /* Test good args */
  17802. if (EXPECT_SUCCESS()) {
  17803. WC_RNG rng;
  17804. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17805. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17806. ExpectIntEQ(wc_InitRng(&rng), 0);
  17807. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17808. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  17809. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17810. wc_ed25519_free(&key);
  17811. }
  17812. #endif
  17813. return EXPECT_RESULT();
  17814. } /* END testing wc_Ed25519PublicKeyToDer */
  17815. /*
  17816. * Testing wc_curve25519_init and wc_curve25519_free.
  17817. */
  17818. static int test_wc_curve25519_init(void)
  17819. {
  17820. EXPECT_DECLS;
  17821. #if defined(HAVE_CURVE25519)
  17822. curve25519_key key;
  17823. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17824. /* Test bad args for wc_curve25519_init */
  17825. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  17826. /* Test good args for wc_curve_25519_free */
  17827. wc_curve25519_free(&key);
  17828. /* Test bad args for wc_curve25519 free. */
  17829. wc_curve25519_free(NULL);
  17830. #endif
  17831. return EXPECT_RESULT();
  17832. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  17833. /*
  17834. * Testing test_wc_curve25519_size.
  17835. */
  17836. static int test_wc_curve25519_size(void)
  17837. {
  17838. EXPECT_DECLS;
  17839. #if defined(HAVE_CURVE25519)
  17840. curve25519_key key;
  17841. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17842. /* Test good args for wc_curve25519_size */
  17843. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  17844. /* Test bad args for wc_curve25519_size */
  17845. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  17846. wc_curve25519_free(&key);
  17847. #endif
  17848. return EXPECT_RESULT();
  17849. } /* END test_wc_curve25519_size*/
  17850. /*
  17851. * Testing test_wc_curve25519_export_key_raw().
  17852. */
  17853. static int test_wc_curve25519_export_key_raw(void)
  17854. {
  17855. EXPECT_DECLS;
  17856. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17857. curve25519_key key;
  17858. WC_RNG rng;
  17859. byte privateKey[CURVE25519_KEYSIZE];
  17860. byte publicKey[CURVE25519_KEYSIZE];
  17861. word32 prvkSz;
  17862. word32 pubkSz;
  17863. byte prik[CURVE25519_KEYSIZE];
  17864. byte pubk[CURVE25519_KEYSIZE];
  17865. word32 prksz;
  17866. word32 pbksz;
  17867. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17868. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17869. ExpectIntEQ(wc_InitRng(&rng), 0);
  17870. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  17871. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  17872. prvkSz = CURVE25519_KEYSIZE;
  17873. pubkSz = CURVE25519_KEYSIZE;
  17874. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  17875. publicKey, &pubkSz), BAD_FUNC_ARG);
  17876. prvkSz = CURVE25519_KEYSIZE;
  17877. pubkSz = CURVE25519_KEYSIZE;
  17878. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  17879. &pubkSz), BAD_FUNC_ARG);
  17880. prvkSz = CURVE25519_KEYSIZE;
  17881. pubkSz = CURVE25519_KEYSIZE;
  17882. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  17883. publicKey, &pubkSz), BAD_FUNC_ARG);
  17884. /* prvkSz = CURVE25519_KEYSIZE; */
  17885. pubkSz = CURVE25519_KEYSIZE;
  17886. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17887. NULL, &pubkSz), BAD_FUNC_ARG);
  17888. prvkSz = CURVE25519_KEYSIZE;
  17889. pubkSz = CURVE25519_KEYSIZE;
  17890. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17891. publicKey, NULL), BAD_FUNC_ARG);
  17892. /* cross-testing */
  17893. prksz = CURVE25519_KEYSIZE;
  17894. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  17895. pbksz = CURVE25519_KEYSIZE;
  17896. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  17897. prvkSz = CURVE25519_KEYSIZE;
  17898. /* pubkSz = CURVE25519_KEYSIZE; */
  17899. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17900. publicKey, &pubkSz), 0);
  17901. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  17902. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  17903. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17904. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17905. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  17906. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  17907. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17908. wc_curve25519_free(&key);
  17909. #endif
  17910. return EXPECT_RESULT();
  17911. } /* end of test_wc_curve25519_export_key_raw */
  17912. /*
  17913. * Testing test_wc_curve25519_export_key_raw_ex().
  17914. */
  17915. static int test_wc_curve25519_export_key_raw_ex(void)
  17916. {
  17917. EXPECT_DECLS;
  17918. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17919. curve25519_key key;
  17920. WC_RNG rng;
  17921. byte privateKey[CURVE25519_KEYSIZE];
  17922. byte publicKey[CURVE25519_KEYSIZE];
  17923. word32 prvkSz;
  17924. word32 pubkSz;
  17925. byte prik[CURVE25519_KEYSIZE];
  17926. byte pubk[CURVE25519_KEYSIZE];
  17927. word32 prksz;
  17928. word32 pbksz;
  17929. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17930. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17931. ExpectIntEQ(wc_InitRng(&rng), 0);
  17932. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  17933. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  17934. prvkSz = CURVE25519_KEYSIZE;
  17935. pubkSz = CURVE25519_KEYSIZE;
  17936. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  17937. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17938. prvkSz = CURVE25519_KEYSIZE;
  17939. pubkSz = CURVE25519_KEYSIZE;
  17940. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  17941. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17942. prvkSz = CURVE25519_KEYSIZE;
  17943. pubkSz = CURVE25519_KEYSIZE;
  17944. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17945. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17946. /* prvkSz = CURVE25519_KEYSIZE; */
  17947. pubkSz = CURVE25519_KEYSIZE;
  17948. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17949. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17950. prvkSz = CURVE25519_KEYSIZE;
  17951. pubkSz = CURVE25519_KEYSIZE;
  17952. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17953. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17954. prvkSz = CURVE25519_KEYSIZE;
  17955. /* pubkSz = CURVE25519_KEYSIZE; */
  17956. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  17957. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17958. prvkSz = CURVE25519_KEYSIZE;
  17959. pubkSz = CURVE25519_KEYSIZE;
  17960. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  17961. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17962. prvkSz = CURVE25519_KEYSIZE;
  17963. pubkSz = CURVE25519_KEYSIZE;
  17964. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17965. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17966. /* prvkSz = CURVE25519_KEYSIZE; */
  17967. pubkSz = CURVE25519_KEYSIZE;
  17968. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17969. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17970. prvkSz = CURVE25519_KEYSIZE;
  17971. pubkSz = CURVE25519_KEYSIZE;
  17972. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17973. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17974. /* illegal value for endian */
  17975. prvkSz = CURVE25519_KEYSIZE;
  17976. /* pubkSz = CURVE25519_KEYSIZE; */
  17977. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17978. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  17979. /* cross-testing */
  17980. prksz = CURVE25519_KEYSIZE;
  17981. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  17982. pbksz = CURVE25519_KEYSIZE;
  17983. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  17984. prvkSz = CURVE25519_KEYSIZE;
  17985. /* pubkSz = CURVE25519_KEYSIZE; */
  17986. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17987. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  17988. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  17989. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  17990. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17991. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17992. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  17993. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  17994. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17995. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  17996. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17997. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17998. /* try once with another endian */
  17999. prvkSz = CURVE25519_KEYSIZE;
  18000. pubkSz = CURVE25519_KEYSIZE;
  18001. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  18002. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  18003. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  18004. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  18005. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18006. wc_curve25519_free(&key);
  18007. #endif
  18008. return EXPECT_RESULT();
  18009. } /* end of test_wc_curve25519_export_key_raw_ex */
  18010. /*
  18011. * Testing wc_curve25519_make_key
  18012. */
  18013. static int test_wc_curve25519_make_key(void)
  18014. {
  18015. EXPECT_DECLS;
  18016. #if defined(HAVE_CURVE25519)
  18017. curve25519_key key;
  18018. WC_RNG rng;
  18019. int keysize;
  18020. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18021. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18022. ExpectIntEQ(wc_InitRng(&rng), 0);
  18023. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18024. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  18025. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  18026. /* test bad cases*/
  18027. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  18028. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  18029. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  18030. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  18031. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18032. wc_curve25519_free(&key);
  18033. #endif
  18034. return EXPECT_RESULT();
  18035. } /* END test_wc_curve25519_make_key*/
  18036. /*
  18037. * Testing wc_curve25519_shared_secret_ex
  18038. */
  18039. static int test_wc_curve25519_shared_secret_ex(void)
  18040. {
  18041. EXPECT_DECLS;
  18042. #if defined(HAVE_CURVE25519)
  18043. curve25519_key private_key;
  18044. curve25519_key public_key;
  18045. WC_RNG rng;
  18046. byte out[CURVE25519_KEYSIZE];
  18047. word32 outLen = sizeof(out);
  18048. int endian = EC25519_BIG_ENDIAN;
  18049. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  18050. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  18051. ExpectIntEQ(wc_InitRng(&rng), 0);
  18052. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  18053. 0);
  18054. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  18055. 0);
  18056. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18057. &outLen, endian), 0);
  18058. /* test bad cases*/
  18059. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  18060. BAD_FUNC_ARG);
  18061. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  18062. endian), BAD_FUNC_ARG);
  18063. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  18064. endian), BAD_FUNC_ARG);
  18065. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  18066. &outLen, endian), BAD_FUNC_ARG);
  18067. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18068. NULL, endian), BAD_FUNC_ARG);
  18069. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  18070. * increasing to 0x8f checks for error being returned*/
  18071. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  18072. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18073. &outLen, endian), ECC_BAD_ARG_E);
  18074. outLen = outLen - 2;
  18075. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18076. &outLen, endian), BAD_FUNC_ARG);
  18077. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18078. wc_curve25519_free(&private_key);
  18079. wc_curve25519_free(&public_key);
  18080. #endif
  18081. return EXPECT_RESULT();
  18082. } /* END test_wc_curve25519_shared_secret_ex*/
  18083. /*
  18084. * Testing wc_curve25519_make_pub
  18085. */
  18086. static int test_wc_curve25519_make_pub(void)
  18087. {
  18088. EXPECT_DECLS;
  18089. #ifdef HAVE_CURVE25519
  18090. curve25519_key key;
  18091. WC_RNG rng;
  18092. byte out[CURVE25519_KEYSIZE];
  18093. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18094. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18095. ExpectIntEQ(wc_InitRng(&rng), 0);
  18096. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18097. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  18098. (int)sizeof(key.k), key.k), 0);
  18099. /* test bad cases*/
  18100. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  18101. (int)sizeof out, out), ECC_BAD_ARG_E);
  18102. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18103. NULL), ECC_BAD_ARG_E);
  18104. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  18105. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  18106. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  18107. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  18108. /* verify clamping test */
  18109. key.k[0] |= ~248;
  18110. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18111. key.k), ECC_BAD_ARG_E);
  18112. key.k[0] &= 248;
  18113. /* repeat the expected-to-succeed test. */
  18114. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18115. key.k), 0);
  18116. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18117. wc_curve25519_free(&key);
  18118. #endif
  18119. return EXPECT_RESULT();
  18120. } /* END test_wc_curve25519_make_pub */
  18121. /*
  18122. * Testing test_wc_curve25519_export_public_ex
  18123. */
  18124. static int test_wc_curve25519_export_public_ex(void)
  18125. {
  18126. EXPECT_DECLS;
  18127. #if defined(HAVE_CURVE25519)
  18128. curve25519_key key;
  18129. WC_RNG rng;
  18130. byte out[CURVE25519_KEYSIZE];
  18131. word32 outLen = sizeof(out);
  18132. int endian = EC25519_BIG_ENDIAN;
  18133. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18134. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18135. ExpectIntEQ(wc_InitRng(&rng), 0);
  18136. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18137. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  18138. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  18139. /* test bad cases*/
  18140. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  18141. BAD_FUNC_ARG);
  18142. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  18143. BAD_FUNC_ARG);
  18144. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  18145. BAD_FUNC_ARG);
  18146. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  18147. BAD_FUNC_ARG);
  18148. outLen = outLen - 2;
  18149. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  18150. ECC_BAD_ARG_E);
  18151. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18152. wc_curve25519_free(&key);
  18153. #endif
  18154. return EXPECT_RESULT();
  18155. } /* END test_wc_curve25519_export_public_ex*/
  18156. /*
  18157. * Testing test_wc_curve25519_import_private_raw_ex
  18158. */
  18159. static int test_wc_curve25519_import_private_raw_ex(void)
  18160. {
  18161. EXPECT_DECLS;
  18162. #if defined(HAVE_CURVE25519)
  18163. curve25519_key key;
  18164. WC_RNG rng;
  18165. byte priv[CURVE25519_KEYSIZE];
  18166. byte pub[CURVE25519_KEYSIZE];
  18167. word32 privSz = sizeof(priv);
  18168. word32 pubSz = sizeof(pub);
  18169. int endian = EC25519_BIG_ENDIAN;
  18170. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18171. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18172. ExpectIntEQ(wc_InitRng(&rng), 0);
  18173. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18174. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  18175. endian), 0);
  18176. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  18177. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18178. &key, endian), 0);
  18179. /* test bad cases*/
  18180. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  18181. endian), BAD_FUNC_ARG);
  18182. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  18183. &key, endian), BAD_FUNC_ARG);
  18184. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  18185. &key, endian), BAD_FUNC_ARG);
  18186. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18187. NULL, endian), BAD_FUNC_ARG);
  18188. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  18189. &key, endian), ECC_BAD_ARG_E);
  18190. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  18191. &key, endian), ECC_BAD_ARG_E);
  18192. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18193. &key, EC25519_LITTLE_ENDIAN), 0);
  18194. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18195. wc_curve25519_free(&key);
  18196. #endif
  18197. return EXPECT_RESULT();
  18198. } /* END test_wc_curve25519_import_private_raw_ex*/
  18199. /*
  18200. * Testing test_wc_curve25519_import_private
  18201. */
  18202. static int test_wc_curve25519_import_private(void)
  18203. {
  18204. EXPECT_DECLS;
  18205. #if defined(HAVE_CURVE25519)
  18206. curve25519_key key;
  18207. WC_RNG rng;
  18208. byte priv[CURVE25519_KEYSIZE];
  18209. word32 privSz = sizeof(priv);
  18210. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18211. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18212. ExpectIntEQ(wc_InitRng(&rng), 0);
  18213. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18214. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  18215. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  18216. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18217. wc_curve25519_free(&key);
  18218. #endif
  18219. return EXPECT_RESULT();
  18220. } /* END test_wc_curve25519_import*/
  18221. /*
  18222. * Testing test_wc_curve25519_export_private_raw_ex
  18223. */
  18224. static int test_wc_curve25519_export_private_raw_ex(void)
  18225. {
  18226. EXPECT_DECLS;
  18227. #if defined(HAVE_CURVE25519)
  18228. curve25519_key key;
  18229. byte out[CURVE25519_KEYSIZE];
  18230. word32 outLen = sizeof(out);
  18231. int endian = EC25519_BIG_ENDIAN;
  18232. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18233. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  18234. 0);
  18235. /* test bad cases*/
  18236. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  18237. BAD_FUNC_ARG);
  18238. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  18239. BAD_FUNC_ARG);
  18240. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  18241. endian), BAD_FUNC_ARG);
  18242. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  18243. BAD_FUNC_ARG);
  18244. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  18245. EC25519_LITTLE_ENDIAN), 0);
  18246. outLen = outLen - 2;
  18247. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  18248. ECC_BAD_ARG_E);
  18249. wc_curve25519_free(&key);
  18250. #endif
  18251. return EXPECT_RESULT();
  18252. } /* END test_wc_curve25519_export_private_raw_ex*/
  18253. /*
  18254. * Testing wc_ed448_make_key().
  18255. */
  18256. static int test_wc_ed448_make_key(void)
  18257. {
  18258. EXPECT_DECLS;
  18259. #if defined(HAVE_ED448)
  18260. ed448_key key;
  18261. WC_RNG rng;
  18262. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  18263. XMEMSET(&key, 0, sizeof(ed448_key));
  18264. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18265. ExpectIntEQ(wc_ed448_init(&key), 0);
  18266. ExpectIntEQ(wc_InitRng(&rng), 0);
  18267. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  18268. ECC_PRIV_KEY_E);
  18269. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18270. /* Test bad args. */
  18271. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  18272. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  18273. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  18274. BAD_FUNC_ARG);
  18275. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  18276. BAD_FUNC_ARG);
  18277. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18278. wc_ed448_free(&key);
  18279. #endif
  18280. return EXPECT_RESULT();
  18281. } /* END test_wc_ed448_make_key */
  18282. /*
  18283. * Testing wc_ed448_init()
  18284. */
  18285. static int test_wc_ed448_init(void)
  18286. {
  18287. EXPECT_DECLS;
  18288. #if defined(HAVE_ED448)
  18289. ed448_key key;
  18290. XMEMSET(&key, 0, sizeof(ed448_key));
  18291. ExpectIntEQ(wc_ed448_init(&key), 0);
  18292. /* Test bad args. */
  18293. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  18294. wc_ed448_free(&key);
  18295. #endif
  18296. return EXPECT_RESULT();
  18297. } /* END test_wc_ed448_init */
  18298. /*
  18299. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  18300. */
  18301. static int test_wc_ed448_sign_msg(void)
  18302. {
  18303. EXPECT_DECLS;
  18304. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  18305. ed448_key key;
  18306. WC_RNG rng;
  18307. byte msg[] = "Everybody gets Friday off.\n";
  18308. byte sig[ED448_SIG_SIZE];
  18309. word32 msglen = sizeof(msg);
  18310. word32 siglen = sizeof(sig);
  18311. word32 badSigLen = sizeof(sig) - 1;
  18312. #ifdef HAVE_ED448_VERIFY
  18313. int verify_ok = 0; /*1 = Verify success.*/
  18314. #endif
  18315. /* Initialize stack variables. */
  18316. XMEMSET(&key, 0, sizeof(ed448_key));
  18317. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18318. XMEMSET(sig, 0, siglen);
  18319. /* Initialize key. */
  18320. ExpectIntEQ(wc_ed448_init(&key), 0);
  18321. ExpectIntEQ(wc_InitRng(&rng), 0);
  18322. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18323. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  18324. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  18325. /* Test bad args. */
  18326. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  18327. BAD_FUNC_ARG);
  18328. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  18329. BAD_FUNC_ARG);
  18330. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  18331. BAD_FUNC_ARG);
  18332. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  18333. BAD_FUNC_ARG);
  18334. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  18335. BUFFER_E);
  18336. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  18337. badSigLen -= 1;
  18338. #ifdef HAVE_ED448_VERIFY
  18339. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  18340. NULL, 0), 0);
  18341. ExpectIntEQ(verify_ok, 1);
  18342. /* Test bad args. */
  18343. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  18344. &key, NULL, 0), BAD_FUNC_ARG);
  18345. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  18346. &key, NULL, 0), BAD_FUNC_ARG);
  18347. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18348. &key, NULL, 0), BAD_FUNC_ARG);
  18349. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  18350. &key, NULL, 0), BAD_FUNC_ARG);
  18351. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  18352. &key, NULL, 0), BAD_FUNC_ARG);
  18353. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  18354. NULL, NULL, 0), BAD_FUNC_ARG);
  18355. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  18356. &key, NULL, 0), BAD_FUNC_ARG);
  18357. #endif /* Verify. */
  18358. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18359. wc_ed448_free(&key);
  18360. #endif
  18361. return EXPECT_RESULT();
  18362. } /* END test_wc_ed448_sign_msg */
  18363. /*
  18364. * Testing wc_ed448_import_public()
  18365. */
  18366. static int test_wc_ed448_import_public(void)
  18367. {
  18368. EXPECT_DECLS;
  18369. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18370. ed448_key pubKey;
  18371. WC_RNG rng;
  18372. const byte in[] =
  18373. "Ed448PublicKeyUnitTest.................................\n";
  18374. word32 inlen = sizeof(in);
  18375. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  18376. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18377. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  18378. ExpectIntEQ(wc_InitRng(&rng), 0);
  18379. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  18380. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  18381. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  18382. /* Test bad args. */
  18383. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  18384. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  18385. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  18386. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18387. wc_ed448_free(&pubKey);
  18388. #endif
  18389. return EXPECT_RESULT();
  18390. } /* END wc_ed448_import_public */
  18391. /*
  18392. * Testing wc_ed448_import_private_key()
  18393. */
  18394. static int test_wc_ed448_import_private_key(void)
  18395. {
  18396. EXPECT_DECLS;
  18397. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18398. ed448_key key;
  18399. WC_RNG rng;
  18400. const byte privKey[] =
  18401. "Ed448PrivateKeyUnitTest................................\n";
  18402. const byte pubKey[] =
  18403. "Ed448PublicKeyUnitTest.................................\n";
  18404. word32 privKeySz = sizeof(privKey);
  18405. word32 pubKeySz = sizeof(pubKey);
  18406. #ifdef HAVE_ED448_KEY_EXPORT
  18407. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18408. word32 bothKeysSz = sizeof(bothKeys);
  18409. #endif
  18410. XMEMSET(&key, 0, sizeof(ed448_key));
  18411. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18412. ExpectIntEQ(wc_ed448_init(&key), 0);
  18413. ExpectIntEQ(wc_InitRng(&rng), 0);
  18414. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18415. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  18416. pubKeySz, &key, 1), 0);
  18417. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  18418. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  18419. #ifdef HAVE_ED448_KEY_EXPORT
  18420. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  18421. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  18422. &key, 1), 0);
  18423. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  18424. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  18425. #endif
  18426. /* Test bad args. */
  18427. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18428. &key), BAD_FUNC_ARG);
  18429. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  18430. &key), BAD_FUNC_ARG);
  18431. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18432. pubKeySz, NULL), BAD_FUNC_ARG);
  18433. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  18434. pubKeySz, &key), BAD_FUNC_ARG);
  18435. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18436. pubKeySz - 1, &key), BAD_FUNC_ARG);
  18437. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  18438. BAD_FUNC_ARG);
  18439. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18440. wc_ed448_free(&key);
  18441. #endif
  18442. return EXPECT_RESULT();
  18443. } /* END test_wc_ed448_import_private_key */
  18444. /*
  18445. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  18446. */
  18447. static int test_wc_ed448_export(void)
  18448. {
  18449. EXPECT_DECLS;
  18450. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18451. ed448_key key;
  18452. WC_RNG rng;
  18453. byte priv[ED448_PRV_KEY_SIZE];
  18454. byte pub[ED448_PUB_KEY_SIZE];
  18455. word32 privSz = sizeof(priv);
  18456. word32 pubSz = sizeof(pub);
  18457. XMEMSET(&key, 0, sizeof(ed448_key));
  18458. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18459. ExpectIntEQ(wc_ed448_init(&key), 0);
  18460. ExpectIntEQ(wc_InitRng(&rng), 0);
  18461. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18462. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  18463. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  18464. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  18465. /* Test bad args. */
  18466. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  18467. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  18468. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  18469. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  18470. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  18471. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  18472. /* Test bad args. */
  18473. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  18474. BAD_FUNC_ARG);
  18475. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  18476. BAD_FUNC_ARG);
  18477. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  18478. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18479. wc_ed448_free(&key);
  18480. #endif
  18481. return EXPECT_RESULT();
  18482. } /* END test_wc_ed448_export */
  18483. /*
  18484. * Testing wc_ed448_size()
  18485. */
  18486. static int test_wc_ed448_size(void)
  18487. {
  18488. EXPECT_DECLS;
  18489. #if defined(HAVE_ED448)
  18490. ed448_key key;
  18491. WC_RNG rng;
  18492. XMEMSET(&key, 0, sizeof(ed448_key));
  18493. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18494. ExpectIntEQ(wc_ed448_init(&key), 0);
  18495. ExpectIntEQ(wc_InitRng(&rng), 0);
  18496. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18497. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  18498. /* Test bad args. */
  18499. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  18500. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  18501. /* Test bad args. */
  18502. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  18503. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  18504. /* Test bad args. */
  18505. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  18506. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  18507. /* Test bad args. */
  18508. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  18509. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18510. wc_ed448_free(&key);
  18511. #endif
  18512. return EXPECT_RESULT();
  18513. } /* END test_wc_ed448_size */
  18514. /*
  18515. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  18516. */
  18517. static int test_wc_ed448_exportKey(void)
  18518. {
  18519. EXPECT_DECLS;
  18520. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18521. ed448_key key;
  18522. WC_RNG rng;
  18523. byte priv[ED448_PRV_KEY_SIZE];
  18524. byte pub[ED448_PUB_KEY_SIZE];
  18525. byte privOnly[ED448_PRV_KEY_SIZE];
  18526. word32 privSz = sizeof(priv);
  18527. word32 pubSz = sizeof(pub);
  18528. word32 privOnlySz = sizeof(privOnly);
  18529. XMEMSET(&key, 0, sizeof(ed448_key));
  18530. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18531. ExpectIntEQ(wc_ed448_init(&key), 0);
  18532. ExpectIntEQ(wc_InitRng(&rng), 0);
  18533. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18534. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  18535. /* Test bad args. */
  18536. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  18537. BAD_FUNC_ARG);
  18538. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  18539. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  18540. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  18541. /* Test bad args. */
  18542. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  18543. BAD_FUNC_ARG);
  18544. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  18545. BAD_FUNC_ARG);
  18546. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  18547. BAD_FUNC_ARG);
  18548. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  18549. BAD_FUNC_ARG);
  18550. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  18551. BAD_FUNC_ARG);
  18552. /* Cross check output. */
  18553. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  18554. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18555. wc_ed448_free(&key);
  18556. #endif
  18557. return EXPECT_RESULT();
  18558. } /* END test_wc_ed448_exportKey */
  18559. /*
  18560. * Testing wc_Ed448PublicKeyToDer
  18561. */
  18562. static int test_wc_Ed448PublicKeyToDer(void)
  18563. {
  18564. EXPECT_DECLS;
  18565. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  18566. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18567. ed448_key key;
  18568. byte derBuf[1024];
  18569. XMEMSET(&key, 0, sizeof(ed448_key));
  18570. /* Test bad args */
  18571. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  18572. ExpectIntEQ(wc_ed448_init(&key), 0);
  18573. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  18574. wc_ed448_free(&key);
  18575. /* Test good args */
  18576. if (EXPECT_SUCCESS()) {
  18577. WC_RNG rng;
  18578. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18579. ExpectIntEQ(wc_ed448_init(&key), 0);
  18580. ExpectIntEQ(wc_InitRng(&rng), 0);
  18581. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18582. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  18583. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18584. wc_ed448_free(&key);
  18585. }
  18586. #endif
  18587. return EXPECT_RESULT();
  18588. } /* END testing wc_Ed448PublicKeyToDer */
  18589. /*
  18590. * Testing wc_curve448_init and wc_curve448_free.
  18591. */
  18592. static int test_wc_curve448_init(void)
  18593. {
  18594. EXPECT_DECLS;
  18595. #if defined(HAVE_CURVE448)
  18596. curve448_key key;
  18597. /* Test bad args for wc_curve448_init */
  18598. ExpectIntEQ(wc_curve448_init(&key), 0);
  18599. /* Test bad args for wc_curve448_init */
  18600. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  18601. /* Test good args for wc_curve_448_free */
  18602. wc_curve448_free(&key);
  18603. /* Test bad args for wc_curve448_free */
  18604. wc_curve448_free(NULL);
  18605. #endif
  18606. return EXPECT_RESULT();
  18607. } /* END test_wc_curve448_init and wc_curve_448_free*/
  18608. /*
  18609. * Testing wc_curve448_make_key
  18610. */
  18611. static int test_wc_curve448_make_key(void)
  18612. {
  18613. EXPECT_DECLS;
  18614. #if defined(HAVE_CURVE448)
  18615. curve448_key key;
  18616. WC_RNG rng;
  18617. int keysize;
  18618. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18619. ExpectIntEQ(wc_curve448_init(&key), 0);
  18620. ExpectIntEQ(wc_InitRng(&rng), 0);
  18621. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18622. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  18623. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  18624. /* test bad cases */
  18625. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  18626. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  18627. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  18628. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  18629. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18630. wc_curve448_free(&key);
  18631. #endif
  18632. return EXPECT_RESULT();
  18633. } /* END test_wc_curve448_make_key*/
  18634. /*
  18635. * Testing test_wc_curve448_shared_secret_ex
  18636. */
  18637. static int test_wc_curve448_shared_secret_ex(void)
  18638. {
  18639. EXPECT_DECLS;
  18640. #if defined(HAVE_CURVE448)
  18641. curve448_key private_key;
  18642. curve448_key public_key;
  18643. WC_RNG rng;
  18644. byte out[CURVE448_KEY_SIZE];
  18645. word32 outLen = sizeof(out);
  18646. int endian = EC448_BIG_ENDIAN;
  18647. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18648. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  18649. ExpectIntEQ(wc_InitRng(&rng), 0);
  18650. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  18651. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  18652. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  18653. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18654. &outLen, endian), 0);
  18655. /* test bad cases */
  18656. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  18657. BAD_FUNC_ARG);
  18658. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  18659. endian), BAD_FUNC_ARG);
  18660. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  18661. endian), BAD_FUNC_ARG);
  18662. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  18663. &outLen, endian), BAD_FUNC_ARG);
  18664. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18665. NULL, endian), BAD_FUNC_ARG);
  18666. outLen = outLen - 2;
  18667. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18668. &outLen, endian), BAD_FUNC_ARG);
  18669. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18670. wc_curve448_free(&private_key);
  18671. wc_curve448_free(&public_key);
  18672. #endif
  18673. return EXPECT_RESULT();
  18674. } /* END test_wc_curve448_shared_secret_ex*/
  18675. /*
  18676. * Testing test_wc_curve448_export_public_ex
  18677. */
  18678. static int test_wc_curve448_export_public_ex(void)
  18679. {
  18680. EXPECT_DECLS;
  18681. #if defined(HAVE_CURVE448)
  18682. WC_RNG rng;
  18683. curve448_key key;
  18684. byte out[CURVE448_KEY_SIZE];
  18685. word32 outLen = sizeof(out);
  18686. int endian = EC448_BIG_ENDIAN;
  18687. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18688. ExpectIntEQ(wc_curve448_init(&key), 0);
  18689. ExpectIntEQ(wc_InitRng(&rng), 0);
  18690. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18691. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  18692. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  18693. /* test bad cases*/
  18694. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  18695. BAD_FUNC_ARG);
  18696. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  18697. BAD_FUNC_ARG);
  18698. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  18699. BAD_FUNC_ARG);
  18700. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  18701. BAD_FUNC_ARG);
  18702. outLen = outLen - 2;
  18703. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  18704. ECC_BAD_ARG_E);
  18705. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18706. wc_curve448_free(&key);
  18707. #endif
  18708. return EXPECT_RESULT();
  18709. } /* END test_wc_curve448_export_public_ex*/
  18710. /*
  18711. * Testing test_wc_curve448_export_private_raw_ex
  18712. */
  18713. static int test_wc_curve448_export_private_raw_ex(void)
  18714. {
  18715. EXPECT_DECLS;
  18716. #if defined(HAVE_CURVE448)
  18717. curve448_key key;
  18718. byte out[CURVE448_KEY_SIZE];
  18719. word32 outLen = sizeof(out);
  18720. int endian = EC448_BIG_ENDIAN;
  18721. ExpectIntEQ(wc_curve448_init(&key), 0);
  18722. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  18723. 0);
  18724. /* test bad cases*/
  18725. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  18726. BAD_FUNC_ARG);
  18727. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  18728. BAD_FUNC_ARG);
  18729. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  18730. BAD_FUNC_ARG);
  18731. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  18732. BAD_FUNC_ARG);
  18733. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  18734. EC448_LITTLE_ENDIAN), 0);
  18735. outLen = outLen - 2;
  18736. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  18737. ECC_BAD_ARG_E);
  18738. wc_curve448_free(&key);
  18739. #endif
  18740. return EXPECT_RESULT();
  18741. } /* END test_wc_curve448_export_private_raw_ex*/
  18742. /*
  18743. * Testing test_wc_curve448_import_private_raw_ex
  18744. */
  18745. static int test_wc_curve448_import_private_raw_ex(void)
  18746. {
  18747. EXPECT_DECLS;
  18748. #if defined(HAVE_CURVE448)
  18749. curve448_key key;
  18750. WC_RNG rng;
  18751. byte priv[CURVE448_KEY_SIZE];
  18752. byte pub[CURVE448_KEY_SIZE];
  18753. word32 privSz = sizeof(priv);
  18754. word32 pubSz = sizeof(pub);
  18755. int endian = EC448_BIG_ENDIAN;
  18756. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18757. ExpectIntEQ(wc_curve448_init(&key), 0);
  18758. ExpectIntEQ(wc_InitRng(&rng), 0);
  18759. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18760. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18761. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  18762. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18763. &key, endian), 0);
  18764. /* test bad cases */
  18765. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  18766. BAD_FUNC_ARG);
  18767. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  18768. &key, endian), BAD_FUNC_ARG);
  18769. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  18770. &key, endian), BAD_FUNC_ARG);
  18771. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18772. NULL, endian), BAD_FUNC_ARG);
  18773. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  18774. &key, endian), ECC_BAD_ARG_E);
  18775. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  18776. &key, endian), ECC_BAD_ARG_E);
  18777. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18778. &key, EC448_LITTLE_ENDIAN), 0);
  18779. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18780. wc_curve448_free(&key);
  18781. #endif
  18782. return EXPECT_RESULT();
  18783. } /* END test_wc_curve448_import_private_raw_ex*/
  18784. /*
  18785. * Testing test_curve448_export_key_raw
  18786. */
  18787. static int test_wc_curve448_export_key_raw(void)
  18788. {
  18789. EXPECT_DECLS;
  18790. #if defined(HAVE_CURVE448)
  18791. curve448_key key;
  18792. WC_RNG rng;
  18793. byte priv[CURVE448_KEY_SIZE];
  18794. byte pub[CURVE448_KEY_SIZE];
  18795. word32 privSz = sizeof(priv);
  18796. word32 pubSz = sizeof(pub);
  18797. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18798. ExpectIntEQ(wc_curve448_init(&key), 0);
  18799. ExpectIntEQ(wc_InitRng(&rng), 0);
  18800. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18801. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18802. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  18803. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  18804. 0);
  18805. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18806. wc_curve448_free(&key);
  18807. #endif
  18808. return EXPECT_RESULT();
  18809. } /* END test_wc_curve448_import_private_raw_ex*/
  18810. /*
  18811. * Testing test_wc_curve448_import_private
  18812. */
  18813. static int test_wc_curve448_import_private(void)
  18814. {
  18815. EXPECT_DECLS;
  18816. #if defined(HAVE_CURVE448)
  18817. curve448_key key;
  18818. WC_RNG rng;
  18819. byte priv[CURVE448_KEY_SIZE];
  18820. word32 privSz = sizeof(priv);
  18821. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18822. ExpectIntEQ(wc_curve448_init(&key), 0);
  18823. ExpectIntEQ(wc_InitRng(&rng), 0);
  18824. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18825. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18826. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  18827. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18828. wc_curve448_free(&key);
  18829. #endif
  18830. return EXPECT_RESULT();
  18831. } /* END test_wc_curve448_import*/
  18832. /*
  18833. * Testing test_wc_curve448_size.
  18834. */
  18835. static int test_wc_curve448_size(void)
  18836. {
  18837. EXPECT_DECLS;
  18838. #if defined(HAVE_CURVE448)
  18839. curve448_key key;
  18840. ExpectIntEQ(wc_curve448_init(&key), 0);
  18841. /* Test good args for wc_curve448_size */
  18842. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  18843. /* Test bad args for wc_curve448_size */
  18844. ExpectIntEQ(wc_curve448_size(NULL), 0);
  18845. wc_curve448_free(&key);
  18846. #endif
  18847. return EXPECT_RESULT();
  18848. } /* END test_wc_curve448_size*/
  18849. /*
  18850. * Testing wc_ecc_make_key.
  18851. */
  18852. static int test_wc_ecc_make_key(void)
  18853. {
  18854. EXPECT_DECLS;
  18855. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18856. ecc_key key;
  18857. WC_RNG rng;
  18858. int ret;
  18859. XMEMSET(&key, 0, sizeof(ecc_key));
  18860. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18861. ExpectIntEQ(wc_ecc_init(&key), 0);
  18862. ExpectIntEQ(wc_InitRng(&rng), 0);
  18863. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18864. #if defined(WOLFSSL_ASYNC_CRYPT)
  18865. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18866. #endif
  18867. ExpectIntEQ(ret, 0);
  18868. /* Pass in bad args. */
  18869. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  18870. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  18871. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18872. wc_ecc_free(&key);
  18873. #ifdef FP_ECC
  18874. wc_ecc_fp_free();
  18875. #endif
  18876. #endif
  18877. return EXPECT_RESULT();
  18878. } /* END test_wc_ecc_make_key */
  18879. /*
  18880. * Testing wc_ecc_init()
  18881. */
  18882. static int test_wc_ecc_init(void)
  18883. {
  18884. EXPECT_DECLS;
  18885. #ifdef HAVE_ECC
  18886. ecc_key key;
  18887. XMEMSET(&key, 0, sizeof(ecc_key));
  18888. ExpectIntEQ(wc_ecc_init(&key), 0);
  18889. /* Pass in bad args. */
  18890. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  18891. wc_ecc_free(&key);
  18892. #endif
  18893. return EXPECT_RESULT();
  18894. } /* END test_wc_ecc_init */
  18895. /*
  18896. * Testing wc_ecc_check_key()
  18897. */
  18898. static int test_wc_ecc_check_key(void)
  18899. {
  18900. EXPECT_DECLS;
  18901. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18902. ecc_key key;
  18903. WC_RNG rng;
  18904. int ret;
  18905. XMEMSET(&rng, 0, sizeof(rng));
  18906. XMEMSET(&key, 0, sizeof(key));
  18907. ExpectIntEQ(wc_ecc_init(&key), 0);
  18908. ExpectIntEQ(wc_InitRng(&rng), 0);
  18909. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18910. #if defined(WOLFSSL_ASYNC_CRYPT)
  18911. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18912. #endif
  18913. ExpectIntEQ(ret, 0);
  18914. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  18915. /* Pass in bad args. */
  18916. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  18917. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18918. wc_ecc_free(&key);
  18919. #ifdef FP_ECC
  18920. wc_ecc_fp_free();
  18921. #endif
  18922. #endif
  18923. return EXPECT_RESULT();
  18924. } /* END test_wc_ecc_check_key */
  18925. /*
  18926. * Testing wc_ecc_get_generator()
  18927. */
  18928. static int test_wc_ecc_get_generator(void)
  18929. {
  18930. EXPECT_DECLS;
  18931. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  18932. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  18933. ecc_point* pt = NULL;
  18934. ExpectNotNull(pt = wc_ecc_new_point());
  18935. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  18936. MP_OKAY);
  18937. /* Test bad args. */
  18938. /* Returns Zero for bad arg. */
  18939. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  18940. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  18941. MP_OKAY);
  18942. /* If we ever get to 1000 curves increase this number */
  18943. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  18944. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  18945. wc_ecc_del_point(pt);
  18946. #endif
  18947. return EXPECT_RESULT();
  18948. } /* END test_wc_ecc_get_generator */
  18949. /*
  18950. * Testing wc_ecc_size()
  18951. */
  18952. static int test_wc_ecc_size(void)
  18953. {
  18954. EXPECT_DECLS;
  18955. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18956. WC_RNG rng;
  18957. ecc_key key;
  18958. int ret;
  18959. XMEMSET(&key, 0, sizeof(ecc_key));
  18960. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18961. ExpectIntEQ(wc_ecc_init(&key), 0);
  18962. ExpectIntEQ(wc_InitRng(&rng), 0);
  18963. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18964. #if defined(WOLFSSL_ASYNC_CRYPT)
  18965. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18966. #endif
  18967. ExpectIntEQ(ret, 0);
  18968. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  18969. /* Test bad args. */
  18970. /* Returns Zero for bad arg. */
  18971. ExpectIntEQ(wc_ecc_size(NULL), 0);
  18972. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18973. wc_ecc_free(&key);
  18974. #endif
  18975. return EXPECT_RESULT();
  18976. } /* END test_wc_ecc_size */
  18977. static int test_wc_ecc_params(void)
  18978. {
  18979. EXPECT_DECLS;
  18980. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  18981. It was added after certifications */
  18982. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18983. const ecc_set_type* ecc_set;
  18984. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  18985. /* Test for SECP256R1 curve */
  18986. int curve_id = ECC_SECP256R1;
  18987. int curve_idx;
  18988. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  18989. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  18990. ExpectIntEQ(ecc_set->id, curve_id);
  18991. #endif
  18992. /* Test case when SECP256R1 is not enabled */
  18993. /* Test that we get curve params for index 0 */
  18994. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  18995. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  18996. return EXPECT_RESULT();
  18997. }
  18998. /*
  18999. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  19000. */
  19001. static int test_wc_ecc_signVerify_hash(void)
  19002. {
  19003. EXPECT_DECLS;
  19004. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  19005. ecc_key key;
  19006. WC_RNG rng;
  19007. int ret;
  19008. #ifdef HAVE_ECC_VERIFY
  19009. int verify = 0;
  19010. #endif
  19011. word32 siglen = ECC_BUFSIZE;
  19012. byte sig[ECC_BUFSIZE];
  19013. byte adjustedSig[ECC_BUFSIZE+1];
  19014. byte digest[] = TEST_STRING;
  19015. word32 digestlen = (word32)TEST_STRING_SZ;
  19016. /* Init stack var */
  19017. XMEMSET(&key, 0, sizeof(ecc_key));
  19018. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19019. XMEMSET(sig, 0, siglen);
  19020. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  19021. /* Init structs. */
  19022. ExpectIntEQ(wc_ecc_init(&key), 0);
  19023. ExpectIntEQ(wc_InitRng(&rng), 0);
  19024. ret = wc_ecc_make_key(&rng, KEY14, &key);
  19025. #if defined(WOLFSSL_ASYNC_CRYPT)
  19026. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19027. #endif
  19028. ExpectIntEQ(ret, 0);
  19029. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  19030. 0);
  19031. /* Check bad args. */
  19032. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  19033. ECC_BAD_ARG_E);
  19034. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  19035. ECC_BAD_ARG_E);
  19036. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  19037. ECC_BAD_ARG_E);
  19038. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  19039. ECC_BAD_ARG_E);
  19040. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  19041. ECC_BAD_ARG_E);
  19042. #ifdef HAVE_ECC_VERIFY
  19043. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  19044. &key), 0);
  19045. ExpectIntEQ(verify, 1);
  19046. /* test check on length of signature passed in */
  19047. XMEMCPY(adjustedSig, sig, siglen);
  19048. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  19049. #ifndef NO_STRICT_ECDSA_LEN
  19050. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19051. &verify, &key), 0);
  19052. #else
  19053. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  19054. * is allowed */
  19055. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19056. &verify, &key), 0);
  19057. #endif
  19058. /* Test bad args. */
  19059. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  19060. &key), ECC_BAD_ARG_E);
  19061. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  19062. ECC_BAD_ARG_E);
  19063. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  19064. ECC_BAD_ARG_E);
  19065. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  19066. NULL), ECC_BAD_ARG_E);
  19067. #endif /* HAVE_ECC_VERIFY */
  19068. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19069. wc_ecc_free(&key);
  19070. #ifdef FP_ECC
  19071. wc_ecc_fp_free();
  19072. #endif
  19073. #endif
  19074. return EXPECT_RESULT();
  19075. } /* END test_wc_ecc_sign_hash */
  19076. /*
  19077. * Testing wc_ecc_shared_secret()
  19078. */
  19079. static int test_wc_ecc_shared_secret(void)
  19080. {
  19081. EXPECT_DECLS;
  19082. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19083. ecc_key key;
  19084. ecc_key pubKey;
  19085. WC_RNG rng;
  19086. #if defined(NO_ECC256)
  19087. int ret;
  19088. #endif
  19089. byte out[KEY32];
  19090. int keySz = sizeof(out);
  19091. word32 outlen = (word32)sizeof(out);
  19092. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19093. const char* qx =
  19094. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  19095. const char* qy =
  19096. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  19097. const char* d =
  19098. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  19099. const char* curveName = "SECP256R1";
  19100. const byte expected_shared_secret[] =
  19101. {
  19102. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  19103. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  19104. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  19105. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  19106. };
  19107. #endif
  19108. PRIVATE_KEY_UNLOCK();
  19109. /* Initialize variables. */
  19110. XMEMSET(&key, 0, sizeof(ecc_key));
  19111. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  19112. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19113. XMEMSET(out, 0, keySz);
  19114. ExpectIntEQ(wc_ecc_init(&key), 0);
  19115. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  19116. ExpectIntEQ(wc_InitRng(&rng), 0);
  19117. #if !defined(NO_ECC256)
  19118. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  19119. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  19120. #else
  19121. ret = wc_ecc_make_key(&rng, keySz, &key);
  19122. #if defined(WOLFSSL_ASYNC_CRYPT)
  19123. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19124. #endif
  19125. ExpectIntEQ(ret, 0);
  19126. ret = wc_ecc_make_key(&rng, keySz, &key);
  19127. #if defined(WOLFSSL_ASYNC_CRYPT)
  19128. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19129. #endif
  19130. ExpectIntEQ(ret, 0);
  19131. #endif
  19132. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19133. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19134. !defined(HAVE_SELFTEST)
  19135. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  19136. #endif
  19137. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  19138. #if !defined(NO_ECC256)
  19139. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  19140. #endif
  19141. /* Test bad args. */
  19142. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  19143. BAD_FUNC_ARG);
  19144. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  19145. BAD_FUNC_ARG);
  19146. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  19147. BAD_FUNC_ARG);
  19148. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  19149. BAD_FUNC_ARG);
  19150. /* Invalid length */
  19151. outlen = 1;
  19152. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  19153. BUFFER_E);
  19154. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19155. wc_ecc_free(&pubKey);
  19156. wc_ecc_free(&key);
  19157. #ifdef FP_ECC
  19158. wc_ecc_fp_free();
  19159. #endif
  19160. PRIVATE_KEY_LOCK();
  19161. #endif
  19162. return EXPECT_RESULT();
  19163. } /* END tests_wc_ecc_shared_secret */
  19164. /*
  19165. * testint wc_ecc_export_x963()
  19166. */
  19167. static int test_wc_ecc_export_x963(void)
  19168. {
  19169. EXPECT_DECLS;
  19170. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19171. ecc_key key;
  19172. WC_RNG rng;
  19173. byte out[ECC_ASN963_MAX_BUF_SZ];
  19174. word32 outlen = sizeof(out);
  19175. int ret;
  19176. PRIVATE_KEY_UNLOCK();
  19177. /* Initialize variables. */
  19178. XMEMSET(&key, 0, sizeof(ecc_key));
  19179. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19180. XMEMSET(out, 0, outlen);
  19181. ExpectIntEQ(wc_ecc_init(&key), 0);
  19182. ExpectIntEQ(wc_InitRng(&rng), 0);
  19183. ret = wc_ecc_make_key(&rng, KEY20, &key);
  19184. #if defined(WOLFSSL_ASYNC_CRYPT)
  19185. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19186. #endif
  19187. ExpectIntEQ(ret, 0);
  19188. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  19189. /* Test bad args. */
  19190. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  19191. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  19192. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  19193. key.idx = -4;
  19194. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  19195. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19196. wc_ecc_free(&key);
  19197. #ifdef FP_ECC
  19198. wc_ecc_fp_free();
  19199. #endif
  19200. PRIVATE_KEY_LOCK();
  19201. #endif
  19202. return EXPECT_RESULT();
  19203. } /* END test_wc_ecc_export_x963 */
  19204. /*
  19205. * Testing wc_ecc_export_x963_ex()
  19206. * compile with --enable-compkey will use compression.
  19207. */
  19208. static int test_wc_ecc_export_x963_ex(void)
  19209. {
  19210. EXPECT_DECLS;
  19211. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19212. ecc_key key;
  19213. WC_RNG rng;
  19214. int ret;
  19215. byte out[ECC_ASN963_MAX_BUF_SZ];
  19216. word32 outlen = sizeof(out);
  19217. #ifdef HAVE_COMP_KEY
  19218. word32 badOutLen = 5;
  19219. #endif
  19220. /* Init stack variables. */
  19221. XMEMSET(&key, 0, sizeof(ecc_key));
  19222. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19223. XMEMSET(out, 0, outlen);
  19224. ExpectIntEQ(wc_ecc_init(&key), 0);
  19225. ExpectIntEQ(wc_InitRng(&rng), 0);
  19226. ret = wc_ecc_make_key(&rng, KEY64, &key);
  19227. #if defined(WOLFSSL_ASYNC_CRYPT)
  19228. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19229. #endif
  19230. ExpectIntEQ(ret, 0);
  19231. #ifdef HAVE_COMP_KEY
  19232. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  19233. #else
  19234. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  19235. #endif
  19236. /* Test bad args. */
  19237. #ifdef HAVE_COMP_KEY
  19238. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  19239. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  19240. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  19241. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  19242. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  19243. #else
  19244. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  19245. LENGTH_ONLY_E);
  19246. #endif
  19247. key.idx = -4;
  19248. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  19249. #else
  19250. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  19251. ECC_BAD_ARG_E);
  19252. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  19253. LENGTH_ONLY_E);
  19254. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  19255. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  19256. ECC_BAD_ARG_E);
  19257. key.idx = -4;
  19258. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  19259. ECC_BAD_ARG_E);
  19260. #endif
  19261. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19262. wc_ecc_free(&key);
  19263. #ifdef FP_ECC
  19264. wc_ecc_fp_free();
  19265. #endif
  19266. #endif
  19267. return EXPECT_RESULT();
  19268. } /* END test_wc_ecc_export_x963_ex */
  19269. /*
  19270. * testing wc_ecc_import_x963()
  19271. */
  19272. static int test_wc_ecc_import_x963(void)
  19273. {
  19274. EXPECT_DECLS;
  19275. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19276. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19277. ecc_key pubKey;
  19278. ecc_key key;
  19279. WC_RNG rng;
  19280. byte x963[ECC_ASN963_MAX_BUF_SZ];
  19281. word32 x963Len = (word32)sizeof(x963);
  19282. int ret;
  19283. /* Init stack variables. */
  19284. XMEMSET(&key, 0, sizeof(ecc_key));
  19285. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  19286. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19287. XMEMSET(x963, 0, x963Len);
  19288. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  19289. ExpectIntEQ(wc_ecc_init(&key), 0);
  19290. ExpectIntEQ(wc_InitRng(&rng), 0);
  19291. ret = wc_ecc_make_key(&rng, KEY24, &key);
  19292. #if defined(WOLFSSL_ASYNC_CRYPT)
  19293. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19294. #endif
  19295. ExpectIntEQ(ret, 0);
  19296. PRIVATE_KEY_UNLOCK();
  19297. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  19298. PRIVATE_KEY_LOCK();
  19299. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  19300. /* Test bad args. */
  19301. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  19302. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  19303. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  19304. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19305. wc_ecc_free(&key);
  19306. wc_ecc_free(&pubKey);
  19307. #ifdef FP_ECC
  19308. wc_ecc_fp_free();
  19309. #endif
  19310. #endif
  19311. return EXPECT_RESULT();
  19312. } /* END wc_ecc_import_x963 */
  19313. /*
  19314. * testing wc_ecc_import_private_key()
  19315. */
  19316. static int test_wc_ecc_import_private_key(void)
  19317. {
  19318. EXPECT_DECLS;
  19319. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19320. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19321. ecc_key key;
  19322. ecc_key keyImp;
  19323. WC_RNG rng;
  19324. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  19325. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  19326. word32 privKeySz = (word32)sizeof(privKey);
  19327. word32 x963KeySz = (word32)sizeof(x963Key);
  19328. int ret;
  19329. /* Init stack variables. */
  19330. XMEMSET(&key, 0, sizeof(ecc_key));
  19331. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  19332. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19333. XMEMSET(privKey, 0, privKeySz);
  19334. XMEMSET(x963Key, 0, x963KeySz);
  19335. ExpectIntEQ(wc_ecc_init(&key), 0);
  19336. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  19337. ExpectIntEQ(wc_InitRng(&rng), 0);
  19338. ret = wc_ecc_make_key(&rng, KEY48, &key);
  19339. #if defined(WOLFSSL_ASYNC_CRYPT)
  19340. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19341. #endif
  19342. ExpectIntEQ(ret, 0);
  19343. PRIVATE_KEY_UNLOCK();
  19344. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  19345. PRIVATE_KEY_LOCK();
  19346. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  19347. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19348. x963KeySz, &keyImp), 0);
  19349. /* Pass in bad args. */
  19350. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19351. x963KeySz, NULL), BAD_FUNC_ARG);
  19352. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  19353. &keyImp), BAD_FUNC_ARG);
  19354. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19355. wc_ecc_free(&keyImp);
  19356. wc_ecc_free(&key);
  19357. #ifdef FP_ECC
  19358. wc_ecc_fp_free();
  19359. #endif
  19360. #endif
  19361. return EXPECT_RESULT();
  19362. } /* END test_wc_ecc_import_private_key */
  19363. /*
  19364. * Testing wc_ecc_export_private_only()
  19365. */
  19366. static int test_wc_ecc_export_private_only(void)
  19367. {
  19368. EXPECT_DECLS;
  19369. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19370. ecc_key key;
  19371. WC_RNG rng;
  19372. byte out[ECC_PRIV_KEY_BUF];
  19373. word32 outlen = sizeof(out);
  19374. int ret;
  19375. /* Init stack variables. */
  19376. XMEMSET(&key, 0, sizeof(ecc_key));
  19377. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19378. XMEMSET(out, 0, outlen);
  19379. ExpectIntEQ(wc_ecc_init(&key), 0);
  19380. ExpectIntEQ(wc_InitRng(&rng), 0);
  19381. ret = wc_ecc_make_key(&rng, KEY32, &key);
  19382. #if defined(WOLFSSL_ASYNC_CRYPT)
  19383. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19384. #endif
  19385. ExpectIntEQ(ret, 0);
  19386. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  19387. /* Pass in bad args. */
  19388. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  19389. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  19390. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  19391. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19392. wc_ecc_free(&key);
  19393. #ifdef FP_ECC
  19394. wc_ecc_fp_free();
  19395. #endif
  19396. #endif
  19397. return EXPECT_RESULT();
  19398. } /* END test_wc_ecc_export_private_only */
  19399. /*
  19400. * Testing wc_ecc_rs_to_sig()
  19401. */
  19402. static int test_wc_ecc_rs_to_sig(void)
  19403. {
  19404. EXPECT_DECLS;
  19405. #if defined(HAVE_ECC) && !defined(NO_ASN)
  19406. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19407. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  19408. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  19409. const char* zeroStr = "0";
  19410. byte sig[ECC_MAX_SIG_SIZE];
  19411. word32 siglen = (word32)sizeof(sig);
  19412. /* R and S max size is the order of curve. 2^192.*/
  19413. int keySz = KEY24;
  19414. byte r[KEY24];
  19415. byte s[KEY24];
  19416. word32 rlen = (word32)sizeof(r);
  19417. word32 slen = (word32)sizeof(s);
  19418. /* Init stack variables. */
  19419. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  19420. XMEMSET(r, 0, keySz);
  19421. XMEMSET(s, 0, keySz);
  19422. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  19423. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  19424. /* Test bad args. */
  19425. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  19426. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  19427. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  19428. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  19429. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  19430. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  19431. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  19432. ECC_BAD_ARG_E);
  19433. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  19434. ECC_BAD_ARG_E);
  19435. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  19436. ECC_BAD_ARG_E);
  19437. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  19438. ECC_BAD_ARG_E);
  19439. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  19440. ECC_BAD_ARG_E);
  19441. #endif
  19442. return EXPECT_RESULT();
  19443. } /* END test_wc_ecc_rs_to_sig */
  19444. static int test_wc_ecc_import_raw(void)
  19445. {
  19446. EXPECT_DECLS;
  19447. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19448. ecc_key key;
  19449. const char* qx =
  19450. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  19451. const char* qy =
  19452. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  19453. const char* d =
  19454. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  19455. const char* curveName = "SECP256R1";
  19456. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19457. const char* kNullStr = "";
  19458. int ret;
  19459. #endif
  19460. XMEMSET(&key, 0, sizeof(ecc_key));
  19461. ExpectIntEQ(wc_ecc_init(&key), 0);
  19462. /* Test good import */
  19463. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  19464. /* Test bad args. */
  19465. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  19466. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  19467. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  19468. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  19469. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19470. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19471. wc_ecc_free(&key);
  19472. #endif
  19473. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  19474. curveName), 0);
  19475. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  19476. #endif
  19477. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  19478. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19479. wc_ecc_free(&key);
  19480. #endif
  19481. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19482. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  19483. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  19484. #else
  19485. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  19486. #endif
  19487. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19488. wc_ecc_free(&key);
  19489. #endif
  19490. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19491. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  19492. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  19493. #else
  19494. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  19495. #endif
  19496. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19497. wc_ecc_free(&key);
  19498. #endif
  19499. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  19500. #endif
  19501. wc_ecc_free(&key);
  19502. #endif
  19503. return EXPECT_RESULT();
  19504. } /* END test_wc_ecc_import_raw */
  19505. static int test_wc_ecc_import_unsigned(void)
  19506. {
  19507. EXPECT_DECLS;
  19508. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  19509. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  19510. HAVE_FIPS_VERSION >= 2))
  19511. ecc_key key;
  19512. const byte qx[] = {
  19513. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  19514. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  19515. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  19516. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  19517. };
  19518. const byte qy[] = {
  19519. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  19520. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  19521. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  19522. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  19523. };
  19524. const byte d[] = {
  19525. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  19526. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  19527. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  19528. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  19529. };
  19530. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19531. const byte nullBytes[32] = {0};
  19532. int ret;
  19533. #endif
  19534. int curveId = ECC_SECP256R1;
  19535. XMEMSET(&key, 0, sizeof(ecc_key));
  19536. ExpectIntEQ(wc_ecc_init(&key), 0);
  19537. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  19538. curveId), 0);
  19539. /* Test bad args. */
  19540. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  19541. curveId), BAD_FUNC_ARG);
  19542. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  19543. curveId), BAD_FUNC_ARG);
  19544. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  19545. curveId), BAD_FUNC_ARG);
  19546. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  19547. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  19548. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19549. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  19550. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  19551. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  19552. #endif
  19553. wc_ecc_free(&key);
  19554. #endif
  19555. return EXPECT_RESULT();
  19556. } /* END test_wc_ecc_import_unsigned */
  19557. /*
  19558. * Testing wc_ecc_sig_size()
  19559. */
  19560. static int test_wc_ecc_sig_size(void)
  19561. {
  19562. EXPECT_DECLS;
  19563. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  19564. ecc_key key;
  19565. WC_RNG rng;
  19566. int keySz = KEY16;
  19567. int ret;
  19568. XMEMSET(&rng, 0, sizeof(rng));
  19569. XMEMSET(&key, 0, sizeof(key));
  19570. ExpectIntEQ(wc_ecc_init(&key), 0);
  19571. ExpectIntEQ(wc_InitRng(&rng), 0);
  19572. ret = wc_ecc_make_key(&rng, keySz, &key);
  19573. #if defined(WOLFSSL_ASYNC_CRYPT)
  19574. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19575. #endif
  19576. ExpectIntEQ(ret, 0);
  19577. ExpectIntLE(wc_ecc_sig_size(&key),
  19578. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  19579. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19580. wc_ecc_free(&key);
  19581. #endif
  19582. return EXPECT_RESULT();
  19583. } /* END test_wc_ecc_sig_size */
  19584. /*
  19585. * Testing wc_ecc_ctx_new()
  19586. */
  19587. static int test_wc_ecc_ctx_new(void)
  19588. {
  19589. EXPECT_DECLS;
  19590. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19591. WC_RNG rng;
  19592. ecEncCtx* cli = NULL;
  19593. ecEncCtx* srv = NULL;
  19594. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19595. ExpectIntEQ(wc_InitRng(&rng), 0);
  19596. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19597. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  19598. wc_ecc_ctx_free(cli);
  19599. cli = NULL;
  19600. wc_ecc_ctx_free(srv);
  19601. /* Test bad args. */
  19602. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  19603. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  19604. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  19605. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19606. wc_ecc_ctx_free(cli);
  19607. #endif
  19608. return EXPECT_RESULT();
  19609. } /* END test_wc_ecc_ctx_new */
  19610. /*
  19611. * Tesing wc_ecc_reset()
  19612. */
  19613. static int test_wc_ecc_ctx_reset(void)
  19614. {
  19615. EXPECT_DECLS;
  19616. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19617. ecEncCtx* ctx = NULL;
  19618. WC_RNG rng;
  19619. XMEMSET(&rng, 0, sizeof(rng));
  19620. ExpectIntEQ(wc_InitRng(&rng), 0);
  19621. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19622. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  19623. /* Pass in bad args. */
  19624. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  19625. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  19626. wc_ecc_ctx_free(ctx);
  19627. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19628. #endif
  19629. return EXPECT_RESULT();
  19630. } /* END test_wc_ecc_ctx_reset */
  19631. /*
  19632. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  19633. */
  19634. static int test_wc_ecc_ctx_set_peer_salt(void)
  19635. {
  19636. EXPECT_DECLS;
  19637. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19638. WC_RNG rng;
  19639. ecEncCtx* cliCtx = NULL;
  19640. ecEncCtx* servCtx = NULL;
  19641. const byte* cliSalt = NULL;
  19642. const byte* servSalt = NULL;
  19643. XMEMSET(&rng, 0, sizeof(rng));
  19644. ExpectIntEQ(wc_InitRng(&rng), 0);
  19645. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19646. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  19647. /* Test bad args. */
  19648. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  19649. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  19650. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  19651. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  19652. /* Test bad args. */
  19653. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  19654. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  19655. wc_ecc_ctx_free(cliCtx);
  19656. wc_ecc_ctx_free(servCtx);
  19657. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19658. #endif
  19659. return EXPECT_RESULT();
  19660. } /* END test_wc_ecc_ctx_set_peer_salt */
  19661. /*
  19662. * Testing wc_ecc_ctx_set_info()
  19663. */
  19664. static int test_wc_ecc_ctx_set_info(void)
  19665. {
  19666. EXPECT_DECLS;
  19667. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19668. ecEncCtx* ctx = NULL;
  19669. WC_RNG rng;
  19670. const char* optInfo = "Optional Test Info.";
  19671. int optInfoSz = (int)XSTRLEN(optInfo);
  19672. const char* badOptInfo = NULL;
  19673. XMEMSET(&rng, 0, sizeof(rng));
  19674. ExpectIntEQ(wc_InitRng(&rng), 0);
  19675. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19676. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  19677. /* Test bad args. */
  19678. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  19679. BAD_FUNC_ARG);
  19680. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  19681. BAD_FUNC_ARG);
  19682. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  19683. wc_ecc_ctx_free(ctx);
  19684. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19685. #endif
  19686. return EXPECT_RESULT();
  19687. } /* END test_wc_ecc_ctx_set_info */
  19688. /*
  19689. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  19690. */
  19691. static int test_wc_ecc_encryptDecrypt(void)
  19692. {
  19693. EXPECT_DECLS;
  19694. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  19695. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  19696. ecc_key srvKey;
  19697. ecc_key cliKey;
  19698. ecc_key tmpKey;
  19699. WC_RNG rng;
  19700. int ret;
  19701. const char* msg = "EccBlock Size 16";
  19702. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  19703. #ifdef WOLFSSL_ECIES_OLD
  19704. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  19705. #elif defined(WOLFSSL_ECIES_GEN_IV)
  19706. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  19707. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  19708. #else
  19709. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  19710. WC_SHA256_DIGEST_SIZE];
  19711. #endif
  19712. word32 outSz = (word32)sizeof(out);
  19713. byte plain[sizeof("EccBlock Size 16")];
  19714. word32 plainSz = (word32)sizeof(plain);
  19715. int keySz = KEY20;
  19716. /* Init stack variables. */
  19717. XMEMSET(out, 0, outSz);
  19718. XMEMSET(plain, 0, plainSz);
  19719. XMEMSET(&rng, 0, sizeof(rng));
  19720. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  19721. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  19722. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  19723. ExpectIntEQ(wc_InitRng(&rng), 0);
  19724. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  19725. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  19726. #if defined(WOLFSSL_ASYNC_CRYPT)
  19727. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  19728. #endif
  19729. ExpectIntEQ(ret, 0);
  19730. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  19731. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  19732. #if defined(WOLFSSL_ASYNC_CRYPT)
  19733. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  19734. #endif
  19735. ExpectIntEQ(ret, 0);
  19736. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  19737. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19738. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19739. !defined(HAVE_SELFTEST)
  19740. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  19741. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  19742. #endif
  19743. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  19744. &outSz, NULL), 0);
  19745. /* Test bad args. */
  19746. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  19747. NULL), BAD_FUNC_ARG);
  19748. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  19749. NULL), BAD_FUNC_ARG);
  19750. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  19751. NULL), BAD_FUNC_ARG);
  19752. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  19753. &outSz, NULL), BAD_FUNC_ARG);
  19754. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  19755. NULL), BAD_FUNC_ARG);
  19756. #ifdef WOLFSSL_ECIES_OLD
  19757. tmpKey.dp = cliKey.dp;
  19758. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  19759. #endif
  19760. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  19761. NULL), 0);
  19762. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  19763. NULL), BAD_FUNC_ARG);
  19764. #ifdef WOLFSSL_ECIES_OLD
  19765. /* NULL parameter allowed in new implementations - public key comes from
  19766. * the message. */
  19767. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  19768. NULL), BAD_FUNC_ARG);
  19769. #endif
  19770. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  19771. NULL), BAD_FUNC_ARG);
  19772. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  19773. NULL), BAD_FUNC_ARG);
  19774. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  19775. BAD_FUNC_ARG);
  19776. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  19777. wc_ecc_free(&tmpKey);
  19778. wc_ecc_free(&srvKey);
  19779. wc_ecc_free(&cliKey);
  19780. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19781. #endif
  19782. return EXPECT_RESULT();
  19783. } /* END test_wc_ecc_encryptDecrypt */
  19784. /*
  19785. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  19786. */
  19787. static int test_wc_ecc_del_point(void)
  19788. {
  19789. EXPECT_DECLS;
  19790. #if defined(HAVE_ECC)
  19791. ecc_point* pt = NULL;
  19792. ExpectNotNull(pt = wc_ecc_new_point());
  19793. wc_ecc_del_point(pt);
  19794. #endif
  19795. return EXPECT_RESULT();
  19796. } /* END test_wc_ecc_del_point */
  19797. /*
  19798. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  19799. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  19800. * and wc_ecc_cmp_point()
  19801. */
  19802. static int test_wc_ecc_pointFns(void)
  19803. {
  19804. EXPECT_DECLS;
  19805. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  19806. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19807. !defined(WOLFSSL_ATECC608A)
  19808. ecc_key key;
  19809. WC_RNG rng;
  19810. int ret;
  19811. ecc_point* point = NULL;
  19812. ecc_point* cpypt = NULL;
  19813. int idx = 0;
  19814. int keySz = KEY32;
  19815. byte der[DER_SZ(KEY32)];
  19816. word32 derlenChk = 0;
  19817. word32 derSz = DER_SZ(KEY32);
  19818. /* Init stack variables. */
  19819. XMEMSET(der, 0, derSz);
  19820. XMEMSET(&key, 0, sizeof(ecc_key));
  19821. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19822. ExpectIntEQ(wc_InitRng(&rng), 0);
  19823. ExpectIntEQ(wc_ecc_init(&key), 0);
  19824. ret = wc_ecc_make_key(&rng, keySz, &key);
  19825. #if defined(WOLFSSL_ASYNC_CRYPT)
  19826. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19827. #endif
  19828. ExpectIntEQ(ret, 0);
  19829. ExpectNotNull(point = wc_ecc_new_point());
  19830. ExpectNotNull(cpypt = wc_ecc_new_point());
  19831. /* Export */
  19832. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  19833. &derlenChk), LENGTH_ONLY_E);
  19834. /* Check length value. */
  19835. ExpectIntEQ(derSz, derlenChk);
  19836. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  19837. &derSz), 0);
  19838. /* Test bad args. */
  19839. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  19840. ECC_BAD_ARG_E);
  19841. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  19842. ECC_BAD_ARG_E);
  19843. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  19844. NULL), ECC_BAD_ARG_E);
  19845. /* Import */
  19846. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  19847. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  19848. /* Test bad args. */
  19849. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  19850. ECC_BAD_ARG_E);
  19851. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  19852. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  19853. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  19854. ECC_BAD_ARG_E);
  19855. /* Copy */
  19856. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  19857. /* Test bad args. */
  19858. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  19859. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  19860. /* Compare point */
  19861. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  19862. /* Test bad args. */
  19863. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  19864. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  19865. /* At infinity if return == 1, otherwise return == 0. */
  19866. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  19867. /* Test bad args. */
  19868. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  19869. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  19870. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  19871. #ifdef USE_ECC_B_PARAM
  19872. /* On curve if ret == 0 */
  19873. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  19874. /* Test bad args. */
  19875. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  19876. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  19877. #endif /* USE_ECC_B_PARAM */
  19878. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  19879. /* Free */
  19880. wc_ecc_del_point(point);
  19881. wc_ecc_del_point(cpypt);
  19882. wc_ecc_free(&key);
  19883. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19884. #endif
  19885. return EXPECT_RESULT();
  19886. } /* END test_wc_ecc_pointFns */
  19887. /*
  19888. * Testing wc_ecc_sahred_secret_ssh()
  19889. */
  19890. static int test_wc_ecc_shared_secret_ssh(void)
  19891. {
  19892. EXPECT_DECLS;
  19893. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  19894. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19895. !defined(WOLFSSL_ATECC608A)
  19896. ecc_key key;
  19897. ecc_key key2;
  19898. WC_RNG rng;
  19899. int ret;
  19900. int keySz = KEY32;
  19901. int key2Sz = KEY24;
  19902. byte secret[KEY32];
  19903. word32 secretLen = keySz;
  19904. /* Init stack variables. */
  19905. XMEMSET(&key, 0, sizeof(ecc_key));
  19906. XMEMSET(&key2, 0, sizeof(ecc_key));
  19907. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19908. XMEMSET(secret, 0, secretLen);
  19909. /* Make keys */
  19910. ExpectIntEQ(wc_ecc_init(&key), 0);
  19911. ExpectIntEQ(wc_InitRng(&rng), 0);
  19912. ret = wc_ecc_make_key(&rng, keySz, &key);
  19913. #if defined(WOLFSSL_ASYNC_CRYPT)
  19914. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19915. #endif
  19916. ExpectIntEQ(ret, 0);
  19917. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19918. ExpectIntEQ(wc_ecc_init(&key2), 0);
  19919. ExpectIntEQ(wc_InitRng(&rng), 0);
  19920. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  19921. #if defined(WOLFSSL_ASYNC_CRYPT)
  19922. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  19923. #endif
  19924. ExpectIntEQ(ret, 0);
  19925. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19926. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19927. !defined(HAVE_SELFTEST)
  19928. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  19929. #endif
  19930. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  19931. &secretLen), 0);
  19932. /* Pass in bad args. */
  19933. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  19934. &secretLen), BAD_FUNC_ARG);
  19935. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  19936. BAD_FUNC_ARG);
  19937. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  19938. BAD_FUNC_ARG);
  19939. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  19940. BAD_FUNC_ARG);
  19941. key.type = ECC_PUBLICKEY;
  19942. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  19943. &secretLen), ECC_BAD_ARG_E);
  19944. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19945. wc_ecc_free(&key);
  19946. wc_ecc_free(&key2);
  19947. #ifdef FP_ECC
  19948. wc_ecc_fp_free();
  19949. #endif
  19950. #endif
  19951. return EXPECT_RESULT();
  19952. } /* END test_wc_ecc_shared_secret_ssh */
  19953. /*
  19954. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  19955. */
  19956. static int test_wc_ecc_verify_hash_ex(void)
  19957. {
  19958. EXPECT_DECLS;
  19959. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  19960. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19961. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  19962. ecc_key key;
  19963. WC_RNG rng;
  19964. int ret;
  19965. mp_int r;
  19966. mp_int s;
  19967. mp_int z;
  19968. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  19969. unsigned char iHash[] = "Everyone gets Friday off.......";
  19970. unsigned char shortHash[] = TEST_STRING;
  19971. word32 hashlen = sizeof(hash);
  19972. word32 iHashLen = sizeof(iHash);
  19973. word32 shortHashLen = sizeof(shortHash);
  19974. int keySz = KEY32;
  19975. int verify_ok = 0;
  19976. XMEMSET(&key, 0, sizeof(ecc_key));
  19977. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19978. XMEMSET(&r, 0, sizeof(mp_int));
  19979. XMEMSET(&s, 0, sizeof(mp_int));
  19980. XMEMSET(&z, 0, sizeof(mp_int));
  19981. /* Initialize r, s and z. */
  19982. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  19983. ExpectIntEQ(wc_ecc_init(&key), 0);
  19984. ExpectIntEQ(wc_InitRng(&rng), 0);
  19985. ret = wc_ecc_make_key(&rng, keySz, &key);
  19986. #if defined(WOLFSSL_ASYNC_CRYPT)
  19987. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19988. #endif
  19989. ExpectIntEQ(ret, 0);
  19990. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  19991. /* verify_ok should be 1. */
  19992. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  19993. 0);
  19994. ExpectIntEQ(verify_ok, 1);
  19995. /* verify_ok should be 0 */
  19996. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  19997. &key), 0);
  19998. ExpectIntEQ(verify_ok, 0);
  19999. /* verify_ok should be 0. */
  20000. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  20001. &verify_ok, &key), 0);
  20002. ExpectIntEQ(verify_ok, 0);
  20003. /* Test bad args. */
  20004. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  20005. ECC_BAD_ARG_E);
  20006. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  20007. ECC_BAD_ARG_E);
  20008. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  20009. ECC_BAD_ARG_E);
  20010. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  20011. ECC_BAD_ARG_E);
  20012. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  20013. ECC_BAD_ARG_E);
  20014. /* Test bad args. */
  20015. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  20016. &verify_ok, &key), ECC_BAD_ARG_E);
  20017. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  20018. &verify_ok, &key), ECC_BAD_ARG_E);
  20019. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  20020. &verify_ok, &key), MP_ZERO_E);
  20021. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  20022. &verify_ok, &key), MP_ZERO_E);
  20023. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  20024. &verify_ok, &key), MP_ZERO_E);
  20025. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  20026. &key), ECC_BAD_ARG_E);
  20027. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  20028. &key), ECC_BAD_ARG_E);
  20029. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  20030. &verify_ok, NULL), ECC_BAD_ARG_E);
  20031. wc_ecc_free(&key);
  20032. mp_free(&r);
  20033. mp_free(&s);
  20034. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20035. #endif
  20036. return EXPECT_RESULT();
  20037. } /* END test_wc_ecc_verify_hash_ex */
  20038. /*
  20039. * Testing wc_ecc_mulmod()
  20040. */
  20041. static int test_wc_ecc_mulmod(void)
  20042. {
  20043. EXPECT_DECLS;
  20044. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  20045. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  20046. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  20047. ecc_key key1;
  20048. ecc_key key2;
  20049. ecc_key key3;
  20050. WC_RNG rng;
  20051. int ret;
  20052. XMEMSET(&key1, 0, sizeof(ecc_key));
  20053. XMEMSET(&key2, 0, sizeof(ecc_key));
  20054. XMEMSET(&key3, 0, sizeof(ecc_key));
  20055. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20056. ExpectIntEQ(wc_ecc_init(&key1), 0);
  20057. ExpectIntEQ(wc_ecc_init(&key2), 0);
  20058. ExpectIntEQ(wc_ecc_init(&key3), 0);
  20059. ExpectIntEQ(wc_InitRng(&rng), 0);
  20060. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  20061. #if defined(WOLFSSL_ASYNC_CRYPT)
  20062. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  20063. #endif
  20064. ExpectIntEQ(ret, 0);
  20065. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20066. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  20067. key1.dp->Af, ECC_SECP256R1), 0);
  20068. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  20069. key1.dp->prime, ECC_SECP256R1), 0);
  20070. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  20071. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  20072. 1), 0);
  20073. /* Test bad args. */
  20074. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  20075. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20076. ECC_BAD_ARG_E);
  20077. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  20078. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20079. ECC_BAD_ARG_E);
  20080. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  20081. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20082. ECC_BAD_ARG_E);
  20083. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  20084. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  20085. wc_ecc_free(&key1);
  20086. wc_ecc_free(&key2);
  20087. wc_ecc_free(&key3);
  20088. #ifdef FP_ECC
  20089. wc_ecc_fp_free();
  20090. #endif
  20091. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  20092. return EXPECT_RESULT();
  20093. } /* END test_wc_ecc_mulmod */
  20094. /*
  20095. * Testing wc_ecc_is_valid_idx()
  20096. */
  20097. static int test_wc_ecc_is_valid_idx(void)
  20098. {
  20099. EXPECT_DECLS;
  20100. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20101. ecc_key key;
  20102. WC_RNG rng;
  20103. int ret;
  20104. int iVal = -2;
  20105. int iVal2 = 3000;
  20106. XMEMSET(&key, 0, sizeof(ecc_key));
  20107. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20108. ExpectIntEQ(wc_ecc_init(&key), 0);
  20109. ExpectIntEQ(wc_InitRng(&rng), 0);
  20110. ret = wc_ecc_make_key(&rng, 32, &key);
  20111. #if defined(WOLFSSL_ASYNC_CRYPT)
  20112. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20113. #endif
  20114. ExpectIntEQ(ret, 0);
  20115. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  20116. /* Test bad args. */
  20117. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  20118. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  20119. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20120. wc_ecc_free(&key);
  20121. #ifdef FP_ECC
  20122. wc_ecc_fp_free();
  20123. #endif
  20124. #endif
  20125. return EXPECT_RESULT();
  20126. } /* END test_wc_ecc_is_valid_idx */
  20127. /*
  20128. * Testing wc_ecc_get_curve_id_from_oid()
  20129. */
  20130. static int test_wc_ecc_get_curve_id_from_oid(void)
  20131. {
  20132. EXPECT_DECLS;
  20133. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  20134. !defined(HAVE_FIPS)
  20135. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  20136. word32 len = sizeof(oid);
  20137. /* Bad Cases */
  20138. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  20139. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  20140. /* Good Case */
  20141. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  20142. #endif
  20143. return EXPECT_RESULT();
  20144. } /* END test_wc_ecc_get_curve_id_from_oid */
  20145. /*
  20146. * Testing wc_ecc_sig_size_calc()
  20147. */
  20148. static int test_wc_ecc_sig_size_calc(void)
  20149. {
  20150. EXPECT_DECLS;
  20151. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  20152. ecc_key key;
  20153. WC_RNG rng;
  20154. int sz = 0;
  20155. int ret;
  20156. XMEMSET(&key, 0, sizeof(ecc_key));
  20157. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20158. ExpectIntEQ(wc_ecc_init(&key), 0);
  20159. ExpectIntEQ(wc_InitRng(&rng), 0);
  20160. ret = wc_ecc_make_key(&rng, 16, &key);
  20161. #if defined(WOLFSSL_ASYNC_CRYPT)
  20162. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20163. #endif
  20164. ExpectIntEQ(ret, 0);
  20165. sz = key.dp->size;
  20166. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  20167. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20168. wc_ecc_free(&key);
  20169. #endif
  20170. return EXPECT_RESULT();
  20171. } /* END test_wc_ecc_sig_size_calc */
  20172. /*
  20173. * Testing wc_ecc_sm2_make_key()
  20174. */
  20175. static int test_wc_ecc_sm2_make_key(void)
  20176. {
  20177. int res = TEST_SKIPPED;
  20178. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  20179. EXPECT_DECLS;
  20180. WC_RNG rng[1];
  20181. ecc_key key[1];
  20182. XMEMSET(rng, 0, sizeof(*rng));
  20183. XMEMSET(key, 0, sizeof(*key));
  20184. ExpectIntEQ(wc_InitRng(rng), 0);
  20185. ExpectIntEQ(wc_ecc_init(key), 0);
  20186. /* Test invalid parameters. */
  20187. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  20188. BAD_FUNC_ARG);
  20189. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  20190. BAD_FUNC_ARG);
  20191. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  20192. BAD_FUNC_ARG);
  20193. /* Test valid parameters. */
  20194. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20195. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  20196. wc_ecc_free(key);
  20197. wc_FreeRng(rng);
  20198. #ifdef FP_ECC
  20199. wc_ecc_fp_free();
  20200. #endif
  20201. res = EXPECT_RESULT();
  20202. #endif
  20203. return res;
  20204. }
  20205. /*
  20206. * Testing wc_ecc_sm2_shared_secret()
  20207. */
  20208. static int test_wc_ecc_sm2_shared_secret(void)
  20209. {
  20210. int res = TEST_SKIPPED;
  20211. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  20212. EXPECT_DECLS;
  20213. WC_RNG rng[1];
  20214. ecc_key keyA[1];
  20215. ecc_key keyB[1];
  20216. byte outA[32];
  20217. byte outB[32];
  20218. word32 outALen = 32;
  20219. word32 outBLen = 32;
  20220. XMEMSET(rng, 0, sizeof(*rng));
  20221. XMEMSET(keyA, 0, sizeof(*keyA));
  20222. XMEMSET(keyB, 0, sizeof(*keyB));
  20223. ExpectIntEQ(wc_InitRng(rng), 0);
  20224. ExpectIntEQ(wc_ecc_init(keyA), 0);
  20225. ExpectIntEQ(wc_ecc_init(keyB), 0);
  20226. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  20227. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  20228. #ifdef ECC_TIMING_RESISTANT
  20229. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  20230. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  20231. #endif
  20232. /* Test invalid parameters. */
  20233. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  20234. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  20235. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  20236. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  20237. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  20238. BAD_FUNC_ARG);
  20239. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  20240. BAD_FUNC_ARG);
  20241. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  20242. BAD_FUNC_ARG);
  20243. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  20244. BAD_FUNC_ARG);
  20245. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  20246. /* Test valid parameters. */
  20247. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  20248. ExpectIntLE(outALen, 32);
  20249. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  20250. ExpectIntLE(outBLen, 32);
  20251. ExpectIntEQ(outALen, outBLen);
  20252. ExpectBufEQ(outA, outB, outALen);
  20253. wc_ecc_free(keyB);
  20254. wc_ecc_free(keyA);
  20255. wc_FreeRng(rng);
  20256. #ifdef FP_ECC
  20257. wc_ecc_fp_free();
  20258. #endif
  20259. res = EXPECT_RESULT();
  20260. #endif
  20261. return res;
  20262. }
  20263. /*
  20264. * Testing wc_ecc_sm2_create_digest()
  20265. */
  20266. static int test_wc_ecc_sm2_create_digest(void)
  20267. {
  20268. int res = TEST_SKIPPED;
  20269. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  20270. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  20271. EXPECT_DECLS;
  20272. ecc_key key[1];
  20273. enum wc_HashType hashType;
  20274. unsigned char pub[] = {
  20275. 0x04,
  20276. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20277. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20278. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20279. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20280. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20281. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20282. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20283. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20284. };
  20285. unsigned char id[] = {
  20286. 0x01, 0x02, 0x03,
  20287. };
  20288. unsigned char msg[] = {
  20289. 0x01, 0x02, 0x03,
  20290. };
  20291. unsigned char hash[32];
  20292. #ifdef WOLFSSL_SM3
  20293. unsigned char expHash[32] = {
  20294. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  20295. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  20296. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  20297. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  20298. };
  20299. #else
  20300. unsigned char expHash[32] = {
  20301. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  20302. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  20303. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  20304. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  20305. };
  20306. #endif
  20307. #ifdef WOLFSSL_SM3
  20308. hashType = WC_HASH_TYPE_SM3;
  20309. #else
  20310. hashType = WC_HASH_TYPE_SHA256;
  20311. #endif
  20312. XMEMSET(key, 0, sizeof(*key));
  20313. ExpectIntEQ(wc_ecc_init(key), 0);
  20314. /* Test with no curve set. */
  20315. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20316. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20317. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20318. /* Test invalid parameters. */
  20319. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20320. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20321. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  20322. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20323. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  20324. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20325. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20326. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  20327. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20328. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  20329. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  20330. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20331. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  20332. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20333. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20334. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  20335. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20336. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  20337. /* Bad hash type. */
  20338. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20339. -1, hash, 0, key), BAD_FUNC_ARG);
  20340. /* Bad hash size. */
  20341. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20342. hashType, hash, 0, key), BUFFER_E);
  20343. /* Test valid parameters. */
  20344. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20345. hashType, hash, sizeof(hash), key), 0);
  20346. ExpectBufEQ(hash, expHash, sizeof(expHash));
  20347. wc_ecc_free(key);
  20348. res = EXPECT_RESULT();
  20349. #endif
  20350. return res;
  20351. }
  20352. /*
  20353. * Testing wc_ecc_sm2_verify_hash_ex()
  20354. */
  20355. static int test_wc_ecc_sm2_verify_hash_ex(void)
  20356. {
  20357. int res = TEST_SKIPPED;
  20358. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  20359. defined(WOLFSSL_PUBLIC_MP)
  20360. EXPECT_DECLS;
  20361. ecc_key key[1];
  20362. mp_int r[1];
  20363. mp_int s[1];
  20364. int verified;
  20365. unsigned char pub[] = {
  20366. 0x04,
  20367. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20368. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20369. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20370. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20371. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20372. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20373. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20374. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20375. };
  20376. unsigned char hash[] = {
  20377. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  20378. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  20379. 0x50, 0x69, 0x5B, 0x20
  20380. };
  20381. unsigned char rData[] = {
  20382. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  20383. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  20384. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  20385. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  20386. };
  20387. unsigned char sData[] = {
  20388. 0x1D,
  20389. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20390. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20391. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20392. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  20393. };
  20394. unsigned char rBadData[] = {
  20395. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  20396. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  20397. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  20398. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  20399. };
  20400. XMEMSET(key, 0, sizeof(*key));
  20401. XMEMSET(r, 0, sizeof(*r));
  20402. XMEMSET(s, 0, sizeof(*s));
  20403. ExpectIntEQ(mp_init(r), 0);
  20404. ExpectIntEQ(mp_init(s), 0);
  20405. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  20406. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  20407. ExpectIntEQ(wc_ecc_init(key), 0);
  20408. /* Test with no curve set. */
  20409. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20410. &verified, key), BAD_FUNC_ARG);
  20411. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20412. /* Test invalid parameters. */
  20413. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20414. NULL, NULL), BAD_FUNC_ARG);
  20415. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  20416. NULL, NULL), BAD_FUNC_ARG);
  20417. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  20418. NULL, NULL), BAD_FUNC_ARG);
  20419. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  20420. NULL, NULL), BAD_FUNC_ARG);
  20421. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20422. &verified, NULL), BAD_FUNC_ARG);
  20423. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20424. NULL, key), BAD_FUNC_ARG);
  20425. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  20426. &verified, key), BAD_FUNC_ARG);
  20427. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  20428. &verified, key), BAD_FUNC_ARG);
  20429. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  20430. &verified, key), BAD_FUNC_ARG);
  20431. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20432. NULL, key), BAD_FUNC_ARG);
  20433. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20434. &verified, NULL), BAD_FUNC_ARG);
  20435. /* Make key not on the SM2 curve. */
  20436. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20437. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20438. &verified, key), BAD_FUNC_ARG);
  20439. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20440. /* Test valid parameters. */
  20441. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20442. &verified, key), 0);
  20443. ExpectIntEQ(verified, 1);
  20444. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  20445. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20446. &verified, key), 0);
  20447. ExpectIntEQ(verified, 0);
  20448. mp_free(s);
  20449. mp_free(r);
  20450. wc_ecc_free(key);
  20451. #ifdef FP_ECC
  20452. wc_ecc_fp_free();
  20453. #endif
  20454. res = EXPECT_RESULT();
  20455. #endif
  20456. return res;
  20457. }
  20458. /*
  20459. * Testing wc_ecc_sm2_verify_hash()
  20460. */
  20461. static int test_wc_ecc_sm2_verify_hash(void)
  20462. {
  20463. int res = TEST_SKIPPED;
  20464. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  20465. EXPECT_DECLS;
  20466. ecc_key key[1];
  20467. int verified;
  20468. unsigned char pub[] = {
  20469. 0x04,
  20470. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20471. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20472. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20473. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20474. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20475. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20476. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20477. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20478. };
  20479. unsigned char hash[] = {
  20480. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  20481. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  20482. 0x50, 0x69, 0x5B, 0x20
  20483. };
  20484. unsigned char sig[] = {
  20485. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  20486. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  20487. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  20488. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  20489. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  20490. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20491. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20492. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20493. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  20494. };
  20495. unsigned char sigBad[] = {
  20496. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  20497. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  20498. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  20499. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  20500. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  20501. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20502. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20503. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20504. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  20505. };
  20506. XMEMSET(key, 0, sizeof(*key));
  20507. ExpectIntEQ(wc_ecc_init(key), 0);
  20508. /* Test with no curve set. */
  20509. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20510. &verified, key), BAD_FUNC_ARG);
  20511. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20512. /* Test invalid parameters. */
  20513. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20514. NULL, NULL), BAD_FUNC_ARG);
  20515. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  20516. NULL, NULL), BAD_FUNC_ARG);
  20517. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  20518. NULL, NULL), BAD_FUNC_ARG);
  20519. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20520. &verified, NULL), BAD_FUNC_ARG);
  20521. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20522. NULL, key), BAD_FUNC_ARG);
  20523. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  20524. &verified, key), BAD_FUNC_ARG);
  20525. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  20526. &verified, key), BAD_FUNC_ARG);
  20527. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20528. NULL, key), BAD_FUNC_ARG);
  20529. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20530. &verified, NULL), BAD_FUNC_ARG);
  20531. /* Make key not on the SM2 curve. */
  20532. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20533. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20534. &verified, key), BAD_FUNC_ARG);
  20535. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20536. /* Test valid parameters. */
  20537. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20538. &verified, key), 0);
  20539. ExpectIntEQ(verified, 1);
  20540. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  20541. sizeof(hash), &verified, key), 0);
  20542. ExpectIntEQ(verified, 0);
  20543. wc_ecc_free(key);
  20544. #ifdef FP_ECC
  20545. wc_ecc_fp_free();
  20546. #endif
  20547. res = EXPECT_RESULT();
  20548. #endif
  20549. return res;
  20550. }
  20551. /*
  20552. * Testing wc_ecc_sm2_verify_hash_ex()
  20553. */
  20554. static int test_wc_ecc_sm2_sign_hash_ex(void)
  20555. {
  20556. int res = TEST_SKIPPED;
  20557. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  20558. defined(WOLFSSL_PUBLIC_MP)
  20559. EXPECT_DECLS;
  20560. WC_RNG rng[1];
  20561. ecc_key key[1];
  20562. mp_int r[1];
  20563. mp_int s[1];
  20564. unsigned char hash[32];
  20565. #ifdef HAVE_ECC_VERIFY
  20566. int verified;
  20567. #endif
  20568. XMEMSET(rng, 0, sizeof(*rng));
  20569. XMEMSET(key, 0, sizeof(*key));
  20570. XMEMSET(r, 0, sizeof(*r));
  20571. XMEMSET(s, 0, sizeof(*s));
  20572. ExpectIntEQ(wc_InitRng(rng), 0);
  20573. ExpectIntEQ(mp_init(r), 0);
  20574. ExpectIntEQ(mp_init(s), 0);
  20575. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  20576. ExpectIntEQ(wc_ecc_init(key), 0);
  20577. /* Test with no curve set. */
  20578. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20579. BAD_FUNC_ARG);
  20580. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20581. /* Test invalid parameters. */
  20582. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  20583. NULL), BAD_FUNC_ARG);
  20584. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  20585. NULL), BAD_FUNC_ARG);
  20586. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  20587. NULL), BAD_FUNC_ARG);
  20588. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  20589. NULL), BAD_FUNC_ARG);
  20590. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  20591. NULL), BAD_FUNC_ARG);
  20592. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  20593. s), BAD_FUNC_ARG);
  20594. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  20595. BAD_FUNC_ARG);
  20596. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  20597. BAD_FUNC_ARG);
  20598. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  20599. BAD_FUNC_ARG);
  20600. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  20601. BAD_FUNC_ARG);
  20602. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  20603. BAD_FUNC_ARG);
  20604. /* Make key not on the SM2 curve. */
  20605. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20606. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20607. BAD_FUNC_ARG);
  20608. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20609. #ifdef WOLFSSL_SP_MATH_ALL
  20610. {
  20611. mp_int smallR[1];
  20612. sp_init_size(smallR, 1);
  20613. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  20614. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  20615. smallR, s), MP_VAL);
  20616. }
  20617. #endif
  20618. /* Test valid parameters. */
  20619. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20620. 0);
  20621. #ifdef HAVE_ECC_VERIFY
  20622. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  20623. key), 0);
  20624. ExpectIntEQ(verified, 1);
  20625. #endif
  20626. mp_free(s);
  20627. mp_free(r);
  20628. wc_ecc_free(key);
  20629. wc_FreeRng(rng);
  20630. #ifdef FP_ECC
  20631. wc_ecc_fp_free();
  20632. #endif
  20633. res = EXPECT_RESULT();
  20634. #endif
  20635. return res;
  20636. }
  20637. /*
  20638. * Testing wc_ecc_sm2_verify_hash()
  20639. */
  20640. static int test_wc_ecc_sm2_sign_hash(void)
  20641. {
  20642. int res = TEST_SKIPPED;
  20643. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  20644. EXPECT_DECLS;
  20645. WC_RNG rng[1];
  20646. ecc_key key[1];
  20647. unsigned char hash[32];
  20648. unsigned char sig[72];
  20649. word32 sigSz = sizeof(sig);
  20650. #ifdef HAVE_ECC_VERIFY
  20651. int verified;
  20652. #endif
  20653. XMEMSET(rng, 0, sizeof(*rng));
  20654. XMEMSET(key, 0, sizeof(*key));
  20655. ExpectIntEQ(wc_InitRng(rng), 0);
  20656. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  20657. ExpectIntEQ(wc_ecc_init(key), 0);
  20658. /* Test with no curve set. */
  20659. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20660. BAD_FUNC_ARG);
  20661. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20662. /* Test invalid parameters. */
  20663. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  20664. NULL), BAD_FUNC_ARG);
  20665. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  20666. NULL), BAD_FUNC_ARG);
  20667. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  20668. NULL), BAD_FUNC_ARG);
  20669. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  20670. NULL), BAD_FUNC_ARG);
  20671. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  20672. NULL), BAD_FUNC_ARG);
  20673. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  20674. key), BAD_FUNC_ARG);
  20675. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  20676. key), BAD_FUNC_ARG);
  20677. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  20678. key), BAD_FUNC_ARG);
  20679. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  20680. key), BAD_FUNC_ARG);
  20681. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  20682. key), BAD_FUNC_ARG);
  20683. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  20684. NULL), BAD_FUNC_ARG);
  20685. /* Make key not on the SM2 curve. */
  20686. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20687. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20688. BAD_FUNC_ARG);
  20689. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20690. /* Test valid parameters. */
  20691. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20692. 0);
  20693. #ifdef HAVE_ECC_VERIFY
  20694. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  20695. &verified, key), 0);
  20696. ExpectIntEQ(verified, 1);
  20697. #endif
  20698. wc_ecc_free(key);
  20699. wc_FreeRng(rng);
  20700. #ifdef FP_ECC
  20701. wc_ecc_fp_free();
  20702. #endif
  20703. res = EXPECT_RESULT();
  20704. #endif
  20705. return res;
  20706. }
  20707. /*
  20708. * Testing ToTraditional
  20709. */
  20710. static int test_ToTraditional(void)
  20711. {
  20712. EXPECT_DECLS;
  20713. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  20714. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  20715. defined(OPENSSL_EXTRA_X509_SMALL))
  20716. XFILE f = XBADFILE;
  20717. byte input[TWOK_BUF];
  20718. word32 sz;
  20719. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  20720. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  20721. if (f != XBADFILE)
  20722. XFCLOSE(f);
  20723. /* Good case */
  20724. ExpectIntGT(ToTraditional(input, sz), 0);
  20725. /* Bad cases */
  20726. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  20727. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  20728. #ifdef WOLFSSL_ASN_TEMPLATE
  20729. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  20730. #else
  20731. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  20732. #endif
  20733. #endif
  20734. return EXPECT_RESULT();
  20735. } /* End test_ToTraditional*/
  20736. /*
  20737. * Testing wc_EccPrivateKeyToDer
  20738. */
  20739. static int test_wc_EccPrivateKeyToDer(void)
  20740. {
  20741. EXPECT_DECLS;
  20742. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20743. byte output[ONEK_BUF];
  20744. ecc_key eccKey;
  20745. WC_RNG rng;
  20746. word32 inLen;
  20747. word32 outLen = 0;
  20748. int ret;
  20749. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  20750. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20751. ExpectIntEQ(wc_InitRng(&rng), 0);
  20752. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  20753. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  20754. #if defined(WOLFSSL_ASYNC_CRYPT)
  20755. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  20756. #endif
  20757. ExpectIntEQ(ret, 0);
  20758. inLen = (word32)sizeof(output);
  20759. /* Bad Cases */
  20760. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20761. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20762. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  20763. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  20764. /* Good Case */
  20765. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  20766. wc_ecc_free(&eccKey);
  20767. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20768. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  20769. {
  20770. /* test importing private only into a PKEY struct */
  20771. EC_KEY* ec = NULL;
  20772. EVP_PKEY* pkey = NULL;
  20773. const unsigned char* der;
  20774. der = output;
  20775. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  20776. der = output;
  20777. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  20778. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  20779. if (EXPECT_FAIL()) {
  20780. EC_KEY_free(ec);
  20781. }
  20782. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  20783. }
  20784. #endif
  20785. #endif
  20786. return EXPECT_RESULT();
  20787. } /* End test_wc_EccPrivateKeyToDer*/
  20788. /*
  20789. * Testing wc_DhPublicKeyDecode
  20790. */
  20791. static int test_wc_DhPublicKeyDecode(void)
  20792. {
  20793. EXPECT_DECLS;
  20794. #ifndef NO_DH
  20795. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  20796. DhKey key;
  20797. word32 inOutIdx;
  20798. XMEMSET(&key, 0, sizeof(DhKey));
  20799. ExpectIntEQ(wc_InitDhKey(&key), 0);
  20800. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  20801. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  20802. BAD_FUNC_ARG);
  20803. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  20804. BAD_FUNC_ARG);
  20805. inOutIdx = 0;
  20806. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  20807. BAD_FUNC_ARG);
  20808. inOutIdx = 0;
  20809. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  20810. BAD_FUNC_ARG);
  20811. inOutIdx = 0;
  20812. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  20813. sizeof_dh_pub_key_der_2048), 0);
  20814. ExpectIntNE(key.p.used, 0);
  20815. ExpectIntNE(key.g.used, 0);
  20816. ExpectIntEQ(key.q.used, 0);
  20817. ExpectIntNE(key.pub.used, 0);
  20818. ExpectIntEQ(key.priv.used, 0);
  20819. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  20820. #endif
  20821. #endif /* !NO_DH */
  20822. return EXPECT_RESULT();
  20823. }
  20824. /*
  20825. * Testing wc_Ed25519KeyToDer
  20826. */
  20827. static int test_wc_Ed25519KeyToDer(void)
  20828. {
  20829. EXPECT_DECLS;
  20830. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20831. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20832. byte output[ONEK_BUF];
  20833. ed25519_key ed25519Key;
  20834. WC_RNG rng;
  20835. word32 inLen;
  20836. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  20837. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20838. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  20839. ExpectIntEQ(wc_InitRng(&rng), 0);
  20840. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  20841. inLen = (word32)sizeof(output);
  20842. /* Bad Cases */
  20843. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20844. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20845. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  20846. /* Good Cases */
  20847. /* length only */
  20848. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  20849. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  20850. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20851. wc_ed25519_free(&ed25519Key);
  20852. #endif
  20853. return EXPECT_RESULT();
  20854. } /* End test_wc_Ed25519KeyToDer*/
  20855. /*
  20856. * Testing wc_Ed25519PrivateKeyToDer
  20857. */
  20858. static int test_wc_Ed25519PrivateKeyToDer(void)
  20859. {
  20860. EXPECT_DECLS;
  20861. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20862. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20863. byte output[ONEK_BUF];
  20864. ed25519_key ed25519PrivKey;
  20865. WC_RNG rng;
  20866. word32 inLen;
  20867. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  20868. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20869. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  20870. ExpectIntEQ(wc_InitRng(&rng), 0);
  20871. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  20872. 0);
  20873. inLen = (word32)sizeof(output);
  20874. /* Bad Cases */
  20875. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20876. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20877. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  20878. BAD_FUNC_ARG);
  20879. /* Good Cases */
  20880. /* length only */
  20881. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  20882. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  20883. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20884. wc_ed25519_free(&ed25519PrivKey);
  20885. #endif
  20886. return EXPECT_RESULT();
  20887. } /* End test_wc_Ed25519PrivateKeyToDer*/
  20888. /*
  20889. * Testing wc_Ed448KeyToDer
  20890. */
  20891. static int test_wc_Ed448KeyToDer(void)
  20892. {
  20893. EXPECT_DECLS;
  20894. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20895. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20896. byte output[ONEK_BUF];
  20897. ed448_key ed448Key;
  20898. WC_RNG rng;
  20899. word32 inLen;
  20900. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  20901. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20902. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  20903. ExpectIntEQ(wc_InitRng(&rng), 0);
  20904. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  20905. inLen = (word32)sizeof(output);
  20906. /* Bad Cases */
  20907. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20908. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20909. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  20910. /* Good Cases */
  20911. /* length only */
  20912. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  20913. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  20914. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20915. wc_ed448_free(&ed448Key);
  20916. #endif
  20917. return EXPECT_RESULT();
  20918. } /* End test_wc_Ed448KeyToDer*/
  20919. /*
  20920. * Testing wc_Ed448PrivateKeyToDer
  20921. */
  20922. static int test_wc_Ed448PrivateKeyToDer(void)
  20923. {
  20924. EXPECT_DECLS;
  20925. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20926. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20927. byte output[ONEK_BUF];
  20928. ed448_key ed448PrivKey;
  20929. WC_RNG rng;
  20930. word32 inLen;
  20931. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  20932. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20933. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  20934. ExpectIntEQ(wc_InitRng(&rng), 0);
  20935. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  20936. 0);
  20937. inLen = (word32)sizeof(output);
  20938. /* Bad Cases */
  20939. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20940. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20941. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  20942. BAD_FUNC_ARG);
  20943. /* Good cases */
  20944. /* length only */
  20945. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  20946. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  20947. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20948. wc_ed448_free(&ed448PrivKey);
  20949. #endif
  20950. return EXPECT_RESULT();
  20951. } /* End test_wc_Ed448PrivateKeyToDer*/
  20952. /*
  20953. * Testing wc_SetSubjectBuffer
  20954. */
  20955. static int test_wc_SetSubjectBuffer(void)
  20956. {
  20957. EXPECT_DECLS;
  20958. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  20959. Cert cert;
  20960. XFILE file = XBADFILE;
  20961. byte* der = NULL;
  20962. word32 derSz;
  20963. derSz = FOURK_BUF;
  20964. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  20965. DYNAMIC_TYPE_TMP_BUFFER));
  20966. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  20967. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  20968. if (file != XBADFILE)
  20969. XFCLOSE(file);
  20970. ExpectIntEQ(wc_InitCert(&cert), 0);
  20971. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  20972. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  20973. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20974. #endif
  20975. return EXPECT_RESULT();
  20976. } /* End test_wc_SetSubjectBuffer*/
  20977. /*
  20978. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  20979. */
  20980. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  20981. {
  20982. EXPECT_DECLS;
  20983. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  20984. WC_RNG rng;
  20985. Cert cert;
  20986. #if !defined(NO_RSA) && defined(HAVE_RSA)
  20987. RsaKey rsaKey;
  20988. int bits = 2048;
  20989. #endif
  20990. #if defined(HAVE_ECC)
  20991. ecc_key eccKey;
  20992. int ret;
  20993. #endif
  20994. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20995. ed25519_key ed25519Key;
  20996. #endif
  20997. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20998. ed448_key ed448Key;
  20999. #endif
  21000. #ifndef HAVE_FIPS
  21001. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  21002. #else
  21003. ExpectIntEQ(wc_InitRng(&rng), 0);
  21004. #endif
  21005. ExpectIntEQ(wc_InitCert(&cert), 0);
  21006. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  21007. /* RSA */
  21008. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  21009. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  21010. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  21011. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  21012. 0);
  21013. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  21014. #endif
  21015. #if defined(HAVE_ECC)
  21016. /* ECC */
  21017. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  21018. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  21019. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21020. #if defined(WOLFSSL_ASYNC_CRYPT)
  21021. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21022. #endif
  21023. ExpectIntEQ(ret, 0);
  21024. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  21025. 0);
  21026. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  21027. #endif
  21028. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21029. /* ED25519 */
  21030. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  21031. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  21032. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  21033. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  21034. &ed25519Key), 0);
  21035. wc_ed25519_free(&ed25519Key);
  21036. #endif
  21037. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21038. /* ED448 */
  21039. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  21040. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  21041. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  21042. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  21043. &ed448Key), 0);
  21044. wc_ed448_free(&ed448Key);
  21045. #endif
  21046. wc_FreeRng(&rng);
  21047. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21048. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  21049. return EXPECT_RESULT();
  21050. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  21051. /*
  21052. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  21053. */
  21054. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  21055. {
  21056. EXPECT_DECLS;
  21057. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  21058. WC_RNG rng;
  21059. Cert cert;
  21060. #if !defined(NO_RSA) && defined(HAVE_RSA)
  21061. RsaKey rsaKey;
  21062. int bits = 2048;
  21063. #endif
  21064. #if defined(HAVE_ECC)
  21065. ecc_key eccKey;
  21066. int ret;
  21067. #endif
  21068. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21069. ed25519_key ed25519Key;
  21070. #endif
  21071. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21072. ed448_key ed448Key;
  21073. #endif
  21074. #ifndef HAVE_FIPS
  21075. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  21076. #else
  21077. ExpectIntEQ(wc_InitRng(&rng), 0);
  21078. #endif
  21079. ExpectIntEQ(wc_InitCert(&cert), 0);
  21080. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  21081. /* RSA */
  21082. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  21083. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  21084. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  21085. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  21086. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  21087. #endif
  21088. #if defined(HAVE_ECC)
  21089. /* ECC */
  21090. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  21091. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  21092. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21093. #if defined(WOLFSSL_ASYNC_CRYPT)
  21094. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21095. #endif
  21096. ExpectIntEQ(ret, 0);
  21097. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  21098. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  21099. #endif
  21100. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21101. /* ED25519 */
  21102. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  21103. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  21104. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  21105. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  21106. &ed25519Key), 0);
  21107. wc_ed25519_free(&ed25519Key);
  21108. #endif
  21109. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21110. /* ED448 */
  21111. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  21112. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  21113. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  21114. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  21115. 0);
  21116. wc_ed448_free(&ed448Key);
  21117. #endif
  21118. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21119. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  21120. return EXPECT_RESULT();
  21121. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  21122. /*
  21123. * Testing wc_PKCS7_New()
  21124. */
  21125. static int test_wc_PKCS7_New(void)
  21126. {
  21127. EXPECT_DECLS;
  21128. #if defined(HAVE_PKCS7)
  21129. PKCS7* pkcs7 = NULL;
  21130. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  21131. wc_PKCS7_Free(pkcs7);
  21132. #endif
  21133. return EXPECT_RESULT();
  21134. } /* END test-wc_PKCS7_New */
  21135. /*
  21136. * Testing wc_PKCS7_Init()
  21137. */
  21138. static int test_wc_PKCS7_Init(void)
  21139. {
  21140. EXPECT_DECLS;
  21141. #if defined(HAVE_PKCS7)
  21142. PKCS7* pkcs7 = NULL;
  21143. void* heap = NULL;
  21144. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  21145. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  21146. /* Pass in bad args. */
  21147. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  21148. wc_PKCS7_Free(pkcs7);
  21149. #endif
  21150. return EXPECT_RESULT();
  21151. } /* END test-wc_PKCS7_Init */
  21152. /*
  21153. * Testing wc_PKCS7_InitWithCert()
  21154. */
  21155. static int test_wc_PKCS7_InitWithCert(void)
  21156. {
  21157. EXPECT_DECLS;
  21158. #if defined(HAVE_PKCS7)
  21159. PKCS7* pkcs7 = NULL;
  21160. #ifndef NO_RSA
  21161. #if defined(USE_CERT_BUFFERS_2048)
  21162. unsigned char cert[sizeof(client_cert_der_2048)];
  21163. int certSz = (int)sizeof(cert);
  21164. XMEMSET(cert, 0, certSz);
  21165. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  21166. #elif defined(USE_CERT_BUFFERS_1024)
  21167. unsigned char cert[sizeof(client_cert_der_1024)];
  21168. int certSz = (int)sizeof(cert);
  21169. XMEMSET(cert, 0, certSz);
  21170. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  21171. #else
  21172. unsigned char cert[ONEK_BUF];
  21173. XFILE fp = XBADFILE;
  21174. int certSz;
  21175. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21176. XBADFILE);
  21177. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21178. fp), 0);
  21179. if (fp != XBADFILE)
  21180. XFCLOSE(fp);
  21181. #endif
  21182. #elif defined(HAVE_ECC)
  21183. #if defined(USE_CERT_BUFFERS_256)
  21184. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21185. int certSz = (int)sizeof(cert);
  21186. XMEMSET(cert, 0, certSz);
  21187. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  21188. #else
  21189. unsigned char cert[ONEK_BUF];
  21190. XFILE fp = XBADFILE;
  21191. int certSz;
  21192. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21193. XBADFILE);
  21194. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  21195. fp), 0);
  21196. if (fp != XBADFILE)
  21197. XFCLOSE(fp);
  21198. #endif
  21199. #else
  21200. #error PKCS7 requires ECC or RSA
  21201. #endif
  21202. #ifdef HAVE_ECC
  21203. {
  21204. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  21205. static unsigned char certWithInvalidEccKey[] = {
  21206. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  21207. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  21208. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  21209. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  21210. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21211. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  21212. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21213. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21214. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21215. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21216. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21217. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21218. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21219. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21220. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21221. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  21222. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  21223. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  21224. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  21225. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  21226. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  21227. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  21228. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  21229. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  21230. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  21231. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  21232. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  21233. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  21234. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  21235. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  21236. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  21237. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  21238. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  21239. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  21240. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  21241. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  21242. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  21243. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  21244. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  21245. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  21246. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21247. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  21248. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  21249. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21250. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  21251. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21252. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  21253. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21254. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21255. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21256. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21257. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21258. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21259. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21260. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21261. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21262. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  21263. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  21264. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  21265. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  21266. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  21267. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  21268. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  21269. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  21270. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  21271. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  21272. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  21273. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  21274. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  21275. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  21276. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  21277. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  21278. 0x08, 0xA8, 0xB4
  21279. };
  21280. #endif
  21281. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21282. /* If initialization is not successful, it's free'd in init func. */
  21283. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  21284. 0);
  21285. wc_PKCS7_Free(pkcs7);
  21286. pkcs7 = NULL;
  21287. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21288. /* Valid initialization usage. */
  21289. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21290. /* Pass in bad args. No need free for null checks, free at end.*/
  21291. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  21292. BAD_FUNC_ARG);
  21293. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  21294. BAD_FUNC_ARG);
  21295. #ifdef HAVE_ECC
  21296. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  21297. sizeof(certWithInvalidEccKey)), 0);
  21298. }
  21299. #endif
  21300. wc_PKCS7_Free(pkcs7);
  21301. #endif
  21302. return EXPECT_RESULT();
  21303. } /* END test_wc_PKCS7_InitWithCert */
  21304. /*
  21305. * Testing wc_PKCS7_EncodeData()
  21306. */
  21307. static int test_wc_PKCS7_EncodeData(void)
  21308. {
  21309. EXPECT_DECLS;
  21310. #if defined(HAVE_PKCS7)
  21311. PKCS7* pkcs7 = NULL;
  21312. byte output[FOURK_BUF];
  21313. byte data[] = "My encoded DER cert.";
  21314. #ifndef NO_RSA
  21315. #if defined(USE_CERT_BUFFERS_2048)
  21316. unsigned char cert[sizeof(client_cert_der_2048)];
  21317. unsigned char key[sizeof(client_key_der_2048)];
  21318. int certSz = (int)sizeof(cert);
  21319. int keySz = (int)sizeof(key);
  21320. XMEMSET(cert, 0, certSz);
  21321. XMEMSET(key, 0, keySz);
  21322. XMEMCPY(cert, client_cert_der_2048, certSz);
  21323. XMEMCPY(key, client_key_der_2048, keySz);
  21324. #elif defined(USE_CERT_BUFFERS_1024)
  21325. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  21326. unsigned char key[sizeof_client_key_der_1024];
  21327. int certSz = (int)sizeof(cert);
  21328. int keySz = (int)sizeof(key);
  21329. XMEMSET(cert, 0, certSz);
  21330. XMEMSET(key, 0, keySz);
  21331. XMEMCPY(cert, client_cert_der_1024, certSz);
  21332. XMEMCPY(key, client_key_der_1024, keySz);
  21333. #else
  21334. unsigned char cert[ONEK_BUF];
  21335. unsigned char key[ONEK_BUF];
  21336. XFILE fp = XBADFILE;
  21337. int certSz;
  21338. int keySz;
  21339. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21340. XBADFILE);
  21341. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21342. fp), 0);
  21343. if (fp != XBADFILE) {
  21344. XFCLOSE(fp);
  21345. fp = XBADFILE;
  21346. }
  21347. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21348. XBADFILE);
  21349. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21350. 0);
  21351. if (fp != XBADFILE)
  21352. XFCLOSE(fp);
  21353. #endif
  21354. #elif defined(HAVE_ECC)
  21355. #if defined(USE_CERT_BUFFERS_256)
  21356. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21357. unsigned char key[sizeof(ecc_clikey_der_256)];
  21358. int certSz = (int)sizeof(cert);
  21359. int keySz = (int)sizeof(key);
  21360. XMEMSET(cert, 0, certSz);
  21361. XMEMSET(key, 0, keySz);
  21362. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21363. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21364. #else
  21365. unsigned char cert[ONEK_BUF];
  21366. unsigned char key[ONEK_BUF];
  21367. XFILE fp = XBADFILE;
  21368. int certSz, keySz;
  21369. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21370. XBADFILE);
  21371. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  21372. fp), 0);
  21373. if (fp != XBADFILE) {
  21374. XFCLOSE(fp);
  21375. fp = XBADFILE;
  21376. }
  21377. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21378. XBADFILE);
  21379. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  21380. 0);
  21381. if (fp != XBADFILE)
  21382. XFCLOSE(fp);
  21383. #endif
  21384. #endif
  21385. XMEMSET(output, 0, sizeof(output));
  21386. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21387. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21388. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  21389. if (pkcs7 != NULL) {
  21390. pkcs7->content = data;
  21391. pkcs7->contentSz = sizeof(data);
  21392. pkcs7->privateKey = key;
  21393. pkcs7->privateKeySz = keySz;
  21394. }
  21395. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  21396. /* Test bad args. */
  21397. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  21398. BAD_FUNC_ARG);
  21399. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  21400. BAD_FUNC_ARG);
  21401. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  21402. wc_PKCS7_Free(pkcs7);
  21403. #endif
  21404. return EXPECT_RESULT();
  21405. } /* END test_wc_PKCS7_EncodeData */
  21406. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  21407. !defined(NO_RSA) && !defined(NO_SHA256)
  21408. /* RSA sign raw digest callback */
  21409. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  21410. byte* out, word32 outSz, byte* privateKey,
  21411. word32 privateKeySz, int devid, int hashOID)
  21412. {
  21413. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  21414. byte digInfoEncoding[] = {
  21415. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  21416. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  21417. 0x00, 0x04, 0x20
  21418. };
  21419. int ret;
  21420. byte digestInfo[ONEK_BUF];
  21421. byte sig[FOURK_BUF];
  21422. word32 digestInfoSz = 0;
  21423. word32 idx = 0;
  21424. RsaKey rsa;
  21425. /* SHA-256 required only for this example callback due to above
  21426. * digInfoEncoding[] */
  21427. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  21428. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  21429. (hashOID != SHA256h)) {
  21430. return -1;
  21431. }
  21432. /* build DigestInfo */
  21433. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  21434. digestInfoSz += sizeof(digInfoEncoding);
  21435. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  21436. digestInfoSz += digestSz;
  21437. /* set up RSA key */
  21438. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  21439. if (ret != 0) {
  21440. return ret;
  21441. }
  21442. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  21443. /* sign DigestInfo */
  21444. if (ret == 0) {
  21445. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  21446. &rsa, pkcs7->rng);
  21447. if (ret > 0) {
  21448. if (ret > (int)outSz) {
  21449. /* output buffer too small */
  21450. ret = -1;
  21451. }
  21452. else {
  21453. /* success, ret holds sig size */
  21454. XMEMCPY(out, sig, ret);
  21455. }
  21456. }
  21457. }
  21458. wc_FreeRsaKey(&rsa);
  21459. return ret;
  21460. }
  21461. #endif
  21462. /*
  21463. * Testing wc_PKCS7_EncodeSignedData()
  21464. */
  21465. static int test_wc_PKCS7_EncodeSignedData(void)
  21466. {
  21467. EXPECT_DECLS;
  21468. #if defined(HAVE_PKCS7)
  21469. PKCS7* pkcs7 = NULL;
  21470. WC_RNG rng;
  21471. byte output[FOURK_BUF];
  21472. byte badOut[1];
  21473. word32 outputSz = (word32)sizeof(output);
  21474. word32 badOutSz = 0;
  21475. byte data[] = "Test data to encode.";
  21476. #ifndef NO_RSA
  21477. #if defined(USE_CERT_BUFFERS_2048)
  21478. byte key[sizeof(client_key_der_2048)];
  21479. byte cert[sizeof(client_cert_der_2048)];
  21480. word32 keySz = (word32)sizeof(key);
  21481. word32 certSz = (word32)sizeof(cert);
  21482. XMEMSET(key, 0, keySz);
  21483. XMEMSET(cert, 0, certSz);
  21484. XMEMCPY(key, client_key_der_2048, keySz);
  21485. XMEMCPY(cert, client_cert_der_2048, certSz);
  21486. #elif defined(USE_CERT_BUFFERS_1024)
  21487. byte key[sizeof_client_key_der_1024];
  21488. byte cert[sizeof(sizeof_client_cert_der_1024)];
  21489. word32 keySz = (word32)sizeof(key);
  21490. word32 certSz = (word32)sizeof(cert);
  21491. XMEMSET(key, 0, keySz);
  21492. XMEMSET(cert, 0, certSz);
  21493. XMEMCPY(key, client_key_der_1024, keySz);
  21494. XMEMCPY(cert, client_cert_der_1024, certSz);
  21495. #else
  21496. unsigned char cert[ONEK_BUF];
  21497. unsigned char key[ONEK_BUF];
  21498. XFILE fp = XBADFILE;
  21499. int certSz;
  21500. int keySz;
  21501. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21502. XBADFILE);
  21503. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21504. fp), 0);
  21505. if (fp != XBADFILE) {
  21506. XFCLOSE(fp);
  21507. fp = XBADFILE;
  21508. }
  21509. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21510. XBADFILE);
  21511. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21512. 0);
  21513. if (fp != XBADFILE)
  21514. XFCLOSE(fp);
  21515. #endif
  21516. #elif defined(HAVE_ECC)
  21517. #if defined(USE_CERT_BUFFERS_256)
  21518. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21519. unsigned char key[sizeof(ecc_clikey_der_256)];
  21520. int certSz = (int)sizeof(cert);
  21521. int keySz = (int)sizeof(key);
  21522. XMEMSET(cert, 0, certSz);
  21523. XMEMSET(key, 0, keySz);
  21524. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  21525. XMEMCPY(key, ecc_clikey_der_256, keySz);
  21526. #else
  21527. unsigned char cert[ONEK_BUF];
  21528. unsigned char key[ONEK_BUF];
  21529. XFILE fp = XBADFILE;
  21530. int certSz;
  21531. int keySz;
  21532. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21533. XBADFILE);
  21534. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  21535. if (fp != XBADFILE) {
  21536. XFCLOSE(fp);
  21537. fp = XBADFILE;
  21538. }
  21539. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21540. XBADFILE);
  21541. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  21542. if (fp != XBADFILE)
  21543. XFCLOSE(fp);
  21544. #endif
  21545. #endif
  21546. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21547. XMEMSET(output, 0, outputSz);
  21548. ExpectIntEQ(wc_InitRng(&rng), 0);
  21549. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21550. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21551. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21552. if (pkcs7 != NULL) {
  21553. pkcs7->content = data;
  21554. pkcs7->contentSz = (word32)sizeof(data);
  21555. pkcs7->privateKey = key;
  21556. pkcs7->privateKeySz = (word32)sizeof(key);
  21557. pkcs7->encryptOID = RSAk;
  21558. #ifdef NO_SHA
  21559. pkcs7->hashOID = SHA256h;
  21560. #else
  21561. pkcs7->hashOID = SHAh;
  21562. #endif
  21563. pkcs7->rng = &rng;
  21564. }
  21565. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  21566. wc_PKCS7_Free(pkcs7);
  21567. pkcs7 = NULL;
  21568. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21569. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21570. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  21571. /* Pass in bad args. */
  21572. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  21573. BAD_FUNC_ARG);
  21574. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  21575. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  21576. badOutSz), BAD_FUNC_ARG);
  21577. if (pkcs7 != NULL) {
  21578. pkcs7->hashOID = 0; /* bad hashOID */
  21579. }
  21580. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  21581. BAD_FUNC_ARG);
  21582. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  21583. !defined(NO_RSA) && !defined(NO_SHA256)
  21584. /* test RSA sign raw digest callback, if using RSA and compiled in.
  21585. * Example callback assumes SHA-256, so only run test if compiled in. */
  21586. wc_PKCS7_Free(pkcs7);
  21587. pkcs7 = NULL;
  21588. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21589. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21590. if (pkcs7 != NULL) {
  21591. pkcs7->content = data;
  21592. pkcs7->contentSz = (word32)sizeof(data);
  21593. pkcs7->privateKey = key;
  21594. pkcs7->privateKeySz = (word32)sizeof(key);
  21595. pkcs7->encryptOID = RSAk;
  21596. pkcs7->hashOID = SHA256h;
  21597. pkcs7->rng = &rng;
  21598. }
  21599. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  21600. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  21601. #endif
  21602. wc_PKCS7_Free(pkcs7);
  21603. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21604. #endif
  21605. return EXPECT_RESULT();
  21606. } /* END test_wc_PKCS7_EncodeSignedData */
  21607. /*
  21608. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  21609. */
  21610. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  21611. {
  21612. EXPECT_DECLS;
  21613. #if defined(HAVE_PKCS7)
  21614. int i;
  21615. PKCS7* pkcs7 = NULL;
  21616. WC_RNG rng;
  21617. byte outputHead[FOURK_BUF/2];
  21618. byte outputFoot[FOURK_BUF/2];
  21619. word32 outputHeadSz = (word32)sizeof(outputHead);
  21620. word32 outputFootSz = (word32)sizeof(outputFoot);
  21621. byte data[FOURK_BUF];
  21622. wc_HashAlg hash;
  21623. #ifdef NO_SHA
  21624. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  21625. #else
  21626. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  21627. #endif
  21628. byte hashBuf[WC_MAX_DIGEST_SIZE];
  21629. word32 hashSz = wc_HashGetDigestSize(hashType);
  21630. #ifndef NO_RSA
  21631. #if defined(USE_CERT_BUFFERS_2048)
  21632. byte key[sizeof(client_key_der_2048)];
  21633. byte cert[sizeof(client_cert_der_2048)];
  21634. word32 keySz = (word32)sizeof(key);
  21635. word32 certSz = (word32)sizeof(cert);
  21636. XMEMSET(key, 0, keySz);
  21637. XMEMSET(cert, 0, certSz);
  21638. XMEMCPY(key, client_key_der_2048, keySz);
  21639. XMEMCPY(cert, client_cert_der_2048, certSz);
  21640. #elif defined(USE_CERT_BUFFERS_1024)
  21641. byte key[sizeof_client_key_der_1024];
  21642. byte cert[sizeof(sizeof_client_cert_der_1024)];
  21643. word32 keySz = (word32)sizeof(key);
  21644. word32 certSz = (word32)sizeof(cert);
  21645. XMEMSET(key, 0, keySz);
  21646. XMEMSET(cert, 0, certSz);
  21647. XMEMCPY(key, client_key_der_1024, keySz);
  21648. XMEMCPY(cert, client_cert_der_1024, certSz);
  21649. #else
  21650. unsigned char cert[ONEK_BUF];
  21651. unsigned char key[ONEK_BUF];
  21652. XFILE fp = XBADFILE;
  21653. int certSz;
  21654. int keySz;
  21655. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21656. XBADFILE);
  21657. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21658. fp), 0);
  21659. if (fp != XBADFILE) {
  21660. XFCLOSE(fp);
  21661. fp = XBADFILE;
  21662. }
  21663. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21664. XBADFILE);
  21665. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21666. 0);
  21667. if (fp != XBADFILE)
  21668. XFCLOSE(fp);
  21669. #endif
  21670. #elif defined(HAVE_ECC)
  21671. #if defined(USE_CERT_BUFFERS_256)
  21672. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21673. unsigned char key[sizeof(ecc_clikey_der_256)];
  21674. int certSz = (int)sizeof(cert);
  21675. int keySz = (int)sizeof(key);
  21676. XMEMSET(cert, 0, certSz);
  21677. XMEMSET(key, 0, keySz);
  21678. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21679. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21680. #else
  21681. unsigned char cert[ONEK_BUF];
  21682. unsigned char key[ONEK_BUF];
  21683. XFILE fp = XBADFILE;
  21684. int certSz;
  21685. int keySz;
  21686. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21687. XBADFILE);
  21688. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  21689. fp), 0);
  21690. if (fp != XBADFILE) {
  21691. XFCLOSE(fp);
  21692. fp = XBADFILE;
  21693. }
  21694. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21695. XBADFILE);
  21696. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  21697. 0);
  21698. if (fp != XBADFILE)
  21699. XFCLOSE(fp);
  21700. #endif
  21701. #endif
  21702. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21703. /* initialize large data with sequence */
  21704. for (i=0; i<(int)sizeof(data); i++)
  21705. data[i] = i & 0xff;
  21706. XMEMSET(outputHead, 0, outputHeadSz);
  21707. XMEMSET(outputFoot, 0, outputFootSz);
  21708. ExpectIntEQ(wc_InitRng(&rng), 0);
  21709. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21710. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21711. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21712. if (pkcs7 != NULL) {
  21713. pkcs7->content = NULL; /* not used for ex */
  21714. pkcs7->contentSz = (word32)sizeof(data);
  21715. pkcs7->privateKey = key;
  21716. pkcs7->privateKeySz = (word32)sizeof(key);
  21717. pkcs7->encryptOID = RSAk;
  21718. #ifdef NO_SHA
  21719. pkcs7->hashOID = SHA256h;
  21720. #else
  21721. pkcs7->hashOID = SHAh;
  21722. #endif
  21723. pkcs7->rng = &rng;
  21724. }
  21725. /* calculate hash for content */
  21726. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  21727. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  21728. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  21729. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  21730. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  21731. /* Perform PKCS7 sign using hash directly */
  21732. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21733. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  21734. ExpectIntGT(outputHeadSz, 0);
  21735. ExpectIntGT(outputFootSz, 0);
  21736. wc_PKCS7_Free(pkcs7);
  21737. pkcs7 = NULL;
  21738. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21739. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21740. /* required parameter even on verify when using _ex, if using outputHead
  21741. * and outputFoot */
  21742. if (pkcs7 != NULL) {
  21743. pkcs7->contentSz = (word32)sizeof(data);
  21744. }
  21745. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21746. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  21747. wc_PKCS7_Free(pkcs7);
  21748. pkcs7 = NULL;
  21749. /* assembly complete PKCS7 sign and use normal verify */
  21750. {
  21751. byte* output = NULL;
  21752. word32 outputSz = 0;
  21753. ExpectNotNull(output = (byte*)XMALLOC(
  21754. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  21755. DYNAMIC_TYPE_TMP_BUFFER));
  21756. if (output != NULL) {
  21757. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  21758. outputSz += outputHeadSz;
  21759. XMEMCPY(&output[outputSz], data, sizeof(data));
  21760. outputSz += sizeof(data);
  21761. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  21762. outputSz += outputFootSz;
  21763. }
  21764. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21765. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21766. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  21767. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21768. }
  21769. /* Pass in bad args. */
  21770. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  21771. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21772. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  21773. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21774. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21775. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21776. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  21777. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21778. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21779. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21780. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21781. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  21782. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21783. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  21784. if (pkcs7 != NULL) {
  21785. pkcs7->hashOID = 0; /* bad hashOID */
  21786. }
  21787. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21788. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21789. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  21790. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21791. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  21792. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21793. #ifndef NO_PKCS7_STREAM
  21794. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21795. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  21796. #else
  21797. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21798. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  21799. #endif
  21800. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  21801. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21802. #ifndef NO_PKCS7_STREAM
  21803. /* can pass in 0 buffer length with streaming API */
  21804. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21805. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  21806. #else
  21807. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21808. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21809. #endif
  21810. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21811. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  21812. #ifndef NO_PKCS7_STREAM
  21813. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21814. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  21815. #else
  21816. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21817. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  21818. #endif
  21819. wc_PKCS7_Free(pkcs7);
  21820. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21821. #endif
  21822. return EXPECT_RESULT();
  21823. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  21824. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  21825. /**
  21826. * Loads certs/keys from files or buffers into the argument buffers,
  21827. * helper function called by CreatePKCS7SignedData().
  21828. *
  21829. * Returns 0 on success, negative on error.
  21830. */
  21831. static int LoadPKCS7SignedDataCerts(
  21832. int useIntermediateCertChain, int pkAlgoType,
  21833. byte* intCARoot, word32* intCARootSz,
  21834. byte* intCA1, word32* intCA1Sz,
  21835. byte* intCA2, word32* intCA2Sz,
  21836. byte* cert, word32* certSz,
  21837. byte* key, word32* keySz)
  21838. {
  21839. EXPECT_DECLS;
  21840. int ret = 0;
  21841. XFILE fp = XBADFILE;
  21842. #ifndef NO_RSA
  21843. const char* intCARootRSA = "./certs/ca-cert.der";
  21844. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  21845. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  21846. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  21847. const char* intServKeyRSA = "./certs/server-key.der";
  21848. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  21849. const char* cli1024Cert = "./certs/1024/client-cert.der";
  21850. const char* cli1024Key = "./certs/1024/client-key.der";
  21851. #endif
  21852. #endif
  21853. #ifdef HAVE_ECC
  21854. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  21855. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  21856. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  21857. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  21858. const char* intServKeyECC = "./certs/ecc-key.der";
  21859. #ifndef USE_CERT_BUFFERS_256
  21860. const char* cliEccCert = "./certs/client-ecc-cert.der";
  21861. const char* cliEccKey = "./certs/client-ecc-key.der";
  21862. #endif
  21863. #endif
  21864. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  21865. ((useIntermediateCertChain == 1) &&
  21866. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  21867. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  21868. return BAD_FUNC_ARG;
  21869. }
  21870. /* Read/load certs and keys to use for signing based on PK type and chain */
  21871. switch (pkAlgoType) {
  21872. #ifndef NO_RSA
  21873. case RSA_TYPE:
  21874. if (useIntermediateCertChain == 1) {
  21875. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  21876. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  21877. if (fp != XBADFILE) {
  21878. XFCLOSE(fp);
  21879. fp = XBADFILE;
  21880. }
  21881. ExpectIntGT(*intCARootSz, 0);
  21882. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  21883. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  21884. if (fp != XBADFILE) {
  21885. XFCLOSE(fp);
  21886. fp = XBADFILE;
  21887. }
  21888. ExpectIntGT(*intCA1Sz, 0);
  21889. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  21890. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  21891. if (fp != XBADFILE) {
  21892. XFCLOSE(fp);
  21893. fp = XBADFILE;
  21894. }
  21895. ExpectIntGT(*intCA2Sz, 0);
  21896. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  21897. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21898. if (fp != XBADFILE) {
  21899. XFCLOSE(fp);
  21900. fp = XBADFILE;
  21901. }
  21902. ExpectIntGT(*certSz, 0);
  21903. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  21904. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21905. if (fp != XBADFILE) {
  21906. XFCLOSE(fp);
  21907. fp = XBADFILE;
  21908. }
  21909. ExpectIntGT(*keySz, 0);
  21910. }
  21911. else {
  21912. #if defined(USE_CERT_BUFFERS_2048)
  21913. *keySz = sizeof_client_key_der_2048;
  21914. *certSz = sizeof_client_cert_der_2048;
  21915. XMEMCPY(key, client_key_der_2048, *keySz);
  21916. XMEMCPY(cert, client_cert_der_2048, *certSz);
  21917. #elif defined(USE_CERT_BUFFERS_1024)
  21918. *keySz = sizeof_client_key_der_1024;
  21919. *certSz = sizeof_client_cert_der_1024;
  21920. XMEMCPY(key, client_key_der_1024, *keySz);
  21921. XMEMCPY(cert, client_cert_der_1024, *certSz);
  21922. #else
  21923. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  21924. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21925. if (fp != XBADFILE) {
  21926. XFCLOSE(fp);
  21927. fp = XBADFILE;
  21928. }
  21929. ExpectIntGT(*keySz, 0);
  21930. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  21931. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21932. if (fp != XBADFILE) {
  21933. XFCLOSE(fp);
  21934. fp = XBADFILE;
  21935. }
  21936. ExpectIntGT(*certSz, 0);
  21937. #endif /* USE_CERT_BUFFERS_2048 */
  21938. }
  21939. break;
  21940. #endif /* !NO_RSA */
  21941. #ifdef HAVE_ECC
  21942. case ECC_TYPE:
  21943. if (useIntermediateCertChain == 1) {
  21944. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  21945. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  21946. if (fp != XBADFILE) {
  21947. XFCLOSE(fp);
  21948. fp = XBADFILE;
  21949. }
  21950. ExpectIntGT(*intCARootSz, 0);
  21951. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  21952. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  21953. if (fp != XBADFILE) {
  21954. XFCLOSE(fp);
  21955. fp = XBADFILE;
  21956. }
  21957. ExpectIntGT(*intCA1Sz, 0);
  21958. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  21959. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  21960. if (fp != XBADFILE) {
  21961. XFCLOSE(fp);
  21962. fp = XBADFILE;
  21963. }
  21964. ExpectIntGT(*intCA2Sz, 0);
  21965. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  21966. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21967. if (fp != XBADFILE) {
  21968. XFCLOSE(fp);
  21969. fp = XBADFILE;
  21970. }
  21971. ExpectIntGT(*certSz, 0);
  21972. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  21973. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21974. if (fp != XBADFILE) {
  21975. XFCLOSE(fp);
  21976. fp = XBADFILE;
  21977. }
  21978. ExpectIntGT(*keySz, 0);
  21979. }
  21980. else {
  21981. #if defined(USE_CERT_BUFFERS_256)
  21982. *keySz = sizeof_ecc_clikey_der_256;
  21983. *certSz = sizeof_cliecc_cert_der_256;
  21984. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  21985. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  21986. #else
  21987. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  21988. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21989. if (fp != XBADFILE) {
  21990. XFCLOSE(fp);
  21991. fp = XBADFILE;
  21992. }
  21993. ExpectIntGT(*keySz, 0);
  21994. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  21995. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21996. if (fp != XBADFILE) {
  21997. XFCLOSE(fp);
  21998. fp = XBADFILE;
  21999. }
  22000. ExpectIntGT(*certSz, 0);
  22001. #endif /* USE_CERT_BUFFERS_256 */
  22002. }
  22003. break;
  22004. #endif /* HAVE_ECC */
  22005. default:
  22006. WOLFSSL_MSG("Unsupported SignedData PK type");
  22007. ret = BAD_FUNC_ARG;
  22008. break;
  22009. }
  22010. if (EXPECT_FAIL() && (ret == 0)) {
  22011. ret = BAD_FUNC_ARG;
  22012. }
  22013. return ret;
  22014. }
  22015. /**
  22016. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  22017. *
  22018. * output output buffer to place SignedData
  22019. * outputSz size of output buffer
  22020. * data data buffer to be signed
  22021. * dataSz size of data buffer
  22022. * withAttribs [1/0] include attributes in SignedData message
  22023. * detachedSig [1/0] create detached signature, no content
  22024. * useIntCertChain [1/0] use certificate chain and include intermediate and
  22025. * root CAs in bundle
  22026. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  22027. *
  22028. * Return size of bundle created on success, negative on error */
  22029. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  22030. byte* data, word32 dataSz,
  22031. int withAttribs, int detachedSig,
  22032. int useIntermediateCertChain,
  22033. int pkAlgoType)
  22034. {
  22035. EXPECT_DECLS;
  22036. int ret = 0;
  22037. WC_RNG rng;
  22038. PKCS7* pkcs7 = NULL;
  22039. static byte messageTypeOid[] =
  22040. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  22041. 0x09, 0x02 };
  22042. static byte messageType[] = { 0x13, 2, '1', '9' };
  22043. PKCS7Attrib attribs[] =
  22044. {
  22045. { messageTypeOid, sizeof(messageTypeOid), messageType,
  22046. sizeof(messageType) }
  22047. };
  22048. byte intCARoot[TWOK_BUF];
  22049. byte intCA1[TWOK_BUF];
  22050. byte intCA2[TWOK_BUF];
  22051. byte cert[TWOK_BUF];
  22052. byte key[TWOK_BUF];
  22053. word32 intCARootSz = sizeof(intCARoot);
  22054. word32 intCA1Sz = sizeof(intCA1);
  22055. word32 intCA2Sz = sizeof(intCA2);
  22056. word32 certSz = sizeof(cert);
  22057. word32 keySz = sizeof(key);
  22058. XMEMSET(intCARoot, 0, intCARootSz);
  22059. XMEMSET(intCA1, 0, intCA1Sz);
  22060. XMEMSET(intCA2, 0, intCA2Sz);
  22061. XMEMSET(cert, 0, certSz);
  22062. XMEMSET(key, 0, keySz);
  22063. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  22064. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  22065. cert, &certSz, key, &keySz);
  22066. ExpectIntEQ(ret, 0);
  22067. XMEMSET(output, 0, outputSz);
  22068. ExpectIntEQ(wc_InitRng(&rng), 0);
  22069. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22070. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22071. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22072. if (useIntermediateCertChain == 1) {
  22073. /* Add intermediate and root CA certs into SignedData Certs SET */
  22074. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  22075. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  22076. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  22077. }
  22078. if (pkcs7 != NULL) {
  22079. pkcs7->content = data;
  22080. pkcs7->contentSz = dataSz;
  22081. pkcs7->privateKey = key;
  22082. pkcs7->privateKeySz = (word32)sizeof(key);
  22083. if (pkAlgoType == RSA_TYPE) {
  22084. pkcs7->encryptOID = RSAk;
  22085. }
  22086. else {
  22087. pkcs7->encryptOID = ECDSAk;
  22088. }
  22089. #ifdef NO_SHA
  22090. pkcs7->hashOID = SHA256h;
  22091. #else
  22092. pkcs7->hashOID = SHAh;
  22093. #endif
  22094. pkcs7->rng = &rng;
  22095. if (withAttribs) {
  22096. /* include a signed attribute */
  22097. pkcs7->signedAttribs = attribs;
  22098. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  22099. }
  22100. }
  22101. if (detachedSig) {
  22102. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  22103. }
  22104. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  22105. ExpectIntGT(outputSz, 0);
  22106. wc_PKCS7_Free(pkcs7);
  22107. pkcs7 = NULL;
  22108. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22109. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22110. if (detachedSig && (pkcs7 != NULL)) {
  22111. pkcs7->content = data;
  22112. pkcs7->contentSz = dataSz;
  22113. }
  22114. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22115. wc_PKCS7_Free(pkcs7);
  22116. wc_FreeRng(&rng);
  22117. if (EXPECT_FAIL()) {
  22118. outputSz = 0;
  22119. }
  22120. return outputSz;
  22121. }
  22122. #endif
  22123. /*
  22124. * Testing wc_PKCS_VerifySignedData()
  22125. */
  22126. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  22127. {
  22128. EXPECT_DECLS;
  22129. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22130. PKCS7* pkcs7 = NULL;
  22131. byte output[6000]; /* Large size needed for bundles with int CA certs */
  22132. word32 outputSz = sizeof(output);
  22133. byte data[] = "Test data to encode.";
  22134. byte badOut[1];
  22135. word32 badOutSz = 0;
  22136. byte badContent[] = "This is different content than was signed";
  22137. wc_HashAlg hash;
  22138. #ifdef NO_SHA
  22139. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  22140. #else
  22141. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  22142. #endif
  22143. byte hashBuf[WC_MAX_DIGEST_SIZE];
  22144. word32 hashSz = wc_HashGetDigestSize(hashType);
  22145. #ifndef NO_RSA
  22146. PKCS7DecodedAttrib* decodedAttrib = NULL;
  22147. /* contentType OID (1.2.840.113549.1.9.3) */
  22148. static const byte contentTypeOid[] =
  22149. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  22150. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  22151. static const byte dataType[] =
  22152. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  22153. /* messageDigest OID (1.2.840.113549.1.9.4) */
  22154. static const byte messageDigestOid[] =
  22155. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  22156. #ifndef NO_ASN_TIME
  22157. /* signingTime OID () */
  22158. static const byte signingTimeOid[] =
  22159. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  22160. #endif
  22161. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  22162. int dateLength = 0;
  22163. byte dateFormat;
  22164. const byte* datePart = NULL;
  22165. struct tm timearg;
  22166. time_t now;
  22167. struct tm* nowTm = NULL;
  22168. #ifdef NEED_TMP_TIME
  22169. struct tm tmpTimeStorage;
  22170. struct tm* tmpTime = &tmpTimeStorage;
  22171. #endif
  22172. #endif /* !NO_ASN && !NO_ASN_TIME */
  22173. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  22174. /* Success test with RSA certs/key */
  22175. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22176. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  22177. /* calculate hash for content, used later */
  22178. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  22179. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  22180. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  22181. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  22182. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22183. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22184. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22185. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22186. /* Check that decoded signed attributes are correct */
  22187. /* messageDigest should be first */
  22188. if (pkcs7 != NULL) {
  22189. decodedAttrib = pkcs7->decodedAttrib;
  22190. }
  22191. ExpectNotNull(decodedAttrib);
  22192. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  22193. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  22194. decodedAttrib->oidSz), 0);
  22195. /* + 2 for OCTET STRING and length bytes */
  22196. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  22197. ExpectNotNull(decodedAttrib->value);
  22198. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  22199. #ifndef NO_ASN_TIME
  22200. /* signingTime should be second */
  22201. if (decodedAttrib != NULL) {
  22202. decodedAttrib = decodedAttrib->next;
  22203. }
  22204. ExpectNotNull(decodedAttrib);
  22205. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  22206. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  22207. decodedAttrib->oidSz), 0);
  22208. ExpectIntGT(decodedAttrib->valueSz, 0);
  22209. ExpectNotNull(decodedAttrib->value);
  22210. #endif
  22211. /* Verify signingTime if ASN and time are available */
  22212. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  22213. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  22214. &datePart, &dateFormat, &dateLength), 0);
  22215. ExpectNotNull(datePart);
  22216. ExpectIntGT(dateLength, 0);
  22217. XMEMSET(&timearg, 0, sizeof(timearg));
  22218. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  22219. &timearg), 0);
  22220. /* Get current time and compare year/month/day against attribute value */
  22221. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  22222. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  22223. ExpectNotNull(nowTm);
  22224. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  22225. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  22226. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  22227. #endif /* !NO_ASN && !NO_ASN_TIME */
  22228. /* contentType should be third */
  22229. if (decodedAttrib != NULL) {
  22230. decodedAttrib = decodedAttrib->next;
  22231. }
  22232. ExpectNotNull(decodedAttrib);
  22233. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  22234. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  22235. decodedAttrib->oidSz), 0);
  22236. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  22237. ExpectNotNull(decodedAttrib->value);
  22238. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  22239. 0);
  22240. #endif /* !NO_RSA */
  22241. /* Test bad args. */
  22242. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  22243. BAD_FUNC_ARG);
  22244. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  22245. BAD_FUNC_ARG);
  22246. #ifndef NO_PKCS7_STREAM
  22247. /* can pass in 0 buffer length with streaming API */
  22248. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22249. badOutSz), WC_PKCS7_WANT_READ_E);
  22250. #else
  22251. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22252. badOutSz), BAD_FUNC_ARG);
  22253. #endif
  22254. wc_PKCS7_Free(pkcs7);
  22255. pkcs7 = NULL;
  22256. #ifndef NO_RSA
  22257. /* Try RSA certs/key/sig first */
  22258. outputSz = sizeof(output);
  22259. XMEMSET(output, 0, outputSz);
  22260. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22261. (word32)sizeof(data),
  22262. 1, 1, 0, RSA_TYPE)), 0);
  22263. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22264. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22265. if (pkcs7 != NULL) {
  22266. pkcs7->content = badContent;
  22267. pkcs7->contentSz = sizeof(badContent);
  22268. }
  22269. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  22270. SIG_VERIFY_E);
  22271. wc_PKCS7_Free(pkcs7);
  22272. pkcs7 = NULL;
  22273. /* Test success case with detached signature and valid content */
  22274. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22275. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22276. if (pkcs7 != NULL) {
  22277. pkcs7->content = data;
  22278. pkcs7->contentSz = sizeof(data);
  22279. }
  22280. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22281. wc_PKCS7_Free(pkcs7);
  22282. pkcs7 = NULL;
  22283. /* verify using pre-computed content digest only (no content) */
  22284. {
  22285. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22286. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  22287. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22288. output, outputSz, NULL, 0), 0);
  22289. wc_PKCS7_Free(pkcs7);
  22290. pkcs7 = NULL;
  22291. }
  22292. #endif /* !NO_RSA */
  22293. /* Test verify on signedData containing intermediate/root CA certs */
  22294. #ifndef NO_RSA
  22295. outputSz = sizeof(output);
  22296. XMEMSET(output, 0, outputSz);
  22297. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22298. (word32)sizeof(data),
  22299. 0, 0, 1, RSA_TYPE)), 0);
  22300. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22301. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22302. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22303. wc_PKCS7_Free(pkcs7);
  22304. pkcs7 = NULL;
  22305. #endif /* !NO_RSA */
  22306. #endif
  22307. return EXPECT_RESULT();
  22308. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  22309. /*
  22310. * Testing wc_PKCS_VerifySignedData()
  22311. */
  22312. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  22313. {
  22314. EXPECT_DECLS;
  22315. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  22316. PKCS7* pkcs7 = NULL;
  22317. byte output[6000]; /* Large size needed for bundles with int CA certs */
  22318. word32 outputSz = sizeof(output);
  22319. byte data[] = "Test data to encode.";
  22320. byte badContent[] = "This is different content than was signed";
  22321. wc_HashAlg hash;
  22322. #ifdef NO_SHA
  22323. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  22324. #else
  22325. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  22326. #endif
  22327. byte hashBuf[WC_MAX_DIGEST_SIZE];
  22328. word32 hashSz = wc_HashGetDigestSize(hashType);
  22329. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  22330. /* Success test with ECC certs/key */
  22331. outputSz = sizeof(output);
  22332. XMEMSET(output, 0, outputSz);
  22333. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22334. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  22335. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22336. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22337. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22338. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22339. wc_PKCS7_Free(pkcs7);
  22340. pkcs7 = NULL;
  22341. /* Invalid content should error, use detached signature so we can
  22342. * easily change content */
  22343. outputSz = sizeof(output);
  22344. XMEMSET(output, 0, outputSz);
  22345. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22346. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  22347. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22348. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22349. if (pkcs7 != NULL) {
  22350. pkcs7->content = badContent;
  22351. pkcs7->contentSz = sizeof(badContent);
  22352. }
  22353. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  22354. SIG_VERIFY_E);
  22355. wc_PKCS7_Free(pkcs7);
  22356. pkcs7 = NULL;
  22357. /* Test success case with detached signature and valid content */
  22358. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22359. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22360. if (pkcs7 != NULL) {
  22361. pkcs7->content = data;
  22362. pkcs7->contentSz = sizeof(data);
  22363. }
  22364. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22365. wc_PKCS7_Free(pkcs7);
  22366. pkcs7 = NULL;
  22367. /* verify using pre-computed content digest only (no content) */
  22368. {
  22369. /* calculate hash for content */
  22370. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  22371. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  22372. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  22373. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  22374. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22375. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  22376. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22377. output, outputSz, NULL, 0), 0);
  22378. wc_PKCS7_Free(pkcs7);
  22379. pkcs7 = NULL;
  22380. }
  22381. /* Test verify on signedData containing intermediate/root CA certs */
  22382. outputSz = sizeof(output);
  22383. XMEMSET(output, 0, outputSz);
  22384. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22385. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  22386. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22387. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22388. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22389. wc_PKCS7_Free(pkcs7);
  22390. pkcs7 = NULL;
  22391. #endif
  22392. return EXPECT_RESULT();
  22393. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  22394. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  22395. !defined(NO_AES_256)
  22396. static const byte defKey[] = {
  22397. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22398. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22399. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22400. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22401. };
  22402. static byte aesHandle[32]; /* simulated hardware key handle */
  22403. /* return 0 on success */
  22404. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  22405. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  22406. byte* in, int inSz, byte* out, void* usrCtx)
  22407. {
  22408. int ret;
  22409. Aes aes;
  22410. if (usrCtx == NULL) {
  22411. /* no simulated handle passed in */
  22412. return -1;
  22413. }
  22414. switch (encryptOID) {
  22415. case AES256CBCb:
  22416. if (ivSz != AES_BLOCK_SIZE)
  22417. return BAD_FUNC_ARG;
  22418. break;
  22419. default:
  22420. WOLFSSL_MSG("Unsupported content cipher type for test");
  22421. return ALGO_ID_E;
  22422. };
  22423. /* simulate using handle to get key */
  22424. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  22425. if (ret == 0) {
  22426. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  22427. if (ret == 0)
  22428. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  22429. wc_AesFree(&aes);
  22430. }
  22431. (void)aad;
  22432. (void)aadSz;
  22433. (void)authTag;
  22434. (void)authTagSz;
  22435. (void)pkcs7;
  22436. return ret;
  22437. }
  22438. /* returns key size on success */
  22439. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  22440. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  22441. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  22442. {
  22443. int ret = -1;
  22444. if (out == NULL)
  22445. return BAD_FUNC_ARG;
  22446. if (keyId[0] != 0x00) {
  22447. return -1;
  22448. }
  22449. if (type != (int)PKCS7_KEKRI) {
  22450. return -1;
  22451. }
  22452. switch (keyWrapAlgo) {
  22453. case AES256_WRAP:
  22454. /* simulate setting a handle for later decryption but use key
  22455. * as handle in the test case here */
  22456. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  22457. aesHandle, sizeof(aesHandle), NULL);
  22458. if (ret < 0)
  22459. return ret;
  22460. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  22461. if (ret < 0)
  22462. return ret;
  22463. /* return key size on success */
  22464. return sizeof(defKey);
  22465. default:
  22466. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  22467. return BAD_KEYWRAP_ALG_E;
  22468. };
  22469. (void)cekSz;
  22470. (void)cek;
  22471. (void)outSz;
  22472. (void)keyIdSz;
  22473. (void)direction;
  22474. (void)orginKey; /* used with KAKRI */
  22475. (void)orginKeySz;
  22476. return ret;
  22477. }
  22478. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  22479. /*
  22480. * Testing wc_PKCS7_EncodeEnvelopedData()
  22481. */
  22482. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  22483. {
  22484. EXPECT_DECLS;
  22485. #if defined(HAVE_PKCS7)
  22486. PKCS7* pkcs7 = NULL;
  22487. #ifdef ECC_TIMING_RESISTANT
  22488. WC_RNG rng;
  22489. #endif
  22490. word32 tempWrd32 = 0;
  22491. byte* tmpBytePtr = NULL;
  22492. const char input[] = "Test data to encode.";
  22493. int i;
  22494. int testSz = 0;
  22495. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  22496. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22497. byte* rsaCert = NULL;
  22498. byte* rsaPrivKey = NULL;
  22499. word32 rsaCertSz;
  22500. word32 rsaPrivKeySz;
  22501. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  22502. !defined(USE_CERT_BUFFERS_2048) )
  22503. static const char* rsaClientCert = "./certs/client-cert.der";
  22504. static const char* rsaClientKey = "./certs/client-key.der";
  22505. rsaCertSz = (word32)sizeof(rsaClientCert);
  22506. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  22507. #endif
  22508. #endif
  22509. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22510. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22511. byte* eccCert = NULL;
  22512. byte* eccPrivKey = NULL;
  22513. word32 eccCertSz;
  22514. word32 eccPrivKeySz;
  22515. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  22516. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  22517. static const char* eccClientKey = "./certs/ecc-client-key.der";
  22518. #endif
  22519. #endif
  22520. /* Generic buffer size. */
  22521. byte output[ONEK_BUF];
  22522. byte decoded[sizeof(input)/sizeof(char)];
  22523. int decodedSz = 0;
  22524. #ifndef NO_FILESYSTEM
  22525. XFILE certFile = XBADFILE;
  22526. XFILE keyFile = XBADFILE;
  22527. #endif
  22528. #ifdef ECC_TIMING_RESISTANT
  22529. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22530. #endif
  22531. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22532. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22533. /* RSA certs and keys. */
  22534. #if defined(USE_CERT_BUFFERS_1024)
  22535. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  22536. /* Allocate buffer space. */
  22537. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  22538. DYNAMIC_TYPE_TMP_BUFFER));
  22539. /* Init buffer. */
  22540. if (rsaCert != NULL) {
  22541. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  22542. }
  22543. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  22544. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  22545. DYNAMIC_TYPE_TMP_BUFFER));
  22546. if (rsaPrivKey != NULL) {
  22547. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  22548. }
  22549. #elif defined(USE_CERT_BUFFERS_2048)
  22550. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  22551. /* Allocate buffer */
  22552. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  22553. DYNAMIC_TYPE_TMP_BUFFER));
  22554. /* Init buffer. */
  22555. if (rsaCert != NULL) {
  22556. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  22557. }
  22558. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  22559. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  22560. DYNAMIC_TYPE_TMP_BUFFER));
  22561. if (rsaPrivKey != NULL) {
  22562. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  22563. }
  22564. #else
  22565. /* File system. */
  22566. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  22567. rsaCertSz = (word32)FOURK_BUF;
  22568. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22569. DYNAMIC_TYPE_TMP_BUFFER));
  22570. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  22571. certFile)) > 0);
  22572. if (certFile != XBADFILE)
  22573. XFCLOSE(certFile);
  22574. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  22575. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22576. DYNAMIC_TYPE_TMP_BUFFER));
  22577. rsaPrivKeySz = (word32)FOURK_BUF;
  22578. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  22579. keyFile)) > 0);
  22580. if (keyFile != XBADFILE)
  22581. XFCLOSE(keyFile);
  22582. #endif /* USE_CERT_BUFFERS */
  22583. #endif /* NO_RSA */
  22584. /* ECC */
  22585. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22586. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22587. #ifdef USE_CERT_BUFFERS_256
  22588. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22589. DYNAMIC_TYPE_TMP_BUFFER));
  22590. /* Init buffer. */
  22591. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  22592. if (eccCert != NULL) {
  22593. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  22594. }
  22595. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22596. DYNAMIC_TYPE_TMP_BUFFER));
  22597. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  22598. if (eccPrivKey != NULL) {
  22599. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  22600. }
  22601. #else /* File system. */
  22602. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  22603. eccCertSz = (word32)FOURK_BUF;
  22604. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22605. DYNAMIC_TYPE_TMP_BUFFER));
  22606. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  22607. certFile)) > 0);
  22608. if (certFile != XBADFILE) {
  22609. XFCLOSE(certFile);
  22610. }
  22611. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  22612. eccPrivKeySz = (word32)FOURK_BUF;
  22613. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22614. DYNAMIC_TYPE_TMP_BUFFER));
  22615. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  22616. keyFile)) > 0);
  22617. if (keyFile != XBADFILE) {
  22618. XFCLOSE(keyFile);
  22619. }
  22620. #endif /* USE_CERT_BUFFERS_256 */
  22621. #endif /* END HAVE_ECC */
  22622. #ifndef NO_FILESYSTEM
  22623. /* Silence. */
  22624. (void)keyFile;
  22625. (void)certFile;
  22626. #endif
  22627. {
  22628. const pkcs7EnvelopedVector testVectors[] = {
  22629. /* DATA is a global variable defined in the makefile. */
  22630. #if !defined(NO_RSA)
  22631. #ifndef NO_DES3
  22632. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  22633. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22634. #endif /* NO_DES3 */
  22635. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22636. #ifndef NO_AES_128
  22637. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  22638. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22639. #endif
  22640. #ifndef NO_AES_192
  22641. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  22642. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22643. #endif
  22644. #ifndef NO_AES_256
  22645. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  22646. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22647. #endif
  22648. #endif /* NO_AES && HAVE_AES_CBC */
  22649. #endif /* NO_RSA */
  22650. #if defined(HAVE_ECC)
  22651. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22652. #if !defined(NO_SHA) && !defined(NO_AES_128)
  22653. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22654. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  22655. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22656. #endif
  22657. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  22658. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22659. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  22660. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22661. #endif
  22662. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  22663. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22664. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  22665. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22666. #endif
  22667. #endif /* NO_AES && HAVE_AES_CBC*/
  22668. #endif /* END HAVE_ECC */
  22669. }; /* END pkcs7EnvelopedVector */
  22670. #ifdef ECC_TIMING_RESISTANT
  22671. ExpectIntEQ(wc_InitRng(&rng), 0);
  22672. #endif
  22673. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22674. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22675. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  22676. for (i = 0; i < testSz; i++) {
  22677. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  22678. (word32)(testVectors + i)->certSz), 0);
  22679. if (pkcs7 != NULL) {
  22680. #ifdef ECC_TIMING_RESISTANT
  22681. pkcs7->rng = &rng;
  22682. #endif
  22683. pkcs7->content = (byte*)(testVectors + i)->content;
  22684. pkcs7->contentSz = (testVectors + i)->contentSz;
  22685. pkcs7->contentOID = (testVectors + i)->contentOID;
  22686. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  22687. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  22688. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  22689. pkcs7->privateKey = (testVectors + i)->privateKey;
  22690. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  22691. }
  22692. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22693. (word32)sizeof(output)), 0);
  22694. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22695. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  22696. ExpectIntGE(decodedSz, 0);
  22697. /* Verify the size of each buffer. */
  22698. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  22699. /* Don't free the last time through the loop. */
  22700. if (i < testSz - 1) {
  22701. wc_PKCS7_Free(pkcs7);
  22702. pkcs7 = NULL;
  22703. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22704. }
  22705. } /* END test loop. */
  22706. }
  22707. /* Test bad args. */
  22708. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  22709. (word32)sizeof(output)), BAD_FUNC_ARG);
  22710. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  22711. (word32)sizeof(output)), BAD_FUNC_ARG);
  22712. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  22713. /* Decode. */
  22714. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  22715. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22716. BAD_FUNC_ARG);
  22717. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22718. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22719. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22720. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  22721. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  22722. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22723. BAD_FUNC_ARG);
  22724. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  22725. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22726. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  22727. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  22728. /* only a failure for KARI test cases */
  22729. if (pkcs7 != NULL) {
  22730. tempWrd32 = pkcs7->singleCertSz;
  22731. pkcs7->singleCertSz = 0;
  22732. }
  22733. #if defined(WOLFSSL_ASN_TEMPLATE)
  22734. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22735. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22736. BUFFER_E);
  22737. #else
  22738. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22739. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22740. ASN_PARSE_E);
  22741. #endif
  22742. if (pkcs7 != NULL) {
  22743. pkcs7->singleCertSz = tempWrd32;
  22744. tmpBytePtr = pkcs7->singleCert;
  22745. pkcs7->singleCert = NULL;
  22746. }
  22747. #if defined(NO_PKCS7_STREAM)
  22748. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  22749. * is returned from kari parse which gets set to bad func arg */
  22750. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22751. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22752. BAD_FUNC_ARG);
  22753. #else
  22754. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22755. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22756. ASN_PARSE_E);
  22757. #endif
  22758. if (pkcs7 != NULL) {
  22759. pkcs7->singleCert = tmpBytePtr;
  22760. }
  22761. #endif
  22762. if (pkcs7 != NULL) {
  22763. tempWrd32 = pkcs7->privateKeySz;
  22764. pkcs7->privateKeySz = 0;
  22765. }
  22766. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22767. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22768. BAD_FUNC_ARG);
  22769. if (pkcs7 != NULL) {
  22770. pkcs7->privateKeySz = tempWrd32;
  22771. tmpBytePtr = pkcs7->privateKey;
  22772. pkcs7->privateKey = NULL;
  22773. }
  22774. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22775. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22776. BAD_FUNC_ARG);
  22777. if (pkcs7 != NULL) {
  22778. pkcs7->privateKey = tmpBytePtr;
  22779. }
  22780. wc_PKCS7_Free(pkcs7);
  22781. pkcs7 = NULL;
  22782. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  22783. /* test of decrypt callback with KEKRI enveloped data */
  22784. {
  22785. int envelopedSz = 0;
  22786. const byte keyId[] = { 0x00 };
  22787. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22788. if (pkcs7 != NULL) {
  22789. pkcs7->content = (byte*)input;
  22790. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  22791. pkcs7->contentOID = DATA;
  22792. pkcs7->encryptOID = AES256CBCb;
  22793. }
  22794. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  22795. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  22796. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  22797. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  22798. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22799. (word32)sizeof(output))), 0);
  22800. wc_PKCS7_Free(pkcs7);
  22801. pkcs7 = NULL;
  22802. /* decode envelopedData */
  22803. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22804. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  22805. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  22806. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22807. envelopedSz, decoded, sizeof(decoded))), 0);
  22808. wc_PKCS7_Free(pkcs7);
  22809. pkcs7 = NULL;
  22810. }
  22811. #endif /* !NO_AES && !NO_AES_256 */
  22812. #ifndef NO_RSA
  22813. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22814. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22815. #endif /* NO_RSA */
  22816. #ifdef HAVE_ECC
  22817. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22818. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22819. #endif /* HAVE_ECC */
  22820. #ifdef ECC_TIMING_RESISTANT
  22821. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22822. #endif
  22823. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  22824. !defined(NO_RSA) && !defined(NO_SHA)
  22825. {
  22826. byte out[7];
  22827. byte *cms = NULL;
  22828. word32 cmsSz;
  22829. XFILE cmsFile = XBADFILE;
  22830. XMEMSET(out, 0, sizeof(out));
  22831. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22832. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  22833. != XBADFILE);
  22834. cmsSz = (word32)FOURK_BUF;
  22835. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22836. DYNAMIC_TYPE_TMP_BUFFER));
  22837. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  22838. if (cmsFile != XBADFILE)
  22839. XFCLOSE(cmsFile);
  22840. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  22841. sizeof_client_cert_der_2048), 0);
  22842. if (pkcs7 != NULL) {
  22843. pkcs7->privateKey = (byte*)client_key_der_2048;
  22844. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  22845. }
  22846. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  22847. 2), 0);
  22848. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  22849. sizeof(out)), 0);
  22850. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22851. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  22852. wc_PKCS7_Free(pkcs7);
  22853. pkcs7 = NULL;
  22854. }
  22855. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  22856. #endif /* HAVE_PKCS7 */
  22857. return EXPECT_RESULT();
  22858. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  22859. /*
  22860. * Testing wc_PKCS7_EncodeEncryptedData()
  22861. */
  22862. static int test_wc_PKCS7_EncodeEncryptedData(void)
  22863. {
  22864. EXPECT_DECLS;
  22865. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  22866. PKCS7* pkcs7 = NULL;
  22867. byte* tmpBytePtr = NULL;
  22868. byte encrypted[TWOK_BUF];
  22869. byte decoded[TWOK_BUF];
  22870. word32 tmpWrd32 = 0;
  22871. int tmpInt = 0;
  22872. int decodedSz;
  22873. int encryptedSz = 0;
  22874. int testSz;
  22875. int i;
  22876. const byte data[] = { /* Hello World */
  22877. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  22878. 0x72,0x6c,0x64
  22879. };
  22880. #ifndef NO_DES3
  22881. byte desKey[] = {
  22882. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  22883. };
  22884. byte des3Key[] = {
  22885. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  22886. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  22887. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  22888. };
  22889. #endif
  22890. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22891. #ifndef NO_AES_128
  22892. byte aes128Key[] = {
  22893. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22894. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22895. };
  22896. #endif
  22897. #ifndef NO_AES_192
  22898. byte aes192Key[] = {
  22899. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22900. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22901. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22902. };
  22903. #endif
  22904. #ifndef NO_AES_256
  22905. byte aes256Key[] = {
  22906. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22907. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22908. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22909. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22910. };
  22911. #endif
  22912. #endif /* !NO_AES && HAVE_AES_CBC */
  22913. const pkcs7EncryptedVector testVectors[] =
  22914. {
  22915. #ifndef NO_DES3
  22916. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  22917. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  22918. #endif /* !NO_DES3 */
  22919. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22920. #ifndef NO_AES_128
  22921. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  22922. sizeof(aes128Key)},
  22923. #endif
  22924. #ifndef NO_AES_192
  22925. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  22926. sizeof(aes192Key)},
  22927. #endif
  22928. #ifndef NO_AES_256
  22929. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  22930. sizeof(aes256Key)},
  22931. #endif
  22932. #endif /* !NO_AES && HAVE_AES_CBC */
  22933. };
  22934. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  22935. for (i = 0; i < testSz; i++) {
  22936. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22937. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22938. if (pkcs7 != NULL) {
  22939. pkcs7->content = (byte*)testVectors[i].content;
  22940. pkcs7->contentSz = testVectors[i].contentSz;
  22941. pkcs7->contentOID = testVectors[i].contentOID;
  22942. pkcs7->encryptOID = testVectors[i].encryptOID;
  22943. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  22944. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  22945. pkcs7->heap = HEAP_HINT;
  22946. }
  22947. /* encode encryptedData */
  22948. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22949. sizeof(encrypted)), 0);
  22950. /* Decode encryptedData */
  22951. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  22952. encryptedSz, decoded, sizeof(decoded)), 0);
  22953. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  22954. /* Keep values for last itr. */
  22955. if (i < testSz - 1) {
  22956. wc_PKCS7_Free(pkcs7);
  22957. pkcs7 = NULL;
  22958. }
  22959. }
  22960. if (pkcs7 == NULL || testSz == 0) {
  22961. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22962. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22963. }
  22964. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  22965. sizeof(encrypted)),BAD_FUNC_ARG);
  22966. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  22967. sizeof(encrypted)), BAD_FUNC_ARG);
  22968. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22969. 0), BAD_FUNC_ARG);
  22970. /* Testing the struct. */
  22971. if (pkcs7 != NULL) {
  22972. tmpBytePtr = pkcs7->content;
  22973. pkcs7->content = NULL;
  22974. }
  22975. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22976. sizeof(encrypted)), BAD_FUNC_ARG);
  22977. if (pkcs7 != NULL) {
  22978. pkcs7->content = tmpBytePtr;
  22979. tmpWrd32 = pkcs7->contentSz;
  22980. pkcs7->contentSz = 0;
  22981. }
  22982. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22983. sizeof(encrypted)), BAD_FUNC_ARG);
  22984. if (pkcs7 != NULL) {
  22985. pkcs7->contentSz = tmpWrd32;
  22986. tmpInt = pkcs7->encryptOID;
  22987. pkcs7->encryptOID = 0;
  22988. }
  22989. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22990. sizeof(encrypted)), BAD_FUNC_ARG);
  22991. if (pkcs7 != NULL) {
  22992. pkcs7->encryptOID = tmpInt;
  22993. tmpBytePtr = pkcs7->encryptionKey;
  22994. pkcs7->encryptionKey = NULL;
  22995. }
  22996. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22997. sizeof(encrypted)), BAD_FUNC_ARG);
  22998. if (pkcs7 != NULL) {
  22999. pkcs7->encryptionKey = tmpBytePtr;
  23000. tmpWrd32 = pkcs7->encryptionKeySz;
  23001. pkcs7->encryptionKeySz = 0;
  23002. }
  23003. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  23004. sizeof(encrypted)), BAD_FUNC_ARG);
  23005. if (pkcs7 != NULL) {
  23006. pkcs7->encryptionKeySz = tmpWrd32;
  23007. }
  23008. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  23009. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23010. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  23011. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23012. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  23013. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23014. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23015. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  23016. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23017. decoded, 0), BAD_FUNC_ARG);
  23018. /* Test struct fields */
  23019. if (pkcs7 != NULL) {
  23020. tmpBytePtr = pkcs7->encryptionKey;
  23021. pkcs7->encryptionKey = NULL;
  23022. }
  23023. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23024. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23025. if (pkcs7 != NULL) {
  23026. pkcs7->encryptionKey = tmpBytePtr;
  23027. pkcs7->encryptionKeySz = 0;
  23028. }
  23029. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  23030. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  23031. wc_PKCS7_Free(pkcs7);
  23032. #endif
  23033. return EXPECT_RESULT();
  23034. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  23035. /*
  23036. * Testing wc_PKCS7_Degenerate()
  23037. */
  23038. static int test_wc_PKCS7_Degenerate(void)
  23039. {
  23040. EXPECT_DECLS;
  23041. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  23042. PKCS7* pkcs7 = NULL;
  23043. char fName[] = "./certs/test-degenerate.p7b";
  23044. XFILE f = XBADFILE;
  23045. byte der[4096];
  23046. word32 derSz = 0;
  23047. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  23048. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23049. if (f != XBADFILE)
  23050. XFCLOSE(f);
  23051. /* test degenerate success */
  23052. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23053. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23054. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23055. #ifndef NO_RSA
  23056. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23057. #else
  23058. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23059. #endif
  23060. wc_PKCS7_Free(pkcs7);
  23061. pkcs7 = NULL;
  23062. /* test with turning off degenerate cases */
  23063. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23064. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23065. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23066. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  23067. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  23068. PKCS7_NO_SIGNER_E);
  23069. wc_PKCS7_Free(pkcs7);
  23070. #endif
  23071. return EXPECT_RESULT();
  23072. } /* END test_wc_PKCS7_Degenerate() */
  23073. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23074. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  23075. static byte berContent[] = {
  23076. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23077. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  23078. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  23079. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  23080. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  23081. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  23082. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  23083. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  23084. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  23085. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  23086. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  23087. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  23088. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  23089. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  23090. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  23091. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  23092. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  23093. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  23094. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23095. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23096. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  23097. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23098. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23099. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  23100. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  23101. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  23102. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  23103. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  23104. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  23105. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  23106. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  23107. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  23108. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  23109. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  23110. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  23111. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  23112. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  23113. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  23114. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  23115. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  23116. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  23117. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  23118. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  23119. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  23120. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23121. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  23122. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  23123. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  23124. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  23125. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  23126. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  23127. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  23128. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  23129. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  23130. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  23131. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  23132. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  23133. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  23134. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  23135. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  23136. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  23137. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  23138. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  23139. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  23140. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  23141. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  23142. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  23143. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  23144. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  23145. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  23146. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  23147. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  23148. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  23149. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  23150. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  23151. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  23152. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  23153. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  23154. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  23155. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  23156. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  23157. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  23158. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  23159. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  23160. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  23161. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  23162. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  23163. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  23164. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  23165. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  23166. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  23167. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  23168. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  23169. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  23170. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  23171. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  23172. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  23173. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  23174. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  23175. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  23176. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  23177. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  23178. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  23179. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  23180. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  23181. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  23182. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  23183. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  23184. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  23185. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  23186. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  23187. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  23188. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  23189. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  23190. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  23191. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  23192. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  23193. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  23194. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  23195. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  23196. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  23197. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  23198. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  23199. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  23200. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  23201. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  23202. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  23203. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  23204. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  23205. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  23206. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  23207. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  23208. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  23209. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  23210. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  23211. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  23212. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  23213. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  23214. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  23215. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  23216. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  23217. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  23218. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  23219. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  23220. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  23221. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  23222. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  23223. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  23224. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  23225. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  23226. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  23227. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  23228. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  23229. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  23230. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  23231. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  23232. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  23233. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  23234. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  23235. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  23236. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  23237. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  23238. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  23239. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  23240. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  23241. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  23242. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  23243. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  23244. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  23245. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  23246. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  23247. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  23248. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  23249. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  23250. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  23251. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  23252. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  23253. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  23254. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  23255. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  23256. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  23257. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  23258. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  23259. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  23260. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  23261. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  23262. 0x00, 0x00, 0x00, 0x00, 0x00
  23263. };
  23264. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  23265. * !NO_DES3 && !NO_SHA
  23266. */
  23267. /*
  23268. * Testing wc_PKCS7_BER()
  23269. */
  23270. static int test_wc_PKCS7_BER(void)
  23271. {
  23272. EXPECT_DECLS;
  23273. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23274. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  23275. PKCS7* pkcs7 = NULL;
  23276. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  23277. XFILE f = XBADFILE;
  23278. byte der[4096];
  23279. #ifndef NO_DES3
  23280. byte decoded[2048];
  23281. #endif
  23282. word32 derSz = 0;
  23283. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  23284. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23285. if (f != XBADFILE) {
  23286. XFCLOSE(f);
  23287. f = XBADFILE;
  23288. }
  23289. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23290. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23291. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23292. #ifndef NO_RSA
  23293. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23294. #else
  23295. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23296. #endif
  23297. wc_PKCS7_Free(pkcs7);
  23298. pkcs7 = NULL;
  23299. #ifndef NO_DES3
  23300. /* decode BER content */
  23301. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  23302. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23303. if (f != XBADFILE) {
  23304. XFCLOSE(f);
  23305. f = XBADFILE;
  23306. }
  23307. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23308. #ifndef NO_RSA
  23309. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23310. #else
  23311. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23312. #endif
  23313. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  23314. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23315. if (f != XBADFILE) {
  23316. XFCLOSE(f);
  23317. f = XBADFILE;
  23318. }
  23319. if (pkcs7 != NULL) {
  23320. pkcs7->privateKey = der;
  23321. pkcs7->privateKeySz = derSz;
  23322. }
  23323. #ifndef NO_RSA
  23324. #ifdef WOLFSSL_SP_MATH
  23325. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23326. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  23327. #else
  23328. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23329. sizeof(berContent), decoded, sizeof(decoded)), 0);
  23330. #endif
  23331. #else
  23332. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23333. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  23334. #endif
  23335. wc_PKCS7_Free(pkcs7);
  23336. #endif /* !NO_DES3 */
  23337. #endif
  23338. return EXPECT_RESULT();
  23339. } /* END test_wc_PKCS7_BER() */
  23340. static int test_wc_PKCS7_signed_enveloped(void)
  23341. {
  23342. EXPECT_DECLS;
  23343. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  23344. !defined(NO_FILESYSTEM)
  23345. XFILE f = XBADFILE;
  23346. PKCS7* pkcs7 = NULL;
  23347. #ifdef HAVE_AES_CBC
  23348. PKCS7* inner = NULL;
  23349. #endif
  23350. void* pt = NULL;
  23351. WC_RNG rng;
  23352. unsigned char key[FOURK_BUF/2];
  23353. unsigned char cert[FOURK_BUF/2];
  23354. unsigned char env[FOURK_BUF];
  23355. int envSz = FOURK_BUF;
  23356. int keySz = 0;
  23357. int certSz = 0;
  23358. unsigned char sig[FOURK_BUF * 2];
  23359. int sigSz = FOURK_BUF * 2;
  23360. #ifdef HAVE_AES_CBC
  23361. unsigned char decoded[FOURK_BUF];
  23362. int decodedSz = FOURK_BUF;
  23363. #endif
  23364. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23365. /* load cert */
  23366. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  23367. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  23368. if (f != XBADFILE) {
  23369. XFCLOSE(f);
  23370. f = XBADFILE;
  23371. }
  23372. /* load key */
  23373. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  23374. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  23375. if (f != XBADFILE) {
  23376. XFCLOSE(f);
  23377. f = XBADFILE;
  23378. }
  23379. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  23380. /* sign cert for envelope */
  23381. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23382. ExpectIntEQ(wc_InitRng(&rng), 0);
  23383. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23384. if (pkcs7 != NULL) {
  23385. pkcs7->content = cert;
  23386. pkcs7->contentSz = certSz;
  23387. pkcs7->contentOID = DATA;
  23388. pkcs7->privateKey = key;
  23389. pkcs7->privateKeySz = keySz;
  23390. pkcs7->encryptOID = RSAk;
  23391. pkcs7->hashOID = SHA256h;
  23392. pkcs7->rng = &rng;
  23393. }
  23394. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23395. wc_PKCS7_Free(pkcs7);
  23396. pkcs7 = NULL;
  23397. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23398. #ifdef HAVE_AES_CBC
  23399. /* create envelope */
  23400. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23401. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23402. if (pkcs7 != NULL) {
  23403. pkcs7->content = sig;
  23404. pkcs7->contentSz = sigSz;
  23405. pkcs7->contentOID = DATA;
  23406. pkcs7->encryptOID = AES256CBCb;
  23407. pkcs7->privateKey = key;
  23408. pkcs7->privateKeySz = keySz;
  23409. }
  23410. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  23411. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  23412. wc_PKCS7_Free(pkcs7);
  23413. pkcs7 = NULL;
  23414. #endif
  23415. /* create bad signed enveloped data */
  23416. sigSz = FOURK_BUF * 2;
  23417. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23418. ExpectIntEQ(wc_InitRng(&rng), 0);
  23419. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23420. if (pkcs7 != NULL) {
  23421. pkcs7->content = env;
  23422. pkcs7->contentSz = envSz;
  23423. pkcs7->contentOID = DATA;
  23424. pkcs7->privateKey = key;
  23425. pkcs7->privateKeySz = keySz;
  23426. pkcs7->encryptOID = RSAk;
  23427. pkcs7->hashOID = SHA256h;
  23428. pkcs7->rng = &rng;
  23429. }
  23430. /* Set no certs in bundle for this test. Hang on to the pointer though to
  23431. * free it later. */
  23432. if (pkcs7 != NULL) {
  23433. pt = (void*)pkcs7->certList;
  23434. pkcs7->certList = NULL; /* no certs in bundle */
  23435. }
  23436. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23437. if (pkcs7 != NULL) {
  23438. /* restore pointer for PKCS7 free call */
  23439. pkcs7->certList = (Pkcs7Cert*)pt;
  23440. }
  23441. wc_PKCS7_Free(pkcs7);
  23442. pkcs7 = NULL;
  23443. /* check verify fails */
  23444. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23445. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23446. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  23447. PKCS7_SIGNEEDS_CHECK);
  23448. /* try verifying the signature manually */
  23449. {
  23450. RsaKey rKey;
  23451. word32 idx = 0;
  23452. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  23453. WC_MAX_DIGEST_SIZE];
  23454. int digestSz;
  23455. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  23456. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  23457. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  23458. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  23459. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  23460. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  23461. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  23462. /* verify was success */
  23463. }
  23464. wc_PKCS7_Free(pkcs7);
  23465. pkcs7 = NULL;
  23466. /* initializing the PKCS7 struct with the signing certificate should pass */
  23467. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23468. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23469. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23470. wc_PKCS7_Free(pkcs7);
  23471. pkcs7 = NULL;
  23472. /* create valid degenerate bundle */
  23473. sigSz = FOURK_BUF * 2;
  23474. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23475. if (pkcs7 != NULL) {
  23476. pkcs7->content = env;
  23477. pkcs7->contentSz = envSz;
  23478. pkcs7->contentOID = DATA;
  23479. pkcs7->privateKey = key;
  23480. pkcs7->privateKeySz = keySz;
  23481. pkcs7->encryptOID = RSAk;
  23482. pkcs7->hashOID = SHA256h;
  23483. pkcs7->rng = &rng;
  23484. }
  23485. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  23486. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23487. wc_PKCS7_Free(pkcs7);
  23488. pkcs7 = NULL;
  23489. wc_FreeRng(&rng);
  23490. /* check verify */
  23491. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23492. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  23493. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23494. ExpectNotNull(pkcs7->content);
  23495. #ifdef HAVE_AES_CBC
  23496. /* check decode */
  23497. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  23498. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  23499. if (inner != NULL) {
  23500. inner->privateKey = key;
  23501. inner->privateKeySz = keySz;
  23502. }
  23503. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  23504. pkcs7->contentSz, decoded, decodedSz)), 0);
  23505. wc_PKCS7_Free(inner);
  23506. inner = NULL;
  23507. #endif
  23508. wc_PKCS7_Free(pkcs7);
  23509. pkcs7 = NULL;
  23510. #ifdef HAVE_AES_CBC
  23511. /* check cert set */
  23512. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23513. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23514. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  23515. ExpectNotNull(pkcs7->singleCert);
  23516. ExpectIntNE(pkcs7->singleCertSz, 0);
  23517. wc_PKCS7_Free(pkcs7);
  23518. pkcs7 = NULL;
  23519. #endif
  23520. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  23521. return EXPECT_RESULT();
  23522. }
  23523. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  23524. {
  23525. EXPECT_DECLS;
  23526. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23527. && !defined(NO_AES)
  23528. PKCS7* pkcs7 = NULL;
  23529. void* heap = NULL;
  23530. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23531. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23532. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  23533. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  23534. wc_PKCS7_Free(pkcs7);
  23535. #endif
  23536. return EXPECT_RESULT();
  23537. }
  23538. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  23539. {
  23540. EXPECT_DECLS;
  23541. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23542. && !defined(NO_AES)
  23543. PKCS7* pkcs7 = NULL;
  23544. void* heap = NULL;
  23545. WOLFSSL_CTX* ctx = NULL;
  23546. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23547. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23548. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23549. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  23550. wc_PKCS7_Free(pkcs7);
  23551. #endif
  23552. return EXPECT_RESULT();
  23553. }
  23554. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  23555. {
  23556. EXPECT_DECLS;
  23557. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23558. && !defined(NO_AES)
  23559. PKCS7* pkcs7 = NULL;
  23560. void* heap = NULL;
  23561. WOLFSSL_CTX* ctx = NULL;
  23562. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23563. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23564. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23565. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  23566. wc_PKCS7_Free(pkcs7);
  23567. #endif
  23568. return EXPECT_RESULT();
  23569. }
  23570. static int test_wc_PKCS7_DecodeCompressedData(void)
  23571. {
  23572. EXPECT_DECLS;
  23573. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23574. && !defined(NO_AES) && defined(HAVE_LIBZ)
  23575. PKCS7* pkcs7 = NULL;
  23576. void* heap = NULL;
  23577. byte out[4096];
  23578. byte* decompressed = NULL;
  23579. int outSz;
  23580. int decompressedSz;
  23581. const char* cert = "./certs/client-cert.pem";
  23582. byte* cert_buf = NULL;
  23583. size_t cert_sz = 0;
  23584. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  23585. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  23586. DYNAMIC_TYPE_TMP_BUFFER)));
  23587. decompressedSz = (int)cert_sz;
  23588. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  23589. if (pkcs7 != NULL) {
  23590. pkcs7->content = (byte*)cert_buf;
  23591. pkcs7->contentSz = (word32)cert_sz;
  23592. pkcs7->contentOID = DATA;
  23593. }
  23594. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  23595. sizeof(out))), 0);
  23596. wc_PKCS7_Free(pkcs7);
  23597. pkcs7 = NULL;
  23598. /* compressed key should be smaller than when started */
  23599. ExpectIntLT(outSz, cert_sz);
  23600. /* test decompression */
  23601. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  23602. ExpectIntEQ(pkcs7->contentOID, 0);
  23603. /* fail case with out buffer too small */
  23604. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23605. decompressed, outSz), 0);
  23606. /* success case */
  23607. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23608. decompressed, decompressedSz), cert_sz);
  23609. ExpectIntEQ(pkcs7->contentOID, DATA);
  23610. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23611. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23612. decompressed = NULL;
  23613. /* test decompression function with different 'max' inputs */
  23614. outSz = sizeof(out);
  23615. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  23616. 0);
  23617. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  23618. out, outSz, 0, heap), 0);
  23619. ExpectNull(decompressed);
  23620. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  23621. out, outSz, 0, heap), 0);
  23622. ExpectNotNull(decompressed);
  23623. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23624. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23625. decompressed = NULL;
  23626. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  23627. out, outSz, 0, heap), 0);
  23628. ExpectNotNull(decompressed);
  23629. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23630. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23631. if (cert_buf != NULL)
  23632. free(cert_buf);
  23633. wc_PKCS7_Free(pkcs7);
  23634. #endif
  23635. return EXPECT_RESULT();
  23636. }
  23637. static int test_wc_i2d_PKCS12(void)
  23638. {
  23639. EXPECT_DECLS;
  23640. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  23641. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23642. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  23643. WC_PKCS12* pkcs12 = NULL;
  23644. unsigned char der[FOURK_BUF * 2];
  23645. unsigned char* pt;
  23646. int derSz = 0;
  23647. unsigned char out[FOURK_BUF * 2];
  23648. int outSz = FOURK_BUF * 2;
  23649. const char p12_f[] = "./certs/test-servercert.p12";
  23650. XFILE f = XBADFILE;
  23651. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  23652. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  23653. if (f != XBADFILE)
  23654. XFCLOSE(f);
  23655. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  23656. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  23657. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23658. ExpectIntEQ(outSz, derSz);
  23659. outSz = derSz - 1;
  23660. pt = out;
  23661. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  23662. outSz = derSz;
  23663. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  23664. ExpectIntEQ((pt == out), 0);
  23665. pt = NULL;
  23666. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  23667. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  23668. wc_PKCS12_free(pkcs12);
  23669. pkcs12 = NULL;
  23670. /* Run the same test but use wc_d2i_PKCS12_fp. */
  23671. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  23672. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  23673. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23674. ExpectIntEQ(outSz, derSz);
  23675. wc_PKCS12_free(pkcs12);
  23676. pkcs12 = NULL;
  23677. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  23678. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  23679. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23680. ExpectIntEQ(outSz, derSz);
  23681. wc_PKCS12_free(pkcs12);
  23682. pkcs12 = NULL;
  23683. #endif
  23684. return EXPECT_RESULT();
  23685. }
  23686. /* Testing wc_SignatureGetSize() for signature type ECC */
  23687. static int test_wc_SignatureGetSize_ecc(void)
  23688. {
  23689. EXPECT_DECLS;
  23690. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  23691. enum wc_SignatureType sig_type;
  23692. word32 key_len;
  23693. ecc_key ecc;
  23694. const char* qx =
  23695. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  23696. const char* qy =
  23697. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  23698. const char* d =
  23699. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  23700. XMEMSET(&ecc, 0, sizeof(ecc_key));
  23701. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  23702. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  23703. /* Input for signature type ECC */
  23704. sig_type = WC_SIGNATURE_TYPE_ECC;
  23705. key_len = sizeof(ecc_key);
  23706. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  23707. /* Test bad args */
  23708. sig_type = (enum wc_SignatureType) 100;
  23709. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  23710. sig_type = WC_SIGNATURE_TYPE_ECC;
  23711. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  23712. key_len = (word32)0;
  23713. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  23714. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  23715. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  23716. return EXPECT_RESULT();
  23717. } /* END test_wc_SignatureGetSize_ecc() */
  23718. /* Testing wc_SignatureGetSize() for signature type rsa */
  23719. static int test_wc_SignatureGetSize_rsa(void)
  23720. {
  23721. EXPECT_DECLS;
  23722. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  23723. enum wc_SignatureType sig_type;
  23724. word32 key_len;
  23725. word32 idx = 0;
  23726. RsaKey rsa_key;
  23727. byte* tmp = NULL;
  23728. size_t bytes;
  23729. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  23730. #ifdef USE_CERT_BUFFERS_1024
  23731. bytes = (size_t)sizeof_client_key_der_1024;
  23732. if (bytes < (size_t)sizeof_client_key_der_1024)
  23733. bytes = (size_t)sizeof_client_cert_der_1024;
  23734. #elif defined(USE_CERT_BUFFERS_2048)
  23735. bytes = (size_t)sizeof_client_key_der_2048;
  23736. if (bytes < (size_t)sizeof_client_cert_der_2048)
  23737. bytes = (size_t)sizeof_client_cert_der_2048;
  23738. #else
  23739. bytes = FOURK_BUF;
  23740. #endif
  23741. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  23742. DYNAMIC_TYPE_TMP_BUFFER));
  23743. if (tmp != NULL) {
  23744. #ifdef USE_CERT_BUFFERS_1024
  23745. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  23746. #elif defined(USE_CERT_BUFFERS_2048)
  23747. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  23748. #elif !defined(NO_FILESYSTEM)
  23749. XFILE file = XBADFILE;
  23750. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  23751. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  23752. if (file != XBADFILE)
  23753. XFCLOSE(file);
  23754. }
  23755. #else
  23756. ExpectFail();
  23757. #endif
  23758. }
  23759. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  23760. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  23761. /* Input for signature type RSA */
  23762. sig_type = WC_SIGNATURE_TYPE_RSA;
  23763. key_len = sizeof(RsaKey);
  23764. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  23765. /* Test bad args */
  23766. sig_type = (enum wc_SignatureType)100;
  23767. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  23768. sig_type = WC_SIGNATURE_TYPE_RSA;
  23769. #ifndef HAVE_USER_RSA
  23770. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  23771. #else
  23772. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  23773. #endif
  23774. key_len = (word32)0;
  23775. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  23776. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  23777. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23778. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  23779. return EXPECT_RESULT();
  23780. } /* END test_wc_SignatureGetSize_rsa(void) */
  23781. /*----------------------------------------------------------------------------*
  23782. | hash.h Tests
  23783. *----------------------------------------------------------------------------*/
  23784. static int test_wc_HashInit(void)
  23785. {
  23786. EXPECT_DECLS;
  23787. int i; /* 0 indicates tests passed, 1 indicates failure */
  23788. wc_HashAlg hash;
  23789. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23790. enum wc_HashType enumArray[] = {
  23791. #ifndef NO_MD5
  23792. WC_HASH_TYPE_MD5,
  23793. #endif
  23794. #ifndef NO_SHA
  23795. WC_HASH_TYPE_SHA,
  23796. #endif
  23797. #ifdef WOLFSSL_SHA224
  23798. WC_HASH_TYPE_SHA224,
  23799. #endif
  23800. #ifndef NO_SHA256
  23801. WC_HASH_TYPE_SHA256,
  23802. #endif
  23803. #ifdef WOLFSSL_SHA384
  23804. WC_HASH_TYPE_SHA384,
  23805. #endif
  23806. #ifdef WOLFSSL_SHA512
  23807. WC_HASH_TYPE_SHA512,
  23808. #endif
  23809. };
  23810. /* dynamically finds the length */
  23811. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23812. /* For loop to test various arguments... */
  23813. for (i = 0; i < enumlen; i++) {
  23814. /* check for bad args */
  23815. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23816. wc_HashFree(&hash, enumArray[i]);
  23817. /* check for null ptr */
  23818. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  23819. } /* end of for loop */
  23820. return EXPECT_RESULT();
  23821. } /* end of test_wc_HashInit */
  23822. /*
  23823. * Unit test function for wc_HashSetFlags()
  23824. */
  23825. static int test_wc_HashSetFlags(void)
  23826. {
  23827. EXPECT_DECLS;
  23828. #ifdef WOLFSSL_HASH_FLAGS
  23829. wc_HashAlg hash;
  23830. word32 flags = 0;
  23831. int i, j;
  23832. int notSupportedLen;
  23833. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23834. enum wc_HashType enumArray[] = {
  23835. #ifndef NO_MD5
  23836. WC_HASH_TYPE_MD5,
  23837. #endif
  23838. #ifndef NO_SHA
  23839. WC_HASH_TYPE_SHA,
  23840. #endif
  23841. #ifdef WOLFSSL_SHA224
  23842. WC_HASH_TYPE_SHA224,
  23843. #endif
  23844. #ifndef NO_SHA256
  23845. WC_HASH_TYPE_SHA256,
  23846. #endif
  23847. #ifdef WOLFSSL_SHA384
  23848. WC_HASH_TYPE_SHA384,
  23849. #endif
  23850. #ifdef WOLFSSL_SHA512
  23851. WC_HASH_TYPE_SHA512,
  23852. #endif
  23853. #ifdef WOLFSSL_SHA3
  23854. WC_HASH_TYPE_SHA3_224,
  23855. #endif
  23856. };
  23857. enum wc_HashType notSupported[] = {
  23858. WC_HASH_TYPE_MD5_SHA,
  23859. WC_HASH_TYPE_MD2,
  23860. WC_HASH_TYPE_MD4,
  23861. WC_HASH_TYPE_BLAKE2B,
  23862. WC_HASH_TYPE_BLAKE2S,
  23863. WC_HASH_TYPE_NONE,
  23864. };
  23865. /* dynamically finds the length */
  23866. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23867. /* For loop to test various arguments... */
  23868. for (i = 0; i < enumlen; i++) {
  23869. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23870. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  23871. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  23872. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  23873. wc_HashFree(&hash, enumArray[i]);
  23874. }
  23875. /* For loop to test not supported cases */
  23876. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23877. for (j = 0; j < notSupportedLen; j++) {
  23878. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  23879. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  23880. BAD_FUNC_ARG);
  23881. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  23882. }
  23883. #endif
  23884. return EXPECT_RESULT();
  23885. } /* END test_wc_HashSetFlags */
  23886. /*
  23887. * Unit test function for wc_HashGetFlags()
  23888. */
  23889. static int test_wc_HashGetFlags(void)
  23890. {
  23891. EXPECT_DECLS;
  23892. #ifdef WOLFSSL_HASH_FLAGS
  23893. wc_HashAlg hash;
  23894. word32 flags = 0;
  23895. int i, j;
  23896. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23897. enum wc_HashType enumArray[] = {
  23898. #ifndef NO_MD5
  23899. WC_HASH_TYPE_MD5,
  23900. #endif
  23901. #ifndef NO_SHA
  23902. WC_HASH_TYPE_SHA,
  23903. #endif
  23904. #ifdef WOLFSSL_SHA224
  23905. WC_HASH_TYPE_SHA224,
  23906. #endif
  23907. #ifndef NO_SHA256
  23908. WC_HASH_TYPE_SHA256,
  23909. #endif
  23910. #ifdef WOLFSSL_SHA384
  23911. WC_HASH_TYPE_SHA384,
  23912. #endif
  23913. #ifdef WOLFSSL_SHA512
  23914. WC_HASH_TYPE_SHA512,
  23915. #endif
  23916. #ifdef WOLFSSL_SHA3
  23917. WC_HASH_TYPE_SHA3_224,
  23918. #endif
  23919. };
  23920. enum wc_HashType notSupported[] = {
  23921. WC_HASH_TYPE_MD5_SHA,
  23922. WC_HASH_TYPE_MD2,
  23923. WC_HASH_TYPE_MD4,
  23924. WC_HASH_TYPE_BLAKE2B,
  23925. WC_HASH_TYPE_BLAKE2S,
  23926. WC_HASH_TYPE_NONE,
  23927. };
  23928. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23929. int notSupportedLen;
  23930. /* For loop to test various arguments... */
  23931. for (i = 0; i < enumlen; i++) {
  23932. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23933. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  23934. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  23935. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  23936. wc_HashFree(&hash, enumArray[i]);
  23937. }
  23938. /* For loop to test not supported cases */
  23939. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23940. for (j = 0; j < notSupportedLen; j++) {
  23941. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  23942. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  23943. BAD_FUNC_ARG);
  23944. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  23945. }
  23946. #endif
  23947. return EXPECT_RESULT();
  23948. } /* END test_wc_HashGetFlags */
  23949. /*----------------------------------------------------------------------------*
  23950. | Compatibility Tests
  23951. *----------------------------------------------------------------------------*/
  23952. /*----------------------------------------------------------------------------*
  23953. | ASN.1 Tests
  23954. *----------------------------------------------------------------------------*/
  23955. static int test_wolfSSL_ASN1_BIT_STRING(void)
  23956. {
  23957. EXPECT_DECLS;
  23958. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  23959. ASN1_BIT_STRING* str = NULL;
  23960. ExpectNotNull(str = ASN1_BIT_STRING_new());
  23961. /* Empty data testing. */
  23962. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  23963. ASN1_BIT_STRING_free(str);
  23964. str = NULL;
  23965. ExpectNotNull(str = ASN1_BIT_STRING_new());
  23966. /* Invalid parameter testing. */
  23967. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  23968. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  23969. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  23970. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  23971. /* No bit string - bit is always 0. */
  23972. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  23973. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  23974. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  23975. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  23976. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  23977. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  23978. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  23979. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  23980. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  23981. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  23982. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  23983. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  23984. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  23985. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  23986. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  23987. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  23988. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  23989. ASN1_BIT_STRING_free(str);
  23990. ASN1_BIT_STRING_free(NULL);
  23991. #endif
  23992. return EXPECT_RESULT();
  23993. }
  23994. static int test_wolfSSL_ASN1_INTEGER(void)
  23995. {
  23996. EXPECT_DECLS;
  23997. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  23998. ASN1_INTEGER* a = NULL;
  23999. ASN1_INTEGER* dup = NULL;
  24000. const unsigned char invalidLenDer[] = {
  24001. 0x02, 0x20, 0x00
  24002. };
  24003. const unsigned char longDer[] = {
  24004. 0x02, 0x20,
  24005. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24006. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24007. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  24008. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  24009. };
  24010. const unsigned char* p;
  24011. /* Invalid parameter testing. */
  24012. ASN1_INTEGER_free(NULL);
  24013. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  24014. ExpectNotNull(a = ASN1_INTEGER_new());
  24015. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  24016. ASN1_INTEGER_free(dup);
  24017. dup = NULL;
  24018. ASN1_INTEGER_free(a);
  24019. a = NULL;
  24020. p = longDer;
  24021. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  24022. p = longDer;
  24023. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  24024. ExpectPtrNE(p, longDer);
  24025. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  24026. ASN1_INTEGER_free(dup);
  24027. ASN1_INTEGER_free(a);
  24028. #endif
  24029. return EXPECT_RESULT();
  24030. }
  24031. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  24032. {
  24033. EXPECT_DECLS;
  24034. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24035. ASN1_INTEGER* a = NULL;
  24036. ASN1_INTEGER* b = NULL;
  24037. ExpectNotNull(a = ASN1_INTEGER_new());
  24038. ExpectNotNull(b = ASN1_INTEGER_new());
  24039. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  24040. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  24041. /* Invalid parameter testing. */
  24042. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  24043. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  24044. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  24045. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24046. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  24047. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24048. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  24049. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24050. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  24051. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24052. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  24053. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  24054. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24055. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  24056. ASN1_INTEGER_free(b);
  24057. ASN1_INTEGER_free(a);
  24058. #endif
  24059. return EXPECT_RESULT();
  24060. }
  24061. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  24062. {
  24063. EXPECT_DECLS;
  24064. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24065. ASN1_INTEGER* ai = NULL;
  24066. ASN1_INTEGER* ai2 = NULL;
  24067. BIGNUM* bn = NULL;
  24068. BIGNUM* bn2 = NULL;
  24069. ExpectNotNull(ai = ASN1_INTEGER_new());
  24070. ExpectNotNull(bn2 = BN_new());
  24071. /* Invalid parameter testing. */
  24072. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  24073. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  24074. /* at the moment hard setting since no set function */
  24075. if (ai != NULL) {
  24076. ai->data[0] = 0xff; /* No DER encoding. */
  24077. ai->length = 1;
  24078. }
  24079. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24080. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24081. BN_free(bn);
  24082. bn = NULL;
  24083. #else
  24084. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  24085. #endif
  24086. if (ai != NULL) {
  24087. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24088. ai->data[1] = 0x04; /* bad length of integer */
  24089. ai->data[2] = 0x03;
  24090. ai->length = 3;
  24091. }
  24092. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24093. /* Interpreted as a number 0x020403. */
  24094. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24095. BN_free(bn);
  24096. bn = NULL;
  24097. #else
  24098. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  24099. #endif
  24100. if (ai != NULL) {
  24101. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24102. ai->data[1] = 0x01; /* length of integer */
  24103. ai->data[2] = 0x03;
  24104. ai->length = 3;
  24105. }
  24106. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24107. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  24108. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24109. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24110. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24111. if (ai != NULL) {
  24112. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24113. ai->data[1] = 0x02; /* length of integer */
  24114. ai->data[2] = 0x00; /* padding byte to ensure positive */
  24115. ai->data[3] = 0xff;
  24116. ai->length = 4;
  24117. }
  24118. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24119. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24120. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24121. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24122. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24123. if (ai != NULL) {
  24124. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24125. ai->data[1] = 0x01; /* length of integer */
  24126. ai->data[2] = 0x00;
  24127. ai->length = 3;
  24128. }
  24129. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24130. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24131. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24132. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24133. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24134. if (ai != NULL) {
  24135. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24136. ai->data[1] = 0x01; /* length of integer */
  24137. ai->data[2] = 0x01;
  24138. ai->length = 3;
  24139. ai->negative = 1;
  24140. }
  24141. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24142. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24143. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24144. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24145. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24146. BN_free(bn2);
  24147. BN_free(bn);
  24148. ASN1_INTEGER_free(ai2);
  24149. ASN1_INTEGER_free(ai);
  24150. #endif
  24151. return EXPECT_RESULT();
  24152. }
  24153. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  24154. {
  24155. EXPECT_DECLS;
  24156. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24157. ASN1_INTEGER *a = NULL;
  24158. long val;
  24159. ExpectNotNull(a = ASN1_INTEGER_new());
  24160. /* Invalid parameter testing. */
  24161. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  24162. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24163. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  24164. #else
  24165. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  24166. #endif
  24167. ASN1_INTEGER_free(a);
  24168. a = NULL;
  24169. ExpectNotNull(a = ASN1_INTEGER_new());
  24170. val = 0;
  24171. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  24172. ASN1_INTEGER_free(a);
  24173. a = NULL;
  24174. /* 0 */
  24175. ExpectNotNull(a = ASN1_INTEGER_new());
  24176. val = 0;
  24177. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24178. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24179. ASN1_INTEGER_free(a);
  24180. a = NULL;
  24181. /* 40 */
  24182. ExpectNotNull(a = ASN1_INTEGER_new());
  24183. val = 40;
  24184. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24185. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24186. ASN1_INTEGER_free(a);
  24187. a = NULL;
  24188. /* -40 */
  24189. ExpectNotNull(a = ASN1_INTEGER_new());
  24190. val = -40;
  24191. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24192. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24193. ASN1_INTEGER_free(a);
  24194. a = NULL;
  24195. /* 128 */
  24196. ExpectNotNull(a = ASN1_INTEGER_new());
  24197. val = 128;
  24198. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24199. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24200. ASN1_INTEGER_free(a);
  24201. a = NULL;
  24202. /* -128 */
  24203. ExpectNotNull(a = ASN1_INTEGER_new());
  24204. val = -128;
  24205. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24206. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24207. ASN1_INTEGER_free(a);
  24208. a = NULL;
  24209. /* 200 */
  24210. ExpectNotNull(a = ASN1_INTEGER_new());
  24211. val = 200;
  24212. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24213. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24214. ASN1_INTEGER_free(a);
  24215. a = NULL;
  24216. /* int max (2147483647) */
  24217. ExpectNotNull(a = ASN1_INTEGER_new());
  24218. val = 2147483647;
  24219. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24220. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24221. ASN1_INTEGER_free(a);
  24222. a = NULL;
  24223. /* int min (-2147483648) */
  24224. ExpectNotNull(a = ASN1_INTEGER_new());
  24225. val = -2147483647 - 1;
  24226. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24227. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24228. ASN1_INTEGER_free(a);
  24229. a = NULL;
  24230. /* long max positive */
  24231. ExpectNotNull(a = ASN1_INTEGER_new());
  24232. val = (long)(((unsigned long)-1) >> 1);
  24233. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24234. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24235. ASN1_INTEGER_free(a);
  24236. #endif
  24237. return EXPECT_RESULT();
  24238. }
  24239. #if defined(OPENSSL_EXTRA)
  24240. typedef struct ASN1IntTestVector {
  24241. const byte* der;
  24242. const size_t derSz;
  24243. const long value;
  24244. } ASN1IntTestVector;
  24245. #endif
  24246. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  24247. {
  24248. EXPECT_DECLS;
  24249. #if defined(OPENSSL_EXTRA)
  24250. size_t i;
  24251. WOLFSSL_ASN1_INTEGER* a = NULL;
  24252. WOLFSSL_ASN1_INTEGER* b = NULL;
  24253. WOLFSSL_ASN1_INTEGER* c = NULL;
  24254. const byte* p = NULL;
  24255. byte* p2 = NULL;
  24256. byte* reEncoded = NULL;
  24257. int reEncodedSz;
  24258. static const byte zeroDer[] = {
  24259. 0x02, 0x01, 0x00
  24260. };
  24261. static const byte oneDer[] = {
  24262. 0x02, 0x01, 0x01
  24263. };
  24264. static const byte negativeDer[] = {
  24265. 0x02, 0x03, 0xC1, 0x16, 0x0D
  24266. };
  24267. static const byte positiveDer[] = {
  24268. 0x02, 0x03, 0x01, 0x00, 0x01
  24269. };
  24270. static const byte primeDer[] = {
  24271. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  24272. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  24273. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  24274. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  24275. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  24276. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  24277. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  24278. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  24279. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  24280. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  24281. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  24282. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  24283. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  24284. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  24285. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  24286. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  24287. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  24288. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  24289. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  24290. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  24291. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  24292. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  24293. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  24294. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  24295. };
  24296. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  24297. static const ASN1IntTestVector testVectors[] = {
  24298. {zeroDer, sizeof(zeroDer), 0},
  24299. {oneDer, sizeof(oneDer), 1},
  24300. {negativeDer, sizeof(negativeDer), -4123123},
  24301. {positiveDer, sizeof(positiveDer), 65537},
  24302. {primeDer, sizeof(primeDer), 0}
  24303. };
  24304. static const size_t NUM_TEST_VECTORS =
  24305. sizeof(testVectors)/sizeof(testVectors[0]);
  24306. /* Check d2i error conditions */
  24307. /* NULL pointer to input. */
  24308. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  24309. ExpectNull(b);
  24310. /* NULL input. */
  24311. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  24312. ExpectNull(b);
  24313. /* 0 length. */
  24314. p = testVectors[0].der;
  24315. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  24316. ExpectNull(b);
  24317. /* Negative length. */
  24318. p = testVectors[0].der;
  24319. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  24320. ExpectNull(b);
  24321. /* Garbage DER input. */
  24322. p = garbageDer;
  24323. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  24324. ExpectNull(b);
  24325. /* Check i2d error conditions */
  24326. /* NULL input. */
  24327. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  24328. /* 0 length input data buffer (a->length == 0). */
  24329. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  24330. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  24331. if (a != NULL)
  24332. a->data = NULL;
  24333. /* NULL input data buffer. */
  24334. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  24335. if (a != NULL) {
  24336. /* Reset a->data. */
  24337. a->data = a->intData;
  24338. }
  24339. /* Set a to valid value. */
  24340. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  24341. /* NULL output buffer. */
  24342. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  24343. wolfSSL_ASN1_INTEGER_free(a);
  24344. a = NULL;
  24345. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  24346. p = testVectors[i].der;
  24347. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  24348. testVectors[i].derSz));
  24349. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24350. if (testVectors[i].derSz <= sizeof(long)) {
  24351. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  24352. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  24353. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  24354. wolfSSL_ASN1_INTEGER_free(c);
  24355. c = NULL;
  24356. }
  24357. /* Convert to DER without a pre-allocated output buffer. */
  24358. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  24359. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  24360. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  24361. /* Convert to DER with a pre-allocated output buffer. In this case, the
  24362. * output buffer pointer should be incremented just past the end of the
  24363. * encoded data. */
  24364. p2 = reEncoded;
  24365. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  24366. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  24367. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  24368. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  24369. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  24370. reEncoded = NULL;
  24371. wolfSSL_ASN1_INTEGER_free(a);
  24372. a = NULL;
  24373. }
  24374. #endif /* OPENSSL_EXTRA */
  24375. return EXPECT_RESULT();
  24376. }
  24377. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  24378. {
  24379. EXPECT_DECLS;
  24380. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  24381. BIO* bio = NULL;
  24382. BIO* out = NULL;
  24383. BIO* fixed = NULL;
  24384. ASN1_INTEGER* ai = NULL;
  24385. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  24386. char tmp[1024];
  24387. int tmpSz;
  24388. const char expected1[] = "123456";
  24389. const char expected2[] = "112345678912345678901234567890";
  24390. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  24391. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  24392. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  24393. ExpectNotNull(ai = ASN1_INTEGER_new());
  24394. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  24395. /* Invalid parameter testing. */
  24396. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  24397. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  24398. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  24399. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  24400. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  24401. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  24402. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  24403. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  24404. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  24405. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  24406. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  24407. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  24408. /* No data to read from BIO. */
  24409. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  24410. /* read first line */
  24411. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24412. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  24413. XMEMSET(tmp, 0, 1024);
  24414. tmpSz = BIO_read(out, tmp, 1024);
  24415. ExpectIntEQ(tmpSz, 6);
  24416. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  24417. /* fail on second line (not % 2) */
  24418. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  24419. /* read 3rd long line */
  24420. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24421. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  24422. XMEMSET(tmp, 0, 1024);
  24423. tmpSz = BIO_read(out, tmp, 1024);
  24424. ExpectIntEQ(tmpSz, 30);
  24425. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  24426. /* fail on empty line */
  24427. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  24428. BIO_free(bio);
  24429. bio = NULL;
  24430. /* Make long integer, requiring dynamic memory, even longer. */
  24431. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  24432. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24433. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  24434. XMEMSET(tmp, 0, 1024);
  24435. tmpSz = BIO_read(out, tmp, 1024);
  24436. ExpectIntEQ(tmpSz, 48);
  24437. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24438. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  24439. XMEMSET(tmp, 0, 1024);
  24440. tmpSz = BIO_read(out, tmp, 1024);
  24441. ExpectIntEQ(tmpSz, 56);
  24442. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  24443. BIO_free(bio);
  24444. BIO_free(out);
  24445. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  24446. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  24447. /* Ensure there is 0 bytes available to write into. */
  24448. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  24449. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  24450. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  24451. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  24452. BIO_free(fixed);
  24453. ASN1_INTEGER_free(ai);
  24454. #endif
  24455. return EXPECT_RESULT();
  24456. }
  24457. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  24458. {
  24459. EXPECT_DECLS;
  24460. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24461. ASN1_INTEGER *a = NULL;
  24462. unsigned char *pp,*tpp;
  24463. int ret;
  24464. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  24465. /* Invalid parameter testing. */
  24466. /* Set pp to an invalid value. */
  24467. pp = NULL;
  24468. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  24469. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  24470. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  24471. /* 40 */
  24472. if (a != NULL) {
  24473. a->intData[0] = ASN_INTEGER;
  24474. a->intData[1] = 1;
  24475. a->intData[2] = 40;
  24476. }
  24477. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24478. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24479. DYNAMIC_TYPE_TMP_BUFFER));
  24480. tpp = pp;
  24481. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24482. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24483. tpp--;
  24484. ExpectIntEQ(*tpp, 40);
  24485. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24486. pp = NULL;
  24487. /* 128 */
  24488. if (a != NULL) {
  24489. a->intData[0] = ASN_INTEGER;
  24490. a->intData[1] = 1;
  24491. a->intData[2] = 128;
  24492. }
  24493. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24494. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24495. DYNAMIC_TYPE_TMP_BUFFER));
  24496. tpp = pp;
  24497. if (tpp != NULL) {
  24498. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24499. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24500. tpp--;
  24501. ExpectIntEQ(*(tpp--), 128);
  24502. ExpectIntEQ(*tpp, 0);
  24503. }
  24504. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24505. pp = NULL;
  24506. /* -40 */
  24507. if (a != NULL) {
  24508. a->intData[0] = ASN_INTEGER;
  24509. a->intData[1] = 1;
  24510. a->intData[2] = 40;
  24511. a->negative = 1;
  24512. }
  24513. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24514. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24515. DYNAMIC_TYPE_TMP_BUFFER));
  24516. tpp = pp;
  24517. if (tpp != NULL) {
  24518. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24519. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24520. tpp--;
  24521. ExpectIntEQ(*tpp, 216);
  24522. }
  24523. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24524. pp = NULL;
  24525. /* -128 */
  24526. if (a != NULL) {
  24527. a->intData[0] = ASN_INTEGER;
  24528. a->intData[1] = 1;
  24529. a->intData[2] = 128;
  24530. a->negative = 1;
  24531. }
  24532. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24533. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24534. DYNAMIC_TYPE_TMP_BUFFER));
  24535. tpp = pp;
  24536. if (tpp != NULL) {
  24537. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24538. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24539. tpp--;
  24540. ExpectIntEQ(*tpp, 128);
  24541. }
  24542. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24543. pp = NULL;
  24544. /* -200 */
  24545. if (a != NULL) {
  24546. a->intData[0] = ASN_INTEGER;
  24547. a->intData[1] = 1;
  24548. a->intData[2] = 200;
  24549. a->negative = 1;
  24550. }
  24551. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24552. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24553. DYNAMIC_TYPE_TMP_BUFFER));
  24554. tpp = pp;
  24555. if (tpp != NULL) {
  24556. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24557. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24558. tpp--;
  24559. ExpectIntEQ(*(tpp--), 56);
  24560. ExpectIntEQ(*tpp, 255);
  24561. }
  24562. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24563. pp = NULL;
  24564. /* Empty */
  24565. if (a != NULL) {
  24566. a->intData[0] = ASN_INTEGER;
  24567. a->intData[1] = 0;
  24568. a->negative = 0;
  24569. }
  24570. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24571. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24572. DYNAMIC_TYPE_TMP_BUFFER));
  24573. tpp = pp;
  24574. if (tpp != NULL) {
  24575. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24576. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24577. tpp--;
  24578. ExpectIntEQ(*tpp, 0);
  24579. }
  24580. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24581. pp = NULL;
  24582. /* 0 */
  24583. if (a != NULL) {
  24584. a->intData[0] = ASN_INTEGER;
  24585. a->intData[1] = 1;
  24586. a->intData[2] = 0;
  24587. a->negative = 1;
  24588. }
  24589. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24590. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24591. DYNAMIC_TYPE_TMP_BUFFER));
  24592. if (tpp != NULL) {
  24593. tpp = pp;
  24594. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24595. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24596. tpp--;
  24597. ExpectIntEQ(*tpp, 0);
  24598. }
  24599. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24600. pp = NULL;
  24601. /* 0x100 */
  24602. if (a != NULL) {
  24603. a->intData[0] = ASN_INTEGER;
  24604. a->intData[1] = 2;
  24605. a->intData[2] = 0x01;
  24606. a->intData[3] = 0x00;
  24607. a->negative = 0;
  24608. }
  24609. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24610. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24611. DYNAMIC_TYPE_TMP_BUFFER));
  24612. if (tpp != NULL) {
  24613. tpp = pp;
  24614. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24615. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24616. tpp -= 2;
  24617. ExpectIntEQ(tpp[0], 0x01);
  24618. ExpectIntEQ(tpp[1], 0x00);
  24619. }
  24620. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24621. pp = NULL;
  24622. /* -0x8000 => 0x8000 */
  24623. if (a != NULL) {
  24624. a->intData[0] = ASN_INTEGER;
  24625. a->intData[1] = 2;
  24626. a->intData[2] = 0x80;
  24627. a->intData[3] = 0x00;
  24628. a->negative = 1;
  24629. }
  24630. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24631. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24632. DYNAMIC_TYPE_TMP_BUFFER));
  24633. tpp = pp;
  24634. if (tpp != NULL) {
  24635. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24636. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24637. tpp -= 2;
  24638. ExpectIntEQ(tpp[0], 0x80);
  24639. ExpectIntEQ(tpp[1], 0x00);
  24640. }
  24641. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24642. pp = NULL;
  24643. /* -0x8001 => 0xFF7FFF */
  24644. if (a != NULL) {
  24645. a->intData[0] = ASN_INTEGER;
  24646. a->intData[1] = 2;
  24647. a->intData[2] = 0x80;
  24648. a->intData[3] = 0x01;
  24649. a->negative = 1;
  24650. }
  24651. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  24652. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24653. DYNAMIC_TYPE_TMP_BUFFER));
  24654. tpp = pp;
  24655. if (tpp != NULL) {
  24656. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24657. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  24658. tpp -= 3;
  24659. ExpectIntEQ(tpp[0], 0xFF);
  24660. ExpectIntEQ(tpp[1], 0x7F);
  24661. ExpectIntEQ(tpp[2], 0xFF);
  24662. }
  24663. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24664. wolfSSL_ASN1_INTEGER_free(a);
  24665. #endif /* OPENSSL_EXTRA && !NO_ASN */
  24666. return EXPECT_RESULT();
  24667. }
  24668. static int test_wolfSSL_ASN1_OBJECT(void)
  24669. {
  24670. EXPECT_DECLS;
  24671. #if defined(OPENSSL_EXTRA)
  24672. ASN1_OBJECT* a = NULL;
  24673. ASN1_OBJECT s;
  24674. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  24675. /* Invalid parameter testing. */
  24676. ASN1_OBJECT_free(NULL);
  24677. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  24678. /* Test that a static ASN1_OBJECT can be freed. */
  24679. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  24680. ASN1_OBJECT_free(&s);
  24681. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  24682. ASN1_OBJECT_free(a);
  24683. a = NULL;
  24684. s.obj = der;
  24685. s.objSz = sizeof(der);
  24686. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  24687. ASN1_OBJECT_free(a);
  24688. ASN1_OBJECT_free(&s);
  24689. #endif /* OPENSSL_EXTRA */
  24690. return EXPECT_RESULT();
  24691. }
  24692. static int test_wolfSSL_ASN1_get_object(void)
  24693. {
  24694. EXPECT_DECLS;
  24695. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  24696. const unsigned char* derBuf = cliecc_cert_der_256;
  24697. const unsigned char* nullPtr = NULL;
  24698. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  24699. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  24700. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  24701. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  24702. const unsigned char* p;
  24703. unsigned char objDer[8];
  24704. unsigned char* der;
  24705. unsigned char* derPtr;
  24706. int len = sizeof_cliecc_cert_der_256;
  24707. long asnLen = 0;
  24708. int tag = 0;
  24709. int cls = 0;
  24710. ASN1_OBJECT* a = NULL;
  24711. ASN1_OBJECT s;
  24712. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  24713. /* Invalid encoding at length. */
  24714. p = objDerInvalidLen;
  24715. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  24716. 0x80);
  24717. p = objDerBadLen;
  24718. /* Error = 0x80, Constructed = 0x20 */
  24719. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  24720. 0x80 | 0x20);
  24721. /* Read a couple TLV triplets and make sure they match the expected values
  24722. */
  24723. /* SEQUENCE */
  24724. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  24725. ExpectIntEQ(asnLen, 862);
  24726. ExpectIntEQ(tag, 0x10);
  24727. ExpectIntEQ(cls, 0);
  24728. /* SEQUENCE */
  24729. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24730. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24731. ExpectIntEQ(asnLen, 772);
  24732. ExpectIntEQ(tag, 0x10);
  24733. ExpectIntEQ(cls, 0);
  24734. /* [0] */
  24735. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24736. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24737. ExpectIntEQ(asnLen, 3);
  24738. ExpectIntEQ(tag, 0);
  24739. ExpectIntEQ(cls, 0x80);
  24740. /* INTEGER */
  24741. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24742. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24743. ExpectIntEQ(asnLen, 1);
  24744. ExpectIntEQ(tag, 0x2);
  24745. ExpectIntEQ(cls, 0);
  24746. derBuf += asnLen;
  24747. /* INTEGER */
  24748. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24749. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24750. ExpectIntEQ(asnLen, 20);
  24751. ExpectIntEQ(tag, 0x2);
  24752. ExpectIntEQ(cls, 0);
  24753. derBuf += asnLen;
  24754. /* SEQUENCE */
  24755. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24756. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24757. ExpectIntEQ(asnLen, 10);
  24758. ExpectIntEQ(tag, 0x10);
  24759. ExpectIntEQ(cls, 0);
  24760. /* Found OBJECT_ID. */
  24761. /* Invalid parameter testing. */
  24762. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  24763. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  24764. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  24765. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  24766. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  24767. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  24768. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  24769. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  24770. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  24771. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  24772. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  24773. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  24774. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  24775. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  24776. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  24777. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  24778. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  24779. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  24780. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  24781. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  24782. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  24783. /* Invalid encoding at length. */
  24784. p = objDerInvalidLen;
  24785. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  24786. p = objDerBadLen;
  24787. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  24788. p = objDerNotObj;
  24789. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  24790. p = objDerNoData;
  24791. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  24792. /* Create an ASN OBJECT from content */
  24793. p = derBuf + 2;
  24794. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  24795. ASN1_OBJECT_free(a);
  24796. a = NULL;
  24797. /* Create an ASN OBJECT from DER */
  24798. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  24799. /* Invalid parameter testing. */
  24800. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  24801. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  24802. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  24803. der = NULL;
  24804. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  24805. derPtr = objDer;
  24806. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  24807. ExpectPtrNE(derPtr, objDer);
  24808. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  24809. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  24810. ASN1_OBJECT_free(a);
  24811. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  24812. return EXPECT_RESULT();
  24813. }
  24814. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  24815. {
  24816. EXPECT_DECLS;
  24817. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  24818. ASN1_OBJECT* obj = NULL;
  24819. ASN1_OBJECT* a = NULL;
  24820. BIO *bio = NULL;
  24821. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  24822. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  24823. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  24824. const unsigned char* p;
  24825. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  24826. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24827. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  24828. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  24829. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  24830. /* No DER encoding in ASN1_OBJECT. */
  24831. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  24832. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24833. ASN1_OBJECT_free(a);
  24834. a = NULL;
  24835. /* DER encoding - not OBJECT_ID */
  24836. p = notObjDer;
  24837. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24838. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24839. ASN1_OBJECT_free(a);
  24840. a = NULL;
  24841. /* Bad length encoding. */
  24842. p = badLenDer;
  24843. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24844. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24845. ASN1_OBJECT_free(a);
  24846. a = NULL;
  24847. /* Good encoding - but unknown. */
  24848. p = goodDer;
  24849. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24850. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24851. ASN1_OBJECT_free(a);
  24852. BIO_free(bio);
  24853. ASN1_OBJECT_free(obj);
  24854. #endif
  24855. return EXPECT_RESULT();
  24856. }
  24857. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  24858. {
  24859. EXPECT_DECLS;
  24860. #if defined(OPENSSL_EXTRA) && \
  24861. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24862. char buf[50] = {0};
  24863. ASN1_OBJECT* obj;
  24864. const char* oid = "2.5.29.19";
  24865. const char* ln = "X509v3 Basic Constraints";
  24866. obj = NULL;
  24867. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  24868. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  24869. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  24870. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  24871. XMEMSET(buf, 0, sizeof(buf));
  24872. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  24873. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  24874. ASN1_OBJECT_free(obj);
  24875. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  24876. return EXPECT_RESULT();
  24877. }
  24878. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  24879. {
  24880. EXPECT_DECLS;
  24881. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  24882. WOLFSSL_STACK* sk = NULL;
  24883. WOLFSSL_ASN1_OBJECT* obj;
  24884. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  24885. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24886. wolfSSL_sk_ASN1_OBJECT_free(sk);
  24887. sk = NULL;
  24888. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24889. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  24890. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  24891. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  24892. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  24893. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  24894. sk = NULL;
  24895. /* obj freed in pop_free call. */
  24896. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  24897. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24898. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  24899. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  24900. wolfSSL_sk_ASN1_OBJECT_free(sk);
  24901. wolfSSL_ASN1_OBJECT_free(obj);
  24902. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  24903. return EXPECT_RESULT();
  24904. }
  24905. static int test_wolfSSL_ASN1_STRING(void)
  24906. {
  24907. EXPECT_DECLS;
  24908. #if defined(OPENSSL_EXTRA)
  24909. ASN1_STRING* str = NULL;
  24910. ASN1_STRING* c = NULL;
  24911. const char data[] = "hello wolfSSL";
  24912. const char data2[] = "Same len data";
  24913. const char longData[] =
  24914. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  24915. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  24916. ASN1_STRING_free(str);
  24917. str = NULL;
  24918. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  24919. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  24920. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  24921. /* Check setting to NULL works. */
  24922. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  24923. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  24924. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  24925. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  24926. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  24927. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  24928. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  24929. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  24930. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  24931. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  24932. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  24933. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  24934. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  24935. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  24936. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  24937. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  24938. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  24939. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  24940. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  24941. /* Check setting back to smaller size frees dynamic data. */
  24942. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  24943. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  24944. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  24945. ExpectNull(ASN1_STRING_get0_data(NULL));
  24946. ExpectNotNull(ASN1_STRING_get0_data(str));
  24947. ExpectNull(ASN1_STRING_data(NULL));
  24948. ExpectNotNull(ASN1_STRING_data(str));
  24949. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  24950. ExpectIntGT(ASN1_STRING_length(str), 0);
  24951. ASN1_STRING_free(c);
  24952. ASN1_STRING_free(str);
  24953. ASN1_STRING_free(NULL);
  24954. #ifndef NO_WOLFSSL_STUB
  24955. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  24956. #endif
  24957. #endif
  24958. return EXPECT_RESULT();
  24959. }
  24960. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  24961. {
  24962. EXPECT_DECLS;
  24963. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  24964. !defined(NO_FILESYSTEM)
  24965. WOLFSSL_X509* x509 = NULL;
  24966. WOLFSSL_X509_NAME* subject = NULL;
  24967. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  24968. WOLFSSL_ASN1_STRING* a = NULL;
  24969. FILE* file = XBADFILE;
  24970. int idx = 0;
  24971. char targetOutput[16] = "www.wolfssl.com";
  24972. unsigned char* actual_output = NULL;
  24973. int len = 0;
  24974. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  24975. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  24976. if (file != NULL)
  24977. fclose(file);
  24978. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  24979. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  24980. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  24981. NID_commonName, -1)), 5);
  24982. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  24983. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  24984. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  24985. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  24986. a = NULL;
  24987. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  24988. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  24989. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  24990. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  24991. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  24992. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  24993. wolfSSL_X509_free(x509);
  24994. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24995. ExpectNotNull(a = ASN1_STRING_new());
  24996. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  24997. ASN1_STRING_free(a);
  24998. #endif
  24999. return EXPECT_RESULT();
  25000. }
  25001. static int test_wolfSSL_i2s_ASN1_STRING(void)
  25002. {
  25003. EXPECT_DECLS;
  25004. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  25005. WOLFSSL_ASN1_STRING* str = NULL;
  25006. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  25007. char* ret = NULL;
  25008. ExpectNotNull(str = ASN1_STRING_new());
  25009. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  25010. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25011. ret = NULL;
  25012. /* No data. */
  25013. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  25014. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25015. ret = NULL;
  25016. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  25017. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  25018. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25019. ret = NULL;
  25020. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  25021. /* No type. */
  25022. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  25023. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25024. ASN1_STRING_free(str);
  25025. #endif
  25026. return EXPECT_RESULT();
  25027. }
  25028. static int test_wolfSSL_ASN1_STRING_canon(void)
  25029. {
  25030. EXPECT_DECLS;
  25031. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  25032. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  25033. defined(OPENSSL_EXTRA_X509_SMALL))
  25034. WOLFSSL_ASN1_STRING* orig = NULL;
  25035. WOLFSSL_ASN1_STRING* canon = NULL;
  25036. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  25037. const char* whitespaceOnly = "\t\r\n";
  25038. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  25039. const char* canonData = "\x01 \x02 \xff tt";
  25040. const char longData[] =
  25041. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  25042. ExpectNotNull(orig = ASN1_STRING_new());
  25043. ExpectNotNull(canon = ASN1_STRING_new());
  25044. /* Invalid parameter testing. */
  25045. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  25046. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  25047. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  25048. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25049. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25050. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  25051. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25052. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25053. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  25054. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25055. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25056. ASN1_STRING_free(orig);
  25057. orig = NULL;
  25058. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  25059. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  25060. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25061. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  25062. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25063. ASN1_STRING_free(orig);
  25064. orig = NULL;
  25065. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  25066. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  25067. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25068. ASN1_STRING_free(orig);
  25069. orig = NULL;
  25070. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  25071. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25072. ASN1_STRING_free(orig);
  25073. ASN1_STRING_free(canon);
  25074. #endif
  25075. #endif
  25076. return EXPECT_RESULT();
  25077. }
  25078. static int test_wolfSSL_ASN1_STRING_print(void)
  25079. {
  25080. EXPECT_DECLS;
  25081. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  25082. !defined(NO_BIO)
  25083. ASN1_STRING* asnStr = NULL;
  25084. const char HELLO_DATA[]= \
  25085. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  25086. #define MAX_UNPRINTABLE_CHAR 32
  25087. #define MAX_BUF 255
  25088. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  25089. unsigned char expected[sizeof(unprintableData)+1];
  25090. unsigned char rbuf[MAX_BUF];
  25091. BIO *bio = NULL;
  25092. int p_len;
  25093. int i;
  25094. /* setup */
  25095. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  25096. unprintableData[i] = HELLO_DATA[i];
  25097. expected[i] = HELLO_DATA[i];
  25098. }
  25099. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  25100. unprintableData[sizeof(HELLO_DATA)+i] = i;
  25101. if (i == (int)'\n' || i == (int)'\r')
  25102. expected[sizeof(HELLO_DATA)+i] = i;
  25103. else
  25104. expected[sizeof(HELLO_DATA)+i] = '.';
  25105. }
  25106. unprintableData[sizeof(unprintableData)-1] = '\0';
  25107. expected[sizeof(expected)-1] = '\0';
  25108. XMEMSET(rbuf, 0, MAX_BUF);
  25109. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25110. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  25111. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25112. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  25113. (int)sizeof(unprintableData)), 1);
  25114. /* test */
  25115. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  25116. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  25117. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  25118. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  25119. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  25120. ExpectStrEQ((char*)rbuf, (const char*)expected);
  25121. BIO_free(bio);
  25122. bio = NULL;
  25123. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25124. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25125. /* Ensure there is 0 bytes available to write into. */
  25126. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  25127. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25128. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25129. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25130. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  25131. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25132. BIO_free(bio);
  25133. ASN1_STRING_free(asnStr);
  25134. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  25135. return EXPECT_RESULT();
  25136. }
  25137. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  25138. {
  25139. EXPECT_DECLS;
  25140. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  25141. ASN1_STRING* asn_str = NULL;
  25142. const char data[] = "Hello wolfSSL!";
  25143. ASN1_STRING* esc_str = NULL;
  25144. const char esc_data[] = "a+;<>";
  25145. ASN1_STRING* neg_int = NULL;
  25146. const char neg_int_data[] = "\xff";
  25147. ASN1_STRING* neg_enum = NULL;
  25148. const char neg_enum_data[] = "\xff";
  25149. BIO *bio = NULL;
  25150. BIO *fixed = NULL;
  25151. unsigned long flags;
  25152. int p_len;
  25153. unsigned char rbuf[255];
  25154. /* setup */
  25155. XMEMSET(rbuf, 0, 255);
  25156. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25157. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  25158. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25159. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25160. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  25161. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25162. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  25163. sizeof(esc_data)), 1);
  25164. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  25165. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  25166. sizeof(neg_int_data) - 1), 1);
  25167. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  25168. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  25169. sizeof(neg_enum_data) - 1), 1);
  25170. /* Invalid parameter testing. */
  25171. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  25172. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  25173. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  25174. /* no flags */
  25175. XMEMSET(rbuf, 0, 255);
  25176. flags = 0;
  25177. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  25178. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  25179. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  25180. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25181. /* Ensure there is 0 bytes available to write into. */
  25182. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25183. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25184. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25185. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25186. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  25187. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25188. /* RFC2253 Escape */
  25189. XMEMSET(rbuf, 0, 255);
  25190. flags = ASN1_STRFLGS_ESC_2253;
  25191. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  25192. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  25193. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  25194. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25195. /* Ensure there is 0 bytes available to write into. */
  25196. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25197. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25198. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25199. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25200. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  25201. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25202. /* Show type */
  25203. XMEMSET(rbuf, 0, 255);
  25204. flags = ASN1_STRFLGS_SHOW_TYPE;
  25205. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  25206. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  25207. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  25208. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25209. /* Ensure there is 0 bytes available to write into. */
  25210. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25211. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25212. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25213. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25214. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  25215. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25216. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  25217. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25218. /* Dump All */
  25219. XMEMSET(rbuf, 0, 255);
  25220. flags = ASN1_STRFLGS_DUMP_ALL;
  25221. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  25222. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  25223. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  25224. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25225. /* Ensure there is 0 bytes available to write into. */
  25226. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25227. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25228. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25229. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25230. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  25231. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25232. /* Dump Der */
  25233. XMEMSET(rbuf, 0, 255);
  25234. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  25235. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  25236. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  25237. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  25238. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25239. /* Ensure there is 0 bytes available to write into. */
  25240. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25241. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25242. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25243. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25244. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  25245. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25246. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  25247. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25248. /* Dump All + Show type */
  25249. XMEMSET(rbuf, 0, 255);
  25250. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25251. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  25252. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  25253. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  25254. /* Dump All + Show type - Negative Integer. */
  25255. XMEMSET(rbuf, 0, 255);
  25256. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25257. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  25258. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  25259. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  25260. /* Dump All + Show type - Negative Enumerated. */
  25261. XMEMSET(rbuf, 0, 255);
  25262. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25263. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  25264. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  25265. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  25266. BIO_free(fixed);
  25267. BIO_free(bio);
  25268. ASN1_STRING_free(asn_str);
  25269. ASN1_STRING_free(esc_str);
  25270. ASN1_STRING_free(neg_int);
  25271. ASN1_STRING_free(neg_enum);
  25272. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  25273. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  25274. #endif
  25275. return EXPECT_RESULT();
  25276. }
  25277. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  25278. {
  25279. EXPECT_DECLS;
  25280. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  25281. ASN1_STRING* asn1str_test = NULL;
  25282. ASN1_STRING* asn1str_answer = NULL;
  25283. /* Each character is encoded using 4 bytes */
  25284. char input[] = {
  25285. 0, 0, 0, 'T',
  25286. 0, 0, 0, 'e',
  25287. 0, 0, 0, 's',
  25288. 0, 0, 0, 't',
  25289. };
  25290. char output[] = "Test";
  25291. char badInput[] = {
  25292. 1, 0, 0, 'T',
  25293. 0, 1, 0, 'e',
  25294. 0, 0, 1, 's',
  25295. };
  25296. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  25297. /* Test wrong type. */
  25298. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25299. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25300. ASN1_STRING_free(asn1str_test);
  25301. asn1str_test = NULL;
  25302. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  25303. /* Test bad length. */
  25304. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  25305. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25306. /* Test bad input. */
  25307. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  25308. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25309. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  25310. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25311. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  25312. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25313. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  25314. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  25315. ExpectNotNull(
  25316. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  25317. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  25318. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  25319. ASN1_STRING_free(asn1str_test);
  25320. ASN1_STRING_free(asn1str_answer);
  25321. #endif /* OPENSSL_ALL && !NO_ASN */
  25322. return EXPECT_RESULT();
  25323. }
  25324. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  25325. {
  25326. EXPECT_DECLS;
  25327. #if defined(OPENSSL_EXTRA)
  25328. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  25329. unsigned char nullstr[32];
  25330. XMEMSET(nullstr, 0, 32);
  25331. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  25332. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  25333. if (asn1_gtime != NULL) {
  25334. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  25335. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  25336. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  25337. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25338. }
  25339. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  25340. #endif /* OPENSSL_EXTRA */
  25341. return EXPECT_RESULT();
  25342. }
  25343. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  25344. {
  25345. EXPECT_DECLS;
  25346. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  25347. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  25348. BIO* bio = NULL;
  25349. unsigned char buf[24];
  25350. int i;
  25351. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25352. BIO_set_write_buf_size(bio, 24);
  25353. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  25354. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  25355. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  25356. /* Type not set. */
  25357. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25358. gtime.type = V_ASN1_GENERALIZEDTIME;
  25359. /* Invalid parameters testing. */
  25360. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  25361. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  25362. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  25363. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  25364. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  25365. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  25366. BIO_free(bio);
  25367. bio = NULL;
  25368. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25369. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25370. /* Ensure there is 0 bytes available to write into. */
  25371. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  25372. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25373. for (i = 1; i < 20; i++) {
  25374. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  25375. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25376. }
  25377. BIO_free(bio);
  25378. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  25379. #endif /* OPENSSL_EXTRA */
  25380. return EXPECT_RESULT();
  25381. }
  25382. static int test_wolfSSL_ASN1_TIME(void)
  25383. {
  25384. EXPECT_DECLS;
  25385. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25386. WOLFSSL_ASN1_TIME* asn_time = NULL;
  25387. unsigned char *data;
  25388. ExpectNotNull(asn_time = ASN1_TIME_new());
  25389. #ifndef NO_WOLFSSL_STUB
  25390. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  25391. #endif
  25392. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  25393. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  25394. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  25395. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  25396. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  25397. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  25398. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  25399. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  25400. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  25401. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  25402. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  25403. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  25404. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  25405. ASN1_TIME_free(asn_time);
  25406. ASN1_TIME_free(NULL);
  25407. #endif
  25408. return EXPECT_RESULT();
  25409. }
  25410. static int test_wolfSSL_ASN1_TIME_to_string(void)
  25411. {
  25412. EXPECT_DECLS;
  25413. #ifndef NO_ASN_TIME
  25414. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25415. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  25416. WOLFSSL_ASN1_TIME* t = NULL;
  25417. char buf[ASN_GENERALIZED_TIME_SIZE];
  25418. ExpectNotNull((t = ASN1_TIME_new()));
  25419. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  25420. /* Invalid parameter testing. */
  25421. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  25422. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  25423. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  25424. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  25425. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  25426. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  25427. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  25428. /* Buffer needs to be longer than minimum of 5 characters. */
  25429. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  25430. ASN1_TIME_free(t);
  25431. #endif
  25432. #endif /* NO_ASN_TIME */
  25433. return EXPECT_RESULT();
  25434. }
  25435. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  25436. {
  25437. EXPECT_DECLS;
  25438. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25439. ASN1_TIME* fromTime = NULL;
  25440. ASN1_TIME* closeToTime = NULL;
  25441. ASN1_TIME* toTime = NULL;
  25442. ASN1_TIME* invalidTime = NULL;
  25443. int daysDiff;
  25444. int secsDiff;
  25445. ExpectNotNull((fromTime = ASN1_TIME_new()));
  25446. /* Feb 22, 2003, 21:15:15 */
  25447. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  25448. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  25449. /* Feb 22, 2003, 21:16:15 */
  25450. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  25451. ExpectNotNull((toTime = ASN1_TIME_new()));
  25452. /* Dec 19, 2010, 18:10:11 */
  25453. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  25454. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  25455. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  25456. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  25457. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  25458. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  25459. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25460. /* Error conditions. */
  25461. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  25462. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  25463. /* If both times are NULL, difference is 0. */
  25464. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  25465. ExpectIntEQ(daysDiff, 0);
  25466. ExpectIntEQ(secsDiff, 0);
  25467. /* If one time is NULL, it defaults to the current time. */
  25468. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  25469. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  25470. /* Normal operation. Both times non-NULL. */
  25471. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25472. ExpectIntEQ(daysDiff, 2856);
  25473. ExpectIntEQ(secsDiff, 75296);
  25474. /* Swapping the times should return negative values. */
  25475. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  25476. ExpectIntEQ(daysDiff, -2856);
  25477. ExpectIntEQ(secsDiff, -75296);
  25478. /* Compare with invalid time string. */
  25479. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  25480. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  25481. /* Compare with days difference of 0. */
  25482. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  25483. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  25484. /* Days and seconds differences not 0. */
  25485. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  25486. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  25487. /* Same time. */
  25488. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  25489. /* Compare regression test: No seconds difference, just difference in days.
  25490. */
  25491. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  25492. ASN1_TIME_set_string(toTime, "19800101000000Z");
  25493. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  25494. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  25495. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  25496. /* Edge case with Unix epoch. */
  25497. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  25498. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  25499. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25500. ExpectIntEQ(daysDiff, 3652);
  25501. ExpectIntEQ(secsDiff, 0);
  25502. /* Edge case with year > 2038 (year 2038 problem). */
  25503. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  25504. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25505. ExpectIntEQ(daysDiff, 2932896);
  25506. ExpectIntEQ(secsDiff, 86399);
  25507. ASN1_TIME_free(fromTime);
  25508. ASN1_TIME_free(closeToTime);
  25509. ASN1_TIME_free(toTime);
  25510. ASN1_TIME_free(invalidTime);
  25511. #endif
  25512. return EXPECT_RESULT();
  25513. }
  25514. static int test_wolfSSL_ASN1_TIME_adj(void)
  25515. {
  25516. EXPECT_DECLS;
  25517. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  25518. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  25519. const int year = 365*24*60*60;
  25520. const int day = 24*60*60;
  25521. const int hour = 60*60;
  25522. const int mini = 60;
  25523. const byte asn_utc_time = ASN_UTC_TIME;
  25524. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  25525. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  25526. #endif
  25527. WOLFSSL_ASN1_TIME* asn_time = NULL;
  25528. WOLFSSL_ASN1_TIME* s = NULL;
  25529. int offset_day;
  25530. long offset_sec;
  25531. char date_str[CTC_DATE_SIZE + 1];
  25532. time_t t;
  25533. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  25534. /* UTC notation test */
  25535. /* 2000/2/15 20:30:00 */
  25536. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  25537. offset_day = 7;
  25538. offset_sec = 45 * mini;
  25539. /* offset_sec = -45 * min;*/
  25540. ExpectNotNull(asn_time =
  25541. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  25542. ExpectTrue(asn_time->type == asn_utc_time);
  25543. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25544. CTC_DATE_SIZE));
  25545. date_str[CTC_DATE_SIZE] = '\0';
  25546. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  25547. /* negative offset */
  25548. offset_sec = -45 * mini;
  25549. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  25550. ExpectNotNull(asn_time);
  25551. ExpectTrue(asn_time->type == asn_utc_time);
  25552. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25553. CTC_DATE_SIZE));
  25554. date_str[CTC_DATE_SIZE] = '\0';
  25555. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  25556. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  25557. s = NULL;
  25558. XMEMSET(date_str, 0, sizeof(date_str));
  25559. /* Generalized time will overflow time_t if not long */
  25560. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  25561. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  25562. DYNAMIC_TYPE_OPENSSL);
  25563. /* GeneralizedTime notation test */
  25564. /* 2055/03/01 09:00:00 */
  25565. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  25566. offset_day = 12;
  25567. offset_sec = 10 * mini;
  25568. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  25569. offset_sec));
  25570. ExpectTrue(asn_time->type == asn_gen_time);
  25571. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25572. CTC_DATE_SIZE));
  25573. date_str[CTC_DATE_SIZE] = '\0';
  25574. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  25575. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  25576. s = NULL;
  25577. XMEMSET(date_str, 0, sizeof(date_str));
  25578. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  25579. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  25580. s = NULL;
  25581. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  25582. offset_day = 7;
  25583. offset_sec = 45 * mini;
  25584. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  25585. offset_sec));
  25586. ExpectTrue(asn_time->type == asn_utc_time);
  25587. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25588. CTC_DATE_SIZE));
  25589. date_str[CTC_DATE_SIZE] = '\0';
  25590. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  25591. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  25592. asn_time = NULL;
  25593. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  25594. offset_sec));
  25595. ExpectTrue(asn_time->type == asn_utc_time);
  25596. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25597. CTC_DATE_SIZE));
  25598. date_str[CTC_DATE_SIZE] = '\0';
  25599. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  25600. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  25601. #endif
  25602. return EXPECT_RESULT();
  25603. }
  25604. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  25605. {
  25606. EXPECT_DECLS;
  25607. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25608. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  25609. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  25610. ASN1_TIME asnTime;
  25611. struct tm tm;
  25612. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  25613. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  25614. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  25615. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  25616. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25617. ExpectIntEQ(tm.tm_sec, 15);
  25618. ExpectIntEQ(tm.tm_min, 15);
  25619. ExpectIntEQ(tm.tm_hour, 21);
  25620. ExpectIntEQ(tm.tm_mday, 22);
  25621. ExpectIntEQ(tm.tm_mon, 1);
  25622. ExpectIntEQ(tm.tm_year, 100);
  25623. ExpectIntEQ(tm.tm_isdst, 0);
  25624. #ifdef XMKTIME
  25625. ExpectIntEQ(tm.tm_wday, 2);
  25626. ExpectIntEQ(tm.tm_yday, 52);
  25627. #endif
  25628. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  25629. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25630. ExpectIntEQ(tm.tm_year, 50);
  25631. /* Get current time. */
  25632. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  25633. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  25634. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  25635. /* 0 length. */
  25636. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25637. /* No type. */
  25638. asnTime.length = 1;
  25639. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25640. /* Not UTCTIME length. */
  25641. asnTime.type = V_ASN1_UTCTIME;
  25642. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25643. /* Not GENERALIZEDTIME length. */
  25644. asnTime.type = V_ASN1_GENERALIZEDTIME;
  25645. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25646. /* Not Zulu timezone. */
  25647. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  25648. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25649. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  25650. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25651. #ifdef XMKTIME
  25652. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  25653. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25654. ExpectIntEQ(tm.tm_sec, 47);
  25655. ExpectIntEQ(tm.tm_min, 22);
  25656. ExpectIntEQ(tm.tm_hour, 21);
  25657. ExpectIntEQ(tm.tm_mday, 12);
  25658. ExpectIntEQ(tm.tm_mon, 4);
  25659. ExpectIntEQ(tm.tm_year, 123);
  25660. ExpectIntEQ(tm.tm_wday, 5);
  25661. ExpectIntEQ(tm.tm_yday, 131);
  25662. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  25663. fields are zeroed out as expected. */
  25664. ExpectIntEQ(tm.tm_isdst, 0);
  25665. #endif
  25666. #endif
  25667. return EXPECT_RESULT();
  25668. }
  25669. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  25670. {
  25671. EXPECT_DECLS;
  25672. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25673. WOLFSSL_ASN1_TIME *t = NULL;
  25674. WOLFSSL_ASN1_TIME *out = NULL;
  25675. WOLFSSL_ASN1_TIME *gtime = NULL;
  25676. int tlen = 0;
  25677. unsigned char *data = NULL;
  25678. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  25679. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  25680. /* type not set. */
  25681. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25682. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25683. t = NULL;
  25684. /* UTC Time test */
  25685. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  25686. if (t != NULL) {
  25687. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  25688. t->type = ASN_UTC_TIME;
  25689. t->length = ASN_UTC_TIME_SIZE;
  25690. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  25691. }
  25692. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  25693. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  25694. out = NULL;
  25695. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25696. wolfSSL_ASN1_TIME_free(gtime);
  25697. gtime = NULL;
  25698. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  25699. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25700. ExpectPtrEq(gtime, out);
  25701. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25702. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25703. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25704. /* Generalized Time test */
  25705. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25706. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  25707. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  25708. if (t != NULL) {
  25709. t->type = ASN_GENERALIZED_TIME;
  25710. t->length = ASN_GENERALIZED_TIME_SIZE;
  25711. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  25712. }
  25713. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  25714. ASN_GENERALIZED_TIME_SIZE);
  25715. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  25716. "20050727123456Z");
  25717. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25718. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25719. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25720. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25721. /* UTC Time to Generalized Time 1900's test */
  25722. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25723. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  25724. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  25725. if (t != NULL) {
  25726. t->type = ASN_UTC_TIME;
  25727. t->length = ASN_UTC_TIME_SIZE;
  25728. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  25729. }
  25730. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25731. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25732. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25733. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  25734. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25735. /* Null parameter test */
  25736. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25737. gtime = NULL;
  25738. out = NULL;
  25739. if (t != NULL) {
  25740. t->type = ASN_UTC_TIME;
  25741. t->length = ASN_UTC_TIME_SIZE;
  25742. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  25743. }
  25744. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  25745. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25746. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25747. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25748. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25749. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25750. #endif
  25751. return EXPECT_RESULT();
  25752. }
  25753. static int test_wolfSSL_ASN1_TIME_print(void)
  25754. {
  25755. EXPECT_DECLS;
  25756. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  25757. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25758. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  25759. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  25760. !defined(NO_ASN_TIME)
  25761. BIO* bio = NULL;
  25762. BIO* fixed = NULL;
  25763. X509* x509 = NULL;
  25764. const unsigned char* der = client_cert_der_2048;
  25765. ASN1_TIME* notAfter;
  25766. ASN1_TIME* notBefore;
  25767. unsigned char buf[25];
  25768. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25769. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25770. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  25771. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  25772. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  25773. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  25774. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  25775. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  25776. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  25777. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  25778. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  25779. /* Test BIO_write fails. */
  25780. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25781. /* Ensure there is 0 bytes available to write into. */
  25782. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  25783. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25784. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25785. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25786. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  25787. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25788. /* create a bad time and test results */
  25789. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  25790. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  25791. if (EXPECT_SUCCESS()) {
  25792. notAfter->data[8] = 0;
  25793. notAfter->data[3] = 0;
  25794. }
  25795. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  25796. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  25797. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  25798. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  25799. BIO_free(bio);
  25800. BIO_free(fixed);
  25801. X509_free(x509);
  25802. #endif
  25803. return EXPECT_RESULT();
  25804. }
  25805. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  25806. {
  25807. EXPECT_DECLS;
  25808. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  25809. BIO* bio = NULL;
  25810. ASN1_UTCTIME* utc = NULL;
  25811. unsigned char buf[25];
  25812. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  25813. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  25814. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  25815. /* Valid date */
  25816. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25817. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  25818. DYNAMIC_TYPE_ASN1));
  25819. if (utc != NULL) {
  25820. utc->type = ASN_UTC_TIME;
  25821. utc->length = ASN_UTC_TIME_SIZE;
  25822. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  25823. }
  25824. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  25825. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  25826. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  25827. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  25828. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  25829. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  25830. XMEMSET(buf, 0, sizeof(buf));
  25831. BIO_free(bio);
  25832. bio = NULL;
  25833. /* Invalid format */
  25834. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25835. if (utc != NULL) {
  25836. utc->type = ASN_UTC_TIME;
  25837. utc->length = ASN_UTC_TIME_SIZE;
  25838. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  25839. }
  25840. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  25841. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  25842. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  25843. /* Invalid type */
  25844. if (utc != NULL) {
  25845. utc->type = ASN_GENERALIZED_TIME;
  25846. utc->length = ASN_GENERALIZED_TIME_SIZE;
  25847. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  25848. }
  25849. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  25850. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  25851. BIO_free(bio);
  25852. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  25853. return EXPECT_RESULT();
  25854. }
  25855. static int test_wolfSSL_ASN1_TYPE(void)
  25856. {
  25857. EXPECT_DECLS;
  25858. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  25859. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  25860. WOLFSSL_ASN1_TYPE* t = NULL;
  25861. WOLFSSL_ASN1_OBJECT* obj = NULL;
  25862. #ifndef NO_ASN_TIME
  25863. WOLFSSL_ASN1_TIME* time = NULL;
  25864. #endif
  25865. WOLFSSL_ASN1_STRING* str = NULL;
  25866. unsigned char data[] = { 0x00 };
  25867. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  25868. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25869. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  25870. wolfSSL_ASN1_TYPE_free(t);
  25871. t = NULL;
  25872. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25873. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  25874. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  25875. wolfSSL_ASN1_TYPE_free(t);
  25876. t = NULL;
  25877. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25878. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  25879. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  25880. wolfSSL_ASN1_TYPE_free(t);
  25881. t = NULL;
  25882. #ifndef NO_ASN_TIME
  25883. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25884. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  25885. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  25886. wolfSSL_ASN1_TYPE_free(t);
  25887. t = NULL;
  25888. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25889. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  25890. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  25891. wolfSSL_ASN1_TYPE_free(t);
  25892. t = NULL;
  25893. #endif
  25894. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25895. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25896. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  25897. wolfSSL_ASN1_TYPE_free(t);
  25898. t = NULL;
  25899. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25900. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25901. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  25902. wolfSSL_ASN1_TYPE_free(t);
  25903. t = NULL;
  25904. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25905. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25906. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  25907. wolfSSL_ASN1_TYPE_free(t);
  25908. t = NULL;
  25909. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25910. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25911. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  25912. wolfSSL_ASN1_TYPE_free(t);
  25913. t = NULL;
  25914. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25915. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25916. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  25917. wolfSSL_ASN1_TYPE_free(t);
  25918. t = NULL;
  25919. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25920. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25921. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  25922. wolfSSL_ASN1_TYPE_free(t);
  25923. t = NULL;
  25924. #endif
  25925. return EXPECT_RESULT();
  25926. }
  25927. /* Testing code used in dpp.c in hostap */
  25928. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  25929. typedef struct {
  25930. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  25931. * as an OID identifying the curve */
  25932. X509_ALGOR *alg;
  25933. /* Compressed format public key per ANSI X9.63 */
  25934. ASN1_BIT_STRING *pub_key;
  25935. } DPP_BOOTSTRAPPING_KEY;
  25936. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  25937. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  25938. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  25939. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  25940. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  25941. typedef struct {
  25942. ASN1_INTEGER *integer;
  25943. } TEST_ASN1;
  25944. ASN1_SEQUENCE(TEST_ASN1) = {
  25945. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  25946. } ASN1_SEQUENCE_END(TEST_ASN1)
  25947. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  25948. typedef struct {
  25949. ASN1_OCTET_STRING *octet_string;
  25950. } TEST_FAIL_ASN1;
  25951. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  25952. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  25953. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  25954. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  25955. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  25956. #endif
  25957. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  25958. {
  25959. EXPECT_DECLS;
  25960. /* Testing code used in dpp.c in hostap */
  25961. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  25962. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  25963. EC_KEY *eckey = NULL;
  25964. EVP_PKEY *key = NULL;
  25965. size_t len;
  25966. unsigned char *der = NULL;
  25967. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  25968. const unsigned char *in = ecc_clikey_der_256;
  25969. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  25970. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  25971. const EC_GROUP *group = NULL;
  25972. const EC_POINT *point = NULL;
  25973. int nid;
  25974. TEST_ASN1 *test_asn1 = NULL;
  25975. TEST_FAIL_ASN1 test_fail_asn1;
  25976. const unsigned char badObjDer[] = { 0x06, 0x00 };
  25977. const unsigned char goodObjDer[] = {
  25978. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  25979. };
  25980. WOLFSSL_ASN1_ITEM emptyTemplate;
  25981. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  25982. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  25983. der = NULL;
  25984. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  25985. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  25986. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  25987. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  25988. (long)sizeof_ecc_clikey_der_256));
  25989. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  25990. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  25991. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  25992. nid = EC_GROUP_get_curve_name(group);
  25993. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  25994. group_obj = OBJ_nid2obj(nid);
  25995. if ((ec_obj != NULL) && (group_obj != NULL)) {
  25996. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  25997. group_obj), 1);
  25998. if (EXPECT_SUCCESS()) {
  25999. ec_obj = NULL;
  26000. group_obj = NULL;
  26001. }
  26002. }
  26003. wolfSSL_ASN1_OBJECT_free(group_obj);
  26004. wolfSSL_ASN1_OBJECT_free(ec_obj);
  26005. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  26006. #ifdef HAVE_COMP_KEY
  26007. ExpectIntGT((len = EC_POINT_point2oct(
  26008. group, point, POINT_CONVERSION_COMPRESSED,
  26009. NULL, 0, NULL)), 0);
  26010. #else
  26011. ExpectIntGT((len = EC_POINT_point2oct(
  26012. group, point, POINT_CONVERSION_UNCOMPRESSED,
  26013. NULL, 0, NULL)), 0);
  26014. #endif
  26015. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  26016. #ifdef HAVE_COMP_KEY
  26017. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  26018. der, len-1, NULL), 0);
  26019. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  26020. der, len, NULL), len);
  26021. #else
  26022. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  26023. der, len-1, NULL), 0);
  26024. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  26025. der, len, NULL), len);
  26026. #endif
  26027. if (EXPECT_SUCCESS()) {
  26028. bootstrap->pub_key->data = der;
  26029. bootstrap->pub_key->length = (int)len;
  26030. /* Not actually used */
  26031. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  26032. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  26033. }
  26034. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  26035. der = NULL;
  26036. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  26037. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  26038. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  26039. EVP_PKEY_free(key);
  26040. EC_KEY_free(eckey);
  26041. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  26042. bootstrap = NULL;
  26043. DPP_BOOTSTRAPPING_KEY_free(NULL);
  26044. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  26045. * a NULL and an empty BIT_STRING. */
  26046. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  26047. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  26048. if (EXPECT_SUCCESS()) {
  26049. bootstrap->alg->algorithm->obj = badObjDer;
  26050. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  26051. }
  26052. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  26053. if (EXPECT_SUCCESS()) {
  26054. bootstrap->alg->parameter->type = V_ASN1_NULL;
  26055. bootstrap->alg->parameter->value.ptr = NULL;
  26056. bootstrap->pub_key->data = NULL;
  26057. bootstrap->pub_key->length = 0;
  26058. /* Not actually used */
  26059. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  26060. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  26061. }
  26062. /* Encode with bad OBJECT_ID. */
  26063. der = NULL;
  26064. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  26065. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  26066. if (EXPECT_SUCCESS()) {
  26067. bootstrap->alg->algorithm->obj = goodObjDer;
  26068. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  26069. bootstrap->alg->algorithm->grp = 2;
  26070. }
  26071. der = NULL;
  26072. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  26073. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  26074. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  26075. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  26076. /* Test integer */
  26077. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  26078. der = NULL;
  26079. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  26080. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  26081. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  26082. TEST_ASN1_free(test_asn1);
  26083. /* Test integer cases. */
  26084. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  26085. TEST_ASN1_free(NULL);
  26086. /* Test error cases. */
  26087. ExpectNull(TEST_FAIL_ASN1_new());
  26088. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  26089. TEST_FAIL_ASN1_free(NULL);
  26090. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  26091. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  26092. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26093. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26094. return EXPECT_RESULT();
  26095. }
  26096. static int test_wolfSSL_lhash(void)
  26097. {
  26098. EXPECT_DECLS;
  26099. #ifdef OPENSSL_ALL
  26100. const char testStr[] = "Like a true nature's child\n"
  26101. "We were born\n"
  26102. "Born to be wild";
  26103. #ifdef NO_SHA
  26104. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  26105. #else
  26106. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26107. #endif
  26108. #endif
  26109. return EXPECT_RESULT();
  26110. }
  26111. static int test_wolfSSL_X509_NAME(void)
  26112. {
  26113. EXPECT_DECLS;
  26114. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26115. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26116. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26117. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26118. defined(OPENSSL_EXTRA))
  26119. X509* x509 = NULL;
  26120. const unsigned char* c;
  26121. unsigned char buf[4096];
  26122. int bytes;
  26123. XFILE f = XBADFILE;
  26124. const X509_NAME* a = NULL;
  26125. const X509_NAME* b = NULL;
  26126. X509_NAME* d2i_name = NULL;
  26127. int sz = 0;
  26128. unsigned char* tmp;
  26129. char file[] = "./certs/ca-cert.der";
  26130. #ifndef OPENSSL_EXTRA_X509_SMALL
  26131. byte empty[] = { /* CN=empty emailAddress= */
  26132. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26133. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26134. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26135. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26136. 0x01, 0x16, 0x00
  26137. };
  26138. #endif
  26139. #ifndef OPENSSL_EXTRA_X509_SMALL
  26140. /* test compile of deprecated function, returns 0 */
  26141. ExpectIntEQ(CRYPTO_thread_id(), 0);
  26142. #endif
  26143. ExpectNotNull(a = X509_NAME_new());
  26144. X509_NAME_free((X509_NAME*)a);
  26145. a = NULL;
  26146. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  26147. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  26148. if (f != XBADFILE)
  26149. XFCLOSE(f);
  26150. c = buf;
  26151. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26152. /* test cmp function */
  26153. ExpectNotNull(a = X509_get_issuer_name(x509));
  26154. ExpectNotNull(b = X509_get_subject_name(x509));
  26155. #ifndef OPENSSL_EXTRA_X509_SMALL
  26156. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26157. #endif
  26158. tmp = buf;
  26159. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26160. if (sz > 0 && tmp == buf) {
  26161. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  26162. __LINE__);
  26163. fprintf(stderr, " Expected pointer to be incremented\n");
  26164. abort();
  26165. }
  26166. #ifndef OPENSSL_EXTRA_X509_SMALL
  26167. tmp = buf;
  26168. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26169. #endif
  26170. /* if output parameter is NULL, should still return required size. */
  26171. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26172. /* retry but with the function creating a buffer */
  26173. tmp = NULL;
  26174. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26175. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26176. tmp = NULL;
  26177. #ifdef WOLFSSL_CERT_NAME_ALL
  26178. /* test for givenName and name */
  26179. {
  26180. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  26181. const byte gName[] = "test-given-name";
  26182. const byte name[] = "test-name";
  26183. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  26184. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  26185. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  26186. 1);
  26187. wolfSSL_X509_NAME_ENTRY_free(entry);
  26188. entry = NULL;
  26189. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  26190. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  26191. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  26192. 1);
  26193. wolfSSL_X509_NAME_ENTRY_free(entry);
  26194. tmp = NULL;
  26195. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26196. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26197. }
  26198. #endif
  26199. b = NULL;
  26200. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26201. #ifndef OPENSSL_EXTRA_X509_SMALL
  26202. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  26203. #endif
  26204. X509_NAME_free((X509_NAME*)b);
  26205. X509_NAME_free(d2i_name);
  26206. d2i_name = NULL;
  26207. X509_free(x509);
  26208. #ifndef OPENSSL_EXTRA_X509_SMALL
  26209. /* test with an empty domain component */
  26210. tmp = empty;
  26211. sz = sizeof(empty);
  26212. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26213. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26214. /* size of empty emailAddress will be 0 */
  26215. tmp = buf;
  26216. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26217. (char*)tmp, sizeof(buf)), 0);
  26218. /* should contain no organization name */
  26219. tmp = buf;
  26220. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26221. (char*)tmp, sizeof(buf)), -1);
  26222. X509_NAME_free(d2i_name);
  26223. #endif
  26224. #endif
  26225. return EXPECT_RESULT();
  26226. }
  26227. static int test_wolfSSL_X509_NAME_hash(void)
  26228. {
  26229. EXPECT_DECLS;
  26230. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  26231. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26232. BIO* bio = NULL;
  26233. X509* x509 = NULL;
  26234. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  26235. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26236. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26237. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26238. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26239. X509_free(x509);
  26240. BIO_free(bio);
  26241. #endif
  26242. return EXPECT_RESULT();
  26243. }
  26244. static int test_wolfSSL_X509_NAME_print_ex(void)
  26245. {
  26246. EXPECT_DECLS;
  26247. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26248. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26249. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26250. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26251. !defined(NO_BIO) && !defined(NO_RSA)
  26252. int memSz;
  26253. byte* mem = NULL;
  26254. BIO* bio = NULL;
  26255. BIO* membio = NULL;
  26256. X509* x509 = NULL;
  26257. X509_NAME* name = NULL;
  26258. const char* expNormal = "C=US, CN=wolfssl.com";
  26259. const char* expReverse = "CN=wolfssl.com, C=US";
  26260. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26261. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26262. const char* expRFC5523 =
  26263. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26264. /* Test with real cert (svrCertFile) first */
  26265. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  26266. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26267. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26268. ExpectNotNull(name = X509_get_subject_name(x509));
  26269. /* Test without flags */
  26270. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26271. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26272. BIO_free(membio);
  26273. membio = NULL;
  26274. /* Test flag: XN_FLAG_RFC2253 */
  26275. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26276. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26277. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26278. BIO_free(membio);
  26279. membio = NULL;
  26280. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26281. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26282. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26283. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26284. BIO_free(membio);
  26285. membio = NULL;
  26286. X509_free(x509);
  26287. BIO_free(bio);
  26288. name = NULL;
  26289. /* Test normal case without escaped characters */
  26290. {
  26291. /* Create name: "/C=US/CN=wolfssl.com" */
  26292. ExpectNotNull(name = X509_NAME_new());
  26293. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26294. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26295. WOLFSSL_SUCCESS);
  26296. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26297. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26298. WOLFSSL_SUCCESS);
  26299. /* Test without flags */
  26300. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26301. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26302. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26303. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  26304. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26305. BIO_free(membio);
  26306. membio = NULL;
  26307. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26308. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26309. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26310. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26311. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26312. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  26313. BIO_free(membio);
  26314. membio = NULL;
  26315. /* Test flags: XN_FLAG_DN_REV - reversed */
  26316. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26317. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26318. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26319. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26320. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  26321. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26322. BIO_free(membio);
  26323. membio = NULL;
  26324. X509_NAME_free(name);
  26325. name = NULL;
  26326. }
  26327. /* Test RFC2253 characters are escaped with backslashes */
  26328. {
  26329. ExpectNotNull(name = X509_NAME_new());
  26330. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26331. /* space at beginning and end, and: ,+"\ */
  26332. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26333. WOLFSSL_SUCCESS);
  26334. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26335. /* # at beginning, and: <>;*/
  26336. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26337. WOLFSSL_SUCCESS);
  26338. /* Test without flags */
  26339. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26340. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26341. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26342. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  26343. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26344. XSTRLEN(expNotEscaped)), 0);
  26345. BIO_free(membio);
  26346. membio = NULL;
  26347. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26348. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26349. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26350. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26351. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26352. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  26353. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26354. BIO_free(membio);
  26355. membio = NULL;
  26356. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26357. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26358. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26359. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26360. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26361. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26362. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26363. XSTRLEN(expNotEscapedRev)), 0);
  26364. BIO_free(membio);
  26365. X509_NAME_free(name);
  26366. }
  26367. #endif
  26368. return EXPECT_RESULT();
  26369. }
  26370. #ifndef NO_BIO
  26371. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26372. {
  26373. EXPECT_DECLS;
  26374. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26375. STACK_OF(X509_INFO) *info_stack = NULL;
  26376. X509_INFO *info = NULL;
  26377. int len;
  26378. int i;
  26379. const char* files[] = {
  26380. cliCertFile,
  26381. cliKeyFile,
  26382. /* This needs to be the order as svrCertFile contains the
  26383. * intermediate cert as well. */
  26384. svrKeyFile,
  26385. svrCertFile,
  26386. NULL,
  26387. };
  26388. const char** curFile;
  26389. BIO *fileBIO = NULL;
  26390. BIO *concatBIO = NULL;
  26391. byte tmp[FOURK_BUF];
  26392. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26393. * to group objects together. */
  26394. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26395. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  26396. int fileLen;
  26397. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26398. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  26399. if (EXPECT_SUCCESS()) {
  26400. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26401. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  26402. fileLen -= len;
  26403. if (EXPECT_FAIL())
  26404. break;
  26405. }
  26406. /* Make sure we read the entire file */
  26407. ExpectIntEQ(fileLen, 0);
  26408. }
  26409. BIO_free(fileBIO);
  26410. fileBIO = NULL;
  26411. }
  26412. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26413. NULL));
  26414. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  26415. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26416. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  26417. ExpectNotNull(info->x509);
  26418. ExpectNull(info->crl);
  26419. if (i != 0) {
  26420. ExpectNotNull(info->x_pkey);
  26421. ExpectIntEQ(X509_check_private_key(info->x509,
  26422. info->x_pkey->dec_pkey), 1);
  26423. }
  26424. else {
  26425. ExpectNull(info->x_pkey);
  26426. }
  26427. }
  26428. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26429. BIO_free(concatBIO);
  26430. #endif
  26431. return EXPECT_RESULT();
  26432. }
  26433. #endif
  26434. #ifndef NO_BIO
  26435. static int test_wolfSSL_X509_INFO(void)
  26436. {
  26437. EXPECT_DECLS;
  26438. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26439. STACK_OF(X509_INFO) *info_stack = NULL;
  26440. X509_INFO *info = NULL;
  26441. BIO *cert = NULL;
  26442. int i;
  26443. /* PEM in hex format to avoid null terminator */
  26444. byte data[] = {
  26445. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  26446. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  26447. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  26448. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  26449. 0x2d, 0x2d, 0x2d
  26450. };
  26451. /* PEM in hex format to avoid null terminator */
  26452. byte data2[] = {
  26453. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  26454. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  26455. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  26456. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  26457. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  26458. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  26459. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  26460. };
  26461. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26462. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26463. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26464. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  26465. ExpectNotNull(info->x509);
  26466. ExpectNull(info->crl);
  26467. ExpectNull(info->x_pkey);
  26468. }
  26469. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26470. info_stack = NULL;
  26471. BIO_free(cert);
  26472. cert = NULL;
  26473. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26474. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26475. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26476. info_stack = NULL;
  26477. BIO_free(cert);
  26478. cert = NULL;
  26479. /* This case should fail due to invalid input. */
  26480. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  26481. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  26482. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26483. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26484. info_stack = NULL;
  26485. BIO_free(cert);
  26486. cert = NULL;
  26487. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  26488. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  26489. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26490. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26491. BIO_free(cert);
  26492. #endif
  26493. return EXPECT_RESULT();
  26494. }
  26495. #endif
  26496. static int test_wolfSSL_X509_subject_name_hash(void)
  26497. {
  26498. EXPECT_DECLS;
  26499. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26500. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26501. X509* x509 = NULL;
  26502. X509_NAME* subjectName = NULL;
  26503. unsigned long ret1 = 0;
  26504. unsigned long ret2 = 0;
  26505. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26506. SSL_FILETYPE_PEM));
  26507. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  26508. /* These two
  26509. * - X509_subject_name_hash(x509)
  26510. * - X509_NAME_hash(X509_get_subject_name(x509))
  26511. * should give the same hash, if !defined(NO_SHA) is true. */
  26512. ret1 = X509_subject_name_hash(x509);
  26513. ExpectIntNE(ret1, 0);
  26514. #if !defined(NO_SHA)
  26515. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  26516. ExpectIntNE(ret2, 0);
  26517. ExpectIntEQ(ret1, ret2);
  26518. #else
  26519. (void) ret2;
  26520. #endif
  26521. X509_free(x509);
  26522. #endif
  26523. return EXPECT_RESULT();
  26524. }
  26525. static int test_wolfSSL_X509_issuer_name_hash(void)
  26526. {
  26527. EXPECT_DECLS;
  26528. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26529. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26530. X509* x509 = NULL;
  26531. X509_NAME* issuertName = NULL;
  26532. unsigned long ret1 = 0;
  26533. unsigned long ret2 = 0;
  26534. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26535. SSL_FILETYPE_PEM));
  26536. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  26537. /* These two
  26538. * - X509_issuer_name_hash(x509)
  26539. * - X509_NAME_hash(X509_get_issuer_name(x509))
  26540. * should give the same hash, if !defined(NO_SHA) is true. */
  26541. ret1 = X509_issuer_name_hash(x509);
  26542. ExpectIntNE(ret1, 0);
  26543. #if !defined(NO_SHA)
  26544. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  26545. ExpectIntNE(ret2, 0);
  26546. ExpectIntEQ(ret1, ret2);
  26547. #else
  26548. (void) ret2;
  26549. #endif
  26550. X509_free(x509);
  26551. #endif
  26552. return EXPECT_RESULT();
  26553. }
  26554. static int test_wolfSSL_X509_check_host(void)
  26555. {
  26556. EXPECT_DECLS;
  26557. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26558. && !defined(NO_SHA) && !defined(NO_RSA)
  26559. X509* x509 = NULL;
  26560. const char altName[] = "example.com";
  26561. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26562. SSL_FILETYPE_PEM));
  26563. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  26564. WOLFSSL_SUCCESS);
  26565. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  26566. WOLFSSL_FAILURE);
  26567. X509_free(x509);
  26568. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  26569. WOLFSSL_FAILURE);
  26570. #endif
  26571. return EXPECT_RESULT();
  26572. }
  26573. static int test_wolfSSL_X509_check_email(void)
  26574. {
  26575. EXPECT_DECLS;
  26576. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  26577. X509* x509 = NULL;
  26578. const char goodEmail[] = "info@wolfssl.com";
  26579. const char badEmail[] = "disinfo@wolfssl.com";
  26580. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26581. SSL_FILETYPE_PEM));
  26582. /* Should fail on non-matching email address */
  26583. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  26584. WOLFSSL_FAILURE);
  26585. /* Should succeed on matching email address */
  26586. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  26587. WOLFSSL_SUCCESS);
  26588. /* Should compute length internally when not provided */
  26589. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  26590. WOLFSSL_SUCCESS);
  26591. /* Should fail when email address is NULL */
  26592. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  26593. WOLFSSL_FAILURE);
  26594. X509_free(x509);
  26595. /* Should fail when x509 is NULL */
  26596. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  26597. WOLFSSL_FAILURE);
  26598. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  26599. return EXPECT_RESULT();
  26600. }
  26601. static int test_wolfSSL_DES(void)
  26602. {
  26603. EXPECT_DECLS;
  26604. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26605. const_DES_cblock myDes;
  26606. DES_cblock iv;
  26607. DES_key_schedule key;
  26608. word32 i;
  26609. DES_LONG dl;
  26610. unsigned char msg[] = "hello wolfssl";
  26611. DES_check_key(1);
  26612. DES_set_key(&myDes, &key);
  26613. /* check, check of odd parity */
  26614. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  26615. myDes[0] = 6; /* set even parity */
  26616. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26617. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  26618. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  26619. /* set odd parity for success case */
  26620. DES_set_odd_parity(&myDes);
  26621. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  26622. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  26623. myDes[3]);
  26624. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  26625. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26626. ExpectIntEQ(key[i], myDes[i]);
  26627. }
  26628. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  26629. /* check weak key */
  26630. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  26631. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26632. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  26633. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  26634. /* now do unchecked copy of a weak key over */
  26635. DES_set_key_unchecked(&myDes, &key);
  26636. /* compare arrays, should be the same */
  26637. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26638. ExpectIntEQ(key[i], myDes[i]);
  26639. }
  26640. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  26641. /* check DES_key_sched API */
  26642. XMEMSET(key, 1, sizeof(DES_key_schedule));
  26643. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  26644. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  26645. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  26646. /* compare arrays, should be the same */
  26647. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26648. ExpectIntEQ(key[i], myDes[i]);
  26649. }
  26650. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  26651. * DES_cbc_encrypt on the input */
  26652. XMEMSET(iv, 0, sizeof(DES_cblock));
  26653. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  26654. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  26655. ExpectIntEQ(dl, 480052723);
  26656. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26657. return EXPECT_RESULT();
  26658. }
  26659. static int test_wc_PemToDer(void)
  26660. {
  26661. EXPECT_DECLS;
  26662. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  26663. int ret;
  26664. DerBuffer* pDer = NULL;
  26665. const char* ca_cert = "./certs/server-cert.pem";
  26666. byte* cert_buf = NULL;
  26667. size_t cert_sz = 0;
  26668. int eccKey = 0;
  26669. EncryptedInfo info;
  26670. XMEMSET(&info, 0, sizeof(info));
  26671. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  26672. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  26673. &info, &eccKey), 0);
  26674. wc_FreeDer(&pDer);
  26675. pDer = NULL;
  26676. if (cert_buf != NULL) {
  26677. free(cert_buf);
  26678. cert_buf = NULL;
  26679. }
  26680. #ifdef HAVE_ECC
  26681. {
  26682. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  26683. byte key_buf[256] = {0};
  26684. /* Test fail of loading a key with cert type */
  26685. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  26686. key_buf[0] = '\n';
  26687. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  26688. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  26689. &pDer, NULL, &info, &eccKey)), 0);
  26690. #ifdef OPENSSL_EXTRA
  26691. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  26692. &pDer, NULL, &info, &eccKey)), 0);
  26693. #endif
  26694. wc_FreeDer(&pDer);
  26695. if (cert_buf != NULL)
  26696. free(cert_buf);
  26697. }
  26698. #endif
  26699. #endif
  26700. return EXPECT_RESULT();
  26701. }
  26702. static int test_wc_AllocDer(void)
  26703. {
  26704. EXPECT_DECLS;
  26705. #if !defined(NO_CERTS)
  26706. DerBuffer* pDer = NULL;
  26707. word32 testSize = 1024;
  26708. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  26709. BAD_FUNC_ARG);
  26710. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  26711. ExpectNotNull(pDer);
  26712. wc_FreeDer(&pDer);
  26713. #endif
  26714. return EXPECT_RESULT();
  26715. }
  26716. static int test_wc_CertPemToDer(void)
  26717. {
  26718. EXPECT_DECLS;
  26719. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  26720. const char* ca_cert = "./certs/ca-cert.pem";
  26721. byte* cert_buf = NULL;
  26722. size_t cert_sz = 0;
  26723. size_t cert_dersz = 0;
  26724. byte* cert_der = NULL;
  26725. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  26726. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26727. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26728. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  26729. (int)cert_dersz, CERT_TYPE), 0);
  26730. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  26731. BAD_FUNC_ARG);
  26732. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  26733. BAD_FUNC_ARG);
  26734. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  26735. BAD_FUNC_ARG);
  26736. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  26737. CERT_TYPE), BAD_FUNC_ARG);
  26738. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  26739. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  26740. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  26741. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  26742. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  26743. CERT_TYPE), BAD_FUNC_ARG);
  26744. if (cert_der != NULL)
  26745. free(cert_der);
  26746. if (cert_buf != NULL)
  26747. free(cert_buf);
  26748. #endif
  26749. return EXPECT_RESULT();
  26750. }
  26751. static int test_wc_KeyPemToDer(void)
  26752. {
  26753. EXPECT_DECLS;
  26754. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26755. int ret;
  26756. const byte cert_buf[] = \
  26757. "-----BEGIN PRIVATE KEY-----\n"
  26758. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  26759. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  26760. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  26761. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  26762. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  26763. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  26764. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  26765. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  26766. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  26767. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  26768. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  26769. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  26770. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  26771. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  26772. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  26773. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  26774. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  26775. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  26776. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  26777. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  26778. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  26779. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  26780. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  26781. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  26782. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  26783. "AQue5o6oVfhgLiJzMdo/77gw\n"
  26784. "-----END PRIVATE KEY-----\n";
  26785. const int cert_sz = sizeof(cert_buf);
  26786. const char cert_pw[] = "password";
  26787. int cert_dersz = 0;
  26788. byte* cert_der = NULL;
  26789. /* Bad arg: Cert buffer is NULL */
  26790. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  26791. BAD_FUNC_ARG);
  26792. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  26793. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  26794. BAD_FUNC_ARG);
  26795. /* Test normal operation */
  26796. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26797. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26798. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  26799. cert_pw), 0);
  26800. ExpectIntLE(ret, cert_sz);
  26801. if (cert_der != NULL) {
  26802. free(cert_der);
  26803. cert_der = NULL;
  26804. }
  26805. /* Test NULL for DER buffer to return needed DER buffer size */
  26806. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  26807. ExpectIntLE(ret, cert_sz);
  26808. if (EXPECT_SUCCESS())
  26809. cert_dersz = ret;
  26810. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26811. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  26812. cert_pw), 0);
  26813. ExpectIntLE(ret, cert_sz);
  26814. if (cert_der != NULL)
  26815. free(cert_der);
  26816. #endif
  26817. return EXPECT_RESULT();
  26818. }
  26819. static int test_wc_PubKeyPemToDer(void)
  26820. {
  26821. EXPECT_DECLS;
  26822. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  26823. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  26824. int ret = 0;
  26825. const char* key = "./certs/ecc-client-keyPub.pem";
  26826. byte* cert_buf = NULL;
  26827. size_t cert_sz = 0, cert_dersz = 0;
  26828. byte* cert_der = NULL;
  26829. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26830. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  26831. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  26832. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26833. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26834. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  26835. (int)cert_dersz), 0);
  26836. if (cert_der != NULL) {
  26837. free(cert_der);
  26838. cert_der = NULL;
  26839. }
  26840. /* Test NULL for DER buffer to return needed DER buffer size */
  26841. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  26842. ExpectIntLE(ret, cert_sz);
  26843. cert_dersz = ret;
  26844. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26845. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  26846. (int)cert_dersz), 0);
  26847. if (cert_der != NULL) {
  26848. free(cert_der);
  26849. }
  26850. if (cert_buf != NULL) {
  26851. free(cert_buf);
  26852. }
  26853. #endif
  26854. return EXPECT_RESULT();
  26855. }
  26856. static int test_wc_PemPubKeyToDer(void)
  26857. {
  26858. EXPECT_DECLS;
  26859. #if !defined(NO_FILESYSTEM) && \
  26860. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  26861. const char* key = "./certs/ecc-client-keyPub.pem";
  26862. size_t cert_dersz = 1024;
  26863. byte* cert_der = NULL;
  26864. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  26865. BAD_FUNC_ARG);
  26866. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26867. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  26868. if (cert_der != NULL) {
  26869. free(cert_der);
  26870. }
  26871. #endif
  26872. return EXPECT_RESULT();
  26873. }
  26874. static int test_wc_GetPubKeyDerFromCert(void)
  26875. {
  26876. EXPECT_DECLS;
  26877. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26878. int ret;
  26879. word32 idx = 0;
  26880. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26881. word32 keyDerSz = (word32)sizeof(keyDer);
  26882. DecodedCert decoded;
  26883. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  26884. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  26885. word32 certBufSz = sizeof(certBuf);
  26886. #endif
  26887. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  26888. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26889. XFILE fp = XBADFILE;
  26890. #endif
  26891. #ifndef NO_RSA
  26892. RsaKey rsaKey;
  26893. #if defined(USE_CERT_BUFFERS_2048)
  26894. byte* rsaCertDer = (byte*)client_cert_der_2048;
  26895. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  26896. #elif defined(USE_CERT_BUFFERS_1024)
  26897. byte* rsaCertDer = (byte*)client_cert_der_1024;
  26898. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  26899. #else
  26900. unsigned char rsaCertDer[TWOK_BUF];
  26901. word32 rsaCertDerSz;
  26902. #endif
  26903. #endif
  26904. #ifdef HAVE_ECC
  26905. ecc_key eccKey;
  26906. #if defined(USE_CERT_BUFFERS_256)
  26907. byte* eccCert = (byte*)cliecc_cert_der_256;
  26908. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  26909. #else
  26910. unsigned char eccCert[ONEK_BUF];
  26911. word32 eccCertSz;
  26912. XFILE fp2 = XBADFILE;
  26913. #endif
  26914. #endif
  26915. #ifndef NO_RSA
  26916. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  26917. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26918. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  26919. fp), 0);
  26920. if (fp != XBADFILE) {
  26921. XFCLOSE(fp);
  26922. fp = XBADFILE;
  26923. }
  26924. #endif
  26925. /* good test case - RSA DER cert */
  26926. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  26927. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  26928. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  26929. ExpectIntGT(keyDerSz, 0);
  26930. /* sanity check, verify we can import DER public key */
  26931. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26932. ExpectIntEQ(ret, 0);
  26933. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  26934. if (ret == 0) {
  26935. wc_FreeRsaKey(&rsaKey);
  26936. }
  26937. /* test LENGTH_ONLY_E case */
  26938. keyDerSz = 0;
  26939. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  26940. LENGTH_ONLY_E);
  26941. ExpectIntGT(keyDerSz, 0);
  26942. /* bad args: DecodedCert NULL */
  26943. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  26944. /* bad args: output key buff size */
  26945. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  26946. /* bad args: zero size output key buffer */
  26947. keyDerSz = 0;
  26948. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  26949. BAD_FUNC_ARG);
  26950. wc_FreeDecodedCert(&decoded);
  26951. /* Certificate Request Tests */
  26952. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  26953. {
  26954. XMEMSET(certBuf, 0, sizeof(certBuf));
  26955. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  26956. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  26957. if (fp != XBADFILE) {
  26958. XFCLOSE(fp);
  26959. }
  26960. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  26961. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  26962. /* good test case - RSA DER certificate request */
  26963. keyDerSz = sizeof(keyDer);
  26964. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  26965. 0);
  26966. ExpectIntGT(keyDerSz, 0);
  26967. /* sanity check, verify we can import DER public key */
  26968. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26969. ExpectIntEQ(ret, 0);
  26970. idx = 0;
  26971. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  26972. if (ret == 0) {
  26973. wc_FreeRsaKey(&rsaKey);
  26974. }
  26975. wc_FreeDecodedCert(&decoded);
  26976. }
  26977. #endif /* WOLFSSL_CERT_REQ */
  26978. #endif /* NO_RSA */
  26979. #ifdef HAVE_ECC
  26980. #ifndef USE_CERT_BUFFERS_256
  26981. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  26982. XBADFILE);
  26983. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  26984. if (fp2 != XBADFILE) {
  26985. XFCLOSE(fp2);
  26986. }
  26987. #endif
  26988. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  26989. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  26990. /* good test case - ECC */
  26991. XMEMSET(keyDer, 0, sizeof(keyDer));
  26992. keyDerSz = sizeof(keyDer);
  26993. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  26994. ExpectIntGT(keyDerSz, 0);
  26995. /* sanity check, verify we can import DER public key */
  26996. ret = wc_ecc_init(&eccKey);
  26997. ExpectIntEQ(ret, 0);
  26998. idx = 0; /* reset idx to 0, used above in RSA case */
  26999. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  27000. if (ret == 0) {
  27001. wc_ecc_free(&eccKey);
  27002. }
  27003. /* test LENGTH_ONLY_E case */
  27004. keyDerSz = 0;
  27005. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  27006. LENGTH_ONLY_E);
  27007. ExpectIntGT(keyDerSz, 0);
  27008. wc_FreeDecodedCert(&decoded);
  27009. #endif
  27010. #endif /* !NO_RSA || HAVE_ECC */
  27011. return EXPECT_RESULT();
  27012. }
  27013. static int test_wc_CheckCertSigPubKey(void)
  27014. {
  27015. EXPECT_DECLS;
  27016. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27017. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  27018. int ret;
  27019. const char* ca_cert = "./certs/ca-cert.pem";
  27020. byte* cert_buf = NULL;
  27021. size_t cert_sz = 0;
  27022. byte* cert_der = NULL;
  27023. word32 cert_dersz = 0;
  27024. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27025. word32 keyDerSz = (word32)sizeof(keyDer);
  27026. DecodedCert decoded;
  27027. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  27028. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  27029. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  27030. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  27031. (int)cert_dersz, CERT_TYPE), 0);
  27032. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  27033. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  27034. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  27035. ExpectIntGT(keyDerSz, 0);
  27036. /* Good test case. */
  27037. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  27038. keyDerSz, RSAk), 0);
  27039. /* No certificate. */
  27040. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  27041. ECDSAk), BAD_FUNC_ARG);
  27042. /* Bad cert size. */
  27043. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  27044. RSAk), 0);
  27045. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  27046. /* No public key. */
  27047. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  27048. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  27049. /* Bad public key size. */
  27050. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27051. RSAk), BAD_FUNC_ARG);
  27052. /* Wrong aglo. */
  27053. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  27054. keyDerSz, ECDSAk), ASN_PARSE_E);
  27055. wc_FreeDecodedCert(&decoded);
  27056. if (cert_der != NULL)
  27057. free(cert_der);
  27058. if (cert_buf != NULL)
  27059. free(cert_buf);
  27060. #endif
  27061. return EXPECT_RESULT();
  27062. }
  27063. static int test_wolfSSL_certs(void)
  27064. {
  27065. EXPECT_DECLS;
  27066. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27067. !defined(NO_RSA)
  27068. X509* x509ext = NULL;
  27069. #ifdef OPENSSL_ALL
  27070. X509* x509 = NULL;
  27071. WOLFSSL_X509_EXTENSION* ext = NULL;
  27072. ASN1_OBJECT* obj = NULL;
  27073. #endif
  27074. WOLFSSL* ssl = NULL;
  27075. WOLFSSL_CTX* ctx = NULL;
  27076. STACK_OF(ASN1_OBJECT)* sk = NULL;
  27077. ASN1_STRING* asn1_str = NULL;
  27078. AUTHORITY_KEYID* akey = NULL;
  27079. BASIC_CONSTRAINTS* bc = NULL;
  27080. int crit;
  27081. #ifndef NO_WOLFSSL_SERVER
  27082. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27083. #else
  27084. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27085. #endif
  27086. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27087. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27088. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27089. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27090. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27091. #endif
  27092. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27093. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27094. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27095. #endif
  27096. ExpectNotNull(ssl = SSL_new(ctx));
  27097. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27098. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27099. #endif
  27100. #ifdef HAVE_PK_CALLBACKS
  27101. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27102. #endif /* HAVE_PK_CALLBACKS */
  27103. /* create and use x509 */
  27104. #ifdef OPENSSL_ALL
  27105. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27106. WOLFSSL_FILETYPE_PEM));
  27107. #endif
  27108. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  27109. WOLFSSL_FILETYPE_PEM));
  27110. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27111. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27112. /* with loading in a new cert the check on private key should now fail */
  27113. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27114. #endif
  27115. #if defined(USE_CERT_BUFFERS_2048)
  27116. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  27117. (unsigned char*)server_cert_der_2048,
  27118. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27119. #endif
  27120. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27121. /************* Get Digest of Certificate ******************/
  27122. {
  27123. byte digest[64]; /* max digest size */
  27124. word32 digestSz;
  27125. XMEMSET(digest, 0, sizeof(digest));
  27126. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27127. WOLFSSL_SUCCESS);
  27128. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27129. WOLFSSL_SUCCESS);
  27130. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27131. WOLFSSL_FAILURE);
  27132. }
  27133. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27134. /* test and checkout X509 extensions */
  27135. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  27136. NID_basic_constraints, &crit, NULL));
  27137. ExpectIntEQ(crit, 0);
  27138. #ifdef OPENSSL_ALL
  27139. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  27140. X509_EXTENSION_free(ext);
  27141. ext = NULL;
  27142. ExpectNotNull(ext = X509_EXTENSION_new());
  27143. X509_EXTENSION_set_critical(ext, 1);
  27144. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27145. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27146. ASN1_OBJECT_free(obj);
  27147. obj = NULL;
  27148. X509_EXTENSION_free(ext);
  27149. ext = NULL;
  27150. ExpectNotNull(ext = X509_EXTENSION_new());
  27151. X509_EXTENSION_set_critical(ext, 0);
  27152. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27153. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27154. NULL);
  27155. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27156. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27157. * and X509_get_ext_d2i has created new */
  27158. asn1_str = NULL;
  27159. X509_EXTENSION_free(ext);
  27160. ext = NULL;
  27161. #endif
  27162. BASIC_CONSTRAINTS_free(bc);
  27163. bc = NULL;
  27164. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  27165. NID_key_usage, &crit, NULL));
  27166. ExpectIntEQ(crit, 1);
  27167. ExpectIntEQ(asn1_str->type, NID_key_usage);
  27168. #ifdef OPENSSL_ALL
  27169. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  27170. X509_EXTENSION_free(ext);
  27171. ext = NULL;
  27172. #endif
  27173. ASN1_STRING_free(asn1_str);
  27174. asn1_str = NULL;
  27175. #ifdef OPENSSL_ALL
  27176. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  27177. NID_ext_key_usage, &crit, NULL));
  27178. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  27179. X509_EXTENSION_free(ext);
  27180. ext = NULL;
  27181. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27182. sk = NULL;
  27183. #else
  27184. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27185. &crit, NULL);
  27186. ExpectNull(sk);
  27187. #endif
  27188. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27189. NID_authority_key_identifier, &crit, NULL));
  27190. #ifdef OPENSSL_ALL
  27191. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  27192. akey));
  27193. X509_EXTENSION_free(ext);
  27194. ext = NULL;
  27195. #endif
  27196. wolfSSL_AUTHORITY_KEYID_free(akey);
  27197. akey = NULL;
  27198. /* NID not yet supported */
  27199. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27200. NID_private_key_usage_period, &crit, NULL));
  27201. ExpectIntEQ(crit, -1);
  27202. sk_ASN1_OBJECT_free(sk);
  27203. sk = NULL;
  27204. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  27205. NID_subject_alt_name, &crit, NULL));
  27206. {
  27207. int i;
  27208. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27209. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27210. ExpectIntEQ(gen->type, GEN_DNS);
  27211. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27212. }
  27213. }
  27214. sk_GENERAL_NAME_free(sk);
  27215. sk = NULL;
  27216. /* NID not yet supported */
  27217. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27218. NID_issuer_alt_name, &crit, NULL));
  27219. ExpectIntEQ(crit, -1);
  27220. sk_ASN1_OBJECT_free(sk);
  27221. sk = NULL;
  27222. /* NID not yet supported */
  27223. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27224. NID_info_access, &crit, NULL));
  27225. sk_ASN1_OBJECT_free(sk);
  27226. sk = NULL;
  27227. /* NID not yet supported */
  27228. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27229. NID_sinfo_access, &crit, NULL));
  27230. ExpectIntEQ(crit, -1);
  27231. sk_ASN1_OBJECT_free(sk);
  27232. sk = NULL;
  27233. /* NID not yet supported */
  27234. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27235. NID_name_constraints, &crit, NULL));
  27236. ExpectIntEQ(crit, -1);
  27237. sk_ASN1_OBJECT_free(sk);
  27238. sk = NULL;
  27239. /* no cert policy set */
  27240. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27241. NID_certificate_policies, &crit, NULL));
  27242. sk_ASN1_OBJECT_free(sk);
  27243. sk = NULL;
  27244. /* NID not yet supported */
  27245. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27246. NID_policy_mappings, &crit, NULL));
  27247. ExpectIntEQ(crit, -1);
  27248. sk_ASN1_OBJECT_free(sk);
  27249. sk = NULL;
  27250. /* NID not yet supported */
  27251. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27252. NID_policy_constraints, &crit, NULL));
  27253. ExpectIntEQ(crit, -1);
  27254. sk_ASN1_OBJECT_free(sk);
  27255. sk = NULL;
  27256. /* NID not yet supported */
  27257. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27258. NID_inhibit_any_policy, &crit, NULL));
  27259. ExpectIntEQ(crit, -1);
  27260. sk_ASN1_OBJECT_free(sk);
  27261. sk = NULL;
  27262. /* NID not yet supported */
  27263. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27264. NID_tlsfeature, &crit, NULL));
  27265. ExpectIntEQ(crit, -1);
  27266. sk_ASN1_OBJECT_free(sk);
  27267. sk = NULL;
  27268. /* test invalid cases */
  27269. crit = 0;
  27270. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  27271. NULL));
  27272. ExpectIntEQ(crit, -1);
  27273. /* NULL passed for criticality. */
  27274. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  27275. NID_tlsfeature, NULL, NULL));
  27276. ExpectIntEQ(SSL_get_hit(ssl), 0);
  27277. #ifdef OPENSSL_ALL
  27278. X509_free(x509);
  27279. #endif
  27280. X509_free(x509ext);
  27281. SSL_free(ssl);
  27282. SSL_CTX_free(ctx);
  27283. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27284. return EXPECT_RESULT();
  27285. }
  27286. static int test_wolfSSL_X509_check_private_key(void)
  27287. {
  27288. EXPECT_DECLS;
  27289. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27290. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27291. X509* x509 = NULL;
  27292. EVP_PKEY* pkey = NULL;
  27293. const byte* key;
  27294. /* Check with correct key */
  27295. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27296. SSL_FILETYPE_PEM)));
  27297. key = client_key_der_2048;
  27298. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  27299. (long)sizeof_client_key_der_2048));
  27300. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  27301. EVP_PKEY_free(pkey);
  27302. pkey = NULL;
  27303. /* Check with wrong key */
  27304. key = server_key_der_2048;
  27305. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  27306. (long)sizeof_server_key_der_2048));
  27307. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  27308. /* test for incorrect parameter */
  27309. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  27310. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  27311. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  27312. EVP_PKEY_free(pkey);
  27313. X509_free(x509);
  27314. #endif
  27315. return EXPECT_RESULT();
  27316. }
  27317. static int test_wolfSSL_private_keys(void)
  27318. {
  27319. EXPECT_DECLS;
  27320. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27321. !defined(NO_FILESYSTEM)
  27322. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27323. WOLFSSL* ssl = NULL;
  27324. WOLFSSL_CTX* ctx = NULL;
  27325. EVP_PKEY* pkey = NULL;
  27326. OpenSSL_add_all_digests();
  27327. OpenSSL_add_all_algorithms();
  27328. #ifndef NO_RSA
  27329. #ifndef NO_WOLFSSL_SERVER
  27330. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27331. #else
  27332. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27333. #endif
  27334. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27335. /* Have to load a cert before you can check the private key against that
  27336. * certificates public key! */
  27337. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27338. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27339. #endif
  27340. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27341. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27342. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27343. #endif
  27344. ExpectNotNull(ssl = SSL_new(ctx));
  27345. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27346. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27347. #endif
  27348. #ifdef USE_CERT_BUFFERS_2048
  27349. {
  27350. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27351. unsigned char buf[FOURK_BUF];
  27352. word32 bufSz;
  27353. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27354. (unsigned char*)client_key_der_2048,
  27355. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27356. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27357. /* Should mismatch now that a different private key loaded */
  27358. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27359. #endif
  27360. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  27361. (unsigned char*)server_key,
  27362. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27363. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27364. /* After loading back in DER format of original key, should match */
  27365. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27366. #endif
  27367. /* test loading private key to the WOLFSSL_CTX */
  27368. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27369. (unsigned char*)client_key_der_2048,
  27370. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27371. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27372. /* Should mismatch now that a different private key loaded */
  27373. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27374. #endif
  27375. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27376. (unsigned char*)server_key,
  27377. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27378. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27379. /* After loading back in DER format of original key, should match */
  27380. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27381. #endif
  27382. /* pkey not set yet, expecting to fail */
  27383. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  27384. /* set PKEY and test again */
  27385. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27386. &server_key, (long)sizeof_server_key_der_2048));
  27387. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27388. /* reuse PKEY structure and test
  27389. * this should be checked with a memory management sanity checker */
  27390. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  27391. server_key = (const unsigned char*)server_key_der_2048;
  27392. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27393. &server_key, (long)sizeof_server_key_der_2048));
  27394. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27395. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  27396. bufSz = FOURK_BUF;
  27397. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  27398. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  27399. RSAk, NULL, 0)), 0);
  27400. server_key = (const unsigned char*)buf;
  27401. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  27402. (long)bufSz));
  27403. }
  27404. #endif
  27405. EVP_PKEY_free(pkey);
  27406. pkey = NULL;
  27407. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  27408. ssl = NULL;
  27409. SSL_CTX_free(ctx);
  27410. ctx = NULL;
  27411. #endif /* end of RSA private key match tests */
  27412. #ifdef HAVE_ECC
  27413. #ifndef NO_WOLFSSL_SERVER
  27414. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27415. #else
  27416. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27417. #endif
  27418. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  27419. WOLFSSL_FILETYPE_PEM));
  27420. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  27421. WOLFSSL_FILETYPE_PEM));
  27422. ExpectNotNull(ssl = SSL_new(ctx));
  27423. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27424. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27425. #endif
  27426. SSL_free(ssl);
  27427. ssl = NULL;
  27428. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  27429. WOLFSSL_FILETYPE_PEM));
  27430. ExpectNotNull(ssl = SSL_new(ctx));
  27431. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  27432. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27433. #endif
  27434. SSL_free(ssl);
  27435. ssl = NULL;
  27436. SSL_CTX_free(ctx);
  27437. ctx = NULL;
  27438. #endif /* end of ECC private key match tests */
  27439. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  27440. #ifndef NO_WOLFSSL_SERVER
  27441. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27442. #else
  27443. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27444. #endif
  27445. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  27446. WOLFSSL_FILETYPE_PEM));
  27447. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  27448. WOLFSSL_FILETYPE_PEM));
  27449. ExpectNotNull(ssl = SSL_new(ctx));
  27450. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27451. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27452. #endif
  27453. SSL_free(ssl);
  27454. ssl = NULL;
  27455. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  27456. WOLFSSL_FILETYPE_PEM));
  27457. ExpectNotNull(ssl = SSL_new(ctx));
  27458. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27459. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27460. #endif
  27461. SSL_free(ssl);
  27462. ssl = NULL;
  27463. SSL_CTX_free(ctx);
  27464. ctx = NULL;
  27465. #endif /* end of Ed25519 private key match tests */
  27466. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  27467. #ifndef NO_WOLFSSL_SERVER
  27468. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27469. #else
  27470. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27471. #endif
  27472. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  27473. WOLFSSL_FILETYPE_PEM));
  27474. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  27475. WOLFSSL_FILETYPE_PEM));
  27476. ExpectNotNull(ssl = SSL_new(ctx));
  27477. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27478. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27479. #endif
  27480. SSL_free(ssl);
  27481. ssl = NULL;
  27482. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  27483. WOLFSSL_FILETYPE_PEM));
  27484. ExpectNotNull(ssl = SSL_new(ctx));
  27485. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27486. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27487. #endif
  27488. SSL_free(ssl);
  27489. ssl = NULL;
  27490. SSL_CTX_free(ctx);
  27491. ctx = NULL;
  27492. #endif /* end of Ed448 private key match tests */
  27493. EVP_cleanup();
  27494. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  27495. CONF_modules_free();
  27496. ENGINE_cleanup();
  27497. CONF_modules_unload();
  27498. (void)ssl;
  27499. (void)ctx;
  27500. (void)pkey;
  27501. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  27502. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27503. return EXPECT_RESULT();
  27504. }
  27505. static int test_wolfSSL_PEM_read_PrivateKey(void)
  27506. {
  27507. EXPECT_DECLS;
  27508. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27509. && !defined(NO_FILESYSTEM)
  27510. XFILE file = XBADFILE;
  27511. const char* fname = "./certs/server-key.pem";
  27512. EVP_PKEY* pkey = NULL;
  27513. RSA* rsa = NULL;
  27514. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  27515. unsigned char* sig = NULL;
  27516. size_t sigLen;
  27517. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  27518. size_t tbsLen = sizeof(tbs);
  27519. /* Check error case. */
  27520. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  27521. /* Read in an RSA key. */
  27522. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27523. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  27524. if (file != XBADFILE)
  27525. XFCLOSE(file);
  27526. /* Make sure the key is usable by signing some data with it. */
  27527. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  27528. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  27529. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  27530. DYNAMIC_TYPE_TMP_BUFFER));
  27531. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27532. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  27533. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  27534. WOLFSSL_SUCCESS);
  27535. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27536. EVP_PKEY_CTX_free(ctx);
  27537. EVP_PKEY_free(pkey);
  27538. #endif
  27539. return EXPECT_RESULT();
  27540. }
  27541. static int test_wolfSSL_PEM_read_PUBKEY(void)
  27542. {
  27543. EXPECT_DECLS;
  27544. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27545. && !defined(NO_FILESYSTEM)
  27546. XFILE file = XBADFILE;
  27547. const char* fname = "./certs/client-keyPub.pem";
  27548. EVP_PKEY* pkey = NULL;
  27549. /* Check error case. */
  27550. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  27551. /* Read in an RSA key. */
  27552. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27553. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  27554. EVP_PKEY_free(pkey);
  27555. if (file != XBADFILE)
  27556. XFCLOSE(file);
  27557. #endif
  27558. return EXPECT_RESULT();
  27559. }
  27560. static int test_wolfSSL_PEM_PrivateKey(void)
  27561. {
  27562. EXPECT_DECLS;
  27563. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27564. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  27565. #ifndef NO_BIO
  27566. BIO* bio = NULL;
  27567. #endif
  27568. EVP_PKEY* pkey = NULL;
  27569. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27570. #ifndef NO_BIO
  27571. /* test creating new EVP_PKEY with bad arg */
  27572. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  27573. /* test loading RSA key using BIO */
  27574. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  27575. {
  27576. XFILE file = XBADFILE;
  27577. const char* fname = "./certs/server-key.pem";
  27578. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  27579. size_t sz;
  27580. byte* buf = NULL;
  27581. EVP_PKEY* pkey2 = NULL;
  27582. EVP_PKEY* pkey3 = NULL;
  27583. RSA* rsa_key = NULL;
  27584. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27585. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27586. ExpectIntGT(sz = XFTELL(file), 0);
  27587. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27588. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27589. if (buf != NULL) {
  27590. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27591. }
  27592. if (file != XBADFILE) {
  27593. XFCLOSE(file);
  27594. file = XBADFILE;
  27595. }
  27596. /* Test using BIO new mem and loading PEM private key */
  27597. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27598. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27599. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27600. buf = NULL;
  27601. BIO_free(bio);
  27602. bio = NULL;
  27603. ExpectNotNull(pkey2 = EVP_PKEY_new());
  27604. if (pkey2 != NULL) {
  27605. pkey2->type = EVP_PKEY_RSA;
  27606. }
  27607. /* Test parameter copy */
  27608. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  27609. EVP_PKEY_free(pkey2);
  27610. EVP_PKEY_free(pkey);
  27611. pkey = NULL;
  27612. /* Qt unit test case : rsa pkcs8 key */
  27613. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  27614. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27615. ExpectIntGT(sz = XFTELL(file), 0);
  27616. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27617. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27618. if (buf) {
  27619. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27620. }
  27621. if (file != XBADFILE) {
  27622. XFCLOSE(file);
  27623. file = XBADFILE;
  27624. }
  27625. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27626. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27627. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27628. buf = NULL;
  27629. BIO_free(bio);
  27630. bio = NULL;
  27631. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27632. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  27633. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  27634. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27635. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27636. #else
  27637. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27638. #endif
  27639. RSA_free(rsa_key);
  27640. EVP_PKEY_free(pkey3);
  27641. EVP_PKEY_free(pkey);
  27642. pkey = NULL;
  27643. }
  27644. #endif
  27645. /* test loading ECC key using BIO */
  27646. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27647. {
  27648. XFILE file = XBADFILE;
  27649. const char* fname = "./certs/ecc-key.pem";
  27650. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  27651. size_t sz = 0;
  27652. byte* buf = NULL;
  27653. EVP_PKEY* pkey2 = NULL;
  27654. EVP_PKEY* pkey3 = NULL;
  27655. EC_KEY* ec_key = NULL;
  27656. int nid = 0;
  27657. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27658. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27659. ExpectIntGT(sz = XFTELL(file), 0);
  27660. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27661. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27662. if (buf) {
  27663. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27664. }
  27665. if (file != XBADFILE) {
  27666. XFCLOSE(file);
  27667. file = XBADFILE;
  27668. }
  27669. /* Test using BIO new mem and loading PEM private key */
  27670. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27671. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27672. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27673. buf = NULL;
  27674. BIO_free(bio);
  27675. bio = NULL;
  27676. ExpectNotNull(pkey2 = EVP_PKEY_new());
  27677. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27678. if (pkey2 != NULL) {
  27679. pkey2->type = EVP_PKEY_EC;
  27680. }
  27681. /* Test parameter copy */
  27682. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  27683. /* Qt unit test case 1*/
  27684. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27685. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27686. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27687. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27688. #else
  27689. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27690. #endif
  27691. /* Test default digest */
  27692. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  27693. ExpectIntEQ(nid, NID_sha256);
  27694. EC_KEY_free(ec_key);
  27695. ec_key = NULL;
  27696. EVP_PKEY_free(pkey3);
  27697. pkey3 = NULL;
  27698. EVP_PKEY_free(pkey2);
  27699. pkey2 = NULL;
  27700. EVP_PKEY_free(pkey);
  27701. pkey = NULL;
  27702. /* Qt unit test case ec pkcs8 key */
  27703. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  27704. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27705. ExpectIntGT(sz = XFTELL(file), 0);
  27706. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27707. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27708. if (buf) {
  27709. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27710. }
  27711. if (file != XBADFILE) {
  27712. XFCLOSE(file);
  27713. file = XBADFILE;
  27714. }
  27715. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27716. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27717. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27718. buf = NULL;
  27719. BIO_free(bio);
  27720. bio = NULL;
  27721. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27722. /* Qt unit test case */
  27723. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27724. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27725. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27726. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27727. #else
  27728. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27729. #endif
  27730. EC_KEY_free(ec_key);
  27731. EVP_PKEY_free(pkey3);
  27732. EVP_PKEY_free(pkey);
  27733. pkey = NULL;
  27734. }
  27735. #endif
  27736. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  27737. defined(WOLFSSL_CERT_GEN))
  27738. {
  27739. #define BIO_PEM_TEST_CHAR 'a'
  27740. EVP_PKEY* pkey2 = NULL;
  27741. unsigned char extra[10];
  27742. int i;
  27743. BIO* pub_bio = NULL;
  27744. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  27745. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27746. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  27747. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27748. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  27749. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  27750. &server_key, (long)sizeof_server_key_der_2048));
  27751. ExpectNull(pkey);
  27752. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27753. &server_key, (long)sizeof_server_key_der_2048));
  27754. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  27755. NULL), WOLFSSL_FAILURE);
  27756. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  27757. NULL), WOLFSSL_FAILURE);
  27758. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  27759. NULL), WOLFSSL_SUCCESS);
  27760. ExpectIntGT(BIO_pending(bio), 0);
  27761. ExpectIntEQ(BIO_pending(bio), 1679);
  27762. /* Check if the pubkey API writes only the public key */
  27763. #ifdef WOLFSSL_KEY_GEN
  27764. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  27765. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  27766. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  27767. ExpectIntGT(BIO_pending(pub_bio), 0);
  27768. /* Previously both the private key and the pubkey calls would write
  27769. * out the private key and the PEM header was the only difference.
  27770. * The public PEM should be significantly shorter than the
  27771. * private key versison. */
  27772. ExpectIntEQ(BIO_pending(pub_bio), 451);
  27773. #endif
  27774. /* test creating new EVP_PKEY with good args */
  27775. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27776. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  27777. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  27778. /* test of reuse of EVP_PKEY */
  27779. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27780. ExpectIntEQ(BIO_pending(bio), 0);
  27781. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27782. SSL_SUCCESS);
  27783. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  27784. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27785. ExpectNotNull(pkey);
  27786. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  27787. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  27788. }
  27789. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  27790. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  27791. for (i = 0; i < 10; i++) {
  27792. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  27793. }
  27794. BIO_free(pub_bio);
  27795. BIO_free(bio);
  27796. bio = NULL;
  27797. EVP_PKEY_free(pkey);
  27798. pkey = NULL;
  27799. EVP_PKEY_free(pkey2);
  27800. }
  27801. #endif
  27802. /* key is DES encrypted */
  27803. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  27804. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  27805. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  27806. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  27807. {
  27808. XFILE f = XBADFILE;
  27809. wc_pem_password_cb* passwd_cb = NULL;
  27810. void* passwd_cb_userdata;
  27811. SSL_CTX* ctx = NULL;
  27812. char passwd[] = "bad password";
  27813. #ifndef WOLFSSL_NO_TLS12
  27814. #ifndef NO_WOLFSSL_SERVER
  27815. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27816. #else
  27817. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27818. #endif
  27819. #else
  27820. #ifndef NO_WOLFSSL_SERVER
  27821. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27822. #else
  27823. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27824. #endif
  27825. #endif
  27826. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27827. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  27828. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  27829. ExpectNull(passwd_cb_userdata =
  27830. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27831. /* fail case with password call back */
  27832. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  27833. (void*)passwd));
  27834. BIO_free(bio);
  27835. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27836. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27837. (void*)passwd));
  27838. BIO_free(bio);
  27839. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  27840. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  27841. if ((bio == NULL) && (f != XBADFILE)) {
  27842. XFCLOSE(f);
  27843. }
  27844. /* use callback that works */
  27845. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27846. (void*)"yassl123"));
  27847. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27848. EVP_PKEY_free(pkey);
  27849. pkey = NULL;
  27850. BIO_free(bio);
  27851. bio = NULL;
  27852. SSL_CTX_free(ctx);
  27853. }
  27854. #endif /* !defined(NO_DES3) */
  27855. #endif /* !NO_BIO */
  27856. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27857. {
  27858. unsigned char buf[2048];
  27859. size_t bytes = 0;
  27860. XFILE f = XBADFILE;
  27861. SSL_CTX* ctx = NULL;
  27862. #ifndef WOLFSSL_NO_TLS12
  27863. #ifndef NO_WOLFSSL_SERVER
  27864. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27865. #else
  27866. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27867. #endif
  27868. #else
  27869. #ifndef NO_WOLFSSL_SERVER
  27870. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27871. #else
  27872. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27873. #endif
  27874. #endif
  27875. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  27876. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  27877. if (f != XBADFILE)
  27878. XFCLOSE(f);
  27879. server_key = buf;
  27880. pkey = NULL;
  27881. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  27882. ExpectNull(pkey);
  27883. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  27884. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27885. EVP_PKEY_free(pkey);
  27886. pkey = NULL;
  27887. SSL_CTX_free(ctx);
  27888. server_key = NULL;
  27889. }
  27890. #endif
  27891. #ifndef NO_BIO
  27892. (void)bio;
  27893. #endif
  27894. (void)pkey;
  27895. (void)server_key;
  27896. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  27897. return EXPECT_RESULT();
  27898. }
  27899. static int test_wolfSSL_PEM_file_RSAKey(void)
  27900. {
  27901. EXPECT_DECLS;
  27902. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27903. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27904. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27905. RSA* rsa = NULL;
  27906. XFILE fp = XBADFILE;
  27907. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  27908. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  27909. if (fp != XBADFILE)
  27910. XFCLOSE(fp);
  27911. ExpectIntEQ(RSA_size(rsa), 256);
  27912. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  27913. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  27914. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  27915. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  27916. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  27917. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  27918. RSA_free(rsa);
  27919. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27920. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27921. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27922. return EXPECT_RESULT();
  27923. }
  27924. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  27925. {
  27926. EXPECT_DECLS;
  27927. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  27928. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  27929. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  27930. RSA* rsa = NULL;
  27931. XFILE f = NULL;
  27932. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  27933. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  27934. ExpectIntEQ(RSA_size(rsa), 256);
  27935. if (f != XBADFILE) {
  27936. XFCLOSE(f);
  27937. f = XBADFILE;
  27938. }
  27939. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  27940. NULL), WOLFSSL_FAILURE);
  27941. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  27942. NULL), WOLFSSL_FAILURE);
  27943. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  27944. WOLFSSL_SUCCESS);
  27945. RSA_free(rsa);
  27946. #ifdef HAVE_ECC
  27947. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  27948. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  27949. if (f != XBADFILE)
  27950. XFCLOSE(f);
  27951. #endif /* HAVE_ECC */
  27952. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27953. return EXPECT_RESULT();
  27954. }
  27955. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  27956. {
  27957. EXPECT_DECLS;
  27958. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27959. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27960. XFILE file = XBADFILE;
  27961. const char* fname = "./certs/client-keyPub.pem";
  27962. RSA *rsa = NULL;
  27963. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  27964. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27965. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  27966. ExpectIntEQ(RSA_size(rsa), 256);
  27967. RSA_free(rsa);
  27968. if (file != XBADFILE)
  27969. XFCLOSE(file);
  27970. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27971. return EXPECT_RESULT();
  27972. }
  27973. #ifndef NO_BIO
  27974. static int test_wolfSSL_PEM_bio_RSAKey(void)
  27975. {
  27976. EXPECT_DECLS;
  27977. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27978. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27979. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27980. RSA* rsa = NULL;
  27981. BIO* bio = NULL;
  27982. /* PrivateKey */
  27983. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27984. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  27985. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  27986. ExpectNotNull(rsa);
  27987. ExpectIntEQ(RSA_size(rsa), 256);
  27988. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27989. NULL), WOLFSSL_FAILURE);
  27990. BIO_free(bio);
  27991. bio = NULL;
  27992. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27993. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  27994. NULL), WOLFSSL_SUCCESS);
  27995. BIO_free(bio);
  27996. bio = NULL;
  27997. RSA_free(rsa);
  27998. rsa = NULL;
  27999. /* PUBKEY */
  28000. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  28001. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28002. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28003. ExpectIntEQ(RSA_size(rsa), 256);
  28004. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28005. BIO_free(bio);
  28006. bio = NULL;
  28007. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28008. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  28009. BIO_free(bio);
  28010. bio = NULL;
  28011. RSA_free(rsa);
  28012. rsa = NULL;
  28013. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  28014. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  28015. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28016. BIO_free(bio);
  28017. bio = NULL;
  28018. RSA_free(rsa);
  28019. rsa = NULL;
  28020. #ifdef HAVE_ECC
  28021. /* ensure that non-rsa keys do not work */
  28022. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28023. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28024. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28025. BIO_free(bio);
  28026. bio = NULL;
  28027. RSA_free(rsa);
  28028. rsa = NULL;
  28029. #endif /* HAVE_ECC */
  28030. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28031. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28032. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28033. return EXPECT_RESULT();
  28034. }
  28035. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  28036. {
  28037. EXPECT_DECLS;
  28038. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28039. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28040. RSA* rsa = NULL;
  28041. RSA* rsa_dup = NULL;
  28042. BIO* bio = NULL;
  28043. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28044. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28045. ExpectIntEQ(RSA_size(rsa), 256);
  28046. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  28047. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  28048. /* Test duplicating empty key. */
  28049. ExpectNotNull(rsa_dup = RSA_new());
  28050. ExpectNull(RSAPublicKey_dup(rsa_dup));
  28051. RSA_free(rsa_dup);
  28052. rsa_dup = NULL;
  28053. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28054. ExpectPtrNE(rsa_dup, rsa);
  28055. #endif
  28056. /* test if valgrind complains about unreleased memory */
  28057. RSA_up_ref(rsa);
  28058. RSA_free(rsa);
  28059. BIO_free(bio);
  28060. bio = NULL;
  28061. RSA_free(rsa);
  28062. rsa = NULL;
  28063. RSA_free(rsa_dup);
  28064. rsa_dup = NULL;
  28065. #ifdef HAVE_ECC
  28066. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28067. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28068. BIO_free(bio);
  28069. #endif /* HAVE_ECC */
  28070. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28071. return EXPECT_RESULT();
  28072. }
  28073. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28074. {
  28075. EXPECT_DECLS;
  28076. #ifndef HAVE_SELFTEST
  28077. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28078. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28079. DSA* dsa = NULL;
  28080. BIO* bio = NULL;
  28081. /* PrivateKey */
  28082. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28083. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28084. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28085. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  28086. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  28087. NULL), WOLFSSL_FAILURE);
  28088. BIO_free(bio);
  28089. bio = NULL;
  28090. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28091. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  28092. NULL), WOLFSSL_SUCCESS);
  28093. BIO_free(bio);
  28094. bio = NULL;
  28095. DSA_free(dsa);
  28096. dsa = NULL;
  28097. /* PUBKEY */
  28098. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28099. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28100. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28101. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  28102. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28103. BIO_free(bio);
  28104. bio = NULL;
  28105. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28106. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28107. BIO_free(bio);
  28108. bio = NULL;
  28109. DSA_free(dsa);
  28110. dsa = NULL;
  28111. #ifdef HAVE_ECC
  28112. /* ensure that non-dsa keys do not work */
  28113. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28114. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28115. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28116. BIO_free(bio);
  28117. bio = NULL;
  28118. DSA_free(dsa);
  28119. dsa = NULL;
  28120. #endif /* HAVE_ECC */
  28121. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28122. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28123. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28124. #endif /* HAVE_SELFTEST */
  28125. return EXPECT_RESULT();
  28126. }
  28127. static int test_wolfSSL_PEM_bio_ECKey(void)
  28128. {
  28129. EXPECT_DECLS;
  28130. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28131. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28132. EC_KEY* ec = NULL;
  28133. EC_KEY* ec2;
  28134. BIO* bio = NULL;
  28135. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28136. unsigned char* pem = NULL;
  28137. int pLen;
  28138. #endif
  28139. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  28140. "MAA=\n"
  28141. "-----END PUBLIC KEY-----";
  28142. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  28143. "MAA=\n"
  28144. "-----END EC PRIVATE KEY-----";
  28145. /* PrivateKey */
  28146. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28147. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28148. ec2 = NULL;
  28149. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  28150. ExpectIntEQ(ec == ec2, 1);
  28151. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28152. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  28153. NULL), WOLFSSL_FAILURE);
  28154. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28155. NULL), WOLFSSL_FAILURE);
  28156. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  28157. WOLFSSL_FAILURE);
  28158. BIO_free(bio);
  28159. bio = NULL;
  28160. /* Public key data - fail. */
  28161. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28162. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28163. BIO_free(bio);
  28164. bio = NULL;
  28165. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28166. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28167. NULL), WOLFSSL_SUCCESS);
  28168. BIO_free(bio);
  28169. bio = NULL;
  28170. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  28171. NULL),WOLFSSL_FAILURE);
  28172. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  28173. WOLFSSL_FAILURE);
  28174. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  28175. WOLFSSL_FAILURE);
  28176. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  28177. WOLFSSL_SUCCESS);
  28178. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28179. NULL), 0);
  28180. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28181. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28182. NULL), 0);
  28183. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28184. NULL), 0);
  28185. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28186. &pLen), 0);
  28187. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28188. &pLen), 0);
  28189. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28190. &pLen), 0);
  28191. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28192. NULL), 0);
  28193. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28194. &pLen), 1);
  28195. ExpectIntGT(pLen, 0);
  28196. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28197. #endif
  28198. EC_KEY_free(ec);
  28199. ec = NULL;
  28200. /* PUBKEY */
  28201. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28202. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28203. ec2 = NULL;
  28204. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  28205. ExpectIntEQ(ec == ec2, 1);
  28206. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28207. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28208. BIO_free(bio);
  28209. bio = NULL;
  28210. /* Test 0x30, 0x00 fails. */
  28211. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  28212. sizeof(ec_key_bad_1)));
  28213. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28214. BIO_free(bio);
  28215. bio = NULL;
  28216. /* Private key data - fail. */
  28217. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28218. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28219. BIO_free(bio);
  28220. bio = NULL;
  28221. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28222. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28223. BIO_free(bio);
  28224. bio = NULL;
  28225. /* Same test as above, but with a file pointer rather than a BIO. */
  28226. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28227. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28228. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28229. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  28230. EC_KEY_free(ec);
  28231. ec = NULL;
  28232. #ifndef NO_RSA
  28233. /* ensure that non-ec keys do not work */
  28234. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28235. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28236. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28237. BIO_free(bio);
  28238. bio = NULL;
  28239. EC_KEY_free(ec);
  28240. ec = NULL;
  28241. #endif /* !NO_RSA */
  28242. /* Test 0x30, 0x00 fails. */
  28243. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  28244. sizeof(ec_priv_key_bad_1)));
  28245. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28246. BIO_free(bio);
  28247. bio = NULL;
  28248. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28249. return EXPECT_RESULT();
  28250. }
  28251. static int test_wolfSSL_PEM_PUBKEY(void)
  28252. {
  28253. EXPECT_DECLS;
  28254. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28255. BIO* bio = NULL;
  28256. EVP_PKEY* pkey = NULL;
  28257. /* test creating new EVP_PKEY with bad arg */
  28258. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28259. /* test loading ECC key using BIO */
  28260. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28261. {
  28262. XFILE file = XBADFILE;
  28263. const char* fname = "./certs/ecc-client-keyPub.pem";
  28264. size_t sz;
  28265. byte* buf = NULL;
  28266. EVP_PKEY* pkey2 = NULL;
  28267. EC_KEY* ec_key = NULL;
  28268. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  28269. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  28270. ExpectIntGT(sz = XFTELL(file), 0);
  28271. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  28272. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28273. if (buf != NULL) {
  28274. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  28275. }
  28276. if (file != XBADFILE) {
  28277. XFCLOSE(file);
  28278. }
  28279. /* Test using BIO new mem and loading PEM private key */
  28280. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28281. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28282. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28283. BIO_free(bio);
  28284. bio = NULL;
  28285. /* Qt unit test case*/
  28286. ExpectNotNull(pkey2 = EVP_PKEY_new());
  28287. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28288. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28289. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28290. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28291. #else
  28292. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28293. #endif
  28294. EC_KEY_free(ec_key);
  28295. EVP_PKEY_free(pkey2);
  28296. EVP_PKEY_free(pkey);
  28297. pkey = NULL;
  28298. }
  28299. #endif
  28300. (void)bio;
  28301. (void)pkey;
  28302. #endif
  28303. return EXPECT_RESULT();
  28304. }
  28305. #endif /* !NO_BIO */
  28306. static int test_DSA_do_sign_verify(void)
  28307. {
  28308. EXPECT_DECLS;
  28309. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28310. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28311. !defined(NO_DSA)
  28312. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28313. DSA_SIG* sig = NULL;
  28314. DSA* dsa = NULL;
  28315. word32 bytes;
  28316. byte sigBin[DSA_SIG_SIZE];
  28317. int dsacheck;
  28318. #ifdef USE_CERT_BUFFERS_1024
  28319. byte tmp[ONEK_BUF];
  28320. XMEMSET(tmp, 0, sizeof(tmp));
  28321. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28322. bytes = sizeof_dsa_key_der_1024;
  28323. #elif defined(USE_CERT_BUFFERS_2048)
  28324. byte tmp[TWOK_BUF];
  28325. XMEMSET(tmp, 0, sizeof(tmp));
  28326. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28327. bytes = sizeof_dsa_key_der_2048;
  28328. #else
  28329. byte tmp[TWOK_BUF];
  28330. XFILE fp = XBADFILE;
  28331. XMEMSET(tmp, 0, sizeof(tmp));
  28332. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  28333. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  28334. if (fp != XBADFILE)
  28335. XFCLOSE(fp);
  28336. #endif /* END USE_CERT_BUFFERS_1024 */
  28337. XMEMSET(digest, 202, sizeof(digest));
  28338. ExpectNotNull(dsa = DSA_new());
  28339. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28340. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28341. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28342. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28343. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28344. DSA_SIG_free(sig);
  28345. DSA_free(dsa);
  28346. #endif
  28347. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28348. return EXPECT_RESULT();
  28349. }
  28350. static int test_wolfSSL_tmp_dh(void)
  28351. {
  28352. EXPECT_DECLS;
  28353. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28354. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28355. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28356. byte buff[6000];
  28357. char file[] = "./certs/dsaparams.pem";
  28358. XFILE f = XBADFILE;
  28359. int bytes = 0;
  28360. DSA* dsa = NULL;
  28361. DH* dh = NULL;
  28362. #if defined(WOLFSSL_DH_EXTRA) && \
  28363. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28364. DH* dh2 = NULL;
  28365. #endif
  28366. BIO* bio = NULL;
  28367. SSL* ssl = NULL;
  28368. SSL_CTX* ctx = NULL;
  28369. #ifndef NO_WOLFSSL_SERVER
  28370. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28371. #else
  28372. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28373. #endif
  28374. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  28375. WOLFSSL_FILETYPE_PEM));
  28376. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  28377. WOLFSSL_FILETYPE_PEM));
  28378. ExpectNotNull(ssl = SSL_new(ctx));
  28379. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  28380. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  28381. if (f != XBADFILE)
  28382. XFCLOSE(f);
  28383. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  28384. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28385. ExpectNotNull(dsa);
  28386. dh = wolfSSL_DSA_dup_DH(dsa);
  28387. ExpectNotNull(dh);
  28388. #if defined(WOLFSSL_DH_EXTRA) && \
  28389. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28390. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  28391. #endif
  28392. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28393. #ifndef NO_WOLFSSL_SERVER
  28394. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28395. #else
  28396. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28397. #endif
  28398. BIO_free(bio);
  28399. DSA_free(dsa);
  28400. DH_free(dh);
  28401. #if defined(WOLFSSL_DH_EXTRA) && \
  28402. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28403. DH_free(dh2);
  28404. #endif
  28405. SSL_free(ssl);
  28406. SSL_CTX_free(ctx);
  28407. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28408. #endif
  28409. return EXPECT_RESULT();
  28410. }
  28411. static int test_wolfSSL_ctrl(void)
  28412. {
  28413. EXPECT_DECLS;
  28414. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28415. byte buff[6000];
  28416. BIO* bio = NULL;
  28417. int bytes;
  28418. BUF_MEM* ptr = NULL;
  28419. XMEMSET(buff, 0, sizeof(buff));
  28420. bytes = sizeof(buff);
  28421. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  28422. ExpectNotNull(BIO_s_socket());
  28423. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28424. /* needs tested after stubs filled out @TODO
  28425. SSL_ctrl
  28426. SSL_CTX_ctrl
  28427. */
  28428. BIO_free(bio);
  28429. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28430. return EXPECT_RESULT();
  28431. }
  28432. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28433. {
  28434. EXPECT_DECLS;
  28435. #ifdef OPENSSL_EXTRA
  28436. static const unsigned char pw[] = "password";
  28437. static const int pwSz = sizeof(pw) - 1;
  28438. size_t checkPwSz = 0;
  28439. const unsigned char* checkPw = NULL;
  28440. WOLFSSL_EVP_PKEY* key = NULL;
  28441. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28442. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28443. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  28444. pwSz));
  28445. if (key != NULL) {
  28446. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  28447. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  28448. ExpectIntEQ(key->pkey_sz, pwSz);
  28449. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28450. }
  28451. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28452. ExpectIntEQ((int)checkPwSz, pwSz);
  28453. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28454. wolfSSL_EVP_PKEY_free(key);
  28455. key = NULL;
  28456. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  28457. 0));
  28458. ExpectIntEQ(key->pkey_sz, 0);
  28459. if (EXPECT_SUCCESS()) {
  28460. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  28461. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28462. }
  28463. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  28464. ExpectIntEQ((int)checkPwSz, 0);
  28465. wolfSSL_EVP_PKEY_free(key);
  28466. key = NULL;
  28467. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  28468. 0));
  28469. ExpectIntEQ(key->pkey_sz, 0);
  28470. if (EXPECT_SUCCESS()) {
  28471. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  28472. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28473. }
  28474. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  28475. ExpectIntEQ((int)checkPwSz, 0);
  28476. wolfSSL_EVP_PKEY_free(key);
  28477. key = NULL;
  28478. #endif /* OPENSSL_EXTRA */
  28479. return EXPECT_RESULT();
  28480. }
  28481. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  28482. {
  28483. EXPECT_DECLS;
  28484. #ifdef OPENSSL_EXTRA
  28485. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  28486. const char *priv = "ABCDEFGHIJKLMNOP";
  28487. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  28488. WOLFSSL_EVP_PKEY* key = NULL;
  28489. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28490. NULL, NULL, AES_128_KEY_SIZE, cipher));
  28491. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28492. NULL, (const unsigned char *)priv, 0, cipher));
  28493. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28494. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  28495. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28496. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  28497. wolfSSL_EVP_PKEY_free(key);
  28498. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  28499. #endif /* OPENSSL_EXTRA */
  28500. return EXPECT_RESULT();
  28501. }
  28502. static int test_wolfSSL_EVP_Digest(void)
  28503. {
  28504. EXPECT_DECLS;
  28505. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28506. const char* in = "abc";
  28507. int inLen = (int)XSTRLEN(in);
  28508. byte out[WC_SHA256_DIGEST_SIZE];
  28509. unsigned int outLen;
  28510. const char* expOut =
  28511. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  28512. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  28513. "\x15\xAD";
  28514. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  28515. "SHA256", NULL), 1);
  28516. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  28517. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  28518. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  28519. return EXPECT_RESULT();
  28520. }
  28521. static int test_wolfSSL_EVP_Digest_all(void)
  28522. {
  28523. EXPECT_DECLS;
  28524. #ifdef OPENSSL_EXTRA
  28525. const char* digests[] = {
  28526. #ifndef NO_MD5
  28527. "MD5",
  28528. #endif
  28529. #ifndef NO_SHA
  28530. "SHA",
  28531. #endif
  28532. #ifdef WOLFSSL_SHA224
  28533. "SHA224",
  28534. #endif
  28535. #ifndef NO_SHA256
  28536. "SHA256",
  28537. #endif
  28538. #ifdef WOLFSSL_SHA384
  28539. "SHA384",
  28540. #endif
  28541. #ifdef WOLFSSL_SHA512
  28542. "SHA512",
  28543. #endif
  28544. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  28545. "SHA512_224",
  28546. #endif
  28547. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  28548. "SHA512_256",
  28549. #endif
  28550. #ifdef WOLFSSL_SHA3
  28551. #ifndef WOLFSSL_NOSHA3_224
  28552. "SHA3_224",
  28553. #endif
  28554. #ifndef WOLFSSL_NOSHA3_256
  28555. "SHA3_256",
  28556. #endif
  28557. "SHA3_384",
  28558. #ifndef WOLFSSL_NOSHA3_512
  28559. "SHA3_512",
  28560. #endif
  28561. #endif /* WOLFSSL_SHA3 */
  28562. NULL
  28563. };
  28564. const char** d;
  28565. const unsigned char in[] = "abc";
  28566. int inLen = XSTR_SIZEOF(in);
  28567. byte out[WC_MAX_DIGEST_SIZE];
  28568. unsigned int outLen;
  28569. for (d = digests; *d != NULL; d++) {
  28570. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  28571. ExpectIntGT(outLen, 0);
  28572. ExpectIntEQ(EVP_MD_size(*d), outLen);
  28573. }
  28574. #endif
  28575. return EXPECT_RESULT();
  28576. }
  28577. static int test_wolfSSL_EVP_MD_size(void)
  28578. {
  28579. EXPECT_DECLS;
  28580. #ifdef OPENSSL_EXTRA
  28581. WOLFSSL_EVP_MD_CTX mdCtx;
  28582. #ifdef WOLFSSL_SHA3
  28583. #ifndef WOLFSSL_NOSHA3_224
  28584. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28585. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  28586. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  28587. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  28588. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28589. #endif
  28590. #ifndef WOLFSSL_NOSHA3_256
  28591. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28592. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  28593. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  28594. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  28595. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28596. #endif
  28597. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28598. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  28599. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  28600. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  28601. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28602. #ifndef WOLFSSL_NOSHA3_512
  28603. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28604. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  28605. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  28606. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  28607. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28608. #endif
  28609. #endif /* WOLFSSL_SHA3 */
  28610. #ifndef NO_SHA256
  28611. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28612. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  28613. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28614. WC_SHA256_DIGEST_SIZE);
  28615. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28616. WC_SHA256_BLOCK_SIZE);
  28617. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  28618. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  28619. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28620. #endif
  28621. #ifndef NO_MD5
  28622. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28623. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  28624. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28625. WC_MD5_DIGEST_SIZE);
  28626. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28627. WC_MD5_BLOCK_SIZE);
  28628. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  28629. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  28630. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28631. #endif
  28632. #ifdef WOLFSSL_SHA224
  28633. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28634. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  28635. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28636. WC_SHA224_DIGEST_SIZE);
  28637. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28638. WC_SHA224_BLOCK_SIZE);
  28639. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  28640. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  28641. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28642. #endif
  28643. #ifdef WOLFSSL_SHA384
  28644. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28645. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  28646. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28647. WC_SHA384_DIGEST_SIZE);
  28648. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28649. WC_SHA384_BLOCK_SIZE);
  28650. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  28651. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  28652. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28653. #endif
  28654. #ifdef WOLFSSL_SHA512
  28655. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28656. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  28657. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28658. WC_SHA512_DIGEST_SIZE);
  28659. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28660. WC_SHA512_BLOCK_SIZE);
  28661. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  28662. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  28663. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28664. #endif
  28665. #ifndef NO_SHA
  28666. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28667. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  28668. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28669. WC_SHA_DIGEST_SIZE);
  28670. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28671. WC_SHA_BLOCK_SIZE);
  28672. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28673. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28674. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28675. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28676. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  28677. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28678. WC_SHA_DIGEST_SIZE);
  28679. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28680. WC_SHA_BLOCK_SIZE);
  28681. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28682. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28683. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28684. #endif
  28685. /* error case */
  28686. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28687. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  28688. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28689. BAD_FUNC_ARG);
  28690. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  28691. /* Cleanup is valid on uninit'ed struct */
  28692. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28693. #endif /* OPENSSL_EXTRA */
  28694. return EXPECT_RESULT();
  28695. }
  28696. static int test_wolfSSL_EVP_MD_pkey_type(void)
  28697. {
  28698. EXPECT_DECLS;
  28699. #ifdef OPENSSL_EXTRA
  28700. const WOLFSSL_EVP_MD* md;
  28701. #ifndef NO_MD5
  28702. ExpectNotNull(md = EVP_md5());
  28703. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  28704. #endif
  28705. #ifndef NO_SHA
  28706. ExpectNotNull(md = EVP_sha1());
  28707. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  28708. #endif
  28709. #ifdef WOLFSSL_SHA224
  28710. ExpectNotNull(md = EVP_sha224());
  28711. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  28712. #endif
  28713. ExpectNotNull(md = EVP_sha256());
  28714. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  28715. #ifdef WOLFSSL_SHA384
  28716. ExpectNotNull(md = EVP_sha384());
  28717. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  28718. #endif
  28719. #ifdef WOLFSSL_SHA512
  28720. ExpectNotNull(md = EVP_sha512());
  28721. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  28722. #endif
  28723. #endif
  28724. return EXPECT_RESULT();
  28725. }
  28726. #ifdef OPENSSL_EXTRA
  28727. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  28728. size_t testKeySz, const char* testData, size_t testDataSz,
  28729. const byte* testResult, size_t testResultSz)
  28730. {
  28731. EXPECT_DECLS;
  28732. unsigned char check[WC_MAX_DIGEST_SIZE];
  28733. size_t checkSz = -1;
  28734. WOLFSSL_EVP_PKEY* key = NULL;
  28735. WOLFSSL_EVP_MD_CTX mdCtx;
  28736. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  28737. testKey, (int)testKeySz));
  28738. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28739. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28740. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28741. (unsigned int)testDataSz), 1);
  28742. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28743. ExpectIntEQ((int)checkSz, (int)testResultSz);
  28744. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28745. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28746. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28747. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28748. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28749. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28750. (unsigned int)testDataSz), 1);
  28751. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28752. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28753. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28754. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28755. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28756. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28757. ExpectIntEQ((int)checkSz, (int)testResultSz);
  28758. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28759. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28760. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28761. (unsigned int)testDataSz - 4), 1);
  28762. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28763. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28764. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28765. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28766. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28767. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28768. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28769. (unsigned int)testDataSz - 4), 1);
  28770. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28771. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28772. wolfSSL_EVP_PKEY_free(key);
  28773. return EXPECT_RESULT();
  28774. }
  28775. #endif
  28776. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  28777. {
  28778. EXPECT_DECLS;
  28779. #ifdef OPENSSL_EXTRA
  28780. static const unsigned char testKey[] =
  28781. {
  28782. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28783. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28784. 0x0b, 0x0b, 0x0b, 0x0b
  28785. };
  28786. static const char testData[] = "Hi There";
  28787. #ifdef WOLFSSL_SHA224
  28788. static const unsigned char testResultSha224[] =
  28789. {
  28790. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  28791. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  28792. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  28793. 0x53, 0x68, 0x4b, 0x22
  28794. };
  28795. #endif
  28796. #ifndef NO_SHA256
  28797. static const unsigned char testResultSha256[] =
  28798. {
  28799. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  28800. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  28801. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  28802. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  28803. };
  28804. #endif
  28805. #ifdef WOLFSSL_SHA384
  28806. static const unsigned char testResultSha384[] =
  28807. {
  28808. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  28809. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  28810. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  28811. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  28812. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  28813. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  28814. };
  28815. #endif
  28816. #ifdef WOLFSSL_SHA512
  28817. static const unsigned char testResultSha512[] =
  28818. {
  28819. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  28820. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  28821. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  28822. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  28823. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  28824. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  28825. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  28826. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  28827. };
  28828. #endif
  28829. #ifdef WOLFSSL_SHA3
  28830. #ifndef WOLFSSL_NOSHA3_224
  28831. static const unsigned char testResultSha3_224[] =
  28832. {
  28833. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  28834. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  28835. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  28836. 0xf3, 0xc8, 0x60, 0xf7
  28837. };
  28838. #endif
  28839. #ifndef WOLFSSL_NOSHA3_256
  28840. static const unsigned char testResultSha3_256[] =
  28841. {
  28842. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  28843. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  28844. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  28845. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  28846. };
  28847. #endif
  28848. #ifndef WOLFSSL_NOSHA3_384
  28849. static const unsigned char testResultSha3_384[] =
  28850. {
  28851. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  28852. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  28853. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  28854. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  28855. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  28856. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  28857. };
  28858. #endif
  28859. #ifndef WOLFSSL_NOSHA3_512
  28860. static const unsigned char testResultSha3_512[] =
  28861. {
  28862. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  28863. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  28864. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  28865. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  28866. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  28867. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  28868. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  28869. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  28870. };
  28871. #endif
  28872. #endif
  28873. #ifndef NO_SHA256
  28874. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  28875. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  28876. sizeof(testResultSha256)), TEST_SUCCESS);
  28877. #endif
  28878. #ifdef WOLFSSL_SHA224
  28879. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  28880. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  28881. sizeof(testResultSha224)), TEST_SUCCESS);
  28882. #endif
  28883. #ifdef WOLFSSL_SHA384
  28884. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  28885. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  28886. sizeof(testResultSha384)), TEST_SUCCESS);
  28887. #endif
  28888. #ifdef WOLFSSL_SHA512
  28889. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  28890. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  28891. sizeof(testResultSha512)), TEST_SUCCESS);
  28892. #endif
  28893. #ifdef WOLFSSL_SHA3
  28894. #ifndef WOLFSSL_NOSHA3_224
  28895. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  28896. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  28897. sizeof(testResultSha3_224)), TEST_SUCCESS);
  28898. #endif
  28899. #ifndef WOLFSSL_NOSHA3_256
  28900. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  28901. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  28902. sizeof(testResultSha3_256)), TEST_SUCCESS);
  28903. #endif
  28904. #ifndef WOLFSSL_NOSHA3_384
  28905. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  28906. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  28907. sizeof(testResultSha3_384)), TEST_SUCCESS);
  28908. #endif
  28909. #ifndef WOLFSSL_NOSHA3_512
  28910. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  28911. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  28912. sizeof(testResultSha3_512)), TEST_SUCCESS);
  28913. #endif
  28914. #endif
  28915. #endif /* OPENSSL_EXTRA */
  28916. return EXPECT_RESULT();
  28917. }
  28918. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  28919. {
  28920. EXPECT_DECLS;
  28921. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  28922. defined(USE_CERT_BUFFERS_2048)
  28923. WOLFSSL_EVP_PKEY* privKey = NULL;
  28924. WOLFSSL_EVP_PKEY* pubKey = NULL;
  28925. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  28926. const char testData[] = "Hi There";
  28927. WOLFSSL_EVP_MD_CTX mdCtx;
  28928. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  28929. int ret;
  28930. size_t checkSz = -1;
  28931. int sz = 2048 / 8;
  28932. const unsigned char* cp;
  28933. const unsigned char* p;
  28934. unsigned char check[2048/8];
  28935. size_t i;
  28936. int paddings[] = {
  28937. RSA_PKCS1_PADDING,
  28938. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  28939. RSA_PKCS1_PSS_PADDING,
  28940. #endif
  28941. };
  28942. cp = client_key_der_2048;
  28943. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  28944. sizeof_client_key_der_2048)));
  28945. p = client_keypub_der_2048;
  28946. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28947. sizeof_client_keypub_der_2048)));
  28948. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28949. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  28950. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28951. NULL, privKey), 1);
  28952. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28953. (unsigned int)XSTRLEN(testData)), 1);
  28954. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28955. ExpectIntEQ((int)checkSz, sz);
  28956. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28957. ExpectIntEQ((int)checkSz,sz);
  28958. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28959. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28960. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  28961. ExpectIntEQ(ret, 1);
  28962. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28963. ExpectIntEQ(ret, 1);
  28964. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28965. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28966. NULL, pubKey), 1);
  28967. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28968. (unsigned int)XSTRLEN(testData)),
  28969. 1);
  28970. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28971. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28972. ExpectIntEQ(ret, 1);
  28973. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28974. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28975. NULL, privKey), 1);
  28976. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28977. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28978. ExpectIntEQ((int)checkSz, sz);
  28979. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28980. ExpectIntEQ((int)checkSz, sz);
  28981. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28982. (unsigned int)XSTRLEN(testData) - 4), 1);
  28983. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28984. ExpectIntEQ((int)checkSz, sz);
  28985. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28986. ExpectIntEQ(ret, 1);
  28987. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28988. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28989. NULL, pubKey), 1);
  28990. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28991. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28992. (unsigned int)XSTRLEN(testData) - 4),
  28993. 1);
  28994. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28995. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28996. ExpectIntEQ(ret, 1);
  28997. /* Check all signing padding types */
  28998. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  28999. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29000. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  29001. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  29002. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29003. paddings[i]), 1);
  29004. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29005. (unsigned int)XSTRLEN(testData)), 1);
  29006. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29007. ExpectIntEQ((int)checkSz, sz);
  29008. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29009. ExpectIntEQ((int)checkSz,sz);
  29010. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29011. ExpectIntEQ(ret, 1);
  29012. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29013. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  29014. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  29015. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29016. paddings[i]), 1);
  29017. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29018. (unsigned int)XSTRLEN(testData)), 1);
  29019. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29020. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29021. ExpectIntEQ(ret, 1);
  29022. }
  29023. wolfSSL_EVP_PKEY_free(pubKey);
  29024. wolfSSL_EVP_PKEY_free(privKey);
  29025. #endif
  29026. return EXPECT_RESULT();
  29027. }
  29028. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  29029. {
  29030. EXPECT_DECLS;
  29031. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29032. WOLFSSL_EVP_PKEY* privKey = NULL;
  29033. WOLFSSL_EVP_PKEY* pubKey = NULL;
  29034. const char testData[] = "Hi There";
  29035. WOLFSSL_EVP_MD_CTX mdCtx;
  29036. int ret;
  29037. size_t checkSz = -1;
  29038. const unsigned char* cp;
  29039. const unsigned char* p;
  29040. unsigned char check[2048/8];
  29041. cp = ecc_clikey_der_256;
  29042. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  29043. sizeof_ecc_clikey_der_256));
  29044. p = ecc_clikeypub_der_256;
  29045. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29046. sizeof_ecc_clikeypub_der_256)));
  29047. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29048. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29049. NULL, privKey), 1);
  29050. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29051. (unsigned int)XSTRLEN(testData)), 1);
  29052. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29053. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29054. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29055. ExpectIntEQ(ret, 1);
  29056. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29057. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29058. NULL, pubKey), 1);
  29059. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29060. (unsigned int)XSTRLEN(testData)),
  29061. 1);
  29062. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29063. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29064. ExpectIntEQ(ret, 1);
  29065. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29066. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29067. NULL, privKey), 1);
  29068. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29069. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29070. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29071. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29072. (unsigned int)XSTRLEN(testData) - 4), 1);
  29073. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29074. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29075. ExpectIntEQ(ret, 1);
  29076. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29077. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29078. NULL, pubKey), 1);
  29079. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29080. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29081. (unsigned int)XSTRLEN(testData) - 4),
  29082. 1);
  29083. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29084. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29085. ExpectIntEQ(ret, 1);
  29086. wolfSSL_EVP_PKEY_free(pubKey);
  29087. wolfSSL_EVP_PKEY_free(privKey);
  29088. #endif
  29089. return EXPECT_RESULT();
  29090. }
  29091. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29092. {
  29093. EXPECT_DECLS;
  29094. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29095. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29096. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29097. char caFile[] = "./certs/client-ca.pem";
  29098. char clientFile[] = "./certs/client-cert.pem";
  29099. SSL_CTX* ctx = NULL;
  29100. X509* x509 = NULL;
  29101. BIO *bio = NULL;
  29102. X509 *cert = NULL;
  29103. X509 *ca = NULL;
  29104. STACK_OF(X509) *chain = NULL;
  29105. STACK_OF(X509) *chain2 = NULL;
  29106. #ifndef NO_WOLFSSL_SERVER
  29107. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29108. #else
  29109. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29110. #endif
  29111. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  29112. WOLFSSL_FILETYPE_PEM));
  29113. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29114. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  29115. WOLFSSL_FILETYPE_PEM));
  29116. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29117. /* additional test of getting EVP_PKEY key size from X509
  29118. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29119. * allowed with user RSA */
  29120. {
  29121. EVP_PKEY* pkey = NULL;
  29122. #if defined(HAVE_ECC)
  29123. X509* ecX509 = NULL;
  29124. #endif /* HAVE_ECC */
  29125. ExpectNotNull(pkey = X509_get_pubkey(x509));
  29126. /* current RSA key is 2048 bit (256 bytes) */
  29127. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  29128. EVP_PKEY_free(pkey);
  29129. pkey = NULL;
  29130. #if defined(HAVE_ECC)
  29131. #if defined(USE_CERT_BUFFERS_256)
  29132. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29133. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29134. SSL_FILETYPE_ASN1));
  29135. #else
  29136. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  29137. cliEccCertFile, SSL_FILETYPE_PEM));
  29138. #endif
  29139. pkey = X509_get_pubkey(ecX509);
  29140. ExpectNotNull(pkey);
  29141. /* current ECC key is 256 bit (32 bytes) */
  29142. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  29143. X509_free(ecX509);
  29144. ecX509 = NULL;
  29145. EVP_PKEY_free(pkey);
  29146. pkey = NULL;
  29147. #endif /* HAVE_ECC */
  29148. }
  29149. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29150. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29151. if (EXPECT_SUCCESS()) {
  29152. x509 = NULL;
  29153. }
  29154. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29155. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  29156. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29157. #endif
  29158. SSL_CTX_free(ctx);
  29159. ctx = NULL;
  29160. #ifndef NO_WOLFSSL_SERVER
  29161. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29162. #else
  29163. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29164. #endif
  29165. /* Test haproxy use case */
  29166. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29167. /* Read Certificate */
  29168. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29169. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29170. ExpectNotNull(chain = sk_X509_new_null());
  29171. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  29172. if (EXPECT_SUCCESS()) {
  29173. ca = NULL;
  29174. }
  29175. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  29176. ExpectNotNull(ca = sk_X509_shift(chain2));
  29177. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29178. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29179. if (EXPECT_SUCCESS()) {
  29180. ca = NULL;
  29181. }
  29182. BIO_free(bio);
  29183. X509_free(cert);
  29184. X509_free(ca);
  29185. X509_free(x509);
  29186. sk_X509_pop_free(chain, X509_free);
  29187. sk_X509_pop_free(chain2, X509_free);
  29188. SSL_CTX_free(ctx);
  29189. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29190. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29191. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29192. return EXPECT_RESULT();
  29193. }
  29194. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29195. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29196. {
  29197. EXPECT_DECLS;
  29198. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29199. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29200. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29201. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29202. callback_functions client_cb;
  29203. callback_functions server_cb;
  29204. int line = 0;
  29205. int flag = ERR_TXT_STRING;
  29206. const char* file = NULL;
  29207. const char* data = NULL;
  29208. /* create a failed connection and inspect the error */
  29209. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29210. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29211. client_cb.method = wolfTLSv1_1_client_method;
  29212. server_cb.method = wolfTLSv1_2_server_method;
  29213. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  29214. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29215. ExpectNotNull(data);
  29216. /* check clearing error state */
  29217. ERR_remove_state(0);
  29218. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29219. ERR_peek_last_error_line(NULL, &line);
  29220. ExpectIntEQ(line, 0);
  29221. ERR_peek_last_error_line(&file, NULL);
  29222. ExpectNull(file);
  29223. /* retry connection to fill error queue */
  29224. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29225. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29226. client_cb.method = wolfTLSv1_1_client_method;
  29227. server_cb.method = wolfTLSv1_2_server_method;
  29228. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  29229. /* check that error code was stored */
  29230. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29231. ERR_peek_last_error_line(NULL, &line);
  29232. ExpectIntNE(line, 0);
  29233. ERR_peek_last_error_line(&file, NULL);
  29234. ExpectNotNull(file);
  29235. fprintf(stderr, "\nTesting error print out\n");
  29236. ERR_print_errors_fp(stderr);
  29237. fprintf(stderr, "Done testing print out\n\n");
  29238. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  29239. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29240. return EXPECT_RESULT();
  29241. }
  29242. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  29243. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29244. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29245. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29246. {
  29247. (void) ok;
  29248. (void) ctx;
  29249. fprintf(stderr, "ENTER verify_cb\n");
  29250. return SSL_SUCCESS;
  29251. }
  29252. #endif
  29253. static int test_wolfSSL_X509_Name_canon(void)
  29254. {
  29255. EXPECT_DECLS;
  29256. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29257. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29258. defined(WOLFSSL_CERT_GEN) && \
  29259. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29260. const long ex_hash1 = 0x0fdb2da4;
  29261. const long ex_hash2 = 0x9f3e8c9e;
  29262. X509_NAME *name = NULL;
  29263. X509 *x509 = NULL;
  29264. XFILE file = XBADFILE;
  29265. unsigned long hash = 0;
  29266. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29267. byte *pbuf = NULL;
  29268. word32 len = 0;
  29269. (void) ex_hash2;
  29270. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  29271. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29272. ExpectNotNull(name = X509_get_issuer_name(x509));
  29273. /* When output buffer is NULL, should return necessary output buffer
  29274. * length.*/
  29275. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29276. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29277. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29278. hash = (((unsigned long)digest[3] << 24) |
  29279. ((unsigned long)digest[2] << 16) |
  29280. ((unsigned long)digest[1] << 8) |
  29281. ((unsigned long)digest[0]));
  29282. ExpectIntEQ(hash, ex_hash1);
  29283. if (file != XBADFILE) {
  29284. XFCLOSE(file);
  29285. file = XBADFILE;
  29286. }
  29287. X509_free(x509);
  29288. x509 = NULL;
  29289. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29290. pbuf = NULL;
  29291. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  29292. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29293. ExpectNotNull(name = X509_get_issuer_name(x509));
  29294. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29295. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29296. hash = (((unsigned long)digest[3] << 24) |
  29297. ((unsigned long)digest[2] << 16) |
  29298. ((unsigned long)digest[1] << 8) |
  29299. ((unsigned long)digest[0]));
  29300. ExpectIntEQ(hash, ex_hash2);
  29301. if (file != XBADFILE)
  29302. XFCLOSE(file);
  29303. X509_free(x509);
  29304. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29305. #endif
  29306. return EXPECT_RESULT();
  29307. }
  29308. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29309. {
  29310. EXPECT_DECLS;
  29311. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29312. const int MAX_DIR = 4;
  29313. const char paths[][32] = {
  29314. "./certs/ed25519",
  29315. "./certs/ecc",
  29316. "./certs/crl",
  29317. "./certs/",
  29318. };
  29319. char CertCrl_path[MAX_FILENAME_SZ];
  29320. char *p;
  29321. X509_STORE* str = NULL;
  29322. X509_LOOKUP* lookup = NULL;
  29323. WOLFSSL_STACK* sk = NULL;
  29324. int len, total_len, i;
  29325. (void)sk;
  29326. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29327. /* illegal string */
  29328. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29329. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29330. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29331. SSL_FILETYPE_PEM,NULL), 0);
  29332. /* free store */
  29333. X509_STORE_free(str);
  29334. str = NULL;
  29335. /* short folder string */
  29336. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29337. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29338. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29339. SSL_FILETYPE_PEM,NULL), 1);
  29340. #if defined(WOLFSSL_INT_H)
  29341. /* only available when including internal.h */
  29342. ExpectNotNull(sk = lookup->dirs->dir_entry);
  29343. #endif
  29344. /* free store */
  29345. X509_STORE_free(str);
  29346. str = NULL;
  29347. /* typical function check */
  29348. p = &CertCrl_path[0];
  29349. total_len = 0;
  29350. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29351. len = (int)XSTRLEN((const char*)&paths[i]);
  29352. total_len += len;
  29353. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29354. p += len;
  29355. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29356. }
  29357. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29358. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29359. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29360. SSL_FILETYPE_PEM,NULL), 1);
  29361. #if defined(WOLFSSL_INT_H)
  29362. /* only available when including internal.h */
  29363. ExpectNotNull(sk = lookup->dirs->dir_entry);
  29364. #endif
  29365. X509_STORE_free(str);
  29366. #endif
  29367. return EXPECT_RESULT();
  29368. }
  29369. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29370. {
  29371. EXPECT_DECLS;
  29372. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29373. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29374. defined(WOLFSSL_SIGNER_DER_CERT)
  29375. X509_STORE_CTX* ctx = NULL;
  29376. X509_STORE* str = NULL;
  29377. X509_LOOKUP* lookup = NULL;
  29378. X509* cert1 = NULL;
  29379. X509* x509Ca = NULL;
  29380. X509* x509Svr = NULL;
  29381. X509* issuer = NULL;
  29382. WOLFSSL_STACK* sk = NULL;
  29383. X509_NAME* caName = NULL;
  29384. X509_NAME* issuerName = NULL;
  29385. XFILE file1 = XBADFILE;
  29386. int i;
  29387. int cert_count = 0;
  29388. int cmp;
  29389. char der[] = "certs/ca-cert.der";
  29390. #ifdef HAVE_CRL
  29391. char pem[][100] = {
  29392. "./certs/crl/crl.pem",
  29393. "./certs/crl/crl2.pem",
  29394. "./certs/crl/caEccCrl.pem",
  29395. "./certs/crl/eccCliCRL.pem",
  29396. "./certs/crl/eccSrvCRL.pem",
  29397. ""
  29398. };
  29399. #endif
  29400. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  29401. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29402. if (file1 != XBADFILE)
  29403. XFCLOSE(file1);
  29404. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29405. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29406. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29407. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29408. SSL_FILETYPE_PEM,NULL), 1);
  29409. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29410. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  29411. /* check if CA cert is loaded into the store */
  29412. for (i = 0; i < cert_count; i++) {
  29413. x509Ca = sk_X509_value(sk, i);
  29414. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29415. }
  29416. ExpectNotNull((x509Svr =
  29417. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29418. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29419. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29420. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29421. ExpectNotNull(issuer);
  29422. caName = X509_get_subject_name(x509Ca);
  29423. ExpectNotNull(caName);
  29424. issuerName = X509_get_subject_name(issuer);
  29425. ExpectNotNull(issuerName);
  29426. cmp = X509_NAME_cmp(caName, issuerName);
  29427. ExpectIntEQ(cmp, 0);
  29428. /* load der format */
  29429. X509_free(issuer);
  29430. issuer = NULL;
  29431. X509_STORE_CTX_free(ctx);
  29432. ctx = NULL;
  29433. X509_STORE_free(str);
  29434. str = NULL;
  29435. sk_X509_pop_free(sk, NULL);
  29436. sk = NULL;
  29437. X509_free(x509Svr);
  29438. x509Svr = NULL;
  29439. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29440. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29441. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29442. SSL_FILETYPE_ASN1,NULL), 1);
  29443. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29444. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  29445. /* check if CA cert is loaded into the store */
  29446. for (i = 0; i < cert_count; i++) {
  29447. x509Ca = sk_X509_value(sk, i);
  29448. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29449. }
  29450. X509_STORE_free(str);
  29451. str = NULL;
  29452. sk_X509_pop_free(sk, NULL);
  29453. sk = NULL;
  29454. X509_free(cert1);
  29455. cert1 = NULL;
  29456. #ifdef HAVE_CRL
  29457. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  29458. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29459. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29460. SSL_FILETYPE_PEM,NULL), 1);
  29461. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29462. "certs/server-revoked-cert.pem",
  29463. SSL_FILETYPE_PEM,NULL), 1);
  29464. if (str) {
  29465. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29466. WOLFSSL_FILETYPE_PEM), 1);
  29467. /* since store hasn't yet known the revoked cert*/
  29468. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29469. "certs/server-revoked-cert.pem",
  29470. WOLFSSL_FILETYPE_PEM), 1);
  29471. }
  29472. for (i = 0; pem[i][0] != '\0'; i++)
  29473. {
  29474. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29475. SSL_FILETYPE_PEM, NULL), 1);
  29476. }
  29477. if (str) {
  29478. /* since store knows crl list */
  29479. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29480. "certs/server-revoked-cert.pem",
  29481. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29482. }
  29483. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29484. X509_STORE_free(str);
  29485. #endif
  29486. #endif
  29487. return EXPECT_RESULT();
  29488. }
  29489. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29490. {
  29491. int res = TEST_SKIPPED;
  29492. #if defined(OPENSSL_EXTRA)
  29493. X509_STORE_CTX_cleanup(NULL);
  29494. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29495. res = TEST_SUCCESS;
  29496. #endif
  29497. return res;
  29498. }
  29499. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29500. {
  29501. EXPECT_DECLS;
  29502. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29503. X509_STORE_CTX* ctx = NULL;
  29504. X509_STORE* str = NULL;
  29505. X509* x509Ca = NULL;
  29506. X509* x509Svr = NULL;
  29507. X509* issuer = NULL;
  29508. X509_NAME* caName = NULL;
  29509. X509_NAME* issuerName = NULL;
  29510. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29511. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29512. ExpectNotNull((x509Ca =
  29513. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  29514. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  29515. ExpectNotNull((x509Svr =
  29516. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29517. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29518. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29519. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  29520. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  29521. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  29522. #ifdef WOLFSSL_SIGNER_DER_CERT
  29523. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  29524. #endif
  29525. X509_free(issuer);
  29526. X509_STORE_CTX_free(ctx);
  29527. X509_free(x509Svr);
  29528. X509_STORE_free(str);
  29529. X509_free(x509Ca);
  29530. #endif
  29531. return EXPECT_RESULT();
  29532. }
  29533. static int test_wolfSSL_PKCS7_certs(void)
  29534. {
  29535. EXPECT_DECLS;
  29536. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  29537. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  29538. STACK_OF(X509)* sk = NULL;
  29539. STACK_OF(X509_INFO)* info_sk = NULL;
  29540. PKCS7 *p7 = NULL;
  29541. BIO* bio = NULL;
  29542. const byte* p = NULL;
  29543. int buflen = 0;
  29544. int i;
  29545. /* Test twice. Once with d2i and once without to test
  29546. * that everything is free'd correctly. */
  29547. for (i = 0; i < 2; i++) {
  29548. ExpectNotNull(p7 = PKCS7_new());
  29549. if (p7 != NULL) {
  29550. p7->version = 1;
  29551. #ifdef NO_SHA
  29552. p7->hashOID = SHA256h;
  29553. #else
  29554. p7->hashOID = SHAh;
  29555. #endif
  29556. }
  29557. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29558. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29559. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29560. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  29561. ExpectNotNull(sk = sk_X509_new_null());
  29562. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  29563. X509_INFO* info = NULL;
  29564. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  29565. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  29566. if (EXPECT_SUCCESS() && (info != NULL)) {
  29567. info->x509 = NULL;
  29568. }
  29569. X509_INFO_free(info);
  29570. }
  29571. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  29572. info_sk = NULL;
  29573. BIO_free(bio);
  29574. bio = NULL;
  29575. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29576. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  29577. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  29578. sk_X509_pop_free(sk, X509_free);
  29579. }
  29580. sk = NULL;
  29581. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  29582. if (i == 0) {
  29583. PKCS7_free(p7);
  29584. p7 = NULL;
  29585. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  29586. if (p7 != NULL) {
  29587. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  29588. * them */
  29589. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  29590. }
  29591. /* PKCS7_free free's the certs */
  29592. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  29593. }
  29594. BIO_free(bio);
  29595. bio = NULL;
  29596. PKCS7_free(p7);
  29597. p7 = NULL;
  29598. }
  29599. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29600. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  29601. return EXPECT_RESULT();
  29602. }
  29603. static int test_wolfSSL_X509_STORE_CTX(void)
  29604. {
  29605. EXPECT_DECLS;
  29606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29607. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29608. X509_STORE_CTX* ctx = NULL;
  29609. X509_STORE* str = NULL;
  29610. X509* x509 = NULL;
  29611. #ifdef OPENSSL_ALL
  29612. X509* x5092 = NULL;
  29613. STACK_OF(X509) *sk = NULL;
  29614. STACK_OF(X509) *sk2 = NULL;
  29615. STACK_OF(X509) *sk3 = NULL;
  29616. #endif
  29617. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29618. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29619. ExpectNotNull((x509 =
  29620. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29621. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  29622. #ifdef OPENSSL_ALL
  29623. /* sk_X509_new only in OPENSSL_ALL */
  29624. sk = sk_X509_new_null();
  29625. ExpectNotNull(sk);
  29626. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  29627. #else
  29628. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  29629. #endif
  29630. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  29631. X509_STORE_CTX_set_error(ctx, -5);
  29632. X509_STORE_CTX_set_error(NULL, -5);
  29633. X509_STORE_CTX_free(ctx);
  29634. ctx = NULL;
  29635. #ifdef OPENSSL_ALL
  29636. sk_X509_pop_free(sk, NULL);
  29637. sk = NULL;
  29638. #endif
  29639. X509_STORE_free(str);
  29640. str = NULL;
  29641. X509_free(x509);
  29642. x509 = NULL;
  29643. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29644. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  29645. X509_STORE_CTX_free(ctx);
  29646. ctx = NULL;
  29647. #ifdef OPENSSL_ALL
  29648. /* test X509_STORE_CTX_get(1)_chain */
  29649. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  29650. SSL_FILETYPE_PEM)));
  29651. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  29652. SSL_FILETYPE_PEM)));
  29653. ExpectNotNull((sk = sk_X509_new_null()));
  29654. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  29655. if (EXPECT_FAIL()) {
  29656. X509_free(x509);
  29657. x509 = NULL;
  29658. }
  29659. ExpectNotNull((str = X509_STORE_new()));
  29660. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  29661. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  29662. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  29663. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  29664. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  29665. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  29666. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  29667. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  29668. X509_STORE_CTX_free(ctx);
  29669. ctx = NULL;
  29670. X509_STORE_free(str);
  29671. str = NULL;
  29672. /* CTX certs not freed yet */
  29673. X509_free(x5092);
  29674. x5092 = NULL;
  29675. sk_X509_pop_free(sk, NULL);
  29676. sk = NULL;
  29677. /* sk3 is dup so free here */
  29678. sk_X509_pop_free(sk3, NULL);
  29679. sk3 = NULL;
  29680. #endif
  29681. /* test X509_STORE_CTX_get/set_ex_data */
  29682. {
  29683. int i = 0, tmpData = 5;
  29684. void* tmpDataRet;
  29685. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29686. #ifdef HAVE_EX_DATA
  29687. for (i = 0; i < MAX_EX_DATA; i++) {
  29688. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29689. WOLFSSL_SUCCESS);
  29690. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29691. ExpectNotNull(tmpDataRet);
  29692. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  29693. }
  29694. #else
  29695. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29696. WOLFSSL_FAILURE);
  29697. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29698. ExpectNull(tmpDataRet);
  29699. #endif
  29700. X509_STORE_CTX_free(ctx);
  29701. ctx = NULL;
  29702. }
  29703. /* test X509_STORE_get/set_ex_data */
  29704. {
  29705. int i = 0, tmpData = 99;
  29706. void* tmpDataRet;
  29707. ExpectNotNull(str = X509_STORE_new());
  29708. #ifdef HAVE_EX_DATA
  29709. for (i = 0; i < MAX_EX_DATA; i++) {
  29710. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29711. WOLFSSL_SUCCESS);
  29712. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29713. ExpectNotNull(tmpDataRet);
  29714. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  29715. }
  29716. #else
  29717. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29718. WOLFSSL_FAILURE);
  29719. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29720. ExpectNull(tmpDataRet);
  29721. #endif
  29722. X509_STORE_free(str);
  29723. str = NULL;
  29724. }
  29725. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29726. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29727. return EXPECT_RESULT();
  29728. }
  29729. static int test_wolfSSL_X509_STORE_set_flags(void)
  29730. {
  29731. EXPECT_DECLS;
  29732. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29733. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29734. X509_STORE* store = NULL;
  29735. X509* x509 = NULL;
  29736. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  29737. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  29738. WOLFSSL_FILETYPE_PEM)));
  29739. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  29740. #ifdef HAVE_CRL
  29741. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29742. WOLFSSL_SUCCESS);
  29743. #else
  29744. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29745. NOT_COMPILED_IN);
  29746. #endif
  29747. wolfSSL_X509_free(x509);
  29748. wolfSSL_X509_STORE_free(store);
  29749. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  29750. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29751. return EXPECT_RESULT();
  29752. }
  29753. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  29754. {
  29755. EXPECT_DECLS;
  29756. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29757. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29758. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  29759. WOLFSSL_X509_STORE* store = NULL;
  29760. WOLFSSL_X509_LOOKUP* lookup = NULL;
  29761. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  29762. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  29763. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  29764. X509_FILETYPE_PEM), 1);
  29765. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  29766. X509_FILETYPE_PEM), 1);
  29767. if (store != NULL) {
  29768. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  29769. WOLFSSL_FILETYPE_PEM), 1);
  29770. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29771. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  29772. }
  29773. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  29774. X509_FILETYPE_PEM), 1);
  29775. if (store != NULL) {
  29776. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29777. WOLFSSL_FILETYPE_PEM), 1);
  29778. }
  29779. wolfSSL_X509_STORE_free(store);
  29780. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  29781. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29782. return EXPECT_RESULT();
  29783. }
  29784. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  29785. {
  29786. EXPECT_DECLS;
  29787. #if defined(OPENSSL_EXTRA)
  29788. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  29789. time_t c_time;
  29790. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  29791. c_time = 365*24*60*60;
  29792. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  29793. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  29794. WOLFSSL_USE_CHECK_TIME);
  29795. ExpectTrue(ctx->param->check_time == c_time);
  29796. wolfSSL_X509_STORE_CTX_free(ctx);
  29797. #endif /* OPENSSL_EXTRA */
  29798. return EXPECT_RESULT();
  29799. }
  29800. static int test_wolfSSL_CTX_get0_set1_param(void)
  29801. {
  29802. EXPECT_DECLS;
  29803. #if defined(OPENSSL_EXTRA)
  29804. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29805. SSL_CTX* ctx = NULL;
  29806. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29807. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  29808. char testIPv4[] = "127.0.0.1";
  29809. char testhostName[] = "foo.hoge.com";
  29810. #ifndef NO_WOLFSSL_SERVER
  29811. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29812. #else
  29813. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29814. #endif
  29815. ExpectNull(SSL_CTX_get0_param(NULL));
  29816. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  29817. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  29818. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  29819. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  29820. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  29821. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  29822. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  29823. WOLFSSL_SUCCESS);
  29824. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  29825. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  29826. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  29827. (int)XSTRLEN(testhostName)));
  29828. ExpectIntEQ(0x01, pParam->hostFlags);
  29829. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29830. /* test for incorrect parameter */
  29831. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  29832. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  29833. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  29834. SSL_CTX_free(ctx);
  29835. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  29836. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29837. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29838. return EXPECT_RESULT();
  29839. }
  29840. static int test_wolfSSL_get0_param(void)
  29841. {
  29842. EXPECT_DECLS;
  29843. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29844. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29845. SSL_CTX* ctx = NULL;
  29846. SSL* ssl = NULL;
  29847. #ifndef NO_WOLFSSL_SERVER
  29848. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29849. #else
  29850. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29851. #endif
  29852. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29853. SSL_FILETYPE_PEM));
  29854. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29855. ExpectNotNull(ssl = SSL_new(ctx));
  29856. ExpectNotNull(SSL_get0_param(ssl));
  29857. SSL_free(ssl);
  29858. SSL_CTX_free(ctx);
  29859. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29860. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29861. return EXPECT_RESULT();
  29862. }
  29863. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  29864. {
  29865. EXPECT_DECLS;
  29866. #if defined(OPENSSL_EXTRA)
  29867. const char host[] = "www.example.com";
  29868. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29869. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  29870. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  29871. if (pParam != NULL) {
  29872. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29873. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  29874. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29875. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29876. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29877. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29878. }
  29879. #endif /* OPENSSL_EXTRA */
  29880. return EXPECT_RESULT();
  29881. }
  29882. static int test_wolfSSL_set1_host(void)
  29883. {
  29884. EXPECT_DECLS;
  29885. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29886. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29887. const char host[] = "www.test_wolfSSL_set1_host.com";
  29888. const char emptyStr[] = "";
  29889. SSL_CTX* ctx = NULL;
  29890. SSL* ssl = NULL;
  29891. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29892. #ifndef NO_WOLFSSL_SERVER
  29893. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29894. #else
  29895. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29896. #endif
  29897. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29898. SSL_FILETYPE_PEM));
  29899. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29900. ExpectNotNull(ssl = SSL_new(ctx));
  29901. pParam = SSL_get0_param(ssl);
  29902. /* we should get back host string */
  29903. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  29904. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29905. /* we should get back empty string */
  29906. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  29907. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  29908. /* we should get back host string */
  29909. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  29910. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29911. /* we should get back empty string */
  29912. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  29913. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  29914. SSL_free(ssl);
  29915. SSL_CTX_free(ctx);
  29916. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29917. #endif /* OPENSSL_EXTRA */
  29918. return EXPECT_RESULT();
  29919. }
  29920. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  29921. {
  29922. EXPECT_DECLS;
  29923. #if defined(OPENSSL_EXTRA)
  29924. unsigned char buf[16] = {0};
  29925. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  29926. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  29927. /* test 127.0.0.1 */
  29928. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  29929. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  29930. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  29931. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  29932. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29933. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  29934. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  29935. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  29936. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29937. ExpectIntEQ(XSTRNCMP(param->ipasc,
  29938. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  29939. /* test 2001:db8:: */
  29940. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29941. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29942. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29943. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  29944. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29945. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  29946. /* test ::1234:5678 */
  29947. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  29948. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29949. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29950. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29951. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29952. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  29953. /* test 2001:db8::1234:5678 */
  29954. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29955. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29956. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29957. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29958. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29959. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  29960. sizeof(param->ipasc)), 0);
  29961. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  29962. /* 2001:db8:1::ab9:c0a8:102 */
  29963. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29964. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  29965. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  29966. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  29967. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29968. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  29969. sizeof(param->ipasc)), 0);
  29970. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29971. #endif /* OPENSSL_EXTRA */
  29972. return EXPECT_RESULT();
  29973. }
  29974. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  29975. {
  29976. EXPECT_DECLS;
  29977. #if defined(OPENSSL_EXTRA)
  29978. X509_STORE* store = NULL;
  29979. X509_STORE_CTX* ctx = NULL;
  29980. X509_STORE_CTX* ctx_no_init = NULL;
  29981. ExpectNotNull((store = X509_STORE_new()));
  29982. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29983. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  29984. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  29985. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  29986. /* should return NULL if ctx has not bee initialized */
  29987. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  29988. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  29989. wolfSSL_X509_STORE_CTX_free(ctx);
  29990. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  29991. X509_STORE_free(store);
  29992. #endif /* OPENSSL_EXTRA */
  29993. return EXPECT_RESULT();
  29994. }
  29995. static int test_wolfSSL_CTX_set_client_CA_list(void)
  29996. {
  29997. EXPECT_DECLS;
  29998. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29999. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  30000. WOLFSSL_CTX* ctx = NULL;
  30001. WOLFSSL* ssl = NULL;
  30002. X509_NAME* name = NULL;
  30003. STACK_OF(X509_NAME)* names = NULL;
  30004. STACK_OF(X509_NAME)* ca_list = NULL;
  30005. int names_len = 0;
  30006. int i;
  30007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30008. /* Send two X501 names in cert request */
  30009. names = SSL_load_client_CA_file(cliCertFile);
  30010. ExpectNotNull(names);
  30011. ca_list = SSL_load_client_CA_file(caCertFile);
  30012. ExpectNotNull(ca_list);
  30013. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  30014. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  30015. if (EXPECT_FAIL()) {
  30016. wolfSSL_X509_NAME_free(name);
  30017. name = NULL;
  30018. }
  30019. SSL_CTX_set_client_CA_list(ctx, names);
  30020. /* This should only free the stack structure */
  30021. sk_X509_NAME_free(ca_list);
  30022. ca_list = NULL;
  30023. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30024. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30025. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30026. for (i = 0; i < names_len; i++) {
  30027. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  30028. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  30029. }
  30030. /* Needed to be able to create ssl object */
  30031. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30032. SSL_FILETYPE_PEM));
  30033. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30034. ExpectNotNull(ssl = wolfSSL_new(ctx));
  30035. /* load again as old names are responsibility of ctx to free*/
  30036. names = SSL_load_client_CA_file(cliCertFile);
  30037. ExpectNotNull(names);
  30038. SSL_set_client_CA_list(ssl, names);
  30039. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  30040. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30041. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30042. for (i = 0; i < names_len; i++) {
  30043. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  30044. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  30045. }
  30046. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  30047. {
  30048. tcp_ready ready;
  30049. func_args server_args;
  30050. callback_functions server_cb;
  30051. THREAD_TYPE serverThread;
  30052. WOLFSSL* ssl_client = NULL;
  30053. WOLFSSL_CTX* ctx_client = NULL;
  30054. SOCKET_T sockfd = 0;
  30055. /* wolfSSL_get_client_CA_list() with handshake */
  30056. StartTCP();
  30057. InitTcpReady(&ready);
  30058. XMEMSET(&server_args, 0, sizeof(func_args));
  30059. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30060. server_args.signal = &ready;
  30061. server_args.callbacks = &server_cb;
  30062. /* we are responsible for free'ing WOLFSSL_CTX */
  30063. server_cb.ctx = ctx;
  30064. server_cb.isSharedCtx = 1;
  30065. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  30066. cliCertFile, 0));
  30067. start_thread(test_server_nofail, &server_args, &serverThread);
  30068. wait_tcp_ready(&server_args);
  30069. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30070. ExpectNotNull(ctx_client =
  30071. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30072. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  30073. ctx_client, caCertFile, 0));
  30074. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  30075. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30076. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  30077. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30078. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  30079. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30080. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30081. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30082. /* We are expecting two cert names to be sent */
  30083. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  30084. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30085. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30086. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  30087. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  30088. }
  30089. wolfSSL_shutdown(ssl_client);
  30090. wolfSSL_free(ssl_client);
  30091. wolfSSL_CTX_free(ctx_client);
  30092. CloseSocket(sockfd);
  30093. join_thread(serverThread);
  30094. FreeTcpReady(&ready);
  30095. }
  30096. #endif
  30097. wolfSSL_free(ssl);
  30098. wolfSSL_CTX_free(ctx);
  30099. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  30100. * !NO_BIO */
  30101. return EXPECT_RESULT();
  30102. }
  30103. static int test_wolfSSL_CTX_add_client_CA(void)
  30104. {
  30105. EXPECT_DECLS;
  30106. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30107. !defined(NO_WOLFSSL_CLIENT)
  30108. WOLFSSL_CTX* ctx = NULL;
  30109. WOLFSSL_X509* x509 = NULL;
  30110. WOLFSSL_X509* x509_a = NULL;
  30111. STACK_OF(X509_NAME)* ca_list = NULL;
  30112. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30113. /* Add client cert */
  30114. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  30115. SSL_FILETYPE_PEM));
  30116. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  30117. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30118. /* Add another client cert */
  30119. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30120. SSL_FILETYPE_PEM));
  30121. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30122. /* test for incorrect parameter */
  30123. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30124. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30125. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30126. X509_free(x509);
  30127. X509_free(x509_a);
  30128. SSL_CTX_free(ctx);
  30129. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30130. return EXPECT_RESULT();
  30131. }
  30132. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  30133. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  30134. {
  30135. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30136. WOLFSSL_CTX* ctx = callbacks->ctx;
  30137. WOLFSSL* ssl = NULL;
  30138. SOCKET_T sfd = 0;
  30139. SOCKET_T cfd = 0;
  30140. word16 port;
  30141. char input[1024];
  30142. int idx;
  30143. int ret, err = 0;
  30144. const char* privateName = "ech-private-name.com";
  30145. int privateNameLen = (int)XSTRLEN(privateName);
  30146. ((func_args*)args)->return_code = TEST_FAIL;
  30147. port = ((func_args*)args)->signal->port;
  30148. AssertIntEQ(WOLFSSL_SUCCESS,
  30149. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30150. AssertIntEQ(WOLFSSL_SUCCESS,
  30151. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30152. WOLFSSL_FILETYPE_PEM));
  30153. AssertIntEQ(WOLFSSL_SUCCESS,
  30154. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30155. WOLFSSL_FILETYPE_PEM));
  30156. if (callbacks->ctx_ready)
  30157. callbacks->ctx_ready(ctx);
  30158. ssl = wolfSSL_new(ctx);
  30159. /* set the sni for the server */
  30160. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  30161. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30162. CloseSocket(sfd);
  30163. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30164. if (callbacks->ssl_ready)
  30165. callbacks->ssl_ready(ssl);
  30166. do {
  30167. err = 0; /* Reset error */
  30168. ret = wolfSSL_accept(ssl);
  30169. if (ret != WOLFSSL_SUCCESS) {
  30170. err = wolfSSL_get_error(ssl, 0);
  30171. }
  30172. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30173. if (ret != WOLFSSL_SUCCESS) {
  30174. char buff[WOLFSSL_MAX_ERROR_SZ];
  30175. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  30176. }
  30177. else {
  30178. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30179. input[idx] = 0;
  30180. fprintf(stderr, "Client message: %s\n", input);
  30181. }
  30182. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  30183. privateNameLen));
  30184. ((func_args*)args)->return_code = TEST_SUCCESS;
  30185. }
  30186. if (callbacks->on_result)
  30187. callbacks->on_result(ssl);
  30188. wolfSSL_shutdown(ssl);
  30189. wolfSSL_free(ssl);
  30190. wolfSSL_CTX_free(ctx);
  30191. CloseSocket(cfd);
  30192. #ifdef FP_ECC
  30193. wc_ecc_fp_free();
  30194. #endif
  30195. WOLFSSL_RETURN_FROM_THREAD(0);
  30196. }
  30197. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  30198. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30199. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30200. {
  30201. AssertNotNull(ssl);
  30202. AssertNotNull(line);
  30203. XFILE fp;
  30204. const byte lf = '\n';
  30205. fp = XFOPEN("./MyKeyLog.txt", "a");
  30206. XFWRITE( line, 1, strlen(line),fp);
  30207. XFWRITE( (void*)&lf,1,1,fp);
  30208. XFCLOSE(fp);
  30209. }
  30210. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30211. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30212. {
  30213. EXPECT_DECLS;
  30214. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30215. !defined(NO_WOLFSSL_CLIENT)
  30216. SSL_CTX* ctx = NULL;
  30217. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30218. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30219. SSL_CTX_free(ctx);
  30220. SSL_CTX_set_keylog_callback(NULL, NULL);
  30221. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30222. return EXPECT_RESULT();
  30223. }
  30224. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30225. {
  30226. EXPECT_DECLS;
  30227. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30228. !defined(NO_WOLFSSL_CLIENT)
  30229. SSL_CTX* ctx = NULL;
  30230. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30231. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30232. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30233. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30234. SSL_CTX_set_keylog_callback(ctx, NULL );
  30235. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30236. SSL_CTX_free(ctx);
  30237. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30238. return EXPECT_RESULT();
  30239. }
  30240. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30241. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  30242. {
  30243. /* set keylog callback */
  30244. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  30245. return TEST_SUCCESS;
  30246. }
  30247. #endif
  30248. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30249. {
  30250. EXPECT_DECLS;
  30251. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30252. /* This test is intended for checking whether keylog callback is called
  30253. * in client during TLS handshake between the client and a server.
  30254. */
  30255. test_ssl_cbf server_cbf;
  30256. test_ssl_cbf client_cbf;
  30257. XFILE fp = XBADFILE;
  30258. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  30259. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  30260. server_cbf.method = wolfTLSv1_2_server_method;
  30261. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  30262. /* clean up keylog file */
  30263. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  30264. if (fp != XBADFILE) {
  30265. XFCLOSE(fp);
  30266. fp = XBADFILE;
  30267. }
  30268. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30269. &server_cbf, NULL), TEST_SUCCESS);
  30270. /* check if the keylog file exists */
  30271. char buff[300] = {0};
  30272. int found = 0;
  30273. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  30274. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  30275. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  30276. found = 1;
  30277. break;
  30278. }
  30279. }
  30280. if (fp != XBADFILE) {
  30281. XFCLOSE(fp);
  30282. }
  30283. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30284. ExpectIntEQ(found, 1);
  30285. /* clean up */
  30286. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  30287. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30288. return EXPECT_RESULT();
  30289. }
  30290. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30291. defined(HAVE_SECRET_CALLBACK)
  30292. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  30293. {
  30294. /* set keylog callback */
  30295. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  30296. return TEST_SUCCESS;
  30297. }
  30298. #endif
  30299. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30300. {
  30301. EXPECT_DECLS;
  30302. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30303. defined(HAVE_SECRET_CALLBACK)
  30304. /* This test is intended for checking whether keylog callback is called
  30305. * in client during TLS handshake between the client and a server.
  30306. */
  30307. test_ssl_cbf server_cbf;
  30308. test_ssl_cbf client_cbf;
  30309. XFILE fp = XBADFILE;
  30310. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  30311. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  30312. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30313. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  30314. /* clean up keylog file */
  30315. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  30316. if (fp != XBADFILE) {
  30317. XFCLOSE(fp);
  30318. fp = XBADFILE;
  30319. }
  30320. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30321. &server_cbf, NULL), TEST_SUCCESS);
  30322. /* check if the keylog file exists */
  30323. {
  30324. char buff[300] = {0};
  30325. int found[4] = {0};
  30326. int numfnd = 0;
  30327. int i;
  30328. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  30329. while (EXPECT_SUCCESS() &&
  30330. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  30331. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  30332. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30333. found[0] = 1;
  30334. continue;
  30335. }
  30336. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  30337. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30338. found[1] = 1;
  30339. continue;
  30340. }
  30341. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  30342. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  30343. found[2] = 1;
  30344. continue;
  30345. }
  30346. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  30347. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  30348. found[3] = 1;
  30349. continue;
  30350. }
  30351. }
  30352. if (fp != XBADFILE)
  30353. XFCLOSE(fp);
  30354. for (i = 0; i < 4; i++) {
  30355. if (found[i] != 0)
  30356. numfnd++;
  30357. }
  30358. ExpectIntEQ(numfnd, 4);
  30359. }
  30360. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  30361. return EXPECT_RESULT();
  30362. }
  30363. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  30364. defined(HAVE_IO_TESTS_DEPENDENCIES)
  30365. static int test_wolfSSL_Tls13_ECH_params(void)
  30366. {
  30367. EXPECT_DECLS;
  30368. #if !defined(NO_WOLFSSL_CLIENT)
  30369. word32 outputLen = 0;
  30370. byte testBuf[72];
  30371. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  30372. WOLFSSL *ssl = wolfSSL_new(ctx);
  30373. ExpectNotNull(ctx);
  30374. ExpectNotNull(ssl);
  30375. /* invalid ctx */
  30376. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  30377. "ech-public-name.com", 0, 0, 0));
  30378. /* invalid public name */
  30379. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  30380. 0, 0));
  30381. /* invalid algorithms */
  30382. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  30383. "ech-public-name.com", 1000, 1000, 1000));
  30384. /* invalid ctx */
  30385. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  30386. &outputLen));
  30387. /* invalid output len */
  30388. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  30389. /* invalid ssl */
  30390. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  30391. (char*)testBuf, sizeof(testBuf)));
  30392. /* invalid configs64 */
  30393. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  30394. sizeof(testBuf)));
  30395. /* invalid size */
  30396. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  30397. (char*)testBuf, 0));
  30398. /* invalid ssl */
  30399. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  30400. sizeof(testBuf)));
  30401. /* invalid configs */
  30402. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  30403. sizeof(testBuf)));
  30404. /* invalid size */
  30405. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  30406. /* invalid ssl */
  30407. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  30408. /* invalid size */
  30409. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  30410. wolfSSL_free(ssl);
  30411. wolfSSL_CTX_free(ctx);
  30412. #endif /* !NO_WOLFSSL_CLIENT */
  30413. return EXPECT_RESULT();
  30414. }
  30415. static int test_wolfSSL_Tls13_ECH(void)
  30416. {
  30417. EXPECT_DECLS;
  30418. tcp_ready ready;
  30419. func_args client_args;
  30420. func_args server_args;
  30421. THREAD_TYPE serverThread;
  30422. callback_functions server_cbf;
  30423. callback_functions client_cbf;
  30424. SOCKET_T sockfd = 0;
  30425. WOLFSSL_CTX* ctx = NULL;
  30426. WOLFSSL* ssl = NULL;
  30427. const char* publicName = "ech-public-name.com";
  30428. const char* privateName = "ech-private-name.com";
  30429. int privateNameLen = 20;
  30430. char reply[1024];
  30431. int replyLen = 0;
  30432. byte rawEchConfig[128];
  30433. word32 rawEchConfigLen = sizeof(rawEchConfig);
  30434. InitTcpReady(&ready);
  30435. ready.port = 22222;
  30436. XMEMSET(&client_args, 0, sizeof(func_args));
  30437. XMEMSET(&server_args, 0, sizeof(func_args));
  30438. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30439. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30440. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30441. /* create the server context here so we can get the ech config */
  30442. ExpectNotNull(server_cbf.ctx =
  30443. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  30444. /* generate ech config */
  30445. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  30446. publicName, 0, 0, 0));
  30447. /* get the config for the client to use */
  30448. ExpectIntEQ(WOLFSSL_SUCCESS,
  30449. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  30450. &rawEchConfigLen));
  30451. server_args.callbacks = &server_cbf;
  30452. server_args.signal = &ready;
  30453. /* start server task */
  30454. start_thread(server_task_ech, &server_args, &serverThread);
  30455. wait_tcp_ready(&server_args);
  30456. /* run as a TLS1.3 client */
  30457. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  30458. ExpectIntEQ(WOLFSSL_SUCCESS,
  30459. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30460. ExpectIntEQ(WOLFSSL_SUCCESS,
  30461. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30462. ExpectIntEQ(WOLFSSL_SUCCESS,
  30463. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30464. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30465. /* get connected the server task */
  30466. ExpectNotNull(ssl = wolfSSL_new(ctx));
  30467. /* set the ech configs for the client */
  30468. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  30469. rawEchConfigLen));
  30470. /* set the sni for the client */
  30471. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  30472. privateName, privateNameLen));
  30473. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30474. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30475. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  30476. privateNameLen);
  30477. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  30478. /* add th null terminator for string compare */
  30479. reply[replyLen] = 0;
  30480. /* check that the server replied with the private name */
  30481. ExpectStrEQ(privateName, reply);
  30482. wolfSSL_free(ssl);
  30483. wolfSSL_CTX_free(ctx);
  30484. CloseSocket(sockfd);
  30485. join_thread(serverThread);
  30486. FreeTcpReady(&ready);
  30487. return EXPECT_RESULT();
  30488. }
  30489. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  30490. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30491. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30492. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30493. static int post_auth_version_cb(WOLFSSL* ssl)
  30494. {
  30495. EXPECT_DECLS;
  30496. /* do handshake and then test version error */
  30497. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30498. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30499. return EXPECT_RESULT();
  30500. }
  30501. static int post_auth_version_client_cb(WOLFSSL* ssl)
  30502. {
  30503. EXPECT_DECLS;
  30504. /* do handshake and then test version error */
  30505. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30506. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30507. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30508. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30509. /* check was added to error queue */
  30510. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30511. /* check the string matches expected string */
  30512. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30513. "WRONG_SSL_VERSION");
  30514. #endif
  30515. return EXPECT_RESULT();
  30516. }
  30517. static int post_auth_cb(WOLFSSL* ssl)
  30518. {
  30519. EXPECT_DECLS;
  30520. WOLFSSL_X509* x509 = NULL;
  30521. /* do handshake and then test version error */
  30522. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30523. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30524. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30525. wolfSSL_X509_free(x509);
  30526. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30527. return EXPECT_RESULT();
  30528. }
  30529. static int set_post_auth_cb(WOLFSSL* ssl)
  30530. {
  30531. if (!wolfSSL_is_server(ssl)) {
  30532. EXPECT_DECLS;
  30533. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30534. return EXPECT_RESULT();
  30535. }
  30536. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30537. return TEST_SUCCESS;
  30538. }
  30539. #endif
  30540. static int test_wolfSSL_Tls13_postauth(void)
  30541. {
  30542. EXPECT_DECLS;
  30543. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30544. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30545. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30546. test_ssl_cbf server_cbf;
  30547. test_ssl_cbf client_cbf;
  30548. /* test version failure doing post auth with TLS 1.2 connection */
  30549. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  30550. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  30551. server_cbf.method = wolfTLSv1_2_server_method;
  30552. server_cbf.ssl_ready = set_post_auth_cb;
  30553. server_cbf.on_result = post_auth_version_cb;
  30554. client_cbf.ssl_ready = set_post_auth_cb;
  30555. client_cbf.on_result = post_auth_version_client_cb;
  30556. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30557. &server_cbf, NULL), TEST_SUCCESS);
  30558. /* tests on post auth with TLS 1.3 */
  30559. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  30560. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  30561. server_cbf.method = wolfTLSv1_3_server_method;
  30562. server_cbf.ssl_ready = set_post_auth_cb;
  30563. client_cbf.ssl_ready = set_post_auth_cb;
  30564. server_cbf.on_result = post_auth_cb;
  30565. client_cbf.on_result = NULL;
  30566. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30567. &server_cbf, NULL), TEST_SUCCESS);
  30568. #endif
  30569. return EXPECT_RESULT();
  30570. }
  30571. static int test_wolfSSL_X509_NID(void)
  30572. {
  30573. EXPECT_DECLS;
  30574. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30575. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  30576. int sigType;
  30577. int nameSz;
  30578. X509* cert = NULL;
  30579. EVP_PKEY* pubKeyTmp = NULL;
  30580. X509_NAME* name = NULL;
  30581. char commonName[80];
  30582. char countryName[80];
  30583. char localityName[80];
  30584. char stateName[80];
  30585. char orgName[80];
  30586. char orgUnit[80];
  30587. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  30588. /* convert cert from DER to internal WOLFSSL_X509 struct */
  30589. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  30590. sizeof_client_cert_der_2048));
  30591. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  30592. /* extract PUBLIC KEY from cert */
  30593. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  30594. /* extract signatureType */
  30595. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  30596. /* extract subjectName info */
  30597. ExpectNotNull(name = X509_get_subject_name(cert));
  30598. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  30599. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30600. NULL, 0)), 0);
  30601. ExpectIntEQ(nameSz, 15);
  30602. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30603. commonName, sizeof(commonName))), 0);
  30604. ExpectIntEQ(nameSz, 15);
  30605. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  30606. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30607. commonName, 9)), 0);
  30608. ExpectIntEQ(nameSz, 8);
  30609. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  30610. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  30611. countryName, sizeof(countryName))), 0);
  30612. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  30613. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  30614. localityName, sizeof(localityName))), 0);
  30615. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  30616. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  30617. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  30618. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  30619. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  30620. orgName, sizeof(orgName))), 0);
  30621. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  30622. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  30623. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  30624. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  30625. EVP_PKEY_free(pubKeyTmp);
  30626. X509_free(cert);
  30627. #endif
  30628. return EXPECT_RESULT();
  30629. }
  30630. static int test_wolfSSL_CTX_set_srp_username(void)
  30631. {
  30632. EXPECT_DECLS;
  30633. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30634. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30635. WOLFSSL_CTX* ctx = NULL;
  30636. WOLFSSL* ssl = NULL;
  30637. const char *username = "TESTUSER";
  30638. const char *password = "TESTPASSWORD";
  30639. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30640. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30641. SSL_SUCCESS);
  30642. wolfSSL_CTX_free(ctx);
  30643. ctx = NULL;
  30644. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30645. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30646. SSL_SUCCESS);
  30647. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30648. SSL_SUCCESS);
  30649. ExpectNotNull(ssl = SSL_new(ctx));
  30650. ExpectNotNull(SSL_get_srp_username(ssl));
  30651. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  30652. wolfSSL_free(ssl);
  30653. wolfSSL_CTX_free(ctx);
  30654. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30655. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30656. return EXPECT_RESULT();
  30657. }
  30658. static int test_wolfSSL_CTX_set_srp_password(void)
  30659. {
  30660. EXPECT_DECLS;
  30661. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  30662. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30663. WOLFSSL_CTX* ctx = NULL;
  30664. const char *username = "TESTUSER";
  30665. const char *password = "TESTPASSWORD";
  30666. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30667. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30668. SSL_SUCCESS);
  30669. wolfSSL_CTX_free(ctx);
  30670. ctx = NULL;
  30671. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30672. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30673. SSL_SUCCESS);
  30674. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30675. SSL_SUCCESS);
  30676. wolfSSL_CTX_free(ctx);
  30677. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30678. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30679. return EXPECT_RESULT();
  30680. }
  30681. static int test_wolfSSL_X509_STORE(void)
  30682. {
  30683. EXPECT_DECLS;
  30684. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30685. X509_STORE *store = NULL;
  30686. #ifdef HAVE_CRL
  30687. X509_STORE_CTX *storeCtx = NULL;
  30688. X509_CRL *crl = NULL;
  30689. X509 *ca = NULL;
  30690. X509 *cert = NULL;
  30691. const char crlPem[] = "./certs/crl/crl.revoked";
  30692. const char srvCert[] = "./certs/server-revoked-cert.pem";
  30693. const char caCert[] = "./certs/ca-cert.pem";
  30694. XFILE fp = XBADFILE;
  30695. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30696. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30697. SSL_FILETYPE_PEM)));
  30698. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30699. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30700. SSL_FILETYPE_PEM)));
  30701. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  30702. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30703. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30704. X509_STORE_free(store);
  30705. store = NULL;
  30706. X509_STORE_CTX_free(storeCtx);
  30707. storeCtx = NULL;
  30708. X509_free(cert);
  30709. cert = NULL;
  30710. X509_free(ca);
  30711. ca = NULL;
  30712. /* should fail to verify now after adding in CRL */
  30713. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30714. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30715. SSL_FILETYPE_PEM)));
  30716. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30717. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  30718. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  30719. NULL, NULL));
  30720. if (fp != XBADFILE)
  30721. XFCLOSE(fp);
  30722. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  30723. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  30724. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  30725. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30726. SSL_FILETYPE_PEM)));
  30727. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30728. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30729. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  30730. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  30731. X509_CRL_free(crl);
  30732. crl = NULL;
  30733. X509_STORE_free(store);
  30734. store = NULL;
  30735. X509_STORE_CTX_free(storeCtx);
  30736. storeCtx = NULL;
  30737. X509_free(cert);
  30738. cert = NULL;
  30739. X509_free(ca);
  30740. ca = NULL;
  30741. #endif /* HAVE_CRL */
  30742. #ifndef WOLFCRYPT_ONLY
  30743. {
  30744. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30745. SSL_CTX* ctx = NULL;
  30746. SSL* ssl = NULL;
  30747. int i;
  30748. for (i = 0; i < 2; i++) {
  30749. #ifndef NO_WOLFSSL_SERVER
  30750. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30751. #else
  30752. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30753. #endif
  30754. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30755. SSL_CTX_set_cert_store(ctx, store);
  30756. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30757. SSL_CTX_set_cert_store(ctx, store);
  30758. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30759. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30760. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30761. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30762. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30763. ExpectNotNull(ssl = SSL_new(ctx));
  30764. if (i == 0) {
  30765. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  30766. SSL_SUCCESS);
  30767. }
  30768. else {
  30769. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  30770. #ifdef OPENSSL_ALL
  30771. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  30772. #endif
  30773. }
  30774. if (EXPECT_FAIL() || (i == 1)) {
  30775. X509_STORE_free(store);
  30776. store = NULL;
  30777. }
  30778. SSL_free(ssl);
  30779. ssl = NULL;
  30780. SSL_CTX_free(ctx);
  30781. ctx = NULL;
  30782. }
  30783. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30784. }
  30785. #endif
  30786. #endif
  30787. return EXPECT_RESULT();
  30788. }
  30789. static int test_wolfSSL_X509_STORE_load_locations(void)
  30790. {
  30791. EXPECT_DECLS;
  30792. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30793. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30794. SSL_CTX *ctx = NULL;
  30795. X509_STORE *store = NULL;
  30796. const char ca_file[] = "./certs/ca-cert.pem";
  30797. const char client_pem_file[] = "./certs/client-cert.pem";
  30798. const char client_der_file[] = "./certs/client-cert.der";
  30799. const char ecc_file[] = "./certs/ecc-key.pem";
  30800. const char certs_path[] = "./certs/";
  30801. const char bad_path[] = "./bad-path/";
  30802. #ifdef HAVE_CRL
  30803. const char crl_path[] = "./certs/crl/";
  30804. const char crl_file[] = "./certs/crl/crl.pem";
  30805. #endif
  30806. #ifndef NO_WOLFSSL_SERVER
  30807. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30808. #else
  30809. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30810. #endif
  30811. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  30812. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  30813. WOLFSSL_SUCCESS);
  30814. /* Test bad arguments */
  30815. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  30816. WOLFSSL_FAILURE);
  30817. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  30818. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  30819. WOLFSSL_FAILURE);
  30820. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  30821. WOLFSSL_FAILURE);
  30822. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  30823. WOLFSSL_FAILURE);
  30824. #ifdef HAVE_CRL
  30825. /* Test with CRL */
  30826. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  30827. WOLFSSL_SUCCESS);
  30828. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  30829. WOLFSSL_SUCCESS);
  30830. #endif
  30831. /* Test with CA */
  30832. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  30833. WOLFSSL_SUCCESS);
  30834. /* Test with client_cert and certs path */
  30835. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  30836. WOLFSSL_SUCCESS);
  30837. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  30838. WOLFSSL_SUCCESS);
  30839. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  30840. /* Clear nodes */
  30841. ERR_clear_error();
  30842. #endif
  30843. SSL_CTX_free(ctx);
  30844. #endif
  30845. return EXPECT_RESULT();
  30846. }
  30847. static int test_X509_STORE_get0_objects(void)
  30848. {
  30849. EXPECT_DECLS;
  30850. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  30851. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30852. X509_STORE *store = NULL;
  30853. X509_STORE *store_cpy = NULL;
  30854. SSL_CTX *ctx = NULL;
  30855. X509_OBJECT *obj = NULL;
  30856. STACK_OF(X509_OBJECT) *objs = NULL;
  30857. int i;
  30858. /* Setup store */
  30859. #ifndef NO_WOLFSSL_SERVER
  30860. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30861. #else
  30862. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30863. #endif
  30864. ExpectNotNull(store_cpy = X509_STORE_new());
  30865. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  30866. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  30867. WOLFSSL_SUCCESS);
  30868. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  30869. WOLFSSL_SUCCESS);
  30870. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  30871. WOLFSSL_SUCCESS);
  30872. #ifdef HAVE_CRL
  30873. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  30874. WOLFSSL_SUCCESS);
  30875. #endif
  30876. /* Store ready */
  30877. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  30878. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  30879. #ifdef HAVE_CRL
  30880. #ifdef WOLFSSL_SIGNER_DER_CERT
  30881. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  30882. #else
  30883. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  30884. #endif
  30885. #else
  30886. #ifdef WOLFSSL_SIGNER_DER_CERT
  30887. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  30888. #else
  30889. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  30890. #endif
  30891. #endif
  30892. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  30893. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  30894. switch (X509_OBJECT_get_type(obj)) {
  30895. case X509_LU_X509:
  30896. {
  30897. WOLFSSL_X509* x509;
  30898. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  30899. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  30900. break;
  30901. }
  30902. case X509_LU_CRL:
  30903. #ifdef HAVE_CRL
  30904. {
  30905. WOLFSSL_CRL* crl = NULL;
  30906. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  30907. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  30908. break;
  30909. }
  30910. #endif
  30911. case X509_LU_NONE:
  30912. default:
  30913. Fail(("X509_OBJECT_get_type should return x509 or crl "
  30914. "(when built with crl support)"),
  30915. ("Unrecognized X509_OBJECT type or none"));
  30916. }
  30917. }
  30918. X509_STORE_free(store_cpy);
  30919. SSL_CTX_free(ctx);
  30920. #endif
  30921. return EXPECT_RESULT();
  30922. }
  30923. static int test_wolfSSL_BN_CTX(void)
  30924. {
  30925. EXPECT_DECLS;
  30926. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  30927. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  30928. WOLFSSL_BN_CTX* bn_ctx = NULL;
  30929. WOLFSSL_BIGNUM* t = NULL;
  30930. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  30931. /* No implementation. */
  30932. BN_CTX_init(NULL);
  30933. ExpectNotNull(t = BN_CTX_get(NULL));
  30934. BN_free(t);
  30935. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  30936. BN_free(t);
  30937. #ifndef NO_WOLFSSL_STUB
  30938. /* No implementation. */
  30939. BN_CTX_start(NULL);
  30940. BN_CTX_start(bn_ctx);
  30941. #endif
  30942. BN_CTX_free(NULL);
  30943. BN_CTX_free(bn_ctx);
  30944. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  30945. return EXPECT_RESULT();
  30946. }
  30947. static int test_wolfSSL_BN(void)
  30948. {
  30949. EXPECT_DECLS;
  30950. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  30951. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  30952. BIGNUM* a = NULL;
  30953. BIGNUM* b = NULL;
  30954. BIGNUM* c = NULL;
  30955. BIGNUM* d = NULL;
  30956. BIGNUM emptyBN;
  30957. /* Setup */
  30958. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  30959. /* internal not set emptyBN. */
  30960. ExpectNotNull(a = BN_new());
  30961. ExpectNotNull(b = BN_new());
  30962. ExpectNotNull(c = BN_dup(b));
  30963. ExpectNotNull(d = BN_new());
  30964. /* Invalid parameter testing. */
  30965. BN_free(NULL);
  30966. ExpectNull(BN_dup(NULL));
  30967. ExpectNull(BN_dup(&emptyBN));
  30968. ExpectNull(BN_copy(NULL, NULL));
  30969. ExpectNull(BN_copy(b, NULL));
  30970. ExpectNull(BN_copy(NULL, c));
  30971. ExpectNull(BN_copy(b, &emptyBN));
  30972. ExpectNull(BN_copy(&emptyBN, c));
  30973. BN_clear(NULL);
  30974. BN_clear(&emptyBN);
  30975. ExpectIntEQ(BN_num_bytes(NULL), 0);
  30976. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  30977. ExpectIntEQ(BN_num_bits(NULL), 0);
  30978. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  30979. ExpectIntEQ(BN_is_negative(NULL), 0);
  30980. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  30981. /* END Invalid Parameters */
  30982. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30983. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30984. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  30985. ExpectIntEQ(BN_num_bits(a), 2);
  30986. ExpectIntEQ(BN_num_bytes(a), 1);
  30987. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  30988. defined(WOLFSSL_SP_INT_NEGATIVE))
  30989. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  30990. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  30991. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  30992. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  30993. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  30994. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30995. {
  30996. /* Do additional tests on negative BN conversions. */
  30997. char* ret = NULL;
  30998. ASN1_INTEGER* asn1 = NULL;
  30999. BIGNUM* tmp = NULL;
  31000. /* Sanity check we have a negative BN. */
  31001. ExpectIntEQ(BN_is_negative(c), 1);
  31002. ExpectNotNull(ret = BN_bn2dec(c));
  31003. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31004. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31005. ret = NULL;
  31006. /* Convert to ASN1_INTEGER and back to BN. */
  31007. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  31008. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  31009. /* After converting back BN should be negative and correct. */
  31010. ExpectIntEQ(BN_is_negative(tmp), 1);
  31011. ExpectNotNull(ret = BN_bn2dec(tmp));
  31012. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31013. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31014. ASN1_INTEGER_free(asn1);
  31015. BN_free(tmp);
  31016. }
  31017. #endif
  31018. ExpectIntEQ(BN_get_word(c), 4);
  31019. #endif
  31020. ExpectIntEQ(BN_set_word(a, 3), 1);
  31021. ExpectIntEQ(BN_set_word(b, 3), 1);
  31022. ExpectIntEQ(BN_set_word(c, 4), 1);
  31023. /* NULL == NULL, NULL < num, num > NULL */
  31024. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  31025. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  31026. ExpectIntLT(BN_cmp(NULL, b), 0);
  31027. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  31028. ExpectIntGT(BN_cmp(a, NULL), 0);
  31029. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  31030. ExpectIntEQ(BN_cmp(a, b), 0);
  31031. ExpectIntLT(BN_cmp(a, c), 0);
  31032. ExpectIntGT(BN_cmp(c, b), 0);
  31033. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  31034. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  31035. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  31036. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  31037. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  31038. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  31039. BN_clear(a);
  31040. BN_free(a);
  31041. BN_free(b);
  31042. BN_free(c);
  31043. BN_clear_free(d);
  31044. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31045. return EXPECT_RESULT();
  31046. }
  31047. static int test_wolfSSL_BN_init(void)
  31048. {
  31049. EXPECT_DECLS;
  31050. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31051. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31052. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  31053. BIGNUM* ap = NULL;
  31054. BIGNUM bv;
  31055. BIGNUM cv;
  31056. BIGNUM dv;
  31057. ExpectNotNull(ap = BN_new());
  31058. BN_init(NULL);
  31059. XMEMSET(&bv, 0, sizeof(bv));
  31060. ExpectNull(BN_dup(&bv));
  31061. BN_init(&bv);
  31062. BN_init(&cv);
  31063. BN_init(&dv);
  31064. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31065. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31066. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31067. /* a^b mod c = */
  31068. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31069. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31070. /* check result 3^2 mod 5 */
  31071. ExpectIntEQ(BN_get_word(&dv), 4);
  31072. /* a*b mod c = */
  31073. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31074. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31075. /* check result 3*2 mod 5 */
  31076. ExpectIntEQ(BN_get_word(&dv), 1);
  31077. BN_free(ap);
  31078. #endif
  31079. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31080. return EXPECT_RESULT();
  31081. }
  31082. static int test_wolfSSL_BN_enc_dec(void)
  31083. {
  31084. EXPECT_DECLS;
  31085. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31086. BIGNUM* a = NULL;
  31087. BIGNUM* b = NULL;
  31088. BIGNUM* c = NULL;
  31089. BIGNUM emptyBN;
  31090. char* str = NULL;
  31091. const char* emptyStr = "";
  31092. const char* numberStr = "12345";
  31093. const char* badStr = "g12345";
  31094. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31095. const char* twoStr = "2";
  31096. #endif
  31097. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  31098. unsigned char outNum[5];
  31099. /* Setup */
  31100. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31101. ExpectNotNull(a = BN_new());
  31102. ExpectNotNull(b = BN_new());
  31103. ExpectIntEQ(BN_set_word(a, 2), 1);
  31104. /* Invalid parameters */
  31105. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  31106. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  31107. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  31108. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  31109. ExpectNull(BN_bn2hex(NULL));
  31110. ExpectNull(BN_bn2hex(&emptyBN));
  31111. ExpectNull(BN_bn2dec(NULL));
  31112. ExpectNull(BN_bn2dec(&emptyBN));
  31113. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  31114. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  31115. ExpectNull(BN_bin2bn(binNum, -1, a));
  31116. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  31117. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  31118. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  31119. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  31120. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  31121. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  31122. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  31123. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  31124. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  31125. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  31126. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  31127. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  31128. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  31129. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  31130. ExpectIntEQ(BN_set_word(a, 2), 1);
  31131. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  31132. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  31133. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  31134. ExpectIntEQ(BN_cmp(a, b), 0);
  31135. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  31136. ExpectIntEQ(BN_cmp(a, b), -1);
  31137. ExpectNotNull(str = BN_bn2hex(a));
  31138. ExpectNotNull(BN_hex2bn(&b, str));
  31139. ExpectIntEQ(BN_cmp(a, b), 0);
  31140. ExpectNotNull(BN_hex2bn(&b, numberStr));
  31141. ExpectIntEQ(BN_cmp(a, b), -1);
  31142. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31143. str = NULL;
  31144. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31145. ExpectNotNull(str = BN_bn2dec(a));
  31146. ExpectStrEQ(str, twoStr);
  31147. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31148. str = NULL;
  31149. #ifndef NO_RSA
  31150. ExpectNotNull(str = BN_bn2dec(a));
  31151. ExpectNotNull(BN_dec2bn(&b, str));
  31152. ExpectIntEQ(BN_cmp(a, b), 0);
  31153. ExpectNotNull(BN_dec2bn(&b, numberStr));
  31154. ExpectIntEQ(BN_cmp(a, b), -1);
  31155. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31156. str = NULL;
  31157. #else
  31158. /* No implementation - fail with good parameters. */
  31159. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  31160. #endif
  31161. #endif
  31162. BN_free(b);
  31163. BN_free(a);
  31164. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31165. return EXPECT_RESULT();
  31166. }
  31167. static int test_wolfSSL_BN_word(void)
  31168. {
  31169. EXPECT_DECLS;
  31170. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31171. BIGNUM* a = NULL;
  31172. BIGNUM* b = NULL;
  31173. BIGNUM* c = NULL;
  31174. BIGNUM av;
  31175. ExpectNotNull(a = BN_new());
  31176. ExpectNotNull(b = BN_new());
  31177. ExpectNotNull(c = BN_new());
  31178. XMEMSET(&av, 0, sizeof(av));
  31179. /* Invalid parameter. */
  31180. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  31181. ExpectIntEQ(BN_add_word(&av, 3), 0);
  31182. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  31183. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  31184. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  31185. ExpectIntEQ(BN_set_word(&av, 3), 0);
  31186. ExpectIntEQ(BN_get_word(NULL), 0);
  31187. ExpectIntEQ(BN_get_word(&av), 0);
  31188. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  31189. ExpectIntEQ(BN_is_word(&av, 3), 0);
  31190. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  31191. !defined(NO_DSA))
  31192. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  31193. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  31194. #endif
  31195. ExpectIntEQ(BN_one(NULL), 0);
  31196. ExpectIntEQ(BN_one(&av), 0);
  31197. BN_zero(NULL);
  31198. BN_zero(&av);
  31199. ExpectIntEQ(BN_is_one(NULL), 0);
  31200. ExpectIntEQ(BN_is_one(&av), 0);
  31201. ExpectIntEQ(BN_is_zero(NULL), 0);
  31202. ExpectIntEQ(BN_is_zero(&av), 0);
  31203. ExpectIntEQ(BN_set_word(a, 3), 1);
  31204. ExpectIntEQ(BN_set_word(b, 2), 1);
  31205. ExpectIntEQ(BN_set_word(c, 5), 1);
  31206. /* a + 3 = */
  31207. ExpectIntEQ(BN_add_word(a, 3), 1);
  31208. /* check result 3 + 3*/
  31209. ExpectIntEQ(BN_get_word(a), 6);
  31210. ExpectIntEQ(BN_is_word(a, 6), 1);
  31211. ExpectIntEQ(BN_is_word(a, 5), 0);
  31212. /* set a back to 3 */
  31213. ExpectIntEQ(BN_set_word(a, 3), 1);
  31214. /* a - 3 = */
  31215. ExpectIntEQ(BN_sub_word(a, 3), 1);
  31216. /* check result 3 - 3*/
  31217. ExpectIntEQ(BN_get_word(a), 0);
  31218. ExpectIntEQ(BN_one(a), 1);
  31219. ExpectIntEQ(BN_is_word(a, 1), 1);
  31220. ExpectIntEQ(BN_is_word(a, 0), 0);
  31221. ExpectIntEQ(BN_is_one(a), 1);
  31222. ExpectIntEQ(BN_is_zero(a), 0);
  31223. BN_zero(a);
  31224. ExpectIntEQ(BN_is_word(a, 0), 1);
  31225. ExpectIntEQ(BN_is_word(a, 1), 0);
  31226. ExpectIntEQ(BN_is_zero(a), 1);
  31227. ExpectIntEQ(BN_is_one(a), 0);
  31228. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  31229. !defined(NO_DSA))
  31230. ExpectIntEQ(BN_set_word(a, 5), 1);
  31231. ExpectIntEQ(BN_mod_word(a, 3), 2);
  31232. ExpectIntEQ(BN_mod_word(a, 0), -1);
  31233. #endif
  31234. BN_free(c);
  31235. BN_free(b);
  31236. BN_free(a);
  31237. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31238. return EXPECT_RESULT();
  31239. }
  31240. static int test_wolfSSL_BN_bits(void)
  31241. {
  31242. EXPECT_DECLS;
  31243. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31244. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31245. BIGNUM* a = NULL;
  31246. BIGNUM emptyBN;
  31247. /* Setup */
  31248. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31249. ExpectNotNull(a = BN_new());
  31250. /* Invalid parameters. */
  31251. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  31252. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  31253. ExpectIntEQ(BN_set_bit(a, -1), 0);
  31254. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  31255. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  31256. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  31257. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  31258. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  31259. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  31260. ExpectIntEQ(BN_is_odd(NULL), 0);
  31261. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  31262. ExpectIntEQ(BN_set_word(a, 0), 1);
  31263. ExpectIntEQ(BN_is_zero(a), 1);
  31264. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  31265. ExpectIntEQ(BN_is_zero(a), 0);
  31266. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  31267. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  31268. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  31269. ExpectIntEQ(BN_is_zero(a), 1);
  31270. ExpectIntEQ(BN_set_bit(a, 0), 1);
  31271. ExpectIntEQ(BN_is_odd(a), 1);
  31272. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  31273. ExpectIntEQ(BN_is_odd(a), 0);
  31274. ExpectIntEQ(BN_set_bit(a, 1), 1);
  31275. ExpectIntEQ(BN_is_odd(a), 0);
  31276. ExpectIntEQ(BN_set_bit(a, 129), 1);
  31277. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  31278. #ifndef NO_WOLFSSL_STUB
  31279. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  31280. #endif
  31281. BN_free(a);
  31282. #endif
  31283. return EXPECT_RESULT();
  31284. }
  31285. static int test_wolfSSL_BN_shift(void)
  31286. {
  31287. EXPECT_DECLS;
  31288. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31289. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31290. BIGNUM* a = NULL;
  31291. BIGNUM* b = NULL;
  31292. BIGNUM emptyBN;
  31293. /* Setup */
  31294. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31295. ExpectNotNull(a = BN_new());
  31296. ExpectNotNull(b = BN_new());
  31297. /* Invalid parameters. */
  31298. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  31299. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  31300. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  31301. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  31302. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  31303. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  31304. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  31305. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  31306. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  31307. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  31308. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  31309. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  31310. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  31311. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  31312. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  31313. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  31314. ExpectIntEQ(BN_set_word(a, 1), 1);
  31315. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  31316. ExpectIntEQ(BN_is_word(b, 2), 1);
  31317. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  31318. ExpectIntEQ(BN_is_word(a, 2), 1);
  31319. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  31320. ExpectIntEQ(BN_is_word(b, 1), 1);
  31321. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  31322. ExpectIntEQ(BN_is_word(a, 1), 1);
  31323. BN_free(b);
  31324. BN_free(a);
  31325. #endif
  31326. return EXPECT_RESULT();
  31327. }
  31328. static int test_wolfSSL_BN_math(void)
  31329. {
  31330. EXPECT_DECLS;
  31331. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31332. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31333. BIGNUM* a = NULL;
  31334. BIGNUM* b = NULL;
  31335. BIGNUM* r = NULL;
  31336. BIGNUM* rem = NULL;
  31337. BIGNUM emptyBN;
  31338. BN_ULONG val1;
  31339. BN_ULONG val2;
  31340. /* Setup */
  31341. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31342. ExpectNotNull(a = BN_new());
  31343. ExpectNotNull(b = BN_new());
  31344. ExpectNotNull(r = BN_new());
  31345. ExpectNotNull(rem = BN_new());
  31346. /* Invalid parameters. */
  31347. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  31348. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  31349. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  31350. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  31351. ExpectIntEQ(BN_add(r, a, NULL), 0);
  31352. ExpectIntEQ(BN_add(r, NULL, b), 0);
  31353. ExpectIntEQ(BN_add(NULL, a, b), 0);
  31354. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  31355. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  31356. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  31357. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  31358. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  31359. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  31360. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  31361. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  31362. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  31363. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  31364. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  31365. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  31366. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  31367. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  31368. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  31369. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  31370. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  31371. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  31372. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  31373. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  31374. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  31375. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  31376. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  31377. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  31378. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  31379. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  31380. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  31381. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  31382. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31383. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  31384. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  31385. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  31386. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  31387. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  31388. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  31389. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  31390. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  31391. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  31392. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  31393. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  31394. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  31395. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  31396. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  31397. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  31398. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31399. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31400. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  31401. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  31402. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  31403. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  31404. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  31405. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  31406. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  31407. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  31408. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  31409. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  31410. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  31411. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  31412. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  31413. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  31414. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31415. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  31416. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  31417. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  31418. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  31419. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  31420. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  31421. /* END Invalid parameters. */
  31422. val1 = 8;
  31423. val2 = 3;
  31424. ExpectIntEQ(BN_set_word(a, val1), 1);
  31425. ExpectIntEQ(BN_set_word(b, val2), 1);
  31426. ExpectIntEQ(BN_add(r, a, b), 1);
  31427. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  31428. ExpectIntEQ(BN_sub(r, a, b), 1);
  31429. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  31430. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  31431. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  31432. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  31433. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  31434. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  31435. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  31436. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  31437. BN_free(rem);
  31438. BN_free(r);
  31439. BN_free(b);
  31440. BN_free(a);
  31441. #endif
  31442. return EXPECT_RESULT();
  31443. }
  31444. static int test_wolfSSL_BN_math_mod(void)
  31445. {
  31446. EXPECT_DECLS;
  31447. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31448. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31449. BIGNUM* a = NULL;
  31450. BIGNUM* b = NULL;
  31451. BIGNUM* m = NULL;
  31452. BIGNUM* r = NULL;
  31453. BIGNUM* t = NULL;
  31454. BIGNUM emptyBN;
  31455. BN_ULONG val1;
  31456. BN_ULONG val2;
  31457. BN_ULONG val3;
  31458. /* Setup */
  31459. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31460. ExpectNotNull(a = BN_new());
  31461. ExpectNotNull(b = BN_new());
  31462. ExpectNotNull(m = BN_new());
  31463. ExpectNotNull(r = BN_new());
  31464. /* Invalid parameters. */
  31465. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  31466. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  31467. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  31468. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  31469. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  31470. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  31471. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  31472. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  31473. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  31474. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31475. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31476. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31477. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31478. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31479. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  31480. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  31481. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  31482. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  31483. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  31484. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  31485. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  31486. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  31487. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  31488. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  31489. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  31490. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  31491. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  31492. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31493. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31494. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31495. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31496. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31497. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  31498. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  31499. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  31500. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  31501. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  31502. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  31503. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  31504. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  31505. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  31506. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  31507. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  31508. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  31509. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  31510. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31511. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31512. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31513. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31514. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31515. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  31516. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  31517. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  31518. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  31519. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  31520. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  31521. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  31522. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  31523. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  31524. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  31525. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  31526. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  31527. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  31528. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  31529. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  31530. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  31531. /* END Invalid parameters. */
  31532. val1 = 9;
  31533. val2 = 13;
  31534. val3 = 5;
  31535. ExpectIntEQ(BN_set_word(a, val1), 1);
  31536. ExpectIntEQ(BN_set_word(b, val2), 1);
  31537. ExpectIntEQ(BN_set_word(m, val3), 1);
  31538. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  31539. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  31540. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  31541. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  31542. ExpectIntEQ(BN_set_word(a, 2), 1);
  31543. ExpectIntEQ(BN_set_word(b, 3), 1);
  31544. ExpectIntEQ(BN_set_word(m, 5), 1);
  31545. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  31546. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  31547. ExpectIntEQ(BN_is_word(r, 3), 1);
  31548. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  31549. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  31550. ExpectIntEQ(BN_is_word(r, 3), 1);
  31551. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  31552. ExpectIntEQ(BN_is_word(t, 3), 1);
  31553. BN_free(t);
  31554. /* No inverse case. No inverse when a divides b. */
  31555. ExpectIntEQ(BN_set_word(a, 3), 1);
  31556. ExpectIntEQ(BN_set_word(m, 9), 1);
  31557. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  31558. BN_free(r);
  31559. BN_free(m);
  31560. BN_free(b);
  31561. BN_free(a);
  31562. #endif
  31563. return EXPECT_RESULT();
  31564. }
  31565. static int test_wolfSSL_BN_math_other(void)
  31566. {
  31567. EXPECT_DECLS;
  31568. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31569. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31570. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31571. BIGNUM* a = NULL;
  31572. BIGNUM* b = NULL;
  31573. BIGNUM* r = NULL;
  31574. BIGNUM emptyBN;
  31575. /* Setup */
  31576. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31577. ExpectNotNull(a = BN_new());
  31578. ExpectNotNull(b = BN_new());
  31579. ExpectNotNull(r = BN_new());
  31580. /* Invalid parameters. */
  31581. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  31582. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  31583. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  31584. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  31585. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  31586. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  31587. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  31588. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31589. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  31590. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  31591. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  31592. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  31593. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  31594. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  31595. /* END Invalid parameters. */
  31596. /* No common factors between 2 and 3. */
  31597. ExpectIntEQ(BN_set_word(a, 2), 1);
  31598. ExpectIntEQ(BN_set_word(b, 3), 1);
  31599. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  31600. ExpectIntEQ(BN_is_word(r, 1), 1);
  31601. /* 3 is largest value that divides both 6 and 9. */
  31602. ExpectIntEQ(BN_set_word(a, 6), 1);
  31603. ExpectIntEQ(BN_set_word(b, 9), 1);
  31604. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  31605. ExpectIntEQ(BN_is_word(r, 3), 1);
  31606. /* GCD of 0 and 0 is undefined. */
  31607. ExpectIntEQ(BN_set_word(a, 0), 1);
  31608. ExpectIntEQ(BN_set_word(b, 0), 1);
  31609. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  31610. /* Teardown */
  31611. BN_free(r);
  31612. BN_free(b);
  31613. BN_free(a);
  31614. #endif
  31615. #endif
  31616. return EXPECT_RESULT();
  31617. }
  31618. static int test_wolfSSL_BN_rand(void)
  31619. {
  31620. EXPECT_DECLS;
  31621. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  31622. BIGNUM* bn = NULL;
  31623. BIGNUM* range = NULL;
  31624. BIGNUM emptyBN;
  31625. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31626. ExpectNotNull(bn = BN_new());
  31627. ExpectNotNull(range = BN_new());
  31628. /* Invalid parameters. */
  31629. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  31630. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  31631. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  31632. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  31633. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  31634. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  31635. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  31636. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  31637. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  31638. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  31639. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  31640. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  31641. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  31642. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  31643. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  31644. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  31645. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  31646. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  31647. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  31648. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31649. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31650. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31651. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31652. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31653. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31654. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31655. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31656. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31657. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31658. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31659. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31660. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31661. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31662. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31663. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31664. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31665. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31666. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31667. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31668. /* 1 bit random value must have no more than one top bit set. */
  31669. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31670. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31671. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31672. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31673. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31674. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31675. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31676. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31677. /* END Invalid parameters. */
  31678. /* 0 bit random: 0. */
  31679. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31680. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31681. ExpectIntEQ(BN_is_zero(bn), 1);
  31682. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  31683. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31684. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31685. ExpectIntEQ(BN_is_zero(bn), 1);
  31686. /* 1 bit random: 0 or 1. */
  31687. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  31688. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31689. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  31690. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31691. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31692. ExpectIntEQ(BN_get_word(bn), 1);
  31693. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31694. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31695. ExpectIntEQ(BN_get_word(bn), 1);
  31696. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  31697. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31698. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  31699. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31700. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31701. ExpectIntEQ(BN_get_word(bn), 1);
  31702. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31703. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31704. ExpectIntEQ(BN_get_word(bn), 1);
  31705. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31706. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31707. ExpectIntEQ(BN_num_bits(bn), 8);
  31708. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31709. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31710. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31711. ExpectIntEQ(BN_num_bits(bn), 8);
  31712. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31713. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  31714. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31715. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31716. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  31717. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  31718. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31719. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31720. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  31721. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31722. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31723. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  31724. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31725. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31726. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  31727. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  31728. * requested number of bits up to the nearest multiple of 8. E.g. in this
  31729. * case, requesting a 13-bit random number would actually return a 16-bit
  31730. * random number. */
  31731. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  31732. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31733. ExpectIntEQ(BN_num_bits(bn), 13);
  31734. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  31735. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31736. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31737. ExpectIntEQ(BN_set_word(range, 0), 1);
  31738. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31739. ExpectIntEQ(BN_set_word(range, 1), 1);
  31740. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31741. BN_free(bn);
  31742. BN_free(range);
  31743. #endif
  31744. return EXPECT_RESULT();
  31745. }
  31746. static int test_wolfSSL_BN_prime(void)
  31747. {
  31748. EXPECT_DECLS;
  31749. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31750. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31751. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31752. BIGNUM* a = NULL;
  31753. BIGNUM* add = NULL;
  31754. BIGNUM* rem = NULL;
  31755. BIGNUM emptyBN;
  31756. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31757. ExpectNotNull(a = BN_new());
  31758. ExpectNotNull(add = BN_new());
  31759. ExpectNotNull(rem = BN_new());
  31760. /* Invalid parameters. */
  31761. /* BN_generate_prime_ex()
  31762. * prime - must have valid BIGNUM
  31763. * bits - Greater then 0
  31764. * safe - not supported, must be 0
  31765. * add - not supported, must be NULL
  31766. * rem - not supported, must be NULL
  31767. * cb - anything
  31768. */
  31769. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  31770. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  31771. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  31772. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  31773. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  31774. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  31775. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  31776. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  31777. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  31778. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  31779. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  31780. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  31781. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  31782. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  31783. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  31784. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  31785. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  31786. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  31787. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  31788. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  31789. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  31790. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  31791. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  31792. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  31793. /* END Invalid parameters. */
  31794. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  31795. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  31796. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  31797. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  31798. BN_free(rem);
  31799. BN_free(add);
  31800. BN_free(a);
  31801. #endif
  31802. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31803. return EXPECT_RESULT();
  31804. }
  31805. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31806. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31807. #define TEST_ARG 0x1234
  31808. static void msg_cb(int write_p, int version, int content_type,
  31809. const void *buf, size_t len, SSL *ssl, void *arg)
  31810. {
  31811. (void)write_p;
  31812. (void)version;
  31813. (void)content_type;
  31814. (void)buf;
  31815. (void)len;
  31816. (void)ssl;
  31817. AssertTrue(arg == (void*)TEST_ARG);
  31818. }
  31819. #endif
  31820. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  31821. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31822. #if defined(SESSION_CERTS)
  31823. #include "wolfssl/internal.h"
  31824. #endif
  31825. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31826. {
  31827. EXPECT_DECLS;
  31828. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31829. STACK_OF(X509)* sk = NULL;
  31830. X509* x509 = NULL;
  31831. int i, num;
  31832. BIO* bio = NULL;
  31833. #endif
  31834. ExpectNotNull(ctx);
  31835. ExpectNotNull(ssl);
  31836. fprintf(stderr, "\n===== msgcb called ====\n");
  31837. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31838. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31839. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31840. ExpectNotNull(SSL_get0_verified_chain(ssl));
  31841. #endif
  31842. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31843. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  31844. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  31845. if (sk == NULL) {
  31846. BIO_free(bio);
  31847. return TEST_FAIL;
  31848. }
  31849. num = sk_X509_num(sk);
  31850. ExpectTrue(num > 0);
  31851. for (i = 0; i < num; i++) {
  31852. ExpectNotNull(x509 = sk_X509_value(sk,i));
  31853. if (x509 == NULL)
  31854. break;
  31855. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  31856. X509_print(bio,x509);
  31857. fprintf(stderr, "\n\n");
  31858. }
  31859. BIO_free(bio);
  31860. #endif
  31861. return EXPECT_RESULT();
  31862. }
  31863. #endif
  31864. static int test_wolfSSL_msgCb(void)
  31865. {
  31866. EXPECT_DECLS;
  31867. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  31868. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31869. test_ssl_cbf client_cb;
  31870. test_ssl_cbf server_cb;
  31871. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31872. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31873. #ifndef WOLFSSL_NO_TLS12
  31874. client_cb.method = wolfTLSv1_2_client_method;
  31875. server_cb.method = wolfTLSv1_2_server_method;
  31876. #else
  31877. client_cb.method = wolfTLSv1_3_client_method;
  31878. server_cb.method = wolfTLSv1_3_server_method;
  31879. #endif
  31880. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31881. &server_cb, msgCb), TEST_SUCCESS);
  31882. #endif
  31883. return EXPECT_RESULT();
  31884. }
  31885. static int test_wolfSSL_either_side(void)
  31886. {
  31887. EXPECT_DECLS;
  31888. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31889. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31890. test_ssl_cbf client_cb;
  31891. test_ssl_cbf server_cb;
  31892. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31893. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31894. /* Use different CTX for client and server */
  31895. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31896. ExpectNotNull(client_cb.ctx);
  31897. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31898. ExpectNotNull(server_cb.ctx);
  31899. /* we are responsible for free'ing WOLFSSL_CTX */
  31900. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31901. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31902. &server_cb, NULL), TEST_SUCCESS);
  31903. wolfSSL_CTX_free(client_cb.ctx);
  31904. wolfSSL_CTX_free(server_cb.ctx);
  31905. #endif
  31906. return EXPECT_RESULT();
  31907. }
  31908. static int test_wolfSSL_DTLS_either_side(void)
  31909. {
  31910. EXPECT_DECLS;
  31911. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31912. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  31913. test_ssl_cbf client_cb;
  31914. test_ssl_cbf server_cb;
  31915. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31916. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31917. /* Use different CTX for client and server */
  31918. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31919. ExpectNotNull(client_cb.ctx);
  31920. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31921. ExpectNotNull(server_cb.ctx);
  31922. /* we are responsible for free'ing WOLFSSL_CTX */
  31923. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31924. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31925. &server_cb, NULL), TEST_SUCCESS);
  31926. wolfSSL_CTX_free(client_cb.ctx);
  31927. wolfSSL_CTX_free(server_cb.ctx);
  31928. #endif
  31929. return EXPECT_RESULT();
  31930. }
  31931. static int test_generate_cookie(void)
  31932. {
  31933. EXPECT_DECLS;
  31934. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  31935. SSL_CTX* ctx = NULL;
  31936. SSL* ssl = NULL;
  31937. byte buf[FOURK_BUF] = {0};
  31938. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  31939. ExpectNotNull(ssl = SSL_new(ctx));
  31940. /* Test unconnected */
  31941. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  31942. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  31943. wolfSSL_SetCookieCtx(ssl, ctx);
  31944. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  31945. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  31946. SSL_free(ssl);
  31947. SSL_CTX_free(ctx);
  31948. #endif
  31949. return EXPECT_RESULT();
  31950. }
  31951. static int test_wolfSSL_set_options(void)
  31952. {
  31953. EXPECT_DECLS;
  31954. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31955. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31956. WOLFSSL* ssl = NULL;
  31957. WOLFSSL_CTX* ctx = NULL;
  31958. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31959. char appData[] = "extra msg";
  31960. #endif
  31961. #ifdef OPENSSL_EXTRA
  31962. unsigned char protos[] = {
  31963. 7, 't', 'l', 's', '/', '1', '.', '2',
  31964. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  31965. };
  31966. unsigned int len = sizeof(protos);
  31967. void *arg = (void *)TEST_ARG;
  31968. #endif
  31969. #ifndef NO_WOLFSSL_SERVER
  31970. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31971. #else
  31972. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31973. #endif
  31974. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31975. WOLFSSL_FILETYPE_PEM));
  31976. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31977. WOLFSSL_FILETYPE_PEM));
  31978. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  31979. == WOLFSSL_OP_NO_TLSv1);
  31980. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  31981. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31982. WOLFSSL_OP_NO_SSLv2)), 0);
  31983. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31984. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31985. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  31986. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31987. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31988. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31989. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31990. WOLFSSL_OP_NO_COMPRESSION));
  31991. wolfSSL_CTX_free(ctx);
  31992. ctx = NULL;
  31993. #ifndef NO_WOLFSSL_SERVER
  31994. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31995. #else
  31996. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31997. #endif
  31998. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31999. WOLFSSL_FILETYPE_PEM));
  32000. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  32001. WOLFSSL_FILETYPE_PEM));
  32002. #ifdef OPENSSL_EXTRA
  32003. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  32004. #endif
  32005. ExpectNotNull(ssl = wolfSSL_new(ctx));
  32006. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  32007. #ifdef HAVE_EX_DATA
  32008. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  32009. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  32010. if (ssl != NULL) {
  32011. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  32012. appData, sizeof(appData)), 0);
  32013. }
  32014. #else
  32015. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  32016. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  32017. #endif
  32018. #endif
  32019. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  32020. WOLFSSL_OP_NO_TLSv1);
  32021. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  32022. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  32023. WOLFSSL_OP_NO_SSLv2)), 0);
  32024. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  32025. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  32026. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  32027. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  32028. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  32029. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  32030. #ifdef OPENSSL_EXTRA
  32031. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  32032. WOLFSSL_OP_NO_COMPRESSION));
  32033. #endif
  32034. #ifdef OPENSSL_EXTRA
  32035. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  32036. wolfSSL_set_msg_callback_arg(ssl, arg);
  32037. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32038. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  32039. #else
  32040. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  32041. #endif
  32042. #endif
  32043. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  32044. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  32045. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  32046. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  32047. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32048. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  32049. #else
  32050. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  32051. #endif
  32052. #endif /* HAVE_ALPN && !NO_BIO */
  32053. #endif
  32054. wolfSSL_free(ssl);
  32055. wolfSSL_CTX_free(ctx);
  32056. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32057. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32058. return EXPECT_RESULT();
  32059. }
  32060. static int test_wolfSSL_sk_SSL_CIPHER(void)
  32061. {
  32062. EXPECT_DECLS;
  32063. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32064. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32065. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32066. SSL* ssl = NULL;
  32067. SSL_CTX* ctx = NULL;
  32068. STACK_OF(SSL_CIPHER) *sk = NULL;
  32069. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  32070. #ifndef NO_WOLFSSL_SERVER
  32071. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32072. #else
  32073. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32074. #endif
  32075. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32076. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32077. ExpectNotNull(ssl = SSL_new(ctx));
  32078. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  32079. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  32080. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  32081. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  32082. /* error case because connection has not been established yet */
  32083. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  32084. sk_SSL_CIPHER_free(dupSk);
  32085. /* sk is pointer to internal struct that should be free'd in SSL_free */
  32086. SSL_free(ssl);
  32087. SSL_CTX_free(ctx);
  32088. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32089. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32090. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32091. return EXPECT_RESULT();
  32092. }
  32093. static int test_wolfSSL_set1_curves_list(void)
  32094. {
  32095. EXPECT_DECLS;
  32096. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  32097. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32098. SSL* ssl = NULL;
  32099. SSL_CTX* ctx = NULL;
  32100. #ifndef NO_WOLFSSL_SERVER
  32101. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32102. #else
  32103. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32104. #endif
  32105. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  32106. SSL_FILETYPE_PEM));
  32107. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  32108. ExpectNotNull(ssl = SSL_new(ctx));
  32109. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  32110. #ifdef HAVE_ECC
  32111. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  32112. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  32113. #endif
  32114. #ifdef HAVE_CURVE25519
  32115. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  32116. #else
  32117. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  32118. #endif
  32119. #ifdef HAVE_CURVE448
  32120. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  32121. #else
  32122. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  32123. #endif
  32124. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  32125. #ifdef HAVE_ECC
  32126. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  32127. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  32128. #endif
  32129. #ifdef HAVE_CURVE25519
  32130. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  32131. #else
  32132. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  32133. #endif
  32134. #ifdef HAVE_CURVE448
  32135. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  32136. #else
  32137. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  32138. #endif
  32139. SSL_free(ssl);
  32140. SSL_CTX_free(ctx);
  32141. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32142. #endif
  32143. return EXPECT_RESULT();
  32144. }
  32145. static int test_wolfSSL_set1_sigalgs_list(void)
  32146. {
  32147. EXPECT_DECLS;
  32148. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  32149. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32150. SSL* ssl = NULL;
  32151. SSL_CTX* ctx = NULL;
  32152. #ifndef NO_WOLFSSL_SERVER
  32153. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32154. #else
  32155. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32156. #endif
  32157. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32158. SSL_FILETYPE_PEM));
  32159. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32160. ExpectNotNull(ssl = SSL_new(ctx));
  32161. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32162. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  32163. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32164. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  32165. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  32166. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  32167. #ifndef NO_RSA
  32168. #ifndef NO_SHA256
  32169. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32170. WOLFSSL_FAILURE);
  32171. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32172. WOLFSSL_FAILURE);
  32173. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  32174. WOLFSSL_SUCCESS);
  32175. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  32176. WOLFSSL_SUCCESS);
  32177. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  32178. WOLFSSL_FAILURE);
  32179. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  32180. WOLFSSL_FAILURE);
  32181. #ifdef WC_RSA_PSS
  32182. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  32183. WOLFSSL_SUCCESS);
  32184. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  32185. WOLFSSL_SUCCESS);
  32186. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  32187. WOLFSSL_SUCCESS);
  32188. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  32189. WOLFSSL_SUCCESS);
  32190. #endif
  32191. #ifdef WOLFSSL_SHA512
  32192. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32193. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32194. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32195. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32196. #elif defined(WOLFSSL_SHA384)
  32197. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32198. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32199. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32200. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32201. #endif
  32202. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  32203. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  32204. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  32205. WOLFSSL_FAILURE);
  32206. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  32207. WOLFSSL_FAILURE);
  32208. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  32209. WOLFSSL_FAILURE);
  32210. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  32211. WOLFSSL_FAILURE);
  32212. #endif
  32213. #endif
  32214. #ifdef HAVE_ECC
  32215. #ifndef NO_SHA256
  32216. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  32217. WOLFSSL_SUCCESS);
  32218. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  32219. WOLFSSL_SUCCESS);
  32220. #ifdef WOLFSSL_SHA512
  32221. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32222. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32223. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32224. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32225. #elif defined(WOLFSSL_SHA384)
  32226. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32227. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32228. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32229. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32230. #endif
  32231. #endif
  32232. #endif
  32233. #ifdef HAVE_ED25519
  32234. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  32235. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  32236. #endif
  32237. #ifdef HAVE_ED448
  32238. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  32239. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  32240. #endif
  32241. #ifndef NO_DSA
  32242. #ifndef NO_SHA256
  32243. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  32244. WOLFSSL_SUCCESS);
  32245. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  32246. WOLFSSL_SUCCESS);
  32247. #endif
  32248. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  32249. defined(WOLFSSL_ALLOW_TLS_SHA1))
  32250. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  32251. WOLFSSL_SUCCESS);
  32252. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  32253. WOLFSSL_SUCCESS);
  32254. #endif
  32255. #endif
  32256. SSL_free(ssl);
  32257. SSL_CTX_free(ctx);
  32258. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32259. #endif
  32260. return EXPECT_RESULT();
  32261. }
  32262. /* Testing wolfSSL_set_tlsext_status_type function.
  32263. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  32264. */
  32265. static int test_wolfSSL_set_tlsext_status_type(void)
  32266. {
  32267. EXPECT_DECLS;
  32268. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  32269. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  32270. SSL* ssl = NULL;
  32271. SSL_CTX* ctx = NULL;
  32272. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32273. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32274. SSL_FILETYPE_PEM));
  32275. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32276. ExpectNotNull(ssl = SSL_new(ctx));
  32277. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  32278. SSL_SUCCESS);
  32279. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  32280. SSL_free(ssl);
  32281. SSL_CTX_free(ctx);
  32282. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32283. return EXPECT_RESULT();
  32284. }
  32285. #ifndef NO_BIO
  32286. static int test_wolfSSL_PEM_read_bio(void)
  32287. {
  32288. EXPECT_DECLS;
  32289. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32290. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32291. byte buff[6000];
  32292. XFILE f = XBADFILE;
  32293. int bytes;
  32294. X509* x509 = NULL;
  32295. BIO* bio = NULL;
  32296. BUF_MEM* buf = NULL;
  32297. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32298. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  32299. if (f != XBADFILE)
  32300. XFCLOSE(f);
  32301. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32302. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32303. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32304. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32305. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32306. /* BIO should return the set EOF value */
  32307. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32308. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32309. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32310. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32311. BIO_free(bio);
  32312. BUF_MEM_free(buf);
  32313. X509_free(x509);
  32314. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32315. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32316. return EXPECT_RESULT();
  32317. }
  32318. #if defined(OPENSSL_EXTRA)
  32319. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32320. long argl, long ret)
  32321. {
  32322. (void)bio;
  32323. (void)cmd;
  32324. (void)argp;
  32325. (void)argi;
  32326. (void)argl;
  32327. return ret;
  32328. }
  32329. #endif
  32330. static int test_wolfSSL_BIO(void)
  32331. {
  32332. EXPECT_DECLS;
  32333. #if defined(OPENSSL_EXTRA)
  32334. const unsigned char* p = NULL;
  32335. byte buff[20];
  32336. BIO* bio1 = NULL;
  32337. BIO* bio2 = NULL;
  32338. BIO* bio3 = NULL;
  32339. char* bufPt = NULL;
  32340. int i;
  32341. for (i = 0; i < 20; i++) {
  32342. buff[i] = i;
  32343. }
  32344. /* test BIO_free with NULL */
  32345. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32346. /* Creating and testing type BIO_s_bio */
  32347. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  32348. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  32349. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  32350. /* read/write before set up */
  32351. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32352. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32353. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  32354. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32355. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32356. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32357. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32358. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  32359. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32360. /* write buffer full */
  32361. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32362. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32363. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32364. /* write the other direction with pair */
  32365. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32366. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  32367. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32368. /* try read */
  32369. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32370. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32371. /* try read using ctrl function */
  32372. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32373. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32374. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32375. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32376. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32377. for (i = 0; i < 20; i++) {
  32378. ExpectIntEQ((int)bufPt[i], i);
  32379. }
  32380. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32381. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32382. for (i = 0; i < 8; i++) {
  32383. ExpectIntEQ((int)bufPt[i], i);
  32384. }
  32385. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32386. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32387. /* new pair */
  32388. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32389. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32390. bio2 = NULL;
  32391. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32392. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32393. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32394. /* test wrap around... */
  32395. ExpectIntEQ(BIO_reset(bio1), 0);
  32396. ExpectIntEQ(BIO_reset(bio3), 0);
  32397. /* fill write buffer, read only small amount then write again */
  32398. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32399. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32400. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32401. for (i = 0; i < 4; i++) {
  32402. ExpectIntEQ(bufPt[i], i);
  32403. }
  32404. /* try writing over read index */
  32405. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32406. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  32407. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32408. /* read and write 0 bytes */
  32409. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32410. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32411. /* should read only to end of write buffer then need to read again */
  32412. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32413. for (i = 0; i < 16; i++) {
  32414. ExpectIntEQ(bufPt[i], buff[4 + i]);
  32415. }
  32416. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32417. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32418. for (i = 0; i < 4; i++) {
  32419. ExpectIntEQ(bufPt[i], 0);
  32420. }
  32421. /* read index should not have advanced with nread0 */
  32422. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32423. for (i = 0; i < 4; i++) {
  32424. ExpectIntEQ(bufPt[i], 0);
  32425. }
  32426. /* write and fill up buffer checking reset of index state */
  32427. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32428. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32429. /* test reset on data in bio1 write buffer */
  32430. ExpectIntEQ(BIO_reset(bio1), 0);
  32431. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32432. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32433. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32434. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32435. ExpectNotNull(p);
  32436. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32437. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32438. for (i = 0; i < 6; i++) {
  32439. ExpectIntEQ(bufPt[i], i);
  32440. }
  32441. /* test case of writing twice with offset read index */
  32442. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32443. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32444. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32445. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32446. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32447. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32448. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32449. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32450. BIO_free(bio1);
  32451. bio1 = NULL;
  32452. BIO_free(bio3);
  32453. bio3 = NULL;
  32454. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32455. {
  32456. BIO* bioA = NULL;
  32457. BIO* bioB = NULL;
  32458. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32459. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32460. BIO_free(bioA);
  32461. bioA = NULL;
  32462. BIO_free(bioB);
  32463. bioB = NULL;
  32464. }
  32465. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32466. /* BIOs with file pointers */
  32467. #if !defined(NO_FILESYSTEM)
  32468. {
  32469. XFILE f1 = XBADFILE;
  32470. XFILE f2 = XBADFILE;
  32471. BIO* f_bio1 = NULL;
  32472. BIO* f_bio2 = NULL;
  32473. unsigned char cert[300];
  32474. char testFile[] = "tests/bio_write_test.txt";
  32475. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32476. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32477. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32478. /* Failure due to wrong BIO type */
  32479. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32480. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32481. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  32482. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32483. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  32484. WOLFSSL_SUCCESS);
  32485. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32486. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32487. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32488. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32489. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32490. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32491. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32492. ExpectIntEQ(BIO_reset(f_bio2), 0);
  32493. ExpectIntEQ(BIO_tell(NULL),-1);
  32494. ExpectIntEQ(BIO_tell(f_bio2),0);
  32495. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  32496. ExpectIntEQ(BIO_tell(f_bio2),4);
  32497. BIO_free(f_bio1);
  32498. f_bio1 = NULL;
  32499. BIO_free(f_bio2);
  32500. f_bio2 = NULL;
  32501. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32502. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32503. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32504. BIO_free(f_bio1);
  32505. f_bio1 = NULL;
  32506. }
  32507. #endif /* !defined(NO_FILESYSTEM) */
  32508. /* BIO info callback */
  32509. {
  32510. const char* testArg = "test";
  32511. BIO* cb_bio = NULL;
  32512. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32513. BIO_set_callback(cb_bio, bioCallback);
  32514. ExpectNotNull(BIO_get_callback(cb_bio));
  32515. BIO_set_callback(cb_bio, NULL);
  32516. ExpectNull(BIO_get_callback(cb_bio));
  32517. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32518. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32519. ExpectNull(BIO_get_callback_arg(NULL));
  32520. BIO_free(cb_bio);
  32521. cb_bio = NULL;
  32522. }
  32523. /* BIO_vfree */
  32524. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  32525. BIO_vfree(NULL);
  32526. BIO_vfree(bio1);
  32527. #endif
  32528. return EXPECT_RESULT();
  32529. }
  32530. #endif /* !NO_BIO */
  32531. static int test_wolfSSL_a2i_IPADDRESS(void)
  32532. {
  32533. EXPECT_DECLS;
  32534. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32535. const unsigned char* data = NULL;
  32536. int dataSz = 0;
  32537. ASN1_OCTET_STRING *st = NULL;
  32538. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32539. const unsigned char ipv6_exp[] = {
  32540. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32541. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32542. };
  32543. const unsigned char ipv6_home[] = {
  32544. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32545. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32546. };
  32547. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32548. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32549. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32550. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  32551. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32552. ASN1_STRING_free(st);
  32553. st = NULL;
  32554. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  32555. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32556. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  32557. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32558. ASN1_STRING_free(st);
  32559. st = NULL;
  32560. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32561. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32562. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  32563. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32564. ASN1_STRING_free(st);
  32565. #endif
  32566. return EXPECT_RESULT();
  32567. }
  32568. static int test_wolfSSL_DES_ecb_encrypt(void)
  32569. {
  32570. EXPECT_DECLS;
  32571. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32572. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  32573. WOLFSSL_DES_key_schedule key;
  32574. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  32575. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  32576. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  32577. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32578. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32579. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32580. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32581. /* Encrypt messages */
  32582. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  32583. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  32584. {
  32585. /* Decrypt messages */
  32586. int ret1 = 0;
  32587. int ret2 = 0;
  32588. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  32589. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  32590. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  32591. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  32592. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  32593. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  32594. }
  32595. #endif
  32596. return EXPECT_RESULT();
  32597. }
  32598. static int test_wolfSSL_X509_cmp_time(void)
  32599. {
  32600. EXPECT_DECLS;
  32601. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32602. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32603. WOLFSSL_ASN1_TIME asn_time;
  32604. time_t t;
  32605. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32606. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32607. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32608. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32609. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32610. #endif
  32611. return EXPECT_RESULT();
  32612. }
  32613. static int test_wolfSSL_X509_time_adj(void)
  32614. {
  32615. EXPECT_DECLS;
  32616. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32617. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32618. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32619. !defined(NO_ASN_TIME)
  32620. X509* x509 = NULL;
  32621. time_t t;
  32622. time_t not_before;
  32623. time_t not_after;
  32624. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32625. client_cert_der_2048, sizeof_client_cert_der_2048,
  32626. WOLFSSL_FILETYPE_ASN1));
  32627. t = 0;
  32628. not_before = wc_Time(0);
  32629. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32630. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32631. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32632. /* Check X509_gmtime_adj, too. */
  32633. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32634. X509_free(x509);
  32635. #endif
  32636. return EXPECT_RESULT();
  32637. }
  32638. static int test_wolfSSL_X509(void)
  32639. {
  32640. EXPECT_DECLS;
  32641. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32642. !defined(NO_RSA)
  32643. X509* x509 = NULL;
  32644. #ifndef NO_BIO
  32645. BIO* bio = NULL;
  32646. X509_STORE_CTX* ctx = NULL;
  32647. X509_STORE* store = NULL;
  32648. #endif
  32649. char der[] = "certs/ca-cert.der";
  32650. XFILE fp = XBADFILE;
  32651. ExpectNotNull(x509 = X509_new());
  32652. X509_free(x509);
  32653. x509 = NULL;
  32654. #ifndef NO_BIO
  32655. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  32656. SSL_FILETYPE_PEM));
  32657. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32658. #ifdef WOLFSSL_CERT_GEN
  32659. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32660. #endif
  32661. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32662. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32663. ExpectNotNull(store = X509_STORE_new());
  32664. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32665. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32666. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32667. X509_STORE_CTX_free(ctx);
  32668. X509_STORE_free(store);
  32669. X509_free(x509);
  32670. x509 = NULL;
  32671. BIO_free(bio);
  32672. #endif
  32673. /** d2i_X509_fp test **/
  32674. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  32675. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32676. ExpectNotNull(x509);
  32677. X509_free(x509);
  32678. x509 = NULL;
  32679. if (fp != XBADFILE) {
  32680. XFCLOSE(fp);
  32681. fp = XBADFILE;
  32682. }
  32683. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  32684. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32685. ExpectNotNull(x509);
  32686. X509_free(x509);
  32687. if (fp != XBADFILE)
  32688. XFCLOSE(fp);
  32689. /* X509_up_ref test */
  32690. ExpectIntEQ(X509_up_ref(NULL), 0);
  32691. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  32692. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32693. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32694. X509_free(x509); /* refCount = 2 */
  32695. X509_free(x509); /* refCount = 1 */
  32696. X509_free(x509); /* refCount = 0, free */
  32697. #endif
  32698. return EXPECT_RESULT();
  32699. }
  32700. static int test_wolfSSL_X509_get_ext_count(void)
  32701. {
  32702. EXPECT_DECLS;
  32703. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32704. !defined(NO_RSA)
  32705. int ret = 0;
  32706. WOLFSSL_X509* x509 = NULL;
  32707. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  32708. XFILE f = XBADFILE;
  32709. /* NULL parameter check */
  32710. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  32711. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32712. SSL_FILETYPE_PEM));
  32713. ExpectIntEQ(X509_get_ext_count(x509), 5);
  32714. wolfSSL_X509_free(x509);
  32715. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  32716. SSL_FILETYPE_PEM));
  32717. ExpectIntEQ(X509_get_ext_count(x509), 5);
  32718. wolfSSL_X509_free(x509);
  32719. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  32720. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  32721. if (f != XBADFILE)
  32722. XFCLOSE(f);
  32723. /* wolfSSL_X509_get_ext_count() valid input */
  32724. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  32725. /* wolfSSL_X509_get_ext_count() NULL argument */
  32726. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  32727. wolfSSL_X509_free(x509);
  32728. #endif
  32729. return EXPECT_RESULT();
  32730. }
  32731. static int test_wolfSSL_X509_sign2(void)
  32732. {
  32733. EXPECT_DECLS;
  32734. /* test requires WOLFSSL_AKID_NAME to match expected output */
  32735. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32736. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  32737. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  32738. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32739. defined(WOLFSSL_IP_ALT_NAME))
  32740. WOLFSSL_X509 *x509 = NULL;
  32741. WOLFSSL_X509 *ca = NULL;
  32742. const unsigned char *der = NULL;
  32743. const unsigned char *pt = NULL;
  32744. WOLFSSL_EVP_PKEY *priv = NULL;
  32745. WOLFSSL_X509_NAME *name = NULL;
  32746. int derSz;
  32747. #ifndef NO_ASN_TIME
  32748. WOLFSSL_ASN1_TIME *notBefore = NULL;
  32749. WOLFSSL_ASN1_TIME *notAfter = NULL;
  32750. const int year = 365*24*60*60;
  32751. const int day = 24*60*60;
  32752. const int hour = 60*60;
  32753. const int mini = 60;
  32754. time_t t;
  32755. #endif
  32756. const unsigned char expected[] = {
  32757. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  32758. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  32759. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  32760. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  32761. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  32762. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  32763. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  32764. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  32765. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  32766. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  32767. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  32768. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  32769. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  32770. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  32771. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32772. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  32773. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  32774. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  32775. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  32776. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  32777. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  32778. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  32779. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  32780. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  32781. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  32782. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  32783. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  32784. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  32785. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  32786. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  32787. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  32788. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  32789. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  32790. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32791. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  32792. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  32793. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  32794. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  32795. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  32796. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  32797. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  32798. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  32799. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  32800. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  32801. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  32802. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  32803. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  32804. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  32805. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  32806. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  32807. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  32808. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  32809. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  32810. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  32811. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  32812. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  32813. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  32814. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  32815. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  32816. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  32817. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  32818. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  32819. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32820. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32821. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  32822. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32823. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32824. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  32825. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  32826. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  32827. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  32828. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  32829. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  32830. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  32831. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  32832. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  32833. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  32834. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  32835. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  32836. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  32837. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  32838. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  32839. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  32840. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  32841. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  32842. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  32843. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  32844. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  32845. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  32846. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  32847. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  32848. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  32849. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  32850. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  32851. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  32852. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  32853. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  32854. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  32855. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  32856. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  32857. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  32858. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  32859. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  32860. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  32861. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  32862. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  32863. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  32864. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  32865. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  32866. };
  32867. pt = ca_key_der_2048;
  32868. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  32869. sizeof_ca_key_der_2048));
  32870. pt = client_cert_der_2048;
  32871. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  32872. sizeof_client_cert_der_2048));
  32873. pt = ca_cert_der_2048;
  32874. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  32875. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  32876. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32877. #ifndef NO_ASN_TIME
  32878. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32879. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  32880. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  32881. ExpectIntEQ(notAfter->length, 13);
  32882. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  32883. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  32884. #endif
  32885. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  32886. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  32887. ExpectIntEQ(derSz, sizeof(expected));
  32888. #ifndef NO_ASN_TIME
  32889. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  32890. #endif
  32891. wolfSSL_X509_free(ca);
  32892. wolfSSL_X509_free(x509);
  32893. wolfSSL_EVP_PKEY_free(priv);
  32894. #ifndef NO_ASN_TIME
  32895. wolfSSL_ASN1_TIME_free(notBefore);
  32896. wolfSSL_ASN1_TIME_free(notAfter);
  32897. #endif
  32898. #endif
  32899. return EXPECT_RESULT();
  32900. }
  32901. static int test_wolfSSL_X509_sign(void)
  32902. {
  32903. EXPECT_DECLS;
  32904. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  32905. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  32906. int ret;
  32907. char *cn = NULL;
  32908. word32 cnSz;
  32909. X509_NAME *name = NULL;
  32910. X509 *x509 = NULL;
  32911. X509 *ca = NULL;
  32912. DecodedCert dCert;
  32913. EVP_PKEY *pub = NULL;
  32914. EVP_PKEY *priv = NULL;
  32915. EVP_MD_CTX *mctx = NULL;
  32916. #if defined(USE_CERT_BUFFERS_1024)
  32917. const unsigned char* rsaPriv = client_key_der_1024;
  32918. const unsigned char* rsaPub = client_keypub_der_1024;
  32919. const unsigned char* certIssuer = client_cert_der_1024;
  32920. long clientKeySz = (long)sizeof_client_key_der_1024;
  32921. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  32922. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  32923. #elif defined(USE_CERT_BUFFERS_2048)
  32924. const unsigned char* rsaPriv = client_key_der_2048;
  32925. const unsigned char* rsaPub = client_keypub_der_2048;
  32926. const unsigned char* certIssuer = client_cert_der_2048;
  32927. long clientKeySz = (long)sizeof_client_key_der_2048;
  32928. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  32929. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  32930. #endif
  32931. byte sn[16];
  32932. int snSz = sizeof(sn);
  32933. /* Set X509_NAME fields */
  32934. ExpectNotNull(name = X509_NAME_new());
  32935. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  32936. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  32937. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32938. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  32939. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32940. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  32941. /* Get private and public keys */
  32942. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  32943. clientKeySz));
  32944. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  32945. ExpectNotNull(x509 = X509_new());
  32946. /* Set version 3 */
  32947. ExpectIntNE(X509_set_version(x509, 2L), 0);
  32948. /* Set subject name, add pubkey, and sign certificate */
  32949. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  32950. X509_NAME_free(name);
  32951. name = NULL;
  32952. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  32953. #ifdef WOLFSSL_ALT_NAMES
  32954. /* Add some subject alt names */
  32955. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  32956. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  32957. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32958. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  32959. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32960. "sphygmomanometer",
  32961. ASN_DNS_TYPE), SSL_SUCCESS);
  32962. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32963. "supercalifragilisticexpialidocious",
  32964. ASN_DNS_TYPE), SSL_SUCCESS);
  32965. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32966. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32967. ASN_DNS_TYPE), SSL_SUCCESS);
  32968. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32969. {
  32970. unsigned char ip4_type[] = {127,128,0,255};
  32971. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32972. 0xff, 0xee, 0x99, 0x88,
  32973. 0x77, 0x66, 0x55, 0x44,
  32974. 0x00, 0x33, 0x22, 0x11};
  32975. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32976. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32977. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32978. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32979. }
  32980. #endif
  32981. #endif /* WOLFSSL_ALT_NAMES */
  32982. /* test valid sign case */
  32983. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  32984. /* test valid X509_sign_ctx case */
  32985. ExpectNotNull(mctx = EVP_MD_CTX_new());
  32986. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32987. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  32988. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32989. ExpectIntEQ(X509_get_ext_count(x509), 1);
  32990. #endif
  32991. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32992. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32993. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32994. #endif
  32995. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32996. WOLFSSL_SUCCESS);
  32997. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32998. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32999. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  33000. * with the MSB set. See GenerateInteger in asn.c */
  33001. #ifndef USE_CERT_BUFFERS_1024
  33002. #ifndef WOLFSSL_ALT_NAMES
  33003. /* Valid case - size should be 781-786 with 16 byte serial number */
  33004. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  33005. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33006. /* Valid case - size should be 955-960 with 16 byte serial number */
  33007. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  33008. #else
  33009. /* Valid case - size should be 926-931 with 16 byte serial number */
  33010. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  33011. #endif
  33012. #else
  33013. #ifndef WOLFSSL_ALT_NAMES
  33014. /* Valid case - size should be 537-542 with 16 byte serial number */
  33015. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  33016. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  33017. /* Valid case - size should be 695-670 with 16 byte serial number */
  33018. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  33019. #else
  33020. /* Valid case - size should be 666-671 with 16 byte serial number */
  33021. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  33022. #endif
  33023. #endif
  33024. /* check that issuer name is as expected after signature */
  33025. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  33026. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  33027. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  33028. ExpectNotNull(name = X509_get_subject_name(ca));
  33029. cnSz = X509_NAME_get_sz(name);
  33030. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33031. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33032. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  33033. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33034. cn = NULL;
  33035. #ifdef WOLFSSL_MULTI_ATTRIB
  33036. /* test adding multiple OU's to the signer */
  33037. ExpectNotNull(name = X509_get_subject_name(ca));
  33038. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33039. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  33040. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  33041. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  33042. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  33043. #endif
  33044. ExpectNotNull(name = X509_get_subject_name(ca));
  33045. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  33046. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33047. ExpectNotNull(name = X509_get_issuer_name(x509));
  33048. cnSz = X509_NAME_get_sz(name);
  33049. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33050. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33051. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  33052. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  33053. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33054. cn = NULL;
  33055. FreeDecodedCert(&dCert);
  33056. /* Test invalid parameters */
  33057. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  33058. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  33059. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  33060. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  33061. EVP_MD_CTX_free(mctx);
  33062. mctx = NULL;
  33063. ExpectNotNull(mctx = EVP_MD_CTX_new());
  33064. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  33065. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  33066. /* test invalid version number */
  33067. #if defined(OPENSSL_ALL)
  33068. ExpectIntNE(X509_set_version(x509, 6L), 0);
  33069. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33070. /* uses ParseCert which fails on bad version number */
  33071. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  33072. #endif
  33073. EVP_MD_CTX_free(mctx);
  33074. EVP_PKEY_free(priv);
  33075. EVP_PKEY_free(pub);
  33076. X509_free(x509);
  33077. X509_free(ca);
  33078. #endif
  33079. return EXPECT_RESULT();
  33080. }
  33081. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  33082. {
  33083. EXPECT_DECLS;
  33084. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33085. X509* x509 = NULL;
  33086. const X509_ALGOR* alg;
  33087. ExpectNotNull(x509 = X509_new());
  33088. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  33089. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  33090. X509_free(x509);
  33091. #endif
  33092. return EXPECT_RESULT();
  33093. }
  33094. static int test_wolfSSL_X509_ALGOR_get0(void)
  33095. {
  33096. EXPECT_DECLS;
  33097. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33098. !defined(NO_SHA256) && !defined(NO_RSA)
  33099. X509* x509 = NULL;
  33100. const ASN1_OBJECT* obj = NULL;
  33101. const X509_ALGOR* alg = NULL;
  33102. int pptype = 0;
  33103. const void *ppval = NULL;
  33104. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  33105. SSL_FILETYPE_PEM));
  33106. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  33107. /* Invalid case */
  33108. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  33109. ExpectNull(obj);
  33110. /* Valid case */
  33111. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  33112. ExpectNotNull(obj);
  33113. ExpectNull(ppval);
  33114. ExpectIntNE(pptype, 0);
  33115. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  33116. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  33117. X509_free(x509);
  33118. #endif
  33119. return EXPECT_RESULT();
  33120. }
  33121. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  33122. {
  33123. EXPECT_DECLS;
  33124. #if defined(OPENSSL_EXTRA)
  33125. X509_VERIFY_PARAM *paramTo = NULL;
  33126. X509_VERIFY_PARAM *paramFrom = NULL;
  33127. char testIPv4[] = "127.0.0.1";
  33128. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33129. char testhostName1[] = "foo.hoge.com";
  33130. char testhostName2[] = "foobar.hoge.com";
  33131. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  33132. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  33133. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  33134. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  33135. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33136. (int)XSTRLEN(testhostName1)), 1);
  33137. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33138. (int)XSTRLEN(testhostName1)));
  33139. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33140. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33141. ExpectIntEQ(0x01, paramFrom->hostFlags);
  33142. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  33143. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  33144. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33145. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  33146. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  33147. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33148. /* null pointer */
  33149. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  33150. /* in the case of "from" null, returns success */
  33151. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  33152. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  33153. /* inherit flags test : VPARAM_DEFAULT */
  33154. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33155. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33156. (int)XSTRLEN(testhostName1)));
  33157. ExpectIntEQ(0x01, paramTo->hostFlags);
  33158. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33159. /* inherit flags test : VPARAM OVERWRITE */
  33160. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33161. (int)XSTRLEN(testhostName2)), 1);
  33162. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33163. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33164. if (paramTo != NULL) {
  33165. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33166. }
  33167. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33168. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33169. (int)XSTRLEN(testhostName1)));
  33170. ExpectIntEQ(0x01, paramTo->hostFlags);
  33171. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33172. /* inherit flags test : VPARAM_RESET_FLAGS */
  33173. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33174. (int)XSTRLEN(testhostName2)), 1);
  33175. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33176. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33177. if (paramTo != NULL) {
  33178. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33179. }
  33180. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33181. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33182. (int)XSTRLEN(testhostName1)));
  33183. ExpectIntEQ(0x01, paramTo->hostFlags);
  33184. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33185. /* inherit flags test : VPARAM_LOCKED */
  33186. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33187. (int)XSTRLEN(testhostName2)), 1);
  33188. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33189. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33190. if (paramTo != NULL) {
  33191. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33192. }
  33193. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33194. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33195. (int)XSTRLEN(testhostName2)));
  33196. ExpectIntEQ(0x00, paramTo->hostFlags);
  33197. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33198. /* test for incorrect parameters */
  33199. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  33200. 0);
  33201. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  33202. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33203. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  33204. 1);
  33205. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  33206. X509_V_FLAG_CRL_CHECK_ALL);
  33207. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  33208. X509_V_FLAG_CRL_CHECK_ALL), 1);
  33209. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  33210. X509_VERIFY_PARAM_free(paramTo);
  33211. X509_VERIFY_PARAM_free(paramFrom);
  33212. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33213. #endif
  33214. return EXPECT_RESULT();
  33215. }
  33216. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33217. static int test_wolfSSL_check_domain_verify_count = 0;
  33218. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33219. WOLFSSL_X509_STORE_CTX* store)
  33220. {
  33221. EXPECT_DECLS;
  33222. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  33223. ExpectIntEQ(preverify, 1);
  33224. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  33225. return EXPECT_SUCCESS();
  33226. }
  33227. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33228. {
  33229. EXPECT_DECLS;
  33230. X509_VERIFY_PARAM *param = NULL;
  33231. ExpectNotNull(param = SSL_get0_param(ssl));
  33232. /* Domain check should only be done on the leaf cert */
  33233. X509_VERIFY_PARAM_set_hostflags(param,
  33234. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33235. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33236. "wolfSSL Server Chain", 0), 1);
  33237. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33238. test_wolfSSL_check_domain_verify_cb);
  33239. return EXPECT_RESULT();
  33240. }
  33241. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33242. {
  33243. EXPECT_DECLS;
  33244. /* Use a cert with different domains in chain */
  33245. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33246. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33247. return EXPECT_RESULT();
  33248. }
  33249. static int test_wolfSSL_check_domain(void)
  33250. {
  33251. EXPECT_DECLS;
  33252. test_ssl_cbf func_cb_client;
  33253. test_ssl_cbf func_cb_server;
  33254. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  33255. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  33256. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33257. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33258. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  33259. &func_cb_server, NULL), TEST_SUCCESS);
  33260. /* Should have been called once for each cert in sent chain */
  33261. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33262. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33263. #else
  33264. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33265. #endif
  33266. return EXPECT_RESULT();
  33267. }
  33268. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  33269. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33270. {
  33271. EXPECT_DECLS;
  33272. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33273. X509* x509 = NULL;
  33274. X509_PUBKEY* pubKey;
  33275. ExpectNotNull(x509 = X509_new());
  33276. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33277. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33278. X509_free(x509);
  33279. #endif
  33280. return EXPECT_RESULT();
  33281. }
  33282. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33283. {
  33284. EXPECT_DECLS;
  33285. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33286. !defined(NO_SHA256) && !defined(NO_RSA)
  33287. X509* x509 = NULL;
  33288. ASN1_OBJECT* obj = NULL;
  33289. const ASN1_OBJECT* pa_oid = NULL;
  33290. X509_PUBKEY* pubKey = NULL;
  33291. X509_PUBKEY* pubKey2 = NULL;
  33292. EVP_PKEY* evpKey = NULL;
  33293. const unsigned char *pk = NULL;
  33294. int ppklen;
  33295. int pptype;
  33296. X509_ALGOR *pa = NULL;
  33297. const void *pval;
  33298. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33299. SSL_FILETYPE_PEM));
  33300. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33301. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33302. ExpectNotNull(pk);
  33303. ExpectNotNull(pa);
  33304. ExpectNotNull(pubKey);
  33305. ExpectIntGT(ppklen, 0);
  33306. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33307. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33308. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  33309. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33310. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33311. ExpectNotNull(pk);
  33312. ExpectNotNull(pa);
  33313. ExpectIntGT(ppklen, 0);
  33314. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33315. ExpectNotNull(pa_oid);
  33316. ExpectNull(pval);
  33317. ExpectIntEQ(pptype, V_ASN1_NULL);
  33318. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33319. X509_PUBKEY_free(pubKey2);
  33320. X509_free(x509);
  33321. EVP_PKEY_free(evpKey);
  33322. #endif
  33323. return EXPECT_RESULT();
  33324. }
  33325. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33326. {
  33327. EXPECT_DECLS;
  33328. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33329. X509* x509 = NULL;
  33330. ASN1_OBJECT* obj = NULL;
  33331. ASN1_OBJECT* poid = NULL;
  33332. const ASN1_OBJECT* pa_oid = NULL;
  33333. X509_PUBKEY* pubKey = NULL;
  33334. X509_PUBKEY* pubKey2 = NULL;
  33335. EVP_PKEY* evpKey = NULL;
  33336. const unsigned char *pk = NULL;
  33337. int ppklen;
  33338. int pptype;
  33339. X509_ALGOR *pa = NULL;
  33340. const void *pval;
  33341. char buf[50];
  33342. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33343. SSL_FILETYPE_PEM));
  33344. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33345. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33346. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  33347. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33348. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33349. ExpectNotNull(pk);
  33350. ExpectNotNull(pa);
  33351. ExpectIntGT(ppklen, 0);
  33352. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33353. ExpectNotNull(pa_oid);
  33354. ExpectNotNull(pval);
  33355. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  33356. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33357. poid = (ASN1_OBJECT *)pval;
  33358. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33359. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33360. X509_PUBKEY_free(pubKey2);
  33361. X509_free(x509);
  33362. EVP_PKEY_free(evpKey);
  33363. #endif
  33364. return EXPECT_RESULT();
  33365. }
  33366. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33367. {
  33368. EXPECT_DECLS;
  33369. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33370. word32 bytes;
  33371. #ifdef USE_CERT_BUFFERS_1024
  33372. byte tmp[ONEK_BUF];
  33373. #elif defined(USE_CERT_BUFFERS_2048)
  33374. byte tmp[TWOK_BUF];
  33375. #else
  33376. byte tmp[TWOK_BUF];
  33377. #endif /* END USE_CERT_BUFFERS_1024 */
  33378. const unsigned char* dsaKeyDer = tmp;
  33379. ASN1_OBJECT* obj = NULL;
  33380. ASN1_STRING* str;
  33381. const ASN1_OBJECT* pa_oid = NULL;
  33382. X509_PUBKEY* pubKey = NULL;
  33383. EVP_PKEY* evpKey = NULL;
  33384. const unsigned char *pk = NULL;
  33385. int ppklen, pptype;
  33386. X509_ALGOR *pa = NULL;
  33387. const void *pval;
  33388. #ifdef USE_CERT_BUFFERS_1024
  33389. XMEMSET(tmp, 0, sizeof(tmp));
  33390. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33391. bytes = sizeof_dsa_key_der_1024;
  33392. #elif defined(USE_CERT_BUFFERS_2048)
  33393. XMEMSET(tmp, 0, sizeof(tmp));
  33394. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33395. bytes = sizeof_dsa_key_der_2048;
  33396. #else
  33397. {
  33398. XFILE fp = XBADFILE;
  33399. XMEMSET(tmp, 0, sizeof(tmp));
  33400. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  33401. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  33402. if (fp != XBADFILE)
  33403. XFCLOSE(fp);
  33404. }
  33405. #endif
  33406. /* Initialize pkey with der format dsa key */
  33407. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33408. ExpectNotNull(pubKey = X509_PUBKEY_new());
  33409. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33410. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33411. ExpectNotNull(pk);
  33412. ExpectNotNull(pa);
  33413. ExpectIntGT(ppklen, 0);
  33414. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33415. ExpectNotNull(pa_oid);
  33416. ExpectNotNull(pval);
  33417. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  33418. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33419. str = (ASN1_STRING *)pval;
  33420. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33421. #ifdef USE_CERT_BUFFERS_1024
  33422. ExpectIntEQ(ASN1_STRING_length(str), 291);
  33423. #else
  33424. ExpectIntEQ(ASN1_STRING_length(str), 549);
  33425. #endif /* END USE_CERT_BUFFERS_1024 */
  33426. X509_PUBKEY_free(pubKey);
  33427. EVP_PKEY_free(evpKey);
  33428. #endif
  33429. return EXPECT_RESULT();
  33430. }
  33431. static int test_wolfSSL_BUF(void)
  33432. {
  33433. EXPECT_DECLS;
  33434. #if defined(OPENSSL_EXTRA)
  33435. BUF_MEM* buf = NULL;
  33436. ExpectNotNull(buf = BUF_MEM_new());
  33437. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  33438. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  33439. BUF_MEM_free(buf);
  33440. #endif
  33441. return EXPECT_RESULT();
  33442. }
  33443. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33444. static int stub_rand_seed(const void *buf, int num)
  33445. {
  33446. (void)buf;
  33447. (void)num;
  33448. return 123;
  33449. }
  33450. static int stub_rand_bytes(unsigned char *buf, int num)
  33451. {
  33452. (void)buf;
  33453. (void)num;
  33454. return 456;
  33455. }
  33456. static byte* was_stub_rand_cleanup_called(void)
  33457. {
  33458. static byte was_called = 0;
  33459. return &was_called;
  33460. }
  33461. static void stub_rand_cleanup(void)
  33462. {
  33463. byte* was_called = was_stub_rand_cleanup_called();
  33464. *was_called = 1;
  33465. return;
  33466. }
  33467. static byte* was_stub_rand_add_called(void)
  33468. {
  33469. static byte was_called = 0;
  33470. return &was_called;
  33471. }
  33472. static int stub_rand_add(const void *buf, int num, double entropy)
  33473. {
  33474. byte* was_called = was_stub_rand_add_called();
  33475. (void)buf;
  33476. (void)num;
  33477. (void)entropy;
  33478. *was_called = 1;
  33479. return 0;
  33480. }
  33481. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33482. {
  33483. (void)buf;
  33484. (void)num;
  33485. return 9876;
  33486. }
  33487. static int stub_rand_status(void)
  33488. {
  33489. return 5432;
  33490. }
  33491. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33492. static int test_wolfSSL_RAND_set_rand_method(void)
  33493. {
  33494. EXPECT_DECLS;
  33495. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33496. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33497. unsigned char* buf = NULL;
  33498. int num = 0;
  33499. double entropy = 0;
  33500. int ret;
  33501. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33502. byte* was_add_called = was_stub_rand_add_called();
  33503. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33504. DYNAMIC_TYPE_TMP_BUFFER));
  33505. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  33506. ExpectIntEQ(*was_cleanup_called, 0);
  33507. RAND_cleanup();
  33508. ExpectIntEQ(*was_cleanup_called, 0);
  33509. rand_methods.seed = &stub_rand_seed;
  33510. rand_methods.bytes = &stub_rand_bytes;
  33511. rand_methods.cleanup = &stub_rand_cleanup;
  33512. rand_methods.add = &stub_rand_add;
  33513. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33514. rand_methods.status = &stub_rand_status;
  33515. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33516. ExpectIntEQ(RAND_seed(buf, num), 123);
  33517. ExpectIntEQ(RAND_bytes(buf, num), 456);
  33518. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33519. ExpectIntEQ(RAND_status(), 5432);
  33520. ExpectIntEQ(*was_add_called, 0);
  33521. /* The function pointer for RAND_add returns int, but RAND_add itself
  33522. * returns void. */
  33523. RAND_add(buf, num, entropy);
  33524. ExpectIntEQ(*was_add_called, 1);
  33525. was_add_called = 0;
  33526. ExpectIntEQ(*was_cleanup_called, 0);
  33527. RAND_cleanup();
  33528. ExpectIntEQ(*was_cleanup_called, 1);
  33529. *was_cleanup_called = 0;
  33530. ret = RAND_set_rand_method(NULL);
  33531. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  33532. ExpectIntNE(RAND_status(), 5432);
  33533. ExpectIntEQ(*was_cleanup_called, 0);
  33534. RAND_cleanup();
  33535. ExpectIntEQ(*was_cleanup_called, 0);
  33536. RAND_set_rand_method(NULL);
  33537. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33538. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33539. return EXPECT_RESULT();
  33540. }
  33541. static int test_wolfSSL_RAND_bytes(void)
  33542. {
  33543. EXPECT_DECLS;
  33544. #if defined(OPENSSL_EXTRA)
  33545. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33546. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33547. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33548. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33549. int max_bufsize;
  33550. byte *my_buf = NULL;
  33551. /* sanity check */
  33552. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  33553. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  33554. max_bufsize = size4;
  33555. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33556. DYNAMIC_TYPE_TMP_BUFFER));
  33557. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  33558. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  33559. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  33560. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  33561. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  33562. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  33563. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  33564. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33565. #endif
  33566. return EXPECT_RESULT();
  33567. }
  33568. static int test_wolfSSL_RAND(void)
  33569. {
  33570. EXPECT_DECLS;
  33571. #if defined(OPENSSL_EXTRA)
  33572. byte seed[16];
  33573. XMEMSET(seed, 0, sizeof(seed));
  33574. /* No global methods set. */
  33575. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  33576. ExpectIntEQ(RAND_poll(), 1);
  33577. RAND_cleanup();
  33578. ExpectIntEQ(RAND_egd(NULL), -1);
  33579. #ifndef NO_FILESYSTEM
  33580. {
  33581. char fname[100];
  33582. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33583. ExpectIntEQ(RAND_write_file(NULL), 0);
  33584. }
  33585. #endif
  33586. #endif
  33587. return EXPECT_RESULT();
  33588. }
  33589. static int test_wolfSSL_PKCS8_Compat(void)
  33590. {
  33591. EXPECT_DECLS;
  33592. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  33593. !defined(NO_BIO)
  33594. PKCS8_PRIV_KEY_INFO* pt = NULL;
  33595. BIO* bio = NULL;
  33596. XFILE f = XBADFILE;
  33597. int bytes;
  33598. char pkcs8_buffer[512];
  33599. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33600. EVP_PKEY *pkey = NULL;
  33601. #endif
  33602. /* file from wolfssl/certs/ directory */
  33603. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  33604. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  33605. 0);
  33606. if (f != XBADFILE)
  33607. XFCLOSE(f);
  33608. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33609. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33610. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33611. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  33612. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  33613. /* gets PKCS8 pointer to pkey */
  33614. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  33615. EVP_PKEY_free(pkey);
  33616. #endif
  33617. BIO_free(bio);
  33618. PKCS8_PRIV_KEY_INFO_free(pt);
  33619. #endif
  33620. return EXPECT_RESULT();
  33621. }
  33622. static int test_wolfSSL_PKCS8_d2i(void)
  33623. {
  33624. EXPECT_DECLS;
  33625. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  33626. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  33627. * requires a 12 byte password in FIPS mode. This test ends up
  33628. * trying to use an 8 byte password. */
  33629. #ifndef NO_FILESYSTEM
  33630. unsigned char pkcs8_buffer[2048];
  33631. const unsigned char* p;
  33632. int bytes;
  33633. XFILE file = XBADFILE;
  33634. WOLFSSL_EVP_PKEY* pkey = NULL;
  33635. #ifndef NO_BIO
  33636. BIO* bio = NULL;
  33637. #if defined(OPENSSL_ALL) && \
  33638. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  33639. defined(HAVE_ECC)) && \
  33640. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33641. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  33642. #endif
  33643. #endif
  33644. #ifndef NO_RSA
  33645. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  33646. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  33647. #ifndef NO_DES3
  33648. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  33649. #endif
  33650. #endif /* NO_RSA */
  33651. #ifdef HAVE_ECC
  33652. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  33653. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  33654. #ifndef NO_DES3
  33655. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  33656. #endif
  33657. #endif /* HAVE_ECC */
  33658. #endif /* !NO_FILESYSTEM */
  33659. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  33660. #ifndef NO_RSA
  33661. #ifdef USE_CERT_BUFFERS_1024
  33662. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  33663. int rsaSz = sizeof_server_key_der_1024;
  33664. #else
  33665. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  33666. int rsaSz = sizeof_server_key_der_2048;
  33667. #endif
  33668. #endif
  33669. #ifdef HAVE_ECC
  33670. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  33671. int ecSz = sizeof_ecc_key_der_256;
  33672. #endif
  33673. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  33674. #ifndef NO_FILESYSTEM
  33675. (void)pkcs8_buffer;
  33676. (void)p;
  33677. (void)bytes;
  33678. (void)file;
  33679. #ifndef NO_BIO
  33680. (void)bio;
  33681. #endif
  33682. #endif
  33683. #ifdef OPENSSL_ALL
  33684. #ifndef NO_RSA
  33685. /* Try to auto-detect normal RSA private key */
  33686. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  33687. EVP_PKEY_free(pkey);
  33688. pkey = NULL;
  33689. #endif
  33690. #ifdef HAVE_ECC
  33691. /* Try to auto-detect normal EC private key */
  33692. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  33693. EVP_PKEY_free(pkey);
  33694. pkey = NULL;
  33695. #endif
  33696. #endif /* OPENSSL_ALL */
  33697. #ifndef NO_FILESYSTEM
  33698. #ifndef NO_RSA
  33699. /* Get DER encoded RSA PKCS#8 data. */
  33700. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  33701. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33702. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33703. file)), 0);
  33704. if (file != XBADFILE) {
  33705. XFCLOSE(file);
  33706. file = XBADFILE;
  33707. }
  33708. p = pkcs8_buffer;
  33709. #ifdef OPENSSL_ALL
  33710. /* Try to decode - auto-detect key type. */
  33711. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33712. #else
  33713. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  33714. #endif
  33715. /* Get PEM encoded RSA PKCS#8 data. */
  33716. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  33717. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33718. file)), 0);
  33719. if (file != XBADFILE) {
  33720. XFCLOSE(file);
  33721. file = XBADFILE;
  33722. }
  33723. #if defined(OPENSSL_ALL) && \
  33724. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33725. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33726. /* Write PKCS#8 PEM to BIO. */
  33727. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33728. NULL), bytes);
  33729. /* Compare file and written data */
  33730. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33731. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33732. BIO_free(bio);
  33733. bio = NULL;
  33734. #if !defined(NO_DES3) && !defined(NO_SHA)
  33735. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33736. /* Write Encrypted PKCS#8 PEM to BIO. */
  33737. bytes = 1834;
  33738. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  33739. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33740. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33741. (void*)"yassl123"));
  33742. EVP_PKEY_free(evpPkey);
  33743. evpPkey = NULL;
  33744. BIO_free(bio);
  33745. bio = NULL;
  33746. #endif /* !NO_DES3 && !NO_SHA */
  33747. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33748. EVP_PKEY_free(pkey);
  33749. pkey = NULL;
  33750. /* PKCS#8 encrypted RSA key */
  33751. #ifndef NO_DES3
  33752. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  33753. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33754. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33755. file)), 0);
  33756. if (file != XBADFILE) {
  33757. XFCLOSE(file);
  33758. file = XBADFILE;
  33759. }
  33760. #if defined(OPENSSL_ALL) && \
  33761. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33762. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33763. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33764. (void*)"yassl123"));
  33765. EVP_PKEY_free(pkey);
  33766. pkey = NULL;
  33767. BIO_free(bio);
  33768. bio = NULL;
  33769. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33770. #endif /* !NO_DES3 */
  33771. #endif /* NO_RSA */
  33772. #ifdef HAVE_ECC
  33773. /* PKCS#8 encode EC key */
  33774. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  33775. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33776. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33777. file)), 0);
  33778. if (file != XBADFILE) {
  33779. XFCLOSE(file);
  33780. file = XBADFILE;
  33781. }
  33782. p = pkcs8_buffer;
  33783. #ifdef OPENSSL_ALL
  33784. /* Try to decode - auto-detect key type. */
  33785. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33786. #else
  33787. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  33788. #endif
  33789. /* Get PEM encoded RSA PKCS#8 data. */
  33790. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  33791. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33792. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33793. file)), 0);
  33794. if (file != XBADFILE) {
  33795. XFCLOSE(file);
  33796. file = XBADFILE;
  33797. }
  33798. #if defined(OPENSSL_ALL) && \
  33799. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  33800. defined(HAVE_AES_CBC)
  33801. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33802. /* Write PKCS#8 PEM to BIO. */
  33803. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33804. NULL), bytes);
  33805. /* Compare file and written data */
  33806. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33807. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33808. BIO_free(bio);
  33809. bio = NULL;
  33810. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33811. /* Write Encrypted PKCS#8 PEM to BIO. */
  33812. bytes = 379;
  33813. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  33814. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33815. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33816. (void*)"yassl123"));
  33817. EVP_PKEY_free(evpPkey);
  33818. evpPkey = NULL;
  33819. BIO_free(bio);
  33820. bio = NULL;
  33821. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  33822. EVP_PKEY_free(pkey);
  33823. pkey = NULL;
  33824. /* PKCS#8 encrypted EC key */
  33825. #ifndef NO_DES3
  33826. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  33827. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33828. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33829. file)), 0);
  33830. if (file != XBADFILE) {
  33831. XFCLOSE(file);
  33832. file = XBADFILE;
  33833. }
  33834. #if defined(OPENSSL_ALL) && \
  33835. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33836. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33837. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33838. (void*)"yassl123"));
  33839. EVP_PKEY_free(pkey);
  33840. pkey = NULL;
  33841. BIO_free(bio);
  33842. bio = NULL;
  33843. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33844. #endif /* !NO_DES3 */
  33845. #endif /* HAVE_ECC */
  33846. #endif /* !NO_FILESYSTEM */
  33847. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  33848. return EXPECT_RESULT();
  33849. }
  33850. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33851. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33852. #define LOGGING_THREADS 5
  33853. #define ERROR_COUNT 10
  33854. /* copied from logging.c since this is not exposed otherwise */
  33855. #ifndef ERROR_QUEUE_MAX
  33856. #ifdef ERROR_QUEUE_PER_THREAD
  33857. #define ERROR_QUEUE_MAX 16
  33858. #else
  33859. /* this breaks from compat of unlimited error queue size */
  33860. #define ERROR_QUEUE_MAX 100
  33861. #endif
  33862. #endif
  33863. static volatile int loggingThreadsReady;
  33864. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  33865. {
  33866. const char* file;
  33867. int line;
  33868. unsigned long err;
  33869. int errorCount = 0;
  33870. int i;
  33871. (void)args;
  33872. while (!loggingThreadsReady);
  33873. for (i = 0; i < ERROR_COUNT; i++)
  33874. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33875. while ((err = ERR_get_error_line(&file, &line))) {
  33876. AssertIntEQ(err, 990 + errorCount);
  33877. errorCount++;
  33878. }
  33879. AssertIntEQ(errorCount, ERROR_COUNT);
  33880. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  33881. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  33882. errorCount = 0;
  33883. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  33884. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33885. while ((err = ERR_get_error_line(&file, &line))) {
  33886. AssertIntEQ(err, 990 + errorCount);
  33887. errorCount++;
  33888. }
  33889. /* test that the 3 errors over the max were dropped */
  33890. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  33891. WOLFSSL_RETURN_FROM_THREAD(0);
  33892. }
  33893. #endif
  33894. static int test_error_queue_per_thread(void)
  33895. {
  33896. int res = TEST_SKIPPED;
  33897. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33898. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33899. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33900. int i;
  33901. ERR_clear_error(); /* clear out any error nodes */
  33902. loggingThreadsReady = 0;
  33903. for (i = 0; i < LOGGING_THREADS; i++)
  33904. start_thread(test_logging, NULL, &loggingThreads[i]);
  33905. loggingThreadsReady = 1;
  33906. for (i = 0; i < LOGGING_THREADS; i++)
  33907. join_thread(loggingThreads[i]);
  33908. res = TEST_SUCCESS;
  33909. #endif
  33910. return res;
  33911. }
  33912. static int test_wolfSSL_ERR_put_error(void)
  33913. {
  33914. EXPECT_DECLS;
  33915. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33916. defined(DEBUG_WOLFSSL)
  33917. const char* file;
  33918. int line;
  33919. ERR_clear_error(); /* clear out any error nodes */
  33920. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33921. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33922. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33923. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  33924. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33925. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  33926. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33927. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  33928. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  33929. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  33930. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  33931. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  33932. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  33933. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  33934. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  33935. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  33936. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  33937. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  33938. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  33939. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  33940. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  33941. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  33942. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  33943. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  33944. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  33945. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  33946. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  33947. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  33948. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  33949. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  33950. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  33951. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  33952. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  33953. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  33954. "this file", 100);
  33955. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  33956. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33957. ExpectIntEQ(line, 100);
  33958. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  33959. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33960. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  33961. #endif
  33962. /* try reading past end of error queue */
  33963. file = NULL;
  33964. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33965. ExpectNull(file);
  33966. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  33967. PEMerr(4,4);
  33968. ExpectIntEQ(ERR_get_error(), 4);
  33969. /* Empty and free up all error nodes */
  33970. ERR_clear_error();
  33971. /* Verify all nodes are cleared */
  33972. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33973. ERR_clear_error();
  33974. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33975. #endif
  33976. return EXPECT_RESULT();
  33977. }
  33978. /*
  33979. * This is a regression test for a bug where the peek/get error functions were
  33980. * drawing from the end of the queue rather than the front.
  33981. */
  33982. static int test_wolfSSL_ERR_get_error_order(void)
  33983. {
  33984. EXPECT_DECLS;
  33985. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  33986. /* Empty the queue. */
  33987. wolfSSL_ERR_clear_error();
  33988. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  33989. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  33990. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  33991. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  33992. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  33993. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  33994. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  33995. return EXPECT_RESULT();
  33996. }
  33997. #ifndef NO_BIO
  33998. static int test_wolfSSL_ERR_print_errors(void)
  33999. {
  34000. EXPECT_DECLS;
  34001. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34002. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  34003. BIO* bio = NULL;
  34004. char buf[1024];
  34005. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34006. ERR_clear_error(); /* clear out any error nodes */
  34007. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34008. /* Choosing -600 as an unused errno. */
  34009. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  34010. ERR_print_errors(bio);
  34011. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  34012. ExpectIntEQ(XSTRNCMP(
  34013. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  34014. buf, 55), 0);
  34015. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  34016. ExpectIntEQ(XSTRNCMP(
  34017. "error:600:wolfSSL library:unknown error number:asn.c:100",
  34018. buf, 56), 0);
  34019. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  34020. ExpectIntEQ(buf[0], '\0');
  34021. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  34022. BIO_free(bio);
  34023. #endif
  34024. return EXPECT_RESULT();
  34025. }
  34026. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34027. defined(DEBUG_WOLFSSL)
  34028. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  34029. {
  34030. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  34031. return 0;
  34032. }
  34033. #endif
  34034. static int test_wolfSSL_ERR_print_errors_cb(void)
  34035. {
  34036. EXPECT_DECLS;
  34037. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  34038. defined(DEBUG_WOLFSSL)
  34039. BIO* bio = NULL;
  34040. char buf[1024];
  34041. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34042. ERR_clear_error(); /* clear out any error nodes */
  34043. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  34044. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  34045. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  34046. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  34047. ExpectIntEQ(XSTRNCMP(
  34048. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  34049. buf, 53), 0);
  34050. ExpectIntEQ(XSTRNCMP(
  34051. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  34052. buf + 53, 55), 0);
  34053. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  34054. BIO_free(bio);
  34055. #endif
  34056. return EXPECT_RESULT();
  34057. }
  34058. /*
  34059. * Testing WOLFSSL_ERROR_MSG
  34060. */
  34061. static int test_WOLFSSL_ERROR_MSG(void)
  34062. {
  34063. int res = TEST_SKIPPED;
  34064. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34065. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34066. const char* msg = TEST_STRING;
  34067. WOLFSSL_ERROR_MSG(msg);
  34068. res = TEST_SUCCESS;
  34069. #endif
  34070. return res;
  34071. } /* End test_WOLFSSL_ERROR_MSG */
  34072. /*
  34073. * Testing wc_ERR_remove_state
  34074. */
  34075. static int test_wc_ERR_remove_state(void)
  34076. {
  34077. int res = TEST_SKIPPED;
  34078. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34079. wc_ERR_remove_state();
  34080. res = TEST_SUCCESS;
  34081. #endif
  34082. return res;
  34083. } /* End test_wc_ERR_remove_state */
  34084. /*
  34085. * Testing wc_ERR_print_errors_fp
  34086. */
  34087. static int test_wc_ERR_print_errors_fp(void)
  34088. {
  34089. EXPECT_DECLS;
  34090. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34091. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34092. long sz;
  34093. XFILE fp = XBADFILE;
  34094. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34095. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  34096. XBADFILE);
  34097. wc_ERR_print_errors_fp(fp);
  34098. #if defined(DEBUG_WOLFSSL)
  34099. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34100. #ifdef NO_ERROR_QUEUE
  34101. ExpectIntEQ(sz = XFTELL(fp), 0);
  34102. #else
  34103. ExpectIntNE(sz = XFTELL(fp), 0);
  34104. #endif
  34105. #endif
  34106. if (fp != XBADFILE)
  34107. XFCLOSE(fp);
  34108. (void)sz;
  34109. #endif
  34110. return EXPECT_RESULT();
  34111. } /* End test_wc_ERR_print_errors_fp */
  34112. #ifdef DEBUG_WOLFSSL
  34113. static void Logging_cb(const int logLevel, const char *const logMessage)
  34114. {
  34115. (void)logLevel;
  34116. (void)logMessage;
  34117. }
  34118. #endif
  34119. /*
  34120. * Testing wolfSSL_GetLoggingCb
  34121. */
  34122. static int test_wolfSSL_GetLoggingCb(void)
  34123. {
  34124. EXPECT_DECLS;
  34125. #ifdef DEBUG_WOLFSSL
  34126. /* Testing without wolfSSL_SetLoggingCb() */
  34127. ExpectNull(wolfSSL_GetLoggingCb());
  34128. /* Testing with wolfSSL_SetLoggingCb() */
  34129. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  34130. ExpectNotNull(wolfSSL_GetLoggingCb());
  34131. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  34132. #endif
  34133. ExpectNull(wolfSSL_GetLoggingCb());
  34134. return EXPECT_RESULT();
  34135. } /* End test_wolfSSL_GetLoggingCb */
  34136. #endif /* !NO_BIO */
  34137. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34138. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34139. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34140. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34141. {
  34142. EXPECT_DECLS;
  34143. static const unsigned char key[] = "simple test key";
  34144. HMAC_CTX* hmac = NULL;
  34145. ENGINE* e = NULL;
  34146. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34147. unsigned int len;
  34148. ExpectNotNull(hmac = HMAC_CTX_new());
  34149. HMAC_CTX_init(hmac);
  34150. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34151. SSL_SUCCESS);
  34152. /* re-using test key as data to hash */
  34153. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34154. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34155. ExpectIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34156. ExpectIntEQ(len, md_len);
  34157. ExpectIntEQ(HMAC_size(hmac), md_len);
  34158. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  34159. HMAC_cleanup(hmac);
  34160. HMAC_CTX_free(hmac);
  34161. len = 0;
  34162. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34163. ExpectIntEQ(len, md_len);
  34164. return EXPECT_RESULT();
  34165. }
  34166. #endif
  34167. static int test_wolfSSL_HMAC(void)
  34168. {
  34169. EXPECT_DECLS;
  34170. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34171. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34172. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34173. #ifndef NO_SHA256
  34174. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  34175. TEST_SUCCESS);
  34176. #endif
  34177. #ifdef WOLFSSL_SHA224
  34178. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  34179. TEST_SUCCESS);
  34180. #endif
  34181. #ifdef WOLFSSL_SHA384
  34182. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  34183. TEST_SUCCESS);
  34184. #endif
  34185. #ifdef WOLFSSL_SHA512
  34186. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  34187. TEST_SUCCESS);
  34188. #endif
  34189. #ifdef WOLFSSL_SHA3
  34190. #ifndef WOLFSSL_NOSHA3_224
  34191. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  34192. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  34193. #endif
  34194. #ifndef WOLFSSL_NOSHA3_256
  34195. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  34196. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  34197. #endif
  34198. #ifndef WOLFSSL_NOSHA3_384
  34199. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  34200. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  34201. #endif
  34202. #ifndef WOLFSSL_NOSHA3_512
  34203. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  34204. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  34205. #endif
  34206. #endif
  34207. #ifndef NO_SHA
  34208. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  34209. TEST_SUCCESS);
  34210. #endif
  34211. #endif
  34212. return EXPECT_RESULT();
  34213. }
  34214. static int test_wolfSSL_CMAC(void)
  34215. {
  34216. EXPECT_DECLS;
  34217. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34218. defined(WOLFSSL_AES_DIRECT)
  34219. int i;
  34220. byte key[AES_128_KEY_SIZE];
  34221. CMAC_CTX* cmacCtx = NULL;
  34222. byte out[AES_BLOCK_SIZE];
  34223. size_t outLen = AES_BLOCK_SIZE;
  34224. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34225. key[i] = i;
  34226. }
  34227. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  34228. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34229. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34230. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34231. NULL), SSL_SUCCESS);
  34232. /* re-using test key as data to hash */
  34233. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34234. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34235. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34236. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  34237. CMAC_CTX_free(cmacCtx);
  34238. /* give a key too small for the cipher, verify we get failure */
  34239. cmacCtx = NULL;
  34240. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  34241. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34242. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  34243. NULL), SSL_FAILURE);
  34244. CMAC_CTX_free(cmacCtx);
  34245. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34246. return EXPECT_RESULT();
  34247. }
  34248. static int test_wolfSSL_OBJ(void)
  34249. {
  34250. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34251. * mode
  34252. */
  34253. EXPECT_DECLS;
  34254. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34255. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34256. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34257. ASN1_OBJECT *obj = NULL;
  34258. ASN1_OBJECT *obj2 = NULL;
  34259. char buf[50];
  34260. XFILE fp = XBADFILE;
  34261. X509 *x509 = NULL;
  34262. X509_NAME *x509Name = NULL;
  34263. X509_NAME_ENTRY *x509NameEntry = NULL;
  34264. ASN1_OBJECT *asn1Name = NULL;
  34265. int numNames = 0;
  34266. BIO *bio = NULL;
  34267. int nid;
  34268. int i, j;
  34269. const char *f[] = {
  34270. #ifndef NO_RSA
  34271. "./certs/ca-cert.der",
  34272. #endif
  34273. #ifdef HAVE_ECC
  34274. "./certs/ca-ecc-cert.der",
  34275. "./certs/ca-ecc384-cert.der",
  34276. #endif
  34277. NULL};
  34278. ASN1_OBJECT *field_name_obj = NULL;
  34279. int lastpos = -1;
  34280. int tmp = -1;
  34281. ASN1_STRING *asn1 = NULL;
  34282. unsigned char *buf_dyn = NULL;
  34283. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34284. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34285. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34286. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34287. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34288. ASN1_OBJECT_free(obj);
  34289. obj = NULL;
  34290. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  34291. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34292. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34293. #ifdef WOLFSSL_CERT_EXT
  34294. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34295. #endif
  34296. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34297. ExpectNotNull(obj2 = OBJ_dup(obj));
  34298. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  34299. ASN1_OBJECT_free(obj);
  34300. obj = NULL;
  34301. ASN1_OBJECT_free(obj2);
  34302. obj2 = NULL;
  34303. for (i = 0; f[i] != NULL; i++)
  34304. {
  34305. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34306. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  34307. if (fp != XBADFILE) {
  34308. XFCLOSE(fp);
  34309. fp = XBADFILE;
  34310. }
  34311. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  34312. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34313. /* Get the Common Name by using OBJ_txt2obj */
  34314. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34315. do
  34316. {
  34317. lastpos = tmp;
  34318. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34319. } while (tmp > -1);
  34320. ExpectIntNE(lastpos, -1);
  34321. ASN1_OBJECT_free(field_name_obj);
  34322. field_name_obj = NULL;
  34323. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34324. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34325. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34326. /*
  34327. * All Common Names should be www.wolfssl.com
  34328. * This makes testing easier as we can test for the expected value.
  34329. */
  34330. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34331. OPENSSL_free(buf_dyn);
  34332. buf_dyn = NULL;
  34333. bio = BIO_new(BIO_s_mem());
  34334. ExpectTrue(bio != NULL);
  34335. for (j = 0; j < numNames; j++)
  34336. {
  34337. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34338. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34339. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34340. }
  34341. BIO_free(bio);
  34342. bio = NULL;
  34343. X509_free(x509);
  34344. x509 = NULL;
  34345. }
  34346. #ifdef HAVE_PKCS12
  34347. {
  34348. PKCS12 *p12 = NULL;
  34349. int boolRet;
  34350. EVP_PKEY *pkey = NULL;
  34351. const char *p12_f[] = {
  34352. #if !defined(NO_DES3) && !defined(NO_RSA)
  34353. "./certs/test-servercert.p12",
  34354. #endif
  34355. NULL};
  34356. for (i = 0; p12_f[i] != NULL; i++)
  34357. {
  34358. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34359. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34360. if (fp != XBADFILE) {
  34361. XFCLOSE(fp);
  34362. fp = XBADFILE;
  34363. }
  34364. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34365. &pkey, &x509, NULL)) > 0);
  34366. wc_PKCS12_free(p12);
  34367. p12 = NULL;
  34368. EVP_PKEY_free(pkey);
  34369. x509Name = X509_get_issuer_name(x509);
  34370. ExpectNotNull(x509Name);
  34371. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34372. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34373. for (j = 0; j < numNames; j++)
  34374. {
  34375. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34376. ExpectNotNull(asn1Name =
  34377. X509_NAME_ENTRY_get_object(x509NameEntry));
  34378. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34379. }
  34380. BIO_free(bio);
  34381. bio = NULL;
  34382. X509_free(x509);
  34383. x509 = NULL;
  34384. }
  34385. }
  34386. #endif /* HAVE_PKCS12 */
  34387. #endif
  34388. return EXPECT_RESULT();
  34389. }
  34390. static int test_wolfSSL_OBJ_cmp(void)
  34391. {
  34392. EXPECT_DECLS;
  34393. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34394. ASN1_OBJECT *obj = NULL;
  34395. ASN1_OBJECT *obj2 = NULL;
  34396. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34397. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34398. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34399. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34400. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34401. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34402. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  34403. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  34404. ASN1_OBJECT_free(obj);
  34405. ASN1_OBJECT_free(obj2);
  34406. #endif
  34407. return EXPECT_RESULT();
  34408. }
  34409. static int test_wolfSSL_OBJ_txt2nid(void)
  34410. {
  34411. EXPECT_DECLS;
  34412. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  34413. defined(WOLFSSL_APACHE_HTTPD)
  34414. int i;
  34415. static const struct {
  34416. const char* sn;
  34417. const char* ln;
  34418. const char* oid;
  34419. int nid;
  34420. } testVals[] = {
  34421. #ifdef WOLFSSL_APACHE_HTTPD
  34422. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34423. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34424. NID_id_on_dnsSRV },
  34425. { "msUPN", "Microsoft User Principal Name",
  34426. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34427. #endif
  34428. { NULL, NULL, NULL, NID_undef }
  34429. };
  34430. /* Invalid cases */
  34431. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34432. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34433. /* Valid cases */
  34434. for (i = 0; testVals[i].sn != NULL; i++) {
  34435. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34436. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34437. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34438. }
  34439. #endif
  34440. return EXPECT_RESULT();
  34441. }
  34442. static int test_wolfSSL_OBJ_txt2obj(void)
  34443. {
  34444. EXPECT_DECLS;
  34445. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34446. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34447. int i;
  34448. char buf[50];
  34449. ASN1_OBJECT* obj = NULL;
  34450. static const struct {
  34451. const char* oidStr;
  34452. const char* sn;
  34453. const char* ln;
  34454. } objs_list[] = {
  34455. #if defined(WOLFSSL_APACHE_HTTPD)
  34456. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34457. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34458. #endif
  34459. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34460. { NULL, NULL, NULL }
  34461. };
  34462. static const struct {
  34463. const char* numeric;
  34464. const char* name;
  34465. } objs_named[] = {
  34466. /* In dictionary but not in normal list. */
  34467. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34468. /* Made up OID. */
  34469. { "1.3.5.7", "1.3.5.7" },
  34470. { NULL, NULL }
  34471. };
  34472. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  34473. ASN1_OBJECT_free(obj);
  34474. obj = NULL;
  34475. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  34476. ASN1_OBJECT_free(obj);
  34477. obj = NULL;
  34478. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34479. /* Test numerical value of oid (oidStr) */
  34480. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34481. /* Convert object back to text to confirm oid is correct */
  34482. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34483. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34484. ASN1_OBJECT_free(obj);
  34485. obj = NULL;
  34486. XMEMSET(buf, 0, sizeof(buf));
  34487. /* Test short name (sn) */
  34488. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34489. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34490. /* Convert object back to text to confirm oid is correct */
  34491. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34492. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34493. ASN1_OBJECT_free(obj);
  34494. obj = NULL;
  34495. XMEMSET(buf, 0, sizeof(buf));
  34496. /* Test long name (ln) - should fail when no_name = 1 */
  34497. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34498. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34499. /* Convert object back to text to confirm oid is correct */
  34500. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34501. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34502. ASN1_OBJECT_free(obj);
  34503. obj = NULL;
  34504. XMEMSET(buf, 0, sizeof(buf));
  34505. }
  34506. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34507. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34508. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34509. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34510. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34511. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34512. ASN1_OBJECT_free(obj);
  34513. obj = NULL;
  34514. }
  34515. #endif
  34516. return EXPECT_RESULT();
  34517. }
  34518. static int test_wolfSSL_PEM_write_bio_X509(void)
  34519. {
  34520. EXPECT_DECLS;
  34521. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  34522. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  34523. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  34524. !defined(NO_FILESYSTEM)
  34525. /* This test contains the hard coded expected
  34526. * lengths. Update if necessary */
  34527. XFILE fp = XBADFILE;
  34528. WOLFSSL_EVP_PKEY *priv = NULL;
  34529. BIO* input = NULL;
  34530. BIO* output = NULL;
  34531. X509* x509a = NULL;
  34532. X509* x509b = NULL;
  34533. ASN1_TIME* notBeforeA = NULL;
  34534. ASN1_TIME* notAfterA = NULL;
  34535. #ifndef NO_ASN_TIME
  34536. ASN1_TIME* notBeforeB = NULL;
  34537. ASN1_TIME* notAfterB = NULL;
  34538. #endif
  34539. int expectedLen;
  34540. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  34541. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  34542. if (fp != XBADFILE) {
  34543. XFCLOSE(fp);
  34544. fp = XBADFILE;
  34545. }
  34546. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  34547. "rb"));
  34548. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34549. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34550. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34551. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  34552. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  34553. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  34554. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34555. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34556. /* compare length against expected */
  34557. expectedLen = 2000;
  34558. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34559. #ifndef NO_ASN_TIME
  34560. /* read exported X509 PEM back into struct, sanity check on export,
  34561. * make sure notBefore/notAfter are the same and certs are identical. */
  34562. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34563. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  34564. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  34565. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34566. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34567. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34568. X509_free(x509b);
  34569. x509b = NULL;
  34570. #endif
  34571. /* Reset output buffer */
  34572. BIO_free(output);
  34573. output = NULL;
  34574. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34575. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34576. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  34577. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34578. x509a->authKeyId = x509a->authKeyIdSrc;
  34579. x509a->authKeyIdSrc = NULL;
  34580. x509a->authKeyIdSrcSz = 0;
  34581. }
  34582. /* Resign to re-generate the der */
  34583. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  34584. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34585. /* Check that we generate a smaller output since the AKID will
  34586. * only contain the KeyIdentifier without any additional
  34587. * information */
  34588. /* Here we copy the validity struct from the original */
  34589. expectedLen = 1688;
  34590. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34591. /* Reset buffers and x509 */
  34592. BIO_free(input);
  34593. input = NULL;
  34594. BIO_free(output);
  34595. output = NULL;
  34596. X509_free(x509a);
  34597. x509a = NULL;
  34598. /* test CA and basicConstSet values are encoded when
  34599. * the cert is a CA */
  34600. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  34601. /* read PEM into X509 struct */
  34602. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34603. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34604. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34605. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34606. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  34607. * values are maintained and certs are identical.*/
  34608. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34609. ExpectIntEQ(x509b->isCa, 1);
  34610. ExpectIntEQ(x509b->basicConstSet, 1);
  34611. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34612. X509_free(x509a);
  34613. x509a = NULL;
  34614. X509_free(x509b);
  34615. x509b = NULL;
  34616. BIO_free(input);
  34617. input = NULL;
  34618. BIO_free(output);
  34619. output = NULL;
  34620. /* test CA and basicConstSet values are encoded when
  34621. * the cert is not CA */
  34622. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  34623. /* read PEM into X509 struct */
  34624. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34625. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34626. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34627. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34628. /* read exported X509 PEM back into struct, ensure isCa and
  34629. * basicConstSet values are maintained and certs are identical */
  34630. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34631. ExpectIntEQ(x509b->isCa, 0);
  34632. ExpectIntEQ(x509b->basicConstSet, 1);
  34633. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34634. wolfSSL_EVP_PKEY_free(priv);
  34635. X509_free(x509a);
  34636. X509_free(x509b);
  34637. BIO_free(input);
  34638. BIO_free(output);
  34639. #endif
  34640. return EXPECT_RESULT();
  34641. }
  34642. static int test_wolfSSL_X509_NAME_ENTRY(void)
  34643. {
  34644. EXPECT_DECLS;
  34645. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34646. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  34647. X509* x509 = NULL;
  34648. #ifndef NO_BIO
  34649. BIO* bio = NULL;
  34650. #endif
  34651. X509_NAME* nm = NULL;
  34652. X509_NAME_ENTRY* entry = NULL;
  34653. unsigned char cn[] = "another name to add";
  34654. #ifdef OPENSSL_ALL
  34655. int i;
  34656. int names_len = 0;
  34657. #endif
  34658. ExpectNotNull(x509 =
  34659. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34660. #ifndef NO_BIO
  34661. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34662. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  34663. #endif
  34664. #ifdef WOLFSSL_CERT_REQ
  34665. {
  34666. X509_REQ* req = NULL;
  34667. #ifndef NO_BIO
  34668. BIO* bReq = NULL;
  34669. #endif
  34670. ExpectNotNull(req =
  34671. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34672. #ifndef NO_BIO
  34673. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  34674. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  34675. BIO_free(bReq);
  34676. #endif
  34677. X509_free(req);
  34678. }
  34679. #endif
  34680. ExpectNotNull(nm = X509_get_subject_name(x509));
  34681. /* Test add entry */
  34682. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  34683. 0x0c, cn, (int)sizeof(cn)));
  34684. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34685. #ifdef WOLFSSL_CERT_EXT
  34686. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  34687. (byte*)"support@wolfssl.com", 19, -1,
  34688. 1), WOLFSSL_SUCCESS);
  34689. #endif
  34690. X509_NAME_ENTRY_free(entry);
  34691. entry = NULL;
  34692. #ifdef WOLFSSL_CERT_REQ
  34693. {
  34694. unsigned char srv_pkcs9p[] = "Server";
  34695. unsigned char fvrtDrnk[] = "tequila";
  34696. unsigned char* der = NULL;
  34697. char* subject = NULL;
  34698. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  34699. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  34700. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  34701. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  34702. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  34703. ExpectNotNull(der);
  34704. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  34705. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  34706. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  34707. #ifdef DEBUG_WOLFSSL
  34708. if (subject != NULL) {
  34709. fprintf(stderr, "\n\t%s\n", subject);
  34710. }
  34711. #endif
  34712. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  34713. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  34714. }
  34715. #endif
  34716. /* Test add entry by text */
  34717. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  34718. 0x0c, cn, (int)sizeof(cn)));
  34719. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  34720. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  34721. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  34722. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  34723. #endif
  34724. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34725. X509_NAME_ENTRY_free(entry);
  34726. entry = NULL;
  34727. /* Test add entry by NID */
  34728. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  34729. cn, -1, -1, 0), SSL_SUCCESS);
  34730. #ifdef OPENSSL_ALL
  34731. /* stack of name entry */
  34732. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  34733. for (i = 0; i < names_len; i++) {
  34734. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  34735. }
  34736. #endif
  34737. #ifndef NO_BIO
  34738. BIO_free(bio);
  34739. #endif
  34740. X509_free(x509); /* free's nm */
  34741. #endif
  34742. return EXPECT_RESULT();
  34743. }
  34744. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  34745. static int test_GENERAL_NAME_set0_othername(void) {
  34746. EXPECT_DECLS;
  34747. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34748. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  34749. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  34750. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  34751. defined(WOLFSSL_FPKI)
  34752. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  34753. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  34754. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  34755. const char * cert_fname = "./certs/server-cert.der";
  34756. const char * key_fname = "./certs/server-key.der";
  34757. X509* x509 = NULL;
  34758. GENERAL_NAME* gn = NULL;
  34759. GENERAL_NAMES* gns = NULL;
  34760. ASN1_OBJECT* upn_oid = NULL;
  34761. ASN1_UTF8STRING *utf8str = NULL;
  34762. ASN1_TYPE *value = NULL;
  34763. X509_EXTENSION * ext = NULL;
  34764. byte* pt = NULL;
  34765. byte der[4096];
  34766. int derSz = 0;
  34767. EVP_PKEY* priv = NULL;
  34768. XFILE f = XBADFILE;
  34769. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  34770. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  34771. if (f != XBADFILE) {
  34772. XFCLOSE(f);
  34773. f = XBADFILE;
  34774. }
  34775. ExpectNotNull(gn = GENERAL_NAME_new());
  34776. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  34777. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  34778. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  34779. ExpectNotNull(value = ASN1_TYPE_new());
  34780. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  34781. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  34782. wolfSSL_ASN1_STRING_free(utf8str);
  34783. }
  34784. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  34785. if (EXPECT_FAIL()) {
  34786. ASN1_TYPE_free(value);
  34787. }
  34788. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  34789. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  34790. if (EXPECT_FAIL()) {
  34791. GENERAL_NAME_free(gn);
  34792. gn = NULL;
  34793. }
  34794. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  34795. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  34796. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  34797. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  34798. if (f != XBADFILE) {
  34799. XFCLOSE(f);
  34800. f = XBADFILE;
  34801. }
  34802. pt = der;
  34803. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  34804. (const unsigned char**)&pt, derSz));
  34805. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34806. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34807. gns = NULL;
  34808. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  34809. NULL));
  34810. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  34811. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  34812. ExpectIntEQ(gn->type, 0);
  34813. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34814. ASN1_OBJECT_free(upn_oid);
  34815. X509_EXTENSION_free(ext);
  34816. X509_free(x509);
  34817. EVP_PKEY_free(priv);
  34818. #endif
  34819. return EXPECT_RESULT();
  34820. }
  34821. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  34822. static int test_othername_and_SID_ext(void) {
  34823. EXPECT_DECLS;
  34824. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34825. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  34826. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  34827. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  34828. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  34829. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  34830. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  34831. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  34832. const char* csr_fname = "./certs/csr.signed.der";
  34833. const char* key_fname = "./certs/server-key.der";
  34834. byte der[4096];
  34835. int derSz = 0;
  34836. X509_REQ* x509 = NULL;
  34837. STACK_OF(X509_EXTENSION) *exts = NULL;
  34838. X509_EXTENSION * san_ext = NULL;
  34839. X509_EXTENSION * ext = NULL;
  34840. GENERAL_NAME* gn = NULL;
  34841. GENERAL_NAMES* gns = NULL;
  34842. ASN1_OBJECT* upn_oid = NULL;
  34843. ASN1_UTF8STRING *utf8str = NULL;
  34844. ASN1_TYPE *value = NULL;
  34845. ASN1_STRING *extval = NULL;
  34846. /* SID extension. SID data format explained here:
  34847. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  34848. */
  34849. uint8_t SidExtension[] = {
  34850. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  34851. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  34852. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  34853. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  34854. uint8_t expectedAltName[] = {
  34855. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  34856. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  34857. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34858. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  34859. X509_EXTENSION *sid_ext = NULL;
  34860. ASN1_OBJECT* sid_oid = NULL;
  34861. ASN1_OCTET_STRING *sid_data = NULL;
  34862. EVP_PKEY* priv = NULL;
  34863. XFILE f = XBADFILE;
  34864. byte* pt = NULL;
  34865. BIO* bio = NULL;
  34866. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  34867. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  34868. if (f != XBADFILE) {
  34869. XFCLOSE(f);
  34870. f = XBADFILE;
  34871. }
  34872. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  34873. ExpectNotNull(gn = GENERAL_NAME_new());
  34874. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  34875. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  34876. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  34877. ExpectNotNull(value = ASN1_TYPE_new());
  34878. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  34879. if (EXPECT_FAIL()) {
  34880. ASN1_UTF8STRING_free(utf8str);
  34881. }
  34882. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  34883. if (EXPECT_FAIL()) {
  34884. ASN1_TYPE_free(value);
  34885. GENERAL_NAME_free(gn);
  34886. gn = NULL;
  34887. }
  34888. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  34889. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  34890. if (EXPECT_FAIL()) {
  34891. GENERAL_NAME_free(gn);
  34892. }
  34893. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  34894. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  34895. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  34896. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  34897. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  34898. sid_data));
  34899. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  34900. /* Ensure an empty stack doesn't raise an error. */
  34901. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  34902. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  34903. if (EXPECT_FAIL()) {
  34904. X509_EXTENSION_free(san_ext);
  34905. }
  34906. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  34907. if (EXPECT_FAIL()) {
  34908. X509_EXTENSION_free(sid_ext);
  34909. }
  34910. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  34911. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  34912. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  34913. if (f != XBADFILE)
  34914. XFCLOSE(f);
  34915. pt = der;
  34916. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  34917. (const unsigned char**)&pt, derSz));
  34918. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  34919. pt = der;
  34920. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  34921. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34922. gns = NULL;
  34923. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  34924. exts = NULL;
  34925. ASN1_OBJECT_free(upn_oid);
  34926. ASN1_OBJECT_free(sid_oid);
  34927. ASN1_OCTET_STRING_free(sid_data);
  34928. X509_REQ_free(x509);
  34929. x509 = NULL;
  34930. EVP_PKEY_free(priv);
  34931. /* At this point everything used to generate what is in der is cleaned up.
  34932. * We now read back from der to confirm the extensions were inserted
  34933. * correctly. */
  34934. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  34935. ExpectNotNull(bio);
  34936. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  34937. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  34938. ExpectNotNull(x509);
  34939. BIO_free(bio);
  34940. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  34941. x509));
  34942. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  34943. /* Check the SID extension. */
  34944. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  34945. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  34946. ExpectIntEQ(extval->length, sizeof(SidExtension));
  34947. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  34948. /* Check the AltNames extension. */
  34949. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  34950. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  34951. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  34952. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  34953. 0);
  34954. /* Cleanup */
  34955. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  34956. NULL));
  34957. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  34958. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  34959. ExpectIntEQ(gn->type, 0);
  34960. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34961. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  34962. X509_REQ_free(x509);
  34963. #endif
  34964. return EXPECT_RESULT();
  34965. }
  34966. static int test_wolfSSL_X509_set_name(void)
  34967. {
  34968. EXPECT_DECLS;
  34969. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34970. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34971. X509* x509 = NULL;
  34972. X509_NAME* name = NULL;
  34973. ExpectNotNull(name = X509_NAME_new());
  34974. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34975. (byte*)"wolfssl.com", 11, 0, 1),
  34976. WOLFSSL_SUCCESS);
  34977. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34978. (byte*)"support@wolfssl.com", 19, -1,
  34979. 1), WOLFSSL_SUCCESS);
  34980. ExpectNotNull(x509 = X509_new());
  34981. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  34982. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  34983. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  34984. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  34985. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  34986. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  34987. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  34988. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34989. X509_free(x509);
  34990. X509_NAME_free(name);
  34991. #endif /* OPENSSL_ALL && !NO_CERTS */
  34992. return EXPECT_RESULT();
  34993. }
  34994. static int test_wolfSSL_X509_set_notAfter(void)
  34995. {
  34996. EXPECT_DECLS;
  34997. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34998. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34999. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  35000. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  35001. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  35002. /* Generalized time will overflow time_t if not long */
  35003. X509* x = NULL;
  35004. BIO* bio = NULL;
  35005. ASN1_TIME *asn_time = NULL;
  35006. ASN1_TIME *time_check = NULL;
  35007. const int year = 365*24*60*60;
  35008. const int day = 24*60*60;
  35009. const int hour = 60*60;
  35010. const int mini = 60;
  35011. int offset_day;
  35012. unsigned char buf[25];
  35013. time_t t;
  35014. /*
  35015. * Setup asn_time. APACHE HTTPD uses time(NULL)
  35016. */
  35017. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  35018. offset_day = 7;
  35019. /*
  35020. * Free these.
  35021. */
  35022. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  35023. ExpectNotNull(asn_time);
  35024. ExpectNotNull(x = X509_new());
  35025. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35026. /*
  35027. * Tests
  35028. */
  35029. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  35030. /* time_check is simply (ANS1_TIME*)x->notAfter */
  35031. ExpectNotNull(time_check = X509_get_notAfter(x));
  35032. /* ANS1_TIME_check validates by checking if argument can be parsed */
  35033. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  35034. /* Convert to human readable format and compare to intended date */
  35035. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  35036. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  35037. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  35038. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  35039. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  35040. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  35041. /*
  35042. * Cleanup
  35043. */
  35044. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  35045. X509_free(x);
  35046. BIO_free(bio);
  35047. #endif
  35048. return EXPECT_RESULT();
  35049. }
  35050. static int test_wolfSSL_X509_set_notBefore(void)
  35051. {
  35052. EXPECT_DECLS;
  35053. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  35054. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  35055. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  35056. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  35057. X509* x = NULL;
  35058. BIO* bio = NULL;
  35059. ASN1_TIME *asn_time = NULL;
  35060. ASN1_TIME *time_check = NULL;
  35061. const int year = 365*24*60*60;
  35062. const int day = 24*60*60;
  35063. const int hour = 60*60;
  35064. const int mini = 60;
  35065. int offset_day;
  35066. unsigned char buf[25];
  35067. time_t t;
  35068. /*
  35069. * Setup asn_time. APACHE HTTPD uses time(NULL)
  35070. */
  35071. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  35072. offset_day = 7;
  35073. /*
  35074. * Free these.
  35075. */
  35076. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  35077. ExpectNotNull(asn_time);
  35078. ExpectNotNull(x = X509_new());
  35079. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35080. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  35081. /*
  35082. * Main Tests
  35083. */
  35084. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  35085. /* time_check == (ANS1_TIME*)x->notBefore */
  35086. ExpectNotNull(time_check = X509_get_notBefore(x));
  35087. /* ANS1_TIME_check validates by checking if argument can be parsed */
  35088. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  35089. /* Convert to human readable format and compare to intended date */
  35090. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  35091. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  35092. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  35093. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  35094. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  35095. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  35096. /*
  35097. * Cleanup
  35098. */
  35099. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  35100. X509_free(x);
  35101. BIO_free(bio);
  35102. #endif
  35103. return EXPECT_RESULT();
  35104. }
  35105. static int test_wolfSSL_X509_set_version(void)
  35106. {
  35107. EXPECT_DECLS;
  35108. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35109. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  35110. X509* x509 = NULL;
  35111. long v = 2L;
  35112. long maxInt = INT_MAX;
  35113. ExpectNotNull(x509 = X509_new());
  35114. /* These should pass. */
  35115. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  35116. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  35117. /* Fail Case: When v(long) is greater than x509->version(int). */
  35118. v = maxInt+1;
  35119. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  35120. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  35121. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  35122. /* Cleanup */
  35123. X509_free(x509);
  35124. #endif
  35125. return EXPECT_RESULT();
  35126. }
  35127. #ifndef NO_BIO
  35128. static int test_wolfSSL_BIO_gets(void)
  35129. {
  35130. EXPECT_DECLS;
  35131. #if defined(OPENSSL_EXTRA)
  35132. BIO* bio = NULL;
  35133. BIO* bio2 = NULL;
  35134. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  35135. char emp[] = "";
  35136. char bio_buffer[20];
  35137. int bufferSz = 20;
  35138. #ifdef OPENSSL_ALL
  35139. BUF_MEM* emp_bm = NULL;
  35140. BUF_MEM* msg_bm = NULL;
  35141. #endif
  35142. /* try with bad args */
  35143. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  35144. #ifdef OPENSSL_ALL
  35145. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  35146. #endif
  35147. /* try with real msg */
  35148. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  35149. XMEMSET(bio_buffer, 0, bufferSz);
  35150. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  35151. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  35152. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  35153. ExpectFalse(bio2 != BIO_next(bio));
  35154. /* make buffer filled with no terminating characters */
  35155. XMEMSET(bio_buffer, 1, bufferSz);
  35156. /* BIO_gets reads a line of data */
  35157. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35158. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35159. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35160. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35161. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35162. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35163. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35164. #ifdef OPENSSL_ALL
  35165. /* test setting the mem_buf manually */
  35166. BIO_free(bio);
  35167. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  35168. ExpectNotNull(emp_bm = BUF_MEM_new());
  35169. ExpectNotNull(msg_bm = BUF_MEM_new());
  35170. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  35171. if (EXPECT_SUCCESS())
  35172. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  35173. /* emp size is 1 for terminator */
  35174. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  35175. if (EXPECT_SUCCESS()) {
  35176. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  35177. emp_bm->data = emp;
  35178. msg_bm->data = msg;
  35179. }
  35180. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  35181. /* check reading an empty string */
  35182. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35183. ExpectStrEQ(emp, bio_buffer);
  35184. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35185. /* BIO_gets reads a line of data */
  35186. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  35187. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35188. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35189. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35190. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35191. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35192. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35193. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35194. if (EXPECT_SUCCESS())
  35195. emp_bm->data = NULL;
  35196. BUF_MEM_free(emp_bm);
  35197. if (EXPECT_SUCCESS())
  35198. msg_bm->data = NULL;
  35199. BUF_MEM_free(msg_bm);
  35200. #endif
  35201. /* check not null terminated string */
  35202. BIO_free(bio);
  35203. bio = NULL;
  35204. msg[0] = 0x33;
  35205. msg[1] = 0x33;
  35206. msg[2] = 0x33;
  35207. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35208. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  35209. ExpectIntEQ(bio_buffer[0], msg[0]);
  35210. ExpectIntEQ(bio_buffer[1], msg[1]);
  35211. ExpectIntNE(bio_buffer[2], msg[2]);
  35212. BIO_free(bio);
  35213. bio = NULL;
  35214. msg[3] = 0x33;
  35215. bio_buffer[3] = 0x33;
  35216. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35217. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  35218. ExpectIntEQ(bio_buffer[0], msg[0]);
  35219. ExpectIntEQ(bio_buffer[1], msg[1]);
  35220. ExpectIntEQ(bio_buffer[2], msg[2]);
  35221. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  35222. /* check reading an empty string */
  35223. BIO_free(bio);
  35224. bio = NULL;
  35225. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  35226. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35227. ExpectStrEQ(emp, bio_buffer);
  35228. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35229. /* check error cases */
  35230. BIO_free(bio);
  35231. bio = NULL;
  35232. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  35233. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35234. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35235. #if !defined(NO_FILESYSTEM)
  35236. {
  35237. BIO* f_bio = NULL;
  35238. XFILE f = XBADFILE;
  35239. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  35240. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35241. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  35242. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  35243. if (EXPECT_FAIL() && (f != XBADFILE)) {
  35244. XFCLOSE(f);
  35245. }
  35246. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35247. BIO_free(f_bio);
  35248. f_bio = NULL;
  35249. }
  35250. #endif /* NO_FILESYSTEM */
  35251. BIO_free(bio);
  35252. bio = NULL;
  35253. BIO_free(bio2);
  35254. bio2 = NULL;
  35255. /* try with type BIO */
  35256. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  35257. sizeof(msg));
  35258. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  35259. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35260. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35261. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  35262. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  35263. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  35264. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  35265. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35266. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35267. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35268. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35269. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35270. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35271. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35272. BIO_free(bio);
  35273. bio = NULL;
  35274. BIO_free(bio2);
  35275. bio2 = NULL;
  35276. /* check reading an empty string */
  35277. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  35278. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  35279. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35280. ExpectStrEQ(emp, bio_buffer);
  35281. BIO_free(bio);
  35282. bio = NULL;
  35283. #endif
  35284. return EXPECT_RESULT();
  35285. }
  35286. static int test_wolfSSL_BIO_puts(void)
  35287. {
  35288. EXPECT_DECLS;
  35289. #if defined(OPENSSL_EXTRA)
  35290. BIO* bio = NULL;
  35291. char input[] = "hello\0world\n.....ok\n\0";
  35292. char output[128];
  35293. XMEMSET(output, 0, sizeof(output));
  35294. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35295. ExpectIntEQ(BIO_puts(bio, input), 5);
  35296. ExpectIntEQ(BIO_pending(bio), 5);
  35297. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  35298. ExpectIntEQ(BIO_pending(bio), 19);
  35299. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  35300. ExpectStrEQ(output, "helloworld\n");
  35301. ExpectIntEQ(BIO_pending(bio), 8);
  35302. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  35303. ExpectStrEQ(output, ".....ok\n");
  35304. ExpectIntEQ(BIO_pending(bio), 0);
  35305. ExpectIntEQ(BIO_puts(bio, ""), -1);
  35306. BIO_free(bio);
  35307. #endif
  35308. return EXPECT_RESULT();
  35309. }
  35310. static int test_wolfSSL_BIO_dump(void)
  35311. {
  35312. EXPECT_DECLS;
  35313. #if defined(OPENSSL_EXTRA)
  35314. BIO* bio;
  35315. static const unsigned char data[] = {
  35316. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  35317. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  35318. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  35319. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  35320. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  35321. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  35322. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  35323. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  35324. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  35325. 0xB4
  35326. };
  35327. /* Generated with OpenSSL. */
  35328. static const char expected[] =
  35329. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  35330. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  35331. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  35332. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  35333. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  35334. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  35335. static const char expectedAll[] =
  35336. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  35337. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  35338. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  35339. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35340. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35341. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35342. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35343. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35344. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35345. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35346. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35347. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35348. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35349. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35350. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35351. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35352. char output[16 * 80];
  35353. int i;
  35354. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35355. /* Example key dumped. */
  35356. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35357. sizeof(expected) - 1);
  35358. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35359. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35360. /* Try every possible value for a character. */
  35361. for (i = 0; i < 256; i++)
  35362. output[i] = i;
  35363. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35364. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35365. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35366. BIO_free(bio);
  35367. #endif
  35368. return EXPECT_RESULT();
  35369. }
  35370. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35371. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35372. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35373. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35374. {
  35375. (void)ssl;
  35376. (void)buf;
  35377. (void)sz;
  35378. (void)ctx;
  35379. return WOLFSSL_CBIO_ERR_WANT_READ;
  35380. }
  35381. #endif
  35382. static int test_wolfSSL_BIO_should_retry(void)
  35383. {
  35384. EXPECT_DECLS;
  35385. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35386. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35387. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35388. tcp_ready ready;
  35389. func_args server_args;
  35390. THREAD_TYPE serverThread;
  35391. SOCKET_T sockfd = 0;
  35392. WOLFSSL_CTX* ctx = NULL;
  35393. WOLFSSL* ssl = NULL;
  35394. char msg[64] = "hello wolfssl!";
  35395. char reply[1024];
  35396. int msgSz = (int)XSTRLEN(msg);
  35397. int ret;
  35398. BIO* bio = NULL;
  35399. XMEMSET(&server_args, 0, sizeof(func_args));
  35400. #ifdef WOLFSSL_TIRTOS
  35401. fdOpenSession(Task_self());
  35402. #endif
  35403. StartTCP();
  35404. InitTcpReady(&ready);
  35405. #if defined(USE_WINDOWS_API)
  35406. /* use RNG to get random port if using windows */
  35407. ready.port = GetRandomPort();
  35408. #endif
  35409. server_args.signal = &ready;
  35410. start_thread(test_server_nofail, &server_args, &serverThread);
  35411. wait_tcp_ready(&server_args);
  35412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35413. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35414. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35415. #endif
  35416. ExpectIntEQ(WOLFSSL_SUCCESS,
  35417. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35418. ExpectIntEQ(WOLFSSL_SUCCESS,
  35419. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35420. ExpectIntEQ(WOLFSSL_SUCCESS,
  35421. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35422. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35423. /* force retry */
  35424. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  35425. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  35426. ExpectNotNull(ssl);
  35427. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35428. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35429. if (EXPECT_FAIL()) {
  35430. wolfSSL_free(ssl);
  35431. ssl = NULL;
  35432. }
  35433. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  35434. ExpectIntNE(BIO_should_retry(bio), 0);
  35435. ExpectIntEQ(BIO_should_read(bio), 0);
  35436. ExpectIntEQ(BIO_should_write(bio), 0);
  35437. /* now perform successful connection */
  35438. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35439. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35440. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  35441. ret = wolfSSL_get_error(ssl, -1);
  35442. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35443. ExpectIntNE(BIO_should_retry(bio), 0);
  35444. if (ret == WOLFSSL_ERROR_WANT_READ)
  35445. ExpectIntEQ(BIO_should_read(bio), 1);
  35446. else
  35447. ExpectIntEQ(BIO_should_read(bio), 0);
  35448. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  35449. ExpectIntEQ(BIO_should_write(bio), 1);
  35450. else
  35451. ExpectIntEQ(BIO_should_write(bio), 0);
  35452. }
  35453. else {
  35454. ExpectIntEQ(BIO_should_retry(bio), 0);
  35455. ExpectIntEQ(BIO_should_read(bio), 0);
  35456. ExpectIntEQ(BIO_should_write(bio), 0);
  35457. }
  35458. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35459. XSTRLEN("I hear you fa shizzle!")), 0);
  35460. BIO_free(bio);
  35461. wolfSSL_CTX_free(ctx);
  35462. CloseSocket(sockfd);
  35463. join_thread(serverThread);
  35464. FreeTcpReady(&ready);
  35465. #ifdef WOLFSSL_TIRTOS
  35466. fdOpenSession(Task_self());
  35467. #endif
  35468. #endif
  35469. return EXPECT_RESULT();
  35470. }
  35471. static int test_wolfSSL_BIO_connect(void)
  35472. {
  35473. EXPECT_DECLS;
  35474. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35475. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35476. tcp_ready ready;
  35477. func_args server_args;
  35478. THREAD_TYPE serverThread;
  35479. BIO *tcpBio = NULL;
  35480. BIO *sslBio = NULL;
  35481. SSL_CTX* ctx = NULL;
  35482. SSL *ssl = NULL;
  35483. SSL *sslPtr;
  35484. char msg[] = "hello wolfssl!";
  35485. char reply[30];
  35486. char buff[10] = {0};
  35487. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35488. ExpectIntEQ(WOLFSSL_SUCCESS,
  35489. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35490. ExpectIntEQ(WOLFSSL_SUCCESS,
  35491. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35492. ExpectIntEQ(WOLFSSL_SUCCESS,
  35493. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35494. /* Setup server */
  35495. XMEMSET(&server_args, 0, sizeof(func_args));
  35496. StartTCP();
  35497. InitTcpReady(&ready);
  35498. #if defined(USE_WINDOWS_API)
  35499. /* use RNG to get random port if using windows */
  35500. ready.port = GetRandomPort();
  35501. #endif
  35502. server_args.signal = &ready;
  35503. start_thread(test_server_nofail, &server_args, &serverThread);
  35504. wait_tcp_ready(&server_args);
  35505. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35506. /* Start the test proper */
  35507. /* Setup the TCP BIO */
  35508. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35509. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35510. /* Setup the SSL object */
  35511. ExpectNotNull(ssl = SSL_new(ctx));
  35512. SSL_set_connect_state(ssl);
  35513. /* Setup the SSL BIO */
  35514. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35515. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35516. if (EXPECT_FAIL()) {
  35517. wolfSSL_free(ssl);
  35518. }
  35519. /* Verify that BIO_get_ssl works. */
  35520. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35521. ExpectPtrEq(ssl, sslPtr);
  35522. /* Link BIO's so that sslBio uses tcpBio for IO */
  35523. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35524. /* Do TCP connect */
  35525. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  35526. /* Do TLS handshake */
  35527. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  35528. /* Test writing */
  35529. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35530. /* Expect length of default wolfSSL reply */
  35531. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35532. /* Clean it all up */
  35533. BIO_free_all(sslBio);
  35534. /* Server clean up */
  35535. join_thread(serverThread);
  35536. FreeTcpReady(&ready);
  35537. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35538. * after. */
  35539. XMEMSET(&server_args, 0, sizeof(func_args));
  35540. StartTCP();
  35541. InitTcpReady(&ready);
  35542. #if defined(USE_WINDOWS_API)
  35543. /* use RNG to get random port if using windows */
  35544. ready.port = GetRandomPort();
  35545. #endif
  35546. server_args.signal = &ready;
  35547. start_thread(test_server_nofail, &server_args, &serverThread);
  35548. wait_tcp_ready(&server_args);
  35549. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35550. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35551. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35552. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35553. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  35554. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  35555. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35556. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35557. /* Attempt to close the TLS connection gracefully. */
  35558. BIO_ssl_shutdown(sslBio);
  35559. BIO_free_all(sslBio);
  35560. join_thread(serverThread);
  35561. FreeTcpReady(&ready);
  35562. SSL_CTX_free(ctx);
  35563. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35564. wc_ecc_fp_free(); /* free per thread cache */
  35565. #endif
  35566. #endif
  35567. return EXPECT_RESULT();
  35568. }
  35569. static int test_wolfSSL_BIO_tls(void)
  35570. {
  35571. EXPECT_DECLS;
  35572. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35573. SSL_CTX* ctx = NULL;
  35574. SSL *ssl = NULL;
  35575. BIO *readBio = NULL;
  35576. BIO *writeBio = NULL;
  35577. int ret;
  35578. int err = 0;
  35579. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35580. ExpectNotNull(ssl = SSL_new(ctx));
  35581. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  35582. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  35583. /* Qt reads data from write-bio,
  35584. * then writes the read data into plain packet.
  35585. * Qt reads data from plain packet,
  35586. * then writes the read data into read-bio.
  35587. */
  35588. SSL_set_bio(ssl, readBio, writeBio);
  35589. do {
  35590. #ifdef WOLFSSL_ASYNC_CRYPT
  35591. if (err == WC_PENDING_E) {
  35592. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35593. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35594. }
  35595. #endif
  35596. ret = SSL_connect(ssl);
  35597. err = SSL_get_error(ssl, 0);
  35598. } while (err == WC_PENDING_E);
  35599. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35600. /* in this use case, should return WANT READ
  35601. * so that Qt will read the data from plain packet for next state.
  35602. */
  35603. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  35604. SSL_free(ssl);
  35605. SSL_CTX_free(ctx);
  35606. #endif
  35607. return EXPECT_RESULT();
  35608. }
  35609. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35610. defined(HAVE_HTTP_CLIENT)
  35611. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35612. {
  35613. BIO* clientBio;
  35614. SSL* sslClient;
  35615. SSL_CTX* ctx;
  35616. char connectAddr[20]; /* IP + port */;
  35617. (void)args;
  35618. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35619. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35620. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35621. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35622. AssertNotNull(sslClient = SSL_new(ctx));
  35623. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35624. SSL_set_bio(sslClient, clientBio, clientBio);
  35625. AssertIntEQ(SSL_connect(sslClient), 1);
  35626. SSL_free(sslClient);
  35627. SSL_CTX_free(ctx);
  35628. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35629. wc_ecc_fp_free(); /* free per thread cache */
  35630. #endif
  35631. WOLFSSL_RETURN_FROM_THREAD(0);
  35632. }
  35633. #endif
  35634. static int test_wolfSSL_BIO_accept(void)
  35635. {
  35636. EXPECT_DECLS;
  35637. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35638. defined(HAVE_HTTP_CLIENT)
  35639. BIO* serverBindBio = NULL;
  35640. BIO* serverAcceptBio = NULL;
  35641. SSL* sslServer = NULL;
  35642. SSL_CTX* ctx = NULL;
  35643. func_args args;
  35644. THREAD_TYPE thread;
  35645. char port[10]; /* 10 bytes should be enough to store the string
  35646. * representation of the port */
  35647. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35648. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  35649. /* First BIO_do_accept binds the port */
  35650. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  35651. XMEMSET(&args, 0, sizeof(func_args));
  35652. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35653. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  35654. /* Let's plug it into SSL to test */
  35655. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35656. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35657. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35658. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35659. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35660. ExpectNotNull(sslServer = SSL_new(ctx));
  35661. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35662. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35663. ExpectIntEQ(SSL_accept(sslServer), 1);
  35664. join_thread(thread);
  35665. BIO_free(serverBindBio);
  35666. SSL_free(sslServer);
  35667. SSL_CTX_free(ctx);
  35668. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35669. wc_ecc_fp_free(); /* free per thread cache */
  35670. #endif
  35671. #endif
  35672. return EXPECT_RESULT();
  35673. }
  35674. static int test_wolfSSL_BIO_write(void)
  35675. {
  35676. EXPECT_DECLS;
  35677. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35678. BIO* bio = NULL;
  35679. BIO* bio64 = NULL;
  35680. BIO* bio_mem = NULL;
  35681. BIO* ptr = NULL;
  35682. int sz;
  35683. char msg[] = "conversion test";
  35684. char out[40];
  35685. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35686. void* bufPtr = NULL;
  35687. BUF_MEM* buf = NULL;
  35688. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  35689. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35690. if (EXPECT_FAIL()) {
  35691. BIO_free(bio64);
  35692. }
  35693. /* now should convert to base64 then write to memory */
  35694. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35695. BIO_flush(bio);
  35696. /* test BIO chain */
  35697. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35698. ExpectNotNull(buf);
  35699. ExpectIntEQ(buf->length, 25);
  35700. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35701. ExpectPtrEq(buf->data, bufPtr);
  35702. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35703. sz = sizeof(out);
  35704. XMEMSET(out, 0, sz);
  35705. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35706. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  35707. /* write then read should return the same message */
  35708. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35709. sz = sizeof(out);
  35710. XMEMSET(out, 0, sz);
  35711. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  35712. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35713. /* now try encoding with no line ending */
  35714. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35715. #ifdef HAVE_EX_DATA
  35716. BIO_set_ex_data(bio64, 0, (void*) "data");
  35717. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35718. #endif
  35719. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35720. BIO_flush(bio);
  35721. sz = sizeof(out);
  35722. XMEMSET(out, 0, sz);
  35723. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35724. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  35725. BIO_free_all(bio); /* frees bio64 also */
  35726. bio = NULL;
  35727. /* test with more than one bio64 in list */
  35728. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  35729. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35730. if (EXPECT_FAIL()) {
  35731. BIO_free(bio64);
  35732. bio64 = NULL;
  35733. }
  35734. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  35735. ExpectNotNull(BIO_push(bio64, bio_mem));
  35736. if (EXPECT_FAIL()) {
  35737. BIO_free(bio_mem);
  35738. }
  35739. /* now should convert to base64 when stored and then decode with read */
  35740. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35741. BIO_flush(bio);
  35742. sz = sizeof(out);
  35743. XMEMSET(out, 0, sz);
  35744. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35745. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  35746. BIO_clear_flags(bio64, ~0);
  35747. BIO_set_retry_read(bio);
  35748. BIO_free_all(bio); /* frees bio64s also */
  35749. bio = NULL;
  35750. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  35751. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35752. BIO_free(bio);
  35753. #endif
  35754. return EXPECT_RESULT();
  35755. }
  35756. static int test_wolfSSL_BIO_printf(void)
  35757. {
  35758. EXPECT_DECLS;
  35759. #if defined(OPENSSL_ALL)
  35760. BIO* bio = NULL;
  35761. int sz = 7;
  35762. char msg[] = "TLS 1.3 for the world";
  35763. char out[60];
  35764. char expected[] = "TLS 1.3 for the world : sz = 7";
  35765. XMEMSET(out, 0, sizeof(out));
  35766. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35767. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35768. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35769. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35770. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35771. BIO_free(bio);
  35772. #endif
  35773. return EXPECT_RESULT();
  35774. }
  35775. static int test_wolfSSL_BIO_f_md(void)
  35776. {
  35777. EXPECT_DECLS;
  35778. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35779. BIO* bio = NULL;
  35780. BIO* mem = NULL;
  35781. char msg[] = "message to hash";
  35782. char out[60];
  35783. EVP_MD_CTX* ctx = NULL;
  35784. const unsigned char testKey[] =
  35785. {
  35786. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35787. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35788. 0x0b, 0x0b, 0x0b, 0x0b
  35789. };
  35790. const char testData[] = "Hi There";
  35791. const unsigned char testResult[] =
  35792. {
  35793. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35794. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35795. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35796. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35797. };
  35798. const unsigned char expectedHash[] =
  35799. {
  35800. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35801. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35802. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35803. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35804. };
  35805. const unsigned char emptyHash[] =
  35806. {
  35807. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35808. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35809. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35810. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35811. };
  35812. unsigned char check[sizeof(testResult) + 1];
  35813. size_t checkSz = -1;
  35814. EVP_PKEY* key = NULL;
  35815. XMEMSET(out, 0, sizeof(out));
  35816. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35817. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  35818. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35819. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35820. /* should not be able to write/read yet since just digest wrapper and no
  35821. * data is passing through the bio */
  35822. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  35823. ExpectIntEQ(BIO_pending(bio), 0);
  35824. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35825. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  35826. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35827. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35828. BIO_reset(bio);
  35829. /* append BIO mem to bio in order to read/write */
  35830. ExpectNotNull(bio = BIO_push(bio, mem));
  35831. XMEMSET(out, 0, sizeof(out));
  35832. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35833. ExpectIntEQ(BIO_pending(bio), 16);
  35834. /* this just reads the message and does not hash it (gets calls final) */
  35835. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35836. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35837. /* create a message digest using BIO */
  35838. XMEMSET(out, 0, sizeof(out));
  35839. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35840. ExpectIntEQ(BIO_pending(mem), 16);
  35841. ExpectIntEQ(BIO_pending(bio), 16);
  35842. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35843. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35844. BIO_free(bio);
  35845. bio = NULL;
  35846. BIO_free(mem);
  35847. mem = NULL;
  35848. /* test with HMAC */
  35849. XMEMSET(out, 0, sizeof(out));
  35850. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35851. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  35852. BIO_get_md_ctx(bio, &ctx);
  35853. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  35854. (int)sizeof(testKey)));
  35855. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35856. ExpectNotNull(bio = BIO_push(bio, mem));
  35857. BIO_write(bio, testData, (int)strlen(testData));
  35858. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  35859. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  35860. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35861. EVP_PKEY_free(key);
  35862. BIO_free(bio);
  35863. BIO_free(mem);
  35864. #endif
  35865. return EXPECT_RESULT();
  35866. }
  35867. static int test_wolfSSL_BIO_up_ref(void)
  35868. {
  35869. EXPECT_DECLS;
  35870. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35871. BIO* bio = NULL;
  35872. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35873. ExpectIntEQ(BIO_up_ref(NULL), 0);
  35874. ExpectIntEQ(BIO_up_ref(bio), 1);
  35875. BIO_free(bio);
  35876. ExpectIntEQ(BIO_up_ref(bio), 1);
  35877. BIO_free(bio);
  35878. BIO_free(bio);
  35879. #endif
  35880. return EXPECT_RESULT();
  35881. }
  35882. static int test_wolfSSL_BIO_reset(void)
  35883. {
  35884. EXPECT_DECLS;
  35885. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35886. BIO* bio = NULL;
  35887. byte buf[16];
  35888. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  35889. (word32)XSTRLEN("secure your data")));
  35890. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  35891. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  35892. XMEMSET(buf, 0, 16);
  35893. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  35894. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  35895. /* You cannot write to MEM BIO with read-only mode. */
  35896. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  35897. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  35898. XMEMSET(buf, 0, 16);
  35899. ExpectIntEQ(BIO_reset(bio), 0);
  35900. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  35901. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  35902. BIO_free(bio);
  35903. #endif
  35904. return EXPECT_RESULT();
  35905. }
  35906. #endif /* !NO_BIO */
  35907. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35908. /* test that the callback arg is correct */
  35909. static int certCbArg = 0;
  35910. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35911. {
  35912. if (ssl == NULL || arg != &certCbArg)
  35913. return 0;
  35914. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35915. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35916. return 0;
  35917. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35918. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35919. return 0;
  35920. return 1;
  35921. }
  35922. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  35923. {
  35924. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35925. return TEST_SUCCESS;
  35926. }
  35927. /**
  35928. * This is only done because test_client_nofail has no way to stop
  35929. * certificate and key loading
  35930. */
  35931. static int clientCertClearCb(WOLFSSL* ssl)
  35932. {
  35933. /* Clear the loaded certs to force the callbacks to set them up */
  35934. SSL_certs_clear(ssl);
  35935. return TEST_SUCCESS;
  35936. }
  35937. static int serverCertCb(WOLFSSL* ssl, void* arg)
  35938. {
  35939. if (ssl == NULL || arg != &certCbArg)
  35940. return 0;
  35941. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  35942. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35943. return 0;
  35944. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  35945. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35946. return 0;
  35947. return 1;
  35948. }
  35949. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  35950. {
  35951. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  35952. return TEST_SUCCESS;
  35953. }
  35954. /**
  35955. * This is only done because test_server_nofail has no way to stop
  35956. * certificate and key loading
  35957. */
  35958. static int serverCertClearCb(WOLFSSL* ssl)
  35959. {
  35960. /* Clear the loaded certs to force the callbacks to set them up */
  35961. SSL_certs_clear(ssl);
  35962. return TEST_SUCCESS;
  35963. }
  35964. #endif
  35965. static int test_wolfSSL_cert_cb(void)
  35966. {
  35967. EXPECT_DECLS;
  35968. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35969. test_ssl_cbf func_cb_client;
  35970. test_ssl_cbf func_cb_server;
  35971. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35972. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35973. func_cb_client.ctx_ready = clientCertSetupCb;
  35974. func_cb_client.ssl_ready = clientCertClearCb;
  35975. func_cb_server.ctx_ready = serverCertSetupCb;
  35976. func_cb_server.ssl_ready = serverCertClearCb;
  35977. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35978. &func_cb_server, NULL), TEST_SUCCESS);
  35979. #endif
  35980. return EXPECT_RESULT();
  35981. }
  35982. static int test_wolfSSL_SESSION(void)
  35983. {
  35984. EXPECT_DECLS;
  35985. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35986. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35987. !defined(NO_SESSION_CACHE)
  35988. WOLFSSL* ssl = NULL;
  35989. WOLFSSL_CTX* ctx = NULL;
  35990. WOLFSSL_SESSION* sess = NULL;
  35991. WOLFSSL_SESSION* sess_copy = NULL;
  35992. #ifdef OPENSSL_EXTRA
  35993. #ifdef HAVE_EXT_CACHE
  35994. unsigned char* sessDer = NULL;
  35995. unsigned char* ptr = NULL;
  35996. int sz;
  35997. #endif
  35998. const unsigned char context[] = "user app context";
  35999. unsigned int contextSz = (unsigned int)sizeof(context);
  36000. #endif
  36001. int ret, err;
  36002. SOCKET_T sockfd;
  36003. tcp_ready ready;
  36004. func_args server_args;
  36005. THREAD_TYPE serverThread;
  36006. char msg[80];
  36007. const char* sendGET = "GET";
  36008. /* TLS v1.3 requires session tickets */
  36009. /* CHACHA and POLY1305 required for myTicketEncCb */
  36010. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  36011. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  36012. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  36013. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  36014. #else
  36015. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36016. #endif
  36017. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  36018. WOLFSSL_FILETYPE_PEM));
  36019. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  36020. WOLFSSL_FILETYPE_PEM));
  36021. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  36022. WOLFSSL_SUCCESS);
  36023. #ifdef WOLFSSL_ENCRYPTED_KEYS
  36024. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  36025. #endif
  36026. #ifdef HAVE_SESSION_TICKET
  36027. /* Use session tickets, for ticket tests below */
  36028. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  36029. #endif
  36030. XMEMSET(&server_args, 0, sizeof(func_args));
  36031. #ifdef WOLFSSL_TIRTOS
  36032. fdOpenSession(Task_self());
  36033. #endif
  36034. StartTCP();
  36035. InitTcpReady(&ready);
  36036. #if defined(USE_WINDOWS_API)
  36037. /* use RNG to get random port if using windows */
  36038. ready.port = GetRandomPort();
  36039. #endif
  36040. server_args.signal = &ready;
  36041. start_thread(test_server_nofail, &server_args, &serverThread);
  36042. wait_tcp_ready(&server_args);
  36043. /* client connection */
  36044. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36045. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  36046. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  36047. #ifdef WOLFSSL_ASYNC_CRYPT
  36048. err = 0; /* Reset error */
  36049. #endif
  36050. do {
  36051. #ifdef WOLFSSL_ASYNC_CRYPT
  36052. if (err == WC_PENDING_E) {
  36053. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36054. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36055. }
  36056. #endif
  36057. ret = wolfSSL_connect(ssl);
  36058. err = wolfSSL_get_error(ssl, 0);
  36059. } while (err == WC_PENDING_E);
  36060. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36061. #ifdef WOLFSSL_ASYNC_CRYPT
  36062. err = 0; /* Reset error */
  36063. #endif
  36064. do {
  36065. #ifdef WOLFSSL_ASYNC_CRYPT
  36066. if (err == WC_PENDING_E) {
  36067. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36068. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36069. }
  36070. #endif
  36071. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  36072. err = wolfSSL_get_error(ssl, 0);
  36073. } while (err == WC_PENDING_E);
  36074. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  36075. #ifdef WOLFSSL_ASYNC_CRYPT
  36076. err = 0; /* Reset error */
  36077. #endif
  36078. do {
  36079. #ifdef WOLFSSL_ASYNC_CRYPT
  36080. if (err == WC_PENDING_E) {
  36081. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36082. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36083. }
  36084. #endif
  36085. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  36086. err = wolfSSL_get_error(ssl, 0);
  36087. } while (err == WC_PENDING_E);
  36088. ExpectIntEQ(ret, 23);
  36089. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  36090. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  36091. #ifdef HAVE_EXT_CACHE
  36092. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  36093. * HAVE_EXT_CACHE we get new objects each time */
  36094. #endif
  36095. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  36096. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  36097. sess = wolfSSL_get_session(ssl);
  36098. #ifdef OPENSSL_EXTRA
  36099. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  36100. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  36101. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  36102. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  36103. #ifdef HAVE_SESSION_TICKET
  36104. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  36105. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  36106. SESSION_TICKET_HINT_DEFAULT);
  36107. #else
  36108. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  36109. #endif
  36110. #else
  36111. (void)sess;
  36112. #endif /* OPENSSL_EXTRA */
  36113. /* Retain copy of the session for later testing */
  36114. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  36115. wolfSSL_shutdown(ssl);
  36116. wolfSSL_free(ssl); ssl = NULL;
  36117. join_thread(serverThread);
  36118. FreeTcpReady(&ready);
  36119. #ifdef WOLFSSL_TIRTOS
  36120. fdOpenSession(Task_self());
  36121. #endif
  36122. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  36123. {
  36124. X509 *x509 = NULL;
  36125. char buf[30];
  36126. int bufSz = 0;
  36127. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  36128. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  36129. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  36130. sizeof(buf))), 0);
  36131. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  36132. if (bufSz == 7) {
  36133. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  36134. }
  36135. if (bufSz == 16) {
  36136. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  36137. }
  36138. }
  36139. #endif
  36140. #ifdef HAVE_EXT_CACHE
  36141. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  36142. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  36143. sess_copy = NULL;
  36144. #endif
  36145. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  36146. /* get session from DER and update the timeout */
  36147. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  36148. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  36149. wolfSSL_SESSION_free(sess); sess = NULL;
  36150. sess = NULL;
  36151. ptr = sessDer;
  36152. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  36153. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  36154. (const unsigned char**)&ptr, sz));
  36155. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  36156. sessDer = NULL;
  36157. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  36158. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36159. #endif
  36160. /* successful set session test */
  36161. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36162. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  36163. #ifdef HAVE_SESSION_TICKET
  36164. /* Test set/get session ticket */
  36165. {
  36166. const char* ticket = "This is a session ticket";
  36167. char buf[64] = {0};
  36168. word32 bufSz = (word32)sizeof(buf);
  36169. ExpectIntEQ(SSL_SUCCESS,
  36170. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  36171. (word32)XSTRLEN(ticket)));
  36172. ExpectIntEQ(SSL_SUCCESS,
  36173. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  36174. ExpectStrEQ(ticket, buf);
  36175. }
  36176. #endif
  36177. #ifdef OPENSSL_EXTRA
  36178. /* session timeout case */
  36179. /* make the session to be expired */
  36180. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  36181. XSLEEP_MS(1200);
  36182. /* SSL_set_session should reject specified session but return success
  36183. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  36184. */
  36185. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36186. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  36187. #else
  36188. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  36189. #endif
  36190. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36191. /* fail case with miss match session context IDs (use compatibility API) */
  36192. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  36193. SSL_SUCCESS);
  36194. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36195. wolfSSL_free(ssl); ssl = NULL;
  36196. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  36197. SSL_FAILURE);
  36198. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  36199. SSL_SUCCESS);
  36200. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36201. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36202. #endif /* OPENSSL_EXTRA */
  36203. wolfSSL_free(ssl);
  36204. wolfSSL_SESSION_free(sess);
  36205. wolfSSL_CTX_free(ctx);
  36206. #endif
  36207. return EXPECT_RESULT();
  36208. }
  36209. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36210. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36211. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  36212. !defined(WOLFSSL_NO_TLS12)
  36213. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  36214. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  36215. {
  36216. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  36217. /* returns previous timeout value */
  36218. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  36219. #else
  36220. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  36221. #endif
  36222. }
  36223. /* set the session to timeout in a second */
  36224. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  36225. {
  36226. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  36227. }
  36228. /* store the client side session from the first successful connection */
  36229. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  36230. {
  36231. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  36232. NULL); /* ref count 1 */
  36233. }
  36234. /* wait till session is expired then set it in the WOLFSSL struct for use */
  36235. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  36236. {
  36237. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  36238. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  36239. WOLFSSL_SUCCESS);
  36240. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  36241. }
  36242. /* set expired session in the WOLFSSL struct for use */
  36243. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  36244. {
  36245. XSLEEP_MS(1200); /* wait a second for session to expire */
  36246. /* set the expired session, call to set session fails but continuing on
  36247. after failure should be handled here */
  36248. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36249. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  36250. WOLFSSL_SUCCESS);
  36251. #else
  36252. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  36253. WOLFSSL_SUCCESS);
  36254. #endif
  36255. }
  36256. /* check that the expired session was not reused */
  36257. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  36258. {
  36259. /* since the session has expired it should not have been reused */
  36260. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  36261. }
  36262. #endif
  36263. static int test_wolfSSL_SESSION_expire_downgrade(void)
  36264. {
  36265. EXPECT_DECLS;
  36266. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36267. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  36268. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  36269. !defined(WOLFSSL_NO_TLS12)
  36270. WOLFSSL_CTX* ctx = NULL;
  36271. callback_functions server_cbf, client_cbf;
  36272. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  36273. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  36274. /* force server side to use TLS 1.2 */
  36275. server_cbf.ctx = ctx;
  36276. server_cbf.method = wolfTLSv1_2_server_method;
  36277. client_cbf.method = wolfSSLv23_client_method;
  36278. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  36279. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  36280. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  36281. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  36282. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  36283. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  36284. /* set the previously created session and wait till expired */
  36285. server_cbf.ctx = ctx;
  36286. client_cbf.method = wolfSSLv23_client_method;
  36287. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  36288. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  36289. client_cbf.on_result =
  36290. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  36291. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  36292. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  36293. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  36294. /* set the previously created expired session */
  36295. server_cbf.ctx = ctx;
  36296. client_cbf.method = wolfSSLv23_client_method;
  36297. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  36298. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  36299. client_cbf.on_result =
  36300. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  36301. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  36302. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  36303. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  36304. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  36305. wolfSSL_CTX_free(ctx);
  36306. #endif
  36307. return EXPECT_RESULT();
  36308. }
  36309. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  36310. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  36311. static int clientSessRemCountMalloc = 0;
  36312. static int serverSessRemCountMalloc = 0;
  36313. static int clientSessRemCountFree = 0;
  36314. static int serverSessRemCountFree = 0;
  36315. static WOLFSSL_CTX* serverSessCtx = NULL;
  36316. static WOLFSSL_SESSION* serverSess = NULL;
  36317. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36318. !defined(NO_SESSION_CACHE_REF)
  36319. static WOLFSSL_CTX* clientSessCtx = NULL;
  36320. static WOLFSSL_SESSION* clientSess = NULL;
  36321. #endif
  36322. static int serverSessRemIdx = 3;
  36323. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  36324. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  36325. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  36326. {
  36327. int* side;
  36328. (void)ctx;
  36329. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  36330. if (side != NULL) {
  36331. if (*side == WOLFSSL_CLIENT_END)
  36332. clientSessRemCountFree++;
  36333. else
  36334. serverSessRemCountFree++;
  36335. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  36336. }
  36337. }
  36338. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  36339. {
  36340. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  36341. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  36342. !defined(NO_SESSION_CACHE_REF)
  36343. {
  36344. EXPECT_DECLS;
  36345. /* Allow downgrade, set min version, and disable TLS 1.3.
  36346. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  36347. * reference to the session cache. But with WOLFSSL_TLS13 and without
  36348. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  36349. * the session in the cache. In this case we need to downgrade to
  36350. * previous versions to just use the legacy session ID field. */
  36351. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  36352. SSL_SUCCESS);
  36353. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  36354. SSL_SUCCESS);
  36355. return EXPECT_RESULT();
  36356. }
  36357. #else
  36358. return TEST_SUCCESS;
  36359. #endif
  36360. }
  36361. static int SessRemSslSetupCb(WOLFSSL* ssl)
  36362. {
  36363. EXPECT_DECLS;
  36364. int* side;
  36365. if (SSL_is_server(ssl)) {
  36366. side = &sessRemCtx_Server;
  36367. serverSessRemCountMalloc++;
  36368. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  36369. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  36370. SSL_SUCCESS);
  36371. }
  36372. else {
  36373. side = &sessRemCtx_Client;
  36374. clientSessRemCountMalloc++;
  36375. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36376. !defined(NO_SESSION_CACHE_REF)
  36377. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  36378. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  36379. SSL_SUCCESS);
  36380. #endif
  36381. }
  36382. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  36383. serverSessRemIdx, side), SSL_SUCCESS);
  36384. return EXPECT_RESULT();
  36385. }
  36386. #endif
  36387. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  36388. {
  36389. EXPECT_DECLS;
  36390. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  36391. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  36392. /* Check that the remove callback gets called for external data in a
  36393. * session object */
  36394. test_ssl_cbf func_cb;
  36395. XMEMSET(&func_cb, 0, sizeof(func_cb));
  36396. func_cb.ctx_ready = SessRemCtxSetupCb;
  36397. func_cb.on_result = SessRemSslSetupCb;
  36398. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  36399. NULL), TEST_SUCCESS);
  36400. /* Both should have been allocated */
  36401. ExpectIntEQ(clientSessRemCountMalloc, 1);
  36402. ExpectIntEQ(serverSessRemCountMalloc, 1);
  36403. /* This should not be called yet. Session wasn't evicted from cache yet. */
  36404. ExpectIntEQ(clientSessRemCountFree, 0);
  36405. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36406. !defined(NO_SESSION_CACHE_REF)
  36407. /* Force a cache lookup */
  36408. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36409. /* Force a cache update */
  36410. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  36411. /* This should set the timeout to 0 and call the remove callback from within
  36412. * the session cache. */
  36413. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  36414. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36415. ExpectIntEQ(clientSessRemCountFree, 1);
  36416. #endif
  36417. /* Server session is in the cache so ex_data isn't free'd with the SSL
  36418. * object */
  36419. ExpectIntEQ(serverSessRemCountFree, 0);
  36420. /* Force a cache lookup */
  36421. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36422. /* Force a cache update */
  36423. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  36424. /* This should set the timeout to 0 and call the remove callback from within
  36425. * the session cache. */
  36426. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  36427. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36428. ExpectIntEQ(serverSessRemCountFree, 1);
  36429. /* Need to free the references that we kept */
  36430. SSL_CTX_free(serverSessCtx);
  36431. SSL_SESSION_free(serverSess);
  36432. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36433. !defined(NO_SESSION_CACHE_REF)
  36434. SSL_CTX_free(clientSessCtx);
  36435. SSL_SESSION_free(clientSess);
  36436. #endif
  36437. #endif
  36438. return EXPECT_RESULT();
  36439. }
  36440. static int test_wolfSSL_ticket_keys(void)
  36441. {
  36442. EXPECT_DECLS;
  36443. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  36444. !defined(NO_WOLFSSL_SERVER)
  36445. WOLFSSL_CTX* ctx = NULL;
  36446. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  36447. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36448. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  36449. WOLFSSL_FAILURE);
  36450. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  36451. WOLFSSL_FAILURE);
  36452. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  36453. WOLFSSL_FAILURE);
  36454. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  36455. WOLFSSL_FAILURE);
  36456. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36457. WOLFSSL_FAILURE);
  36458. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36459. WOLFSSL_FAILURE);
  36460. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36461. WOLFSSL_FAILURE);
  36462. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36463. WOLFSSL_FAILURE);
  36464. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36465. WOLFSSL_FAILURE);
  36466. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36467. WOLFSSL_FAILURE);
  36468. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36469. WOLFSSL_FAILURE);
  36470. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36471. WOLFSSL_FAILURE);
  36472. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36473. WOLFSSL_FAILURE);
  36474. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36475. WOLFSSL_FAILURE);
  36476. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36477. WOLFSSL_SUCCESS);
  36478. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36479. WOLFSSL_SUCCESS);
  36480. wolfSSL_CTX_free(ctx);
  36481. #endif
  36482. return EXPECT_RESULT();
  36483. }
  36484. #ifndef NO_BIO
  36485. static int test_wolfSSL_d2i_PUBKEY(void)
  36486. {
  36487. EXPECT_DECLS;
  36488. #if defined(OPENSSL_EXTRA)
  36489. BIO* bio = NULL;
  36490. EVP_PKEY* pkey = NULL;
  36491. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36492. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  36493. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36494. /* RSA PUBKEY test */
  36495. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  36496. sizeof_client_keypub_der_2048), 0);
  36497. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36498. EVP_PKEY_free(pkey);
  36499. pkey = NULL;
  36500. #endif
  36501. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36502. /* ECC PUBKEY test */
  36503. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36504. sizeof_ecc_clikeypub_der_256), 0);
  36505. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36506. EVP_PKEY_free(pkey);
  36507. pkey = NULL;
  36508. #endif
  36509. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36510. /* DSA PUBKEY test */
  36511. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36512. sizeof_dsa_pub_key_der_2048), 0);
  36513. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36514. EVP_PKEY_free(pkey);
  36515. pkey = NULL;
  36516. #endif
  36517. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36518. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36519. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36520. (HAVE_FIPS_VERSION > 2))
  36521. /* DH PUBKEY test */
  36522. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36523. sizeof_dh_pub_key_der_2048), 0);
  36524. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36525. EVP_PKEY_free(pkey);
  36526. pkey = NULL;
  36527. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36528. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36529. BIO_free(bio);
  36530. (void)pkey;
  36531. #endif
  36532. return EXPECT_RESULT();
  36533. }
  36534. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36535. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36536. {
  36537. EXPECT_DECLS;
  36538. BIO* bio = NULL;
  36539. EVP_PKEY* pkey = NULL;
  36540. WOLFSSL_CTX* ctx = NULL;
  36541. #if defined(WOLFSSL_KEY_GEN)
  36542. unsigned char buff[4096];
  36543. unsigned char* bufPtr = buff;
  36544. #endif
  36545. /* test creating new EVP_PKEY with bad arg */
  36546. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36547. /* test loading RSA key using BIO */
  36548. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36549. {
  36550. XFILE file = XBADFILE;
  36551. const char* fname = "./certs/server-key.der";
  36552. size_t sz;
  36553. byte* buf = NULL;
  36554. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  36555. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36556. ExpectTrue((sz = XFTELL(file)) != 0);
  36557. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  36558. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36559. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  36560. if (file != XBADFILE) {
  36561. XFCLOSE(file);
  36562. }
  36563. /* Test using BIO new mem and loading DER private key */
  36564. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36565. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36566. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36567. BIO_free(bio);
  36568. bio = NULL;
  36569. EVP_PKEY_free(pkey);
  36570. pkey = NULL;
  36571. }
  36572. #endif
  36573. /* test loading ECC key using BIO */
  36574. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36575. {
  36576. XFILE file = XBADFILE;
  36577. const char* fname = "./certs/ecc-key.der";
  36578. size_t sz;
  36579. byte* buf = NULL;
  36580. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  36581. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36582. ExpectTrue((sz = XFTELL(file)) != 0);
  36583. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  36584. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36585. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  36586. if (file != XBADFILE)
  36587. XFCLOSE(file);
  36588. /* Test using BIO new mem and loading DER private key */
  36589. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36590. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36591. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36592. BIO_free(bio);
  36593. bio = NULL;
  36594. EVP_PKEY_free(pkey);
  36595. pkey = NULL;
  36596. }
  36597. #endif
  36598. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36599. #ifndef NO_WOLFSSL_SERVER
  36600. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36601. #else
  36602. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36603. #endif
  36604. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36605. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36606. {
  36607. RSA* rsa = NULL;
  36608. /* Tests bad parameters */
  36609. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36610. /* RSA not set yet, expecting to fail*/
  36611. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36612. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36613. /* set RSA using bio*/
  36614. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  36615. sizeof_client_key_der_2048), 0);
  36616. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36617. ExpectNotNull(rsa);
  36618. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36619. /* i2d RSAprivate key tests */
  36620. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36621. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36622. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36623. sizeof_client_key_der_2048);
  36624. bufPtr -= sizeof_client_key_der_2048;
  36625. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36626. sizeof_client_key_der_2048), 0);
  36627. bufPtr = NULL;
  36628. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36629. sizeof_client_key_der_2048);
  36630. ExpectNotNull(bufPtr);
  36631. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36632. sizeof_client_key_der_2048), 0);
  36633. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36634. RSA_free(rsa);
  36635. rsa = RSA_new();
  36636. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36637. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36638. RSA_free(rsa);
  36639. }
  36640. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36641. SSL_CTX_free(ctx);
  36642. ctx = NULL;
  36643. BIO_free(bio);
  36644. bio = NULL;
  36645. return EXPECT_RESULT();
  36646. }
  36647. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  36648. #endif /* !NO_BIO */
  36649. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36650. {
  36651. EXPECT_DECLS;
  36652. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36653. !defined(NO_RSA)
  36654. X509* x509 = NULL;
  36655. GENERAL_NAME* gn = NULL;
  36656. unsigned char buf[4096];
  36657. const unsigned char* bufPt = NULL;
  36658. int bytes = 0;
  36659. int i;
  36660. int j;
  36661. XFILE f = XBADFILE;
  36662. STACK_OF(GENERAL_NAME)* sk = NULL;
  36663. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  36664. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36665. if (f != XBADFILE)
  36666. XFCLOSE(f);
  36667. for (j = 0; j < 2; ++j) {
  36668. bufPt = buf;
  36669. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36670. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36671. NID_subject_alt_name, NULL, NULL));
  36672. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36673. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36674. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36675. if (gn != NULL) {
  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. }
  36689. X509_free(x509);
  36690. x509 = NULL;
  36691. if (j == 0) {
  36692. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36693. }
  36694. else {
  36695. /*
  36696. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36697. * it was supposed to. This is a regression test for that bug.
  36698. */
  36699. GENERAL_NAMES_free(sk);
  36700. }
  36701. sk = NULL;
  36702. }
  36703. #endif
  36704. return EXPECT_RESULT();
  36705. }
  36706. static int test_wolfSSL_GENERAL_NAME_print(void)
  36707. {
  36708. EXPECT_DECLS;
  36709. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36710. X509* x509 = NULL;
  36711. GENERAL_NAME* gn = NULL;
  36712. unsigned char buf[4096];
  36713. const unsigned char* bufPt = NULL;
  36714. int bytes;
  36715. XFILE f = XBADFILE;
  36716. STACK_OF(GENERAL_NAME)* sk = NULL;
  36717. BIO* out = NULL;
  36718. unsigned char outbuf[128];
  36719. X509_EXTENSION* ext = NULL;
  36720. AUTHORITY_INFO_ACCESS* aia = NULL;
  36721. ACCESS_DESCRIPTION* ad = NULL;
  36722. ASN1_IA5STRING *dnsname = NULL;
  36723. const unsigned char v4Addr[] = {192,168,53,1};
  36724. const unsigned char v6Addr[] =
  36725. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36726. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36727. const unsigned char email[] =
  36728. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36729. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36730. const char* dnsStr = "DNS:example.com";
  36731. const char* uriStr = "URI:http://127.0.0.1:22220";
  36732. const char* v4addStr = "IP Address:192.168.53.1";
  36733. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36734. const char* emailStr = "email:info@wolfssl.com";
  36735. const char* othrStr = "othername:<unsupported>";
  36736. const char* x400Str = "X400Name:<unsupported>";
  36737. const char* ediStr = "EdiPartyName:<unsupported>";
  36738. /* BIO to output */
  36739. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  36740. /* test for NULL param */
  36741. gn = NULL;
  36742. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36743. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36744. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36745. /* test for GEN_DNS */
  36746. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  36747. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36748. if (f != XBADFILE) {
  36749. XFCLOSE(f);
  36750. f = XBADFILE;
  36751. }
  36752. bufPt = buf;
  36753. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36754. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36755. NID_subject_alt_name, NULL, NULL));
  36756. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36757. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36758. XMEMSET(outbuf, 0, sizeof(outbuf));
  36759. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36760. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36761. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36762. sk = NULL;
  36763. X509_free(x509);
  36764. x509 = NULL;
  36765. /* Lets test for setting as well. */
  36766. ExpectNotNull(gn = GENERAL_NAME_new());
  36767. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  36768. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  36769. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  36770. dnsname = NULL;
  36771. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36772. XMEMSET(outbuf, 0, sizeof(outbuf));
  36773. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36774. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36775. GENERAL_NAME_free(gn);
  36776. /* test for GEN_URI */
  36777. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  36778. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36779. if (f != XBADFILE) {
  36780. XFCLOSE(f);
  36781. f = XBADFILE;
  36782. }
  36783. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36784. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  36785. ext));
  36786. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  36787. if (ad != NULL) {
  36788. gn = ad->location;
  36789. }
  36790. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36791. gn = NULL;
  36792. XMEMSET(outbuf,0,sizeof(outbuf));
  36793. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36794. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36795. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36796. aia = NULL;
  36797. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36798. ExpectNotNull(aia);
  36799. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36800. aia = NULL;
  36801. X509_free(x509);
  36802. x509 = NULL;
  36803. /* test for GEN_IPADD */
  36804. /* ip v4 address */
  36805. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36806. if (gn != NULL) {
  36807. gn->type = GEN_IPADD;
  36808. if (gn->d.iPAddress != NULL) {
  36809. gn->d.iPAddress->length = sizeof(v4Addr);
  36810. }
  36811. }
  36812. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36813. sizeof(v4Addr)), 1);
  36814. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36815. XMEMSET(outbuf,0,sizeof(outbuf));
  36816. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36817. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36818. GENERAL_NAME_free(gn);
  36819. gn = NULL;
  36820. /* ip v6 address */
  36821. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36822. if (gn != NULL) {
  36823. gn->type = GEN_IPADD;
  36824. if (gn->d.iPAddress != NULL) {
  36825. gn->d.iPAddress->length = sizeof(v6Addr);
  36826. }
  36827. }
  36828. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36829. sizeof(v6Addr)), 1);
  36830. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36831. XMEMSET(outbuf,0,sizeof(outbuf));
  36832. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36833. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36834. GENERAL_NAME_free(gn);
  36835. gn = NULL;
  36836. /* test for GEN_EMAIL */
  36837. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36838. if (gn != NULL) {
  36839. gn->type = GEN_EMAIL;
  36840. if (gn->d.rfc822Name != NULL) {
  36841. gn->d.rfc822Name->length = sizeof(email);
  36842. }
  36843. }
  36844. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  36845. 1);
  36846. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36847. XMEMSET(outbuf,0,sizeof(outbuf));
  36848. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36849. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36850. GENERAL_NAME_free(gn);
  36851. gn = NULL;
  36852. /* test for GEN_OTHERNAME */
  36853. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36854. if (gn != NULL) {
  36855. gn->type = GEN_OTHERNAME;
  36856. }
  36857. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36858. XMEMSET(outbuf,0,sizeof(outbuf));
  36859. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36860. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36861. GENERAL_NAME_free(gn);
  36862. gn = NULL;
  36863. /* test for GEN_X400 */
  36864. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36865. if (gn != NULL) {
  36866. gn->type = GEN_X400;
  36867. }
  36868. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36869. XMEMSET(outbuf,0,sizeof(outbuf));
  36870. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36871. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36872. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  36873. if (gn != NULL) {
  36874. gn->type = GEN_IA5;
  36875. }
  36876. GENERAL_NAME_free(gn);
  36877. gn = NULL;
  36878. /* test for GEN_EDIPARTY */
  36879. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36880. if (gn != NULL) {
  36881. gn->type = GEN_EDIPARTY;
  36882. }
  36883. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36884. XMEMSET(outbuf,0,sizeof(outbuf));
  36885. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36886. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36887. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  36888. if (gn != NULL) {
  36889. gn->type = GEN_IA5;
  36890. }
  36891. GENERAL_NAME_free(gn);
  36892. gn = NULL;
  36893. BIO_free(out);
  36894. #endif /* OPENSSL_ALL */
  36895. return EXPECT_RESULT();
  36896. }
  36897. static int test_wolfSSL_sk_DIST_POINT(void)
  36898. {
  36899. EXPECT_DECLS;
  36900. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36901. !defined(NO_RSA)
  36902. X509* x509 = NULL;
  36903. unsigned char buf[4096];
  36904. const unsigned char* bufPt;
  36905. int bytes;
  36906. int i;
  36907. int j;
  36908. XFILE f = XBADFILE;
  36909. DIST_POINT* dp;
  36910. DIST_POINT_NAME* dpn;
  36911. GENERAL_NAME* gn;
  36912. ASN1_IA5STRING* uri;
  36913. STACK_OF(DIST_POINT)* dps = NULL;
  36914. STACK_OF(GENERAL_NAME)* gns = NULL;
  36915. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36916. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  36917. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36918. if (f != XBADFILE)
  36919. XFCLOSE(f);
  36920. bufPt = buf;
  36921. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36922. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36923. NID_crl_distribution_points, NULL, NULL));
  36924. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  36925. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36926. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  36927. ExpectNotNull(dpn = dp->distpoint);
  36928. /* this should be type 0, fullname */
  36929. ExpectIntEQ(dpn->type, 0);
  36930. ExpectNotNull(gns = dp->distpoint->name.fullname);
  36931. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36932. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36933. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  36934. ExpectIntEQ(gn->type, GEN_URI);
  36935. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  36936. ExpectNotNull(uri->data);
  36937. ExpectIntGT(uri->length, 0);
  36938. }
  36939. }
  36940. X509_free(x509);
  36941. CRL_DIST_POINTS_free(dps);
  36942. #endif
  36943. return EXPECT_RESULT();
  36944. }
  36945. static int test_wolfSSL_MD4(void)
  36946. {
  36947. EXPECT_DECLS;
  36948. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36949. MD4_CTX md4;
  36950. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36951. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36952. "789012345678901234567890";
  36953. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36954. "\xcc\x05\x36";
  36955. int msgSz = (int)XSTRLEN(msg);
  36956. XMEMSET(out, 0, sizeof(out));
  36957. MD4_Init(&md4);
  36958. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36959. MD4_Final(out, &md4);
  36960. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36961. #endif
  36962. return EXPECT_RESULT();
  36963. }
  36964. static int test_wolfSSL_verify_mode(void)
  36965. {
  36966. EXPECT_DECLS;
  36967. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36968. WOLFSSL* ssl = NULL;
  36969. WOLFSSL_CTX* ctx = NULL;
  36970. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36971. ExpectNotNull(ssl = SSL_new(ctx));
  36972. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36973. SSL_free(ssl);
  36974. ssl = NULL;
  36975. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36976. ExpectNotNull(ssl = SSL_new(ctx));
  36977. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36978. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36979. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36980. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36981. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36982. SSL_free(ssl);
  36983. ssl = NULL;
  36984. wolfSSL_CTX_set_verify(ctx,
  36985. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36986. ExpectNotNull(ssl = SSL_new(ctx));
  36987. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36988. ExpectIntEQ(SSL_get_verify_mode(ssl),
  36989. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36990. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36991. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  36992. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36993. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36994. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36995. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36996. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36997. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36998. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36999. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  37000. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  37001. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  37002. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  37003. #endif
  37004. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  37005. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  37006. SSL_free(ssl);
  37007. SSL_CTX_free(ctx);
  37008. #endif
  37009. return EXPECT_RESULT();
  37010. }
  37011. static int test_wolfSSL_verify_depth(void)
  37012. {
  37013. EXPECT_DECLS;
  37014. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37015. WOLFSSL* ssl = NULL;
  37016. WOLFSSL_CTX* ctx = NULL;
  37017. long depth;
  37018. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37019. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  37020. ExpectNotNull(ssl = SSL_new(ctx));
  37021. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  37022. SSL_free(ssl);
  37023. ssl = NULL;
  37024. SSL_CTX_set_verify_depth(ctx, -1);
  37025. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  37026. SSL_CTX_set_verify_depth(ctx, 2);
  37027. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  37028. ExpectNotNull(ssl = SSL_new(ctx));
  37029. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  37030. SSL_free(ssl);
  37031. SSL_CTX_free(ctx);
  37032. #endif
  37033. return EXPECT_RESULT();
  37034. }
  37035. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37036. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  37037. * buffer of 64 bytes.
  37038. *
  37039. * returns the size of the digest buffer on success and a negative value on
  37040. * failure.
  37041. */
  37042. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  37043. int* sz)
  37044. {
  37045. EXPECT_DECLS;
  37046. HMAC_CTX ctx1;
  37047. HMAC_CTX ctx2;
  37048. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  37049. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  37050. unsigned char long_key[] =
  37051. "0123456789012345678901234567890123456789"
  37052. "0123456789012345678901234567890123456789"
  37053. "0123456789012345678901234567890123456789"
  37054. "0123456789012345678901234567890123456789";
  37055. unsigned char msg[] = "message to hash";
  37056. unsigned int digestSz = 64;
  37057. int keySz = sizeof(key);
  37058. int long_keySz = sizeof(long_key);
  37059. int msgSz = sizeof(msg);
  37060. unsigned char digest2[64];
  37061. unsigned int digestSz2 = 64;
  37062. HMAC_CTX_init(&ctx1);
  37063. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37064. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37065. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37066. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37067. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37068. HMAC_CTX_cleanup(&ctx1);
  37069. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37070. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  37071. HMAC_CTX_cleanup(&ctx2);
  37072. ExpectIntEQ(digestSz, digestSz2);
  37073. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37074. /* test HMAC_Init with NULL key */
  37075. /* init after copy */
  37076. HMAC_CTX_init(&ctx1);
  37077. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37078. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37079. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37080. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37081. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37082. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37083. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37084. HMAC_CTX_cleanup(&ctx1);
  37085. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37086. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37087. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37088. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37089. HMAC_CTX_cleanup(&ctx2);
  37090. ExpectIntEQ(digestSz, digestSz2);
  37091. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37092. /* long key */
  37093. HMAC_CTX_init(&ctx1);
  37094. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  37095. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37096. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37097. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37098. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37099. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37100. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37101. HMAC_CTX_cleanup(&ctx1);
  37102. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  37103. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37104. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37105. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37106. HMAC_CTX_cleanup(&ctx2);
  37107. ExpectIntEQ(digestSz, digestSz2);
  37108. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37109. /* init before copy */
  37110. HMAC_CTX_init(&ctx1);
  37111. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  37112. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37113. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  37114. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  37115. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37116. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  37117. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  37118. HMAC_CTX_cleanup(&ctx1);
  37119. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37120. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  37121. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  37122. HMAC_CTX_cleanup(&ctx2);
  37123. ExpectIntEQ(digestSz, digestSz2);
  37124. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  37125. *sz = digestSz;
  37126. return EXPECT_RESULT();
  37127. }
  37128. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  37129. static int test_wolfSSL_HMAC_CTX(void)
  37130. {
  37131. EXPECT_DECLS;
  37132. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  37133. unsigned char digest[64];
  37134. int digestSz;
  37135. #ifndef NO_SHA
  37136. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  37137. TEST_SUCCESS);
  37138. ExpectIntEQ(digestSz, 20);
  37139. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  37140. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  37141. #endif /* !NO_SHA */
  37142. #ifdef WOLFSSL_SHA224
  37143. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  37144. TEST_SUCCESS);
  37145. ExpectIntEQ(digestSz, 28);
  37146. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  37147. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  37148. "\x02\x0E", digest, digestSz), 0);
  37149. #endif /* WOLFSSL_SHA224 */
  37150. #ifndef NO_SHA256
  37151. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  37152. TEST_SUCCESS);
  37153. ExpectIntEQ(digestSz, 32);
  37154. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37155. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37156. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37157. #endif /* !NO_SHA256 */
  37158. #ifdef WOLFSSL_SHA384
  37159. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  37160. TEST_SUCCESS);
  37161. ExpectIntEQ(digestSz, 48);
  37162. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37163. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37164. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37165. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37166. digest, digestSz), 0);
  37167. #endif /* WOLFSSL_SHA384 */
  37168. #ifdef WOLFSSL_SHA512
  37169. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  37170. TEST_SUCCESS);
  37171. ExpectIntEQ(digestSz, 64);
  37172. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37173. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37174. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37175. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37176. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37177. digest, digestSz), 0);
  37178. #endif /* WOLFSSL_SHA512 */
  37179. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  37180. HAVE_FIPS_VERSION <= 2)
  37181. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  37182. TEST_SUCCESS);
  37183. ExpectIntEQ(digestSz, 16);
  37184. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37185. "\xE4\x98\xDD", digest, digestSz), 0);
  37186. #endif /* !NO_MD5 */
  37187. #endif
  37188. return EXPECT_RESULT();
  37189. }
  37190. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37191. static void sslMsgCb(int w, int version, int type, const void* buf,
  37192. size_t sz, SSL* ssl, void* arg)
  37193. {
  37194. int i;
  37195. unsigned char* pt = (unsigned char*)buf;
  37196. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  37197. (w)?"Writing":"Reading", (int)sz, version, type);
  37198. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  37199. fprintf(stderr, "\n");
  37200. (void)ssl;
  37201. (void)arg;
  37202. }
  37203. #endif /* OPENSSL_EXTRA */
  37204. static int test_wolfSSL_msg_callback(void)
  37205. {
  37206. EXPECT_DECLS;
  37207. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37208. WOLFSSL* ssl = NULL;
  37209. WOLFSSL_CTX* ctx = NULL;
  37210. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37211. ExpectNotNull(ssl = SSL_new(ctx));
  37212. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  37213. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  37214. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  37215. SSL_free(ssl);
  37216. SSL_CTX_free(ctx);
  37217. #endif
  37218. return EXPECT_RESULT();
  37219. }
  37220. static int test_wolfSSL_SHA(void)
  37221. {
  37222. EXPECT_DECLS;
  37223. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37224. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37225. (!defined(HAVE_FIPS) || \
  37226. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37227. {
  37228. const unsigned char in[] = "abc";
  37229. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37230. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37231. unsigned char out[WC_SHA_DIGEST_SIZE];
  37232. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37233. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37234. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37235. /* SHA interface test */
  37236. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37237. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37238. ExpectNotNull(SHA(in, 0, out));
  37239. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37240. ExpectNotNull(SHA(NULL, 0, out));
  37241. ExpectNotNull(SHA(NULL, 0, NULL));
  37242. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37243. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37244. }
  37245. #endif
  37246. #if !defined(NO_SHA256)
  37247. {
  37248. const unsigned char in[] = "abc";
  37249. unsigned char expected[] =
  37250. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37251. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37252. "\x15\xAD";
  37253. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37254. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37255. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37256. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37257. #else
  37258. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37259. #endif
  37260. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37261. }
  37262. #endif
  37263. #if defined(WOLFSSL_SHA384)
  37264. {
  37265. const unsigned char in[] = "abc";
  37266. unsigned char expected[] =
  37267. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37268. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37269. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37270. "\xc8\x25\xa7";
  37271. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37272. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37273. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37274. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37275. #else
  37276. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37277. #endif
  37278. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37279. }
  37280. #endif
  37281. #if defined(WOLFSSL_SHA512)
  37282. {
  37283. const unsigned char in[] = "abc";
  37284. unsigned char expected[] =
  37285. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37286. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37287. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37288. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37289. "\xa5\x4c\xa4\x9f";
  37290. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37291. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37292. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37293. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37294. #else
  37295. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37296. #endif
  37297. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37298. }
  37299. #endif
  37300. #endif
  37301. return EXPECT_RESULT();
  37302. }
  37303. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  37304. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37305. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37306. static void binary_dump(void *ptr, int size)
  37307. {
  37308. #ifdef WOLFSSL_EVP_PRINT
  37309. int i = 0;
  37310. unsigned char *p = (unsigned char *) ptr;
  37311. fprintf(stderr, "{");
  37312. while ((p != NULL) && (i < size)) {
  37313. if ((i % 8) == 0) {
  37314. fprintf(stderr, "\n");
  37315. fprintf(stderr, " ");
  37316. }
  37317. fprintf(stderr, "0x%02x, ", p[i]);
  37318. i++;
  37319. }
  37320. fprintf(stderr, "\n};\n");
  37321. #else
  37322. (void) ptr;
  37323. (void) size;
  37324. #endif
  37325. }
  37326. static int last_val = 0x0f;
  37327. static int check_result(unsigned char *data, int len)
  37328. {
  37329. int i;
  37330. for ( ; len; ) {
  37331. last_val = (last_val + 1) % 16;
  37332. for (i = 0; i < 16; len--, i++, data++)
  37333. if (*data != last_val) {
  37334. return -1;
  37335. }
  37336. }
  37337. return 0;
  37338. }
  37339. static int r_offset;
  37340. static int w_offset;
  37341. static void init_offset(void)
  37342. {
  37343. r_offset = 0;
  37344. w_offset = 0;
  37345. }
  37346. static void get_record(unsigned char *data, unsigned char *buf, int len)
  37347. {
  37348. XMEMCPY(buf, data+r_offset, len);
  37349. r_offset += len;
  37350. }
  37351. static void set_record(unsigned char *data, unsigned char *buf, int len)
  37352. {
  37353. XMEMCPY(data+w_offset, buf, len);
  37354. w_offset += len;
  37355. }
  37356. static void set_plain(unsigned char *plain, int rec)
  37357. {
  37358. int i, j;
  37359. unsigned char *p = plain;
  37360. #define BLOCKSZ 16
  37361. for (i=0; i<(rec/BLOCKSZ); i++) {
  37362. for (j=0; j<BLOCKSZ; j++)
  37363. *p++ = (i % 16);
  37364. }
  37365. }
  37366. #endif
  37367. static int test_wolfSSL_EVP_Cipher_extra(void)
  37368. {
  37369. EXPECT_DECLS;
  37370. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37371. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37372. /* aes128-cbc, keylen=16, ivlen=16 */
  37373. byte aes128_cbc_key[] = {
  37374. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37375. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37376. };
  37377. byte aes128_cbc_iv[] = {
  37378. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  37379. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  37380. };
  37381. /* teset data size table */
  37382. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  37383. int test_drive2[] = {8, 3, 8, 512, 0};
  37384. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  37385. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  37386. int test_drive_len[100];
  37387. int ret = 0;
  37388. EVP_CIPHER_CTX *evp = NULL;
  37389. int ilen = 0;
  37390. int klen = 0;
  37391. int i, j;
  37392. const EVP_CIPHER *type;
  37393. byte *iv;
  37394. byte *key;
  37395. int ivlen;
  37396. int keylen;
  37397. #define RECORDS 16
  37398. #define BUFFSZ 512
  37399. byte plain [BUFFSZ * RECORDS];
  37400. byte cipher[BUFFSZ * RECORDS];
  37401. byte inb[BUFFSZ];
  37402. byte outb[BUFFSZ+16];
  37403. int outl = 0;
  37404. int inl;
  37405. iv = aes128_cbc_iv;
  37406. ivlen = sizeof(aes128_cbc_iv);
  37407. key = aes128_cbc_key;
  37408. keylen = sizeof(aes128_cbc_key);
  37409. type = EVP_aes_128_cbc();
  37410. set_plain(plain, BUFFSZ * RECORDS);
  37411. SSL_library_init();
  37412. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  37413. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37414. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37415. klen = EVP_CIPHER_CTX_key_length(evp);
  37416. if (klen > 0 && keylen != klen) {
  37417. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37418. }
  37419. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37420. if (ilen > 0 && ivlen != ilen) {
  37421. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37422. }
  37423. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37424. for (j = 0; j<RECORDS; j++)
  37425. {
  37426. inl = BUFFSZ;
  37427. get_record(plain, inb, inl);
  37428. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  37429. set_record(cipher, outb, outl);
  37430. }
  37431. for (i = 0; test_drive[i]; i++) {
  37432. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37433. init_offset();
  37434. test_drive_len[i] = 0;
  37435. for (j = 0; test_drive[i][j]; j++)
  37436. {
  37437. inl = test_drive[i][j];
  37438. test_drive_len[i] += inl;
  37439. get_record(plain, inb, inl);
  37440. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  37441. 0);
  37442. /* output to cipher buffer, so that following Dec test can detect
  37443. if any error */
  37444. set_record(cipher, outb, outl);
  37445. }
  37446. EVP_CipherFinal(evp, outb, &outl);
  37447. if (outl > 0)
  37448. set_record(cipher, outb, outl);
  37449. }
  37450. for (i = 0; test_drive[i]; i++) {
  37451. last_val = 0x0f;
  37452. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  37453. init_offset();
  37454. for (j = 0; test_drive[i][j]; j++) {
  37455. inl = test_drive[i][j];
  37456. get_record(cipher, inb, inl);
  37457. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  37458. 0);
  37459. binary_dump(outb, outl);
  37460. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  37461. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  37462. }
  37463. ret = EVP_CipherFinal(evp, outb, &outl);
  37464. binary_dump(outb, outl);
  37465. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  37466. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  37467. ExpectTrue(ret);
  37468. }
  37469. EVP_CIPHER_CTX_free(evp);
  37470. evp = NULL;
  37471. /* Do an extra test to verify correct behavior with empty input. */
  37472. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  37473. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37474. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37475. klen = EVP_CIPHER_CTX_key_length(evp);
  37476. if (klen > 0 && keylen != klen) {
  37477. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37478. }
  37479. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37480. if (ilen > 0 && ivlen != ilen) {
  37481. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37482. }
  37483. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37484. /* outl should be set to 0 after passing NULL, 0 for input args. */
  37485. outl = -1;
  37486. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  37487. ExpectIntEQ(outl, 0);
  37488. EVP_CIPHER_CTX_free(evp);
  37489. #endif /* test_EVP_Cipher */
  37490. return EXPECT_RESULT();
  37491. }
  37492. static int test_wolfSSL_PEM_read_DHparams(void)
  37493. {
  37494. EXPECT_DECLS;
  37495. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  37496. !defined(NO_FILESYSTEM)
  37497. DH* dh = NULL;
  37498. XFILE fp = XBADFILE;
  37499. unsigned char derOut[300];
  37500. unsigned char* derOutBuf = derOut;
  37501. int derOutSz = 0;
  37502. unsigned char derExpected[300];
  37503. int derExpectedSz = 0;
  37504. XMEMSET(derOut, 0, sizeof(derOut));
  37505. XMEMSET(derExpected, 0, sizeof(derExpected));
  37506. /* open DH param file, read into DH struct */
  37507. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  37508. /* bad args */
  37509. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  37510. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  37511. /* good args */
  37512. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37513. if (fp != XBADFILE) {
  37514. XFCLOSE(fp);
  37515. fp = XBADFILE;
  37516. }
  37517. /* read in certs/dh2048.der for comparison against exported params */
  37518. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  37519. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  37520. fp), 0);
  37521. if (fp != XBADFILE) {
  37522. XFCLOSE(fp);
  37523. fp = XBADFILE;
  37524. }
  37525. /* export DH back to DER and compare */
  37526. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  37527. ExpectIntEQ(derOutSz, derExpectedSz);
  37528. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  37529. DH_free(dh);
  37530. dh = NULL;
  37531. /* Test parsing with X9.42 header */
  37532. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  37533. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37534. if (fp != XBADFILE)
  37535. XFCLOSE(fp);
  37536. DH_free(dh);
  37537. #endif
  37538. return EXPECT_RESULT();
  37539. }
  37540. static int test_wolfSSL_AES_ecb_encrypt(void)
  37541. {
  37542. EXPECT_DECLS;
  37543. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37544. AES_KEY aes;
  37545. const byte msg[] =
  37546. {
  37547. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37548. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37549. };
  37550. const byte verify[] =
  37551. {
  37552. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37553. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37554. };
  37555. const byte key[] =
  37556. {
  37557. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37558. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37559. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37560. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37561. };
  37562. byte out[AES_BLOCK_SIZE];
  37563. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37564. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37565. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37566. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37567. #ifdef HAVE_AES_DECRYPT
  37568. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37569. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37570. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37571. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37572. #endif
  37573. /* test bad arguments */
  37574. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37575. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37576. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37577. #endif
  37578. return EXPECT_RESULT();
  37579. }
  37580. static int test_wolfSSL_MD5(void)
  37581. {
  37582. EXPECT_DECLS;
  37583. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37584. byte input1[] = "";
  37585. byte input2[] = "message digest";
  37586. byte hash[WC_MD5_DIGEST_SIZE];
  37587. unsigned char output1[] =
  37588. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37589. unsigned char output2[] =
  37590. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37591. WOLFSSL_MD5_CTX md5;
  37592. XMEMSET(&md5, 0, sizeof(md5));
  37593. /* Test cases for illegal parameters */
  37594. ExpectIntEQ(MD5_Init(NULL), 0);
  37595. ExpectIntEQ(MD5_Init(&md5), 1);
  37596. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37597. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37598. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37599. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37600. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37601. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37602. /* Init MD5 CTX */
  37603. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37604. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37605. 1);
  37606. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37607. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37608. /* Init MD5 CTX */
  37609. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37610. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37611. (int)XSTRLEN((const char*)input2)), 1);
  37612. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37613. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37614. #if !defined(NO_OLD_NAMES) && \
  37615. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37616. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37617. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37618. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37619. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37620. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37621. &hash);
  37622. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37623. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37624. &hash);
  37625. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37626. {
  37627. byte data[] = "Data to be hashed.";
  37628. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37629. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37630. ExpectNotNull(MD5(data, sizeof(data), hash));
  37631. ExpectNotNull(MD5(NULL, 0, hash));
  37632. ExpectNull(MD5(NULL, sizeof(data), hash));
  37633. }
  37634. #endif
  37635. #endif
  37636. return EXPECT_RESULT();
  37637. }
  37638. static int test_wolfSSL_MD5_Transform(void)
  37639. {
  37640. EXPECT_DECLS;
  37641. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37642. byte input1[] = "";
  37643. byte input2[] = "abc";
  37644. byte local[WC_MD5_BLOCK_SIZE];
  37645. word32 sLen = 0;
  37646. #ifdef BIG_ENDIAN_ORDER
  37647. unsigned char output1[] =
  37648. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37649. unsigned char output2[] =
  37650. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37651. #else
  37652. unsigned char output1[] =
  37653. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37654. unsigned char output2[] =
  37655. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37656. #endif
  37657. union {
  37658. wc_Md5 native;
  37659. MD5_CTX compat;
  37660. } md5;
  37661. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37662. XMEMSET(&local, 0, sizeof(local));
  37663. /* sanity check */
  37664. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37665. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37666. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37667. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37668. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37669. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37670. /* Init MD5 CTX */
  37671. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37672. /* Do Transform*/
  37673. sLen = (word32)XSTRLEN((char*)input1);
  37674. XMEMCPY(local, input1, sLen);
  37675. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37676. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37677. /* Init MD5 CTX */
  37678. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37679. sLen = (word32)XSTRLEN((char*)input2);
  37680. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37681. XMEMCPY(local, input2, sLen);
  37682. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37683. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37684. #endif
  37685. return EXPECT_RESULT();
  37686. }
  37687. static int test_wolfSSL_SHA224(void)
  37688. {
  37689. EXPECT_DECLS;
  37690. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37691. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37692. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37693. unsigned char input[] =
  37694. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37695. unsigned char output[] =
  37696. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37697. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37698. size_t inLen;
  37699. byte hash[WC_SHA224_DIGEST_SIZE];
  37700. inLen = XSTRLEN((char*)input);
  37701. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37702. ExpectNull(SHA224(NULL, inLen, hash));
  37703. ExpectNotNull(SHA224(input, 0, hash));
  37704. ExpectNotNull(SHA224(input, inLen, NULL));
  37705. ExpectNotNull(SHA224(NULL, 0, hash));
  37706. ExpectNotNull(SHA224(NULL, 0, NULL));
  37707. ExpectNotNull(SHA224(input, inLen, hash));
  37708. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37709. #endif
  37710. return EXPECT_RESULT();
  37711. }
  37712. static int test_wolfSSL_SHA_Transform(void)
  37713. {
  37714. EXPECT_DECLS;
  37715. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37716. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37717. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37718. byte input1[] = "";
  37719. byte input2[] = "abc";
  37720. byte local[WC_SHA_BLOCK_SIZE];
  37721. word32 sLen = 0;
  37722. #ifdef BIG_ENDIAN_ORDER
  37723. unsigned char output1[] =
  37724. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37725. "\x09\xf7\x3a\x93";
  37726. unsigned char output2[] =
  37727. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37728. "\x7c\x6e\x08\xb8";
  37729. #else
  37730. unsigned char output1[] =
  37731. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37732. "\x93\x3a\xf7\x09";
  37733. unsigned char output2[] =
  37734. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37735. "\xb8\x08\x6e\x7c";
  37736. #endif
  37737. union {
  37738. wc_Sha native;
  37739. SHA_CTX compat;
  37740. } sha;
  37741. union {
  37742. wc_Sha native;
  37743. SHA_CTX compat;
  37744. } sha1;
  37745. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37746. XMEMSET(&local, 0, sizeof(local));
  37747. /* sanity check */
  37748. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37749. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37750. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37751. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37752. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37753. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37754. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37755. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37756. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37757. /* Init SHA CTX */
  37758. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37759. /* Do Transform*/
  37760. sLen = (word32)XSTRLEN((char*)input1);
  37761. XMEMCPY(local, input1, sLen);
  37762. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37763. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37764. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37765. /* Init SHA CTX */
  37766. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37767. sLen = (word32)XSTRLEN((char*)input2);
  37768. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37769. XMEMCPY(local, input2, sLen);
  37770. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37771. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37772. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37773. /* SHA1 */
  37774. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37775. /* Init SHA CTX */
  37776. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37777. /* Do Transform*/
  37778. sLen = (word32)XSTRLEN((char*)input1);
  37779. XMEMCPY(local, input1, sLen);
  37780. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37781. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37782. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37783. /* Init SHA CTX */
  37784. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37785. sLen = (word32)XSTRLEN((char*)input2);
  37786. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37787. XMEMCPY(local, input2, sLen);
  37788. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37789. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37790. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37791. #endif
  37792. #endif
  37793. return EXPECT_RESULT();
  37794. }
  37795. static int test_wolfSSL_SHA256_Transform(void)
  37796. {
  37797. EXPECT_DECLS;
  37798. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37799. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37800. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37801. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37802. !defined(WOLFSSL_KCAPI_HASH)
  37803. byte input1[] = "";
  37804. byte input2[] = "abc";
  37805. byte local[WC_SHA256_BLOCK_SIZE];
  37806. word32 sLen = 0;
  37807. #ifdef BIG_ENDIAN_ORDER
  37808. unsigned char output1[] =
  37809. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37810. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37811. unsigned char output2[] =
  37812. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37813. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37814. #else
  37815. unsigned char output1[] =
  37816. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37817. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37818. unsigned char output2[] =
  37819. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37820. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37821. #endif
  37822. union {
  37823. wc_Sha256 native;
  37824. SHA256_CTX compat;
  37825. } sha256;
  37826. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37827. XMEMSET(&local, 0, sizeof(local));
  37828. /* sanity check */
  37829. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37830. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37831. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37832. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37833. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37834. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37835. /* Init SHA256 CTX */
  37836. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37837. /* Do Transform*/
  37838. sLen = (word32)XSTRLEN((char*)input1);
  37839. XMEMCPY(local, input1, sLen);
  37840. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37841. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37842. 0);
  37843. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37844. /* Init SHA256 CTX */
  37845. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37846. sLen = (word32)XSTRLEN((char*)input2);
  37847. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37848. XMEMCPY(local, input2, sLen);
  37849. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37850. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37851. 0);
  37852. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37853. #endif
  37854. #endif
  37855. return EXPECT_RESULT();
  37856. }
  37857. static int test_wolfSSL_SHA256(void)
  37858. {
  37859. EXPECT_DECLS;
  37860. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37861. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37862. unsigned char input[] =
  37863. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37864. unsigned char output[] =
  37865. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37866. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37867. "\x06\xC1";
  37868. size_t inLen;
  37869. byte hash[WC_SHA256_DIGEST_SIZE];
  37870. inLen = XSTRLEN((char*)input);
  37871. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37872. ExpectNotNull(SHA256(input, inLen, hash));
  37873. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37874. #endif
  37875. return EXPECT_RESULT();
  37876. }
  37877. static int test_wolfSSL_SHA512_Transform(void)
  37878. {
  37879. EXPECT_DECLS;
  37880. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37881. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37882. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37883. !defined(WOLFSSL_KCAPI_HASH)
  37884. byte input1[] = "";
  37885. byte input2[] = "abc";
  37886. byte local[WC_SHA512_BLOCK_SIZE];
  37887. word32 sLen = 0;
  37888. #ifdef BIG_ENDIAN_ORDER
  37889. unsigned char output1[] =
  37890. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37891. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37892. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37893. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37894. unsigned char output2[] =
  37895. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37896. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37897. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37898. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37899. #else
  37900. unsigned char output1[] =
  37901. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37902. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37903. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37904. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37905. unsigned char output2[] =
  37906. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37907. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37908. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37909. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37910. #endif
  37911. union {
  37912. wc_Sha512 native;
  37913. SHA512_CTX compat;
  37914. } sha512;
  37915. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37916. XMEMSET(&local, 0, sizeof(local));
  37917. /* sanity check */
  37918. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37919. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37920. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37921. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37922. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37923. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37924. /* Init SHA512 CTX */
  37925. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37926. /* Do Transform*/
  37927. sLen = (word32)XSTRLEN((char*)input1);
  37928. XMEMCPY(local, input1, sLen);
  37929. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37930. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37931. WC_SHA512_DIGEST_SIZE), 0);
  37932. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37933. /* Init SHA512 CTX */
  37934. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37935. sLen = (word32)XSTRLEN((char*)input2);
  37936. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37937. XMEMCPY(local, input2, sLen);
  37938. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37939. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37940. WC_SHA512_DIGEST_SIZE), 0);
  37941. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37942. (void)input1;
  37943. #endif
  37944. #endif
  37945. return EXPECT_RESULT();
  37946. }
  37947. static int test_wolfSSL_X509_get_serialNumber(void)
  37948. {
  37949. EXPECT_DECLS;
  37950. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37951. ASN1_INTEGER* a = NULL;
  37952. BIGNUM* bn = NULL;
  37953. X509* x509 = NULL;
  37954. char *serialHex = NULL;
  37955. byte serial[3];
  37956. int serialSz;
  37957. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37958. SSL_FILETYPE_PEM));
  37959. ExpectNotNull(a = X509_get_serialNumber(x509));
  37960. /* check on value of ASN1 Integer */
  37961. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37962. a = NULL;
  37963. /* test setting serial number and then retrieving it */
  37964. ExpectNotNull(a = ASN1_INTEGER_new());
  37965. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  37966. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37967. serialSz = sizeof(serial);
  37968. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37969. WOLFSSL_SUCCESS);
  37970. ExpectIntEQ(serialSz, 1);
  37971. ExpectIntEQ(serial[0], 3);
  37972. ASN1_INTEGER_free(a);
  37973. a = NULL;
  37974. /* test setting serial number with 0's in it */
  37975. serial[0] = 0x01;
  37976. serial[1] = 0x00;
  37977. serial[2] = 0x02;
  37978. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37979. if (a != NULL) {
  37980. a->data[0] = ASN_INTEGER;
  37981. a->data[1] = sizeof(serial);
  37982. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37983. a->length = sizeof(serial) + 2;
  37984. }
  37985. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37986. XMEMSET(serial, 0, sizeof(serial));
  37987. serialSz = sizeof(serial);
  37988. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37989. WOLFSSL_SUCCESS);
  37990. ExpectIntEQ(serialSz, 3);
  37991. ExpectIntEQ(serial[0], 0x01);
  37992. ExpectIntEQ(serial[1], 0x00);
  37993. ExpectIntEQ(serial[2], 0x02);
  37994. ASN1_INTEGER_free(a);
  37995. a = NULL;
  37996. X509_free(x509); /* free's a */
  37997. ExpectNotNull(serialHex = BN_bn2hex(bn));
  37998. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37999. ExpectStrEQ(serialHex, "01");
  38000. #else
  38001. ExpectStrEQ(serialHex, "1");
  38002. #endif
  38003. OPENSSL_free(serialHex);
  38004. ExpectIntEQ(BN_get_word(bn), 1);
  38005. BN_free(bn);
  38006. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  38007. ExpectNotNull(a = ASN1_INTEGER_new());
  38008. if (a != NULL) {
  38009. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  38010. DYNAMIC_TYPE_OPENSSL));
  38011. a->isDynamic = 1;
  38012. ASN1_INTEGER_free(a);
  38013. }
  38014. #endif
  38015. return EXPECT_RESULT();
  38016. }
  38017. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  38018. {
  38019. EXPECT_DECLS;
  38020. #if defined(OPENSSL_EXTRA)
  38021. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  38022. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  38023. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  38024. #endif
  38025. return EXPECT_RESULT();
  38026. }
  38027. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  38028. {
  38029. EXPECT_DECLS;
  38030. #if defined(OPENSSL_EXTRA)
  38031. #define MAX_HEXSTR_BUFSZ 9
  38032. #define NUM_CASES 5
  38033. struct Output {
  38034. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  38035. long ret;
  38036. };
  38037. int i;
  38038. int j;
  38039. const char* inputs[NUM_CASES] = {
  38040. "aabcd1357e",
  38041. "01:12:23:34:a5:b6:c7:d8:e9",
  38042. ":01:02",
  38043. "012",
  38044. ":ab:ac:d"
  38045. };
  38046. struct Output expectedOutputs[NUM_CASES] = {
  38047. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  38048. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  38049. {{0x01, 0x02}, 2},
  38050. {{0x00}, 0},
  38051. {{0x00}, 0}
  38052. };
  38053. long len = 0;
  38054. unsigned char* returnedBuf = NULL;
  38055. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  38056. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  38057. if (returnedBuf == NULL) {
  38058. ExpectIntEQ(expectedOutputs[i].ret, 0);
  38059. continue;
  38060. }
  38061. ExpectIntEQ(expectedOutputs[i].ret, len);
  38062. for (j = 0; j < len; ++j) {
  38063. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  38064. }
  38065. OPENSSL_free(returnedBuf);
  38066. returnedBuf = NULL;
  38067. }
  38068. #endif
  38069. return EXPECT_RESULT();
  38070. }
  38071. static int test_wolfSSL_X509_CA_num(void)
  38072. {
  38073. EXPECT_DECLS;
  38074. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38075. defined(HAVE_ECC) && !defined(NO_RSA)
  38076. WOLFSSL_X509_STORE *store = NULL;
  38077. WOLFSSL_X509 *x509_1 = NULL;
  38078. WOLFSSL_X509 *x509_2 = NULL;
  38079. int ca_num = 0;
  38080. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  38081. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38082. WOLFSSL_FILETYPE_PEM));
  38083. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  38084. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  38085. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  38086. WOLFSSL_FILETYPE_PEM));
  38087. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  38088. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  38089. wolfSSL_X509_free(x509_1);
  38090. wolfSSL_X509_free(x509_2);
  38091. wolfSSL_X509_STORE_free(store);
  38092. #endif
  38093. return EXPECT_RESULT();
  38094. }
  38095. static int test_wolfSSL_X509_check_ca(void)
  38096. {
  38097. EXPECT_DECLS;
  38098. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  38099. WOLFSSL_X509 *x509 = NULL;
  38100. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38101. WOLFSSL_FILETYPE_PEM));
  38102. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  38103. wolfSSL_X509_free(x509);
  38104. #endif
  38105. return EXPECT_RESULT();
  38106. }
  38107. static int test_wolfSSL_X509_check_ip_asc(void)
  38108. {
  38109. EXPECT_DECLS;
  38110. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  38111. WOLFSSL_X509 *x509 = NULL;
  38112. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  38113. WOLFSSL_FILETYPE_PEM));
  38114. #if 0
  38115. /* TODO: add cert gen for testing positive case */
  38116. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  38117. #endif
  38118. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  38119. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  38120. wolfSSL_X509_free(x509);
  38121. #endif
  38122. return EXPECT_RESULT();
  38123. }
  38124. static int test_wolfSSL_make_cert(void)
  38125. {
  38126. EXPECT_DECLS;
  38127. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  38128. defined(WOLFSSL_CERT_EXT)
  38129. int ret;
  38130. Cert cert;
  38131. CertName name;
  38132. RsaKey key;
  38133. WC_RNG rng;
  38134. byte der[FOURK_BUF];
  38135. word32 idx;
  38136. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  38137. #ifdef OPENSSL_EXTRA
  38138. const unsigned char* pt;
  38139. int certSz;
  38140. X509* x509 = NULL;
  38141. X509_NAME* x509name;
  38142. X509_NAME_ENTRY* entry;
  38143. ASN1_STRING* entryValue;
  38144. #endif
  38145. XMEMSET(&name, 0, sizeof(CertName));
  38146. /* set up cert name */
  38147. XMEMCPY(name.country, "US", sizeof("US"));
  38148. name.countryEnc = CTC_PRINTABLE;
  38149. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  38150. name.stateEnc = CTC_UTF8;
  38151. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  38152. name.localityEnc = CTC_UTF8;
  38153. XMEMCPY(name.sur, "Test", sizeof("Test"));
  38154. name.surEnc = CTC_UTF8;
  38155. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  38156. name.orgEnc = CTC_UTF8;
  38157. XMEMCPY(name.unit, "Development", sizeof("Development"));
  38158. name.unitEnc = CTC_UTF8;
  38159. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  38160. name.commonNameEnc = CTC_UTF8;
  38161. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  38162. name.serialDevEnc = CTC_PRINTABLE;
  38163. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  38164. name.userIdEnc = CTC_PRINTABLE;
  38165. #ifdef WOLFSSL_MULTI_ATTRIB
  38166. #if CTC_MAX_ATTRIB > 2
  38167. {
  38168. NameAttrib* n;
  38169. n = &name.name[0];
  38170. n->id = ASN_DOMAIN_COMPONENT;
  38171. n->type = CTC_UTF8;
  38172. n->sz = sizeof("com");
  38173. XMEMCPY(n->value, "com", sizeof("com"));
  38174. n = &name.name[1];
  38175. n->id = ASN_DOMAIN_COMPONENT;
  38176. n->type = CTC_UTF8;
  38177. n->sz = sizeof("wolfssl");
  38178. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  38179. }
  38180. #endif
  38181. #endif /* WOLFSSL_MULTI_ATTRIB */
  38182. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  38183. #ifndef HAVE_FIPS
  38184. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  38185. #else
  38186. ExpectIntEQ(wc_InitRng(&rng), 0);
  38187. #endif
  38188. /* load test RSA key */
  38189. idx = 0;
  38190. #if defined(USE_CERT_BUFFERS_1024)
  38191. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  38192. sizeof_server_key_der_1024), 0);
  38193. #elif defined(USE_CERT_BUFFERS_2048)
  38194. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  38195. sizeof_server_key_der_2048), 0);
  38196. #else
  38197. /* error case, no RSA key loaded, happens later */
  38198. (void)idx;
  38199. #endif
  38200. XMEMSET(&cert, 0 , sizeof(Cert));
  38201. ExpectIntEQ(wc_InitCert(&cert), 0);
  38202. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  38203. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  38204. cert.serialSz = (int)sizeof(mySerial);
  38205. cert.isCA = 1;
  38206. #ifndef NO_SHA256
  38207. cert.sigType = CTC_SHA256wRSA;
  38208. #else
  38209. cert.sigType = CTC_SHAwRSA;
  38210. #endif
  38211. /* add SKID from the Public Key */
  38212. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38213. /* add AKID from the Public Key */
  38214. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38215. ret = 0;
  38216. do {
  38217. #if defined(WOLFSSL_ASYNC_CRYPT)
  38218. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  38219. #endif
  38220. if (ret >= 0) {
  38221. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  38222. }
  38223. } while (ret == WC_PENDING_E);
  38224. ExpectIntGT(ret, 0);
  38225. #ifdef OPENSSL_EXTRA
  38226. /* der holds a certificate with DC's now check X509 parsing of it */
  38227. certSz = ret;
  38228. pt = der;
  38229. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  38230. ExpectNotNull(x509name = X509_get_subject_name(x509));
  38231. #ifdef WOLFSSL_MULTI_ATTRIB
  38232. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38233. -1)), 5);
  38234. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38235. idx)), 6);
  38236. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38237. idx)), -1);
  38238. #endif /* WOLFSSL_MULTI_ATTRIB */
  38239. /* compare DN at index 0 */
  38240. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  38241. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38242. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  38243. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  38244. #ifndef WOLFSSL_MULTI_ATTRIB
  38245. /* compare Serial Number */
  38246. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  38247. -1)), 7);
  38248. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38249. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38250. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  38251. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  38252. #endif
  38253. #ifdef WOLFSSL_MULTI_ATTRIB
  38254. /* get first and second DC and compare result */
  38255. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38256. -1)), 5);
  38257. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38258. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38259. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  38260. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38261. idx)), 6);
  38262. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38263. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38264. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  38265. #endif /* WOLFSSL_MULTI_ATTRIB */
  38266. /* try invalid index locations for regression test and sanity check */
  38267. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  38268. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  38269. X509_free(x509);
  38270. #endif /* OPENSSL_EXTRA */
  38271. wc_FreeRsaKey(&key);
  38272. wc_FreeRng(&rng);
  38273. #endif
  38274. return EXPECT_RESULT();
  38275. }
  38276. static int test_wolfSSL_X509_get_version(void)
  38277. {
  38278. EXPECT_DECLS;
  38279. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38280. WOLFSSL_X509 *x509 = NULL;
  38281. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38282. WOLFSSL_FILETYPE_PEM));
  38283. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  38284. wolfSSL_X509_free(x509);
  38285. #endif
  38286. return EXPECT_RESULT();
  38287. }
  38288. static int test_wolfSSL_DES_ncbc(void)
  38289. {
  38290. EXPECT_DECLS;
  38291. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38292. const_DES_cblock myDes;
  38293. DES_cblock iv = {1};
  38294. DES_key_schedule key = {0};
  38295. unsigned char msg[] = "hello wolfssl";
  38296. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38297. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38298. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38299. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38300. /* partial block test */
  38301. DES_set_key(&key, &myDes);
  38302. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38303. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38304. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38305. DES_set_key(&key, &myDes);
  38306. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38307. *((byte*)&iv) = 1;
  38308. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38309. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38310. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38311. /* full block test */
  38312. DES_set_key(&key, &myDes);
  38313. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38314. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38315. *((byte*)&iv) = 1;
  38316. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38317. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38318. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38319. DES_set_key(&key, &myDes);
  38320. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38321. *((byte*)&iv) = 1;
  38322. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38323. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38324. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38325. #endif
  38326. return EXPECT_RESULT();
  38327. }
  38328. static int test_wolfSSL_AES_cbc_encrypt(void)
  38329. {
  38330. EXPECT_DECLS;
  38331. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  38332. AES_KEY aes;
  38333. AES_KEY* aesN = NULL;
  38334. size_t len = 0;
  38335. size_t lenB = 0;
  38336. int keySz0 = 0;
  38337. int keySzN = -1;
  38338. byte out[AES_BLOCK_SIZE] = {0};
  38339. byte* outN = NULL;
  38340. /* Test vectors retrieved from:
  38341. * <begin URL>
  38342. * https://csrc.nist.gov/
  38343. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38344. * documents/aes/KAT_AES.zip
  38345. * </end URL>
  38346. */
  38347. const byte* pt128N = NULL;
  38348. byte* key128N = NULL;
  38349. byte* iv128N = NULL;
  38350. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38351. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38352. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38353. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38354. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38355. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38356. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38357. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38358. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38359. len = sizeof(pt128);
  38360. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38361. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38362. ExpectIntNE(XMEMCMP(b, g, h), i)
  38363. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38364. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38365. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38366. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38367. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38368. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38369. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38370. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38371. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38372. /* Stressing wolfSSL_AES_set_encrypt_key */
  38373. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38374. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38375. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38376. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38377. /* Stressing wolfSSL_AES_set_decrypt_key */
  38378. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38379. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38380. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38381. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38382. #ifdef WOLFSSL_AES_128
  38383. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38384. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38385. RESET_IV(iv128tmp, iv128);
  38386. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38387. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38388. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38389. wc_AesFree((Aes*)&aes);
  38390. #ifdef HAVE_AES_DECRYPT
  38391. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38392. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38393. RESET_IV(iv128tmp, iv128);
  38394. len = sizeof(ct128);
  38395. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38396. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38397. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38398. wc_AesFree((Aes*)&aes);
  38399. #endif
  38400. #endif /* WOLFSSL_AES_128 */
  38401. #ifdef WOLFSSL_AES_192
  38402. {
  38403. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38404. * Appendix F.2.3 */
  38405. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38406. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38407. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38408. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38409. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38410. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38411. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38412. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38413. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38414. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38415. len = sizeof(pt192);
  38416. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38417. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38418. RESET_IV(iv192tmp, iv192);
  38419. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38420. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38421. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38422. wc_AesFree((Aes*)&aes);
  38423. #ifdef HAVE_AES_DECRYPT
  38424. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38425. len = sizeof(ct192);
  38426. RESET_IV(iv192tmp, iv192);
  38427. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38428. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38429. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38430. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38431. wc_AesFree((Aes*)&aes);
  38432. #endif
  38433. }
  38434. #endif /* WOLFSSL_AES_192 */
  38435. #ifdef WOLFSSL_AES_256
  38436. {
  38437. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38438. * Appendix F.2.5 */
  38439. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38440. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38441. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38442. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38443. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38444. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38445. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38446. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38447. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38448. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38449. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38450. len = sizeof(pt256);
  38451. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38452. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38453. RESET_IV(iv256tmp, iv256);
  38454. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38455. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38456. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38457. wc_AesFree((Aes*)&aes);
  38458. #ifdef HAVE_AES_DECRYPT
  38459. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38460. len = sizeof(ct256);
  38461. RESET_IV(iv256tmp, iv256);
  38462. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38463. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38464. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38465. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38466. wc_AesFree((Aes*)&aes);
  38467. #endif
  38468. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38469. {
  38470. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38471. byte wrapPlain[sizeof(key256)] = { 0 };
  38472. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38473. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38474. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38475. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38476. 15), WOLFSSL_FAILURE);
  38477. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38478. sizeof(key256)), sizeof(wrapCipher));
  38479. wc_AesFree((Aes*)&aes);
  38480. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38481. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38482. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38483. 23), WOLFSSL_FAILURE);
  38484. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38485. sizeof(wrapCipher)), sizeof(wrapPlain));
  38486. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38487. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38488. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38489. wc_AesFree((Aes*)&aes);
  38490. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38491. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38492. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38493. sizeof(key256)), sizeof(wrapCipher));
  38494. wc_AesFree((Aes*)&aes);
  38495. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38496. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38497. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38498. sizeof(wrapCipher)), sizeof(wrapPlain));
  38499. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38500. wc_AesFree((Aes*)&aes);
  38501. }
  38502. #endif /* HAVE_AES_KEYWRAP */
  38503. }
  38504. #endif /* WOLFSSL_AES_256 */
  38505. #endif
  38506. return EXPECT_RESULT();
  38507. }
  38508. static int test_wolfSSL_CRYPTO_cts128(void)
  38509. {
  38510. EXPECT_DECLS;
  38511. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38512. defined(HAVE_CTS)
  38513. byte tmp[64]; /* Largest vector size */
  38514. /* Test vectors taken form RFC3962 Appendix B */
  38515. const testVector vects[] = {
  38516. {
  38517. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38518. "\x20",
  38519. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38520. "\x97",
  38521. 17, 17
  38522. },
  38523. {
  38524. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38525. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38526. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38527. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38528. 31, 31
  38529. },
  38530. {
  38531. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38532. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38533. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38534. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38535. 32, 32
  38536. },
  38537. {
  38538. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38539. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38540. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38541. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38542. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38543. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38544. 47, 47
  38545. },
  38546. {
  38547. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38548. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38549. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38550. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38551. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38552. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38553. 48, 48
  38554. },
  38555. {
  38556. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38557. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38558. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38559. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38560. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38561. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38562. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38563. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38564. 64, 64
  38565. }
  38566. };
  38567. byte keyBytes[AES_128_KEY_SIZE] = {
  38568. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38569. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38570. };
  38571. size_t i;
  38572. XMEMSET(tmp, 0, sizeof(tmp));
  38573. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38574. AES_KEY encKey;
  38575. AES_KEY decKey;
  38576. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38577. XMEMSET(iv, 0, sizeof(iv));
  38578. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38579. &encKey), 0);
  38580. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38581. &decKey), 0);
  38582. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38583. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38584. vects[i].outLen);
  38585. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38586. XMEMSET(iv, 0, sizeof(iv));
  38587. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38588. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38589. vects[i].inLen);
  38590. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38591. }
  38592. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38593. return EXPECT_RESULT();
  38594. }
  38595. #if defined(OPENSSL_ALL)
  38596. static int test_wolfSSL_sk_CIPHER_description(void)
  38597. {
  38598. EXPECT_DECLS;
  38599. #if !defined(NO_RSA)
  38600. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38601. int i;
  38602. int numCiphers = 0;
  38603. const SSL_METHOD *method = NULL;
  38604. const SSL_CIPHER *cipher = NULL;
  38605. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38606. SSL_CTX *ctx = NULL;
  38607. SSL *ssl = NULL;
  38608. char buf[256];
  38609. char test_str[9] = "0000000";
  38610. const char badStr[] = "unknown";
  38611. const char certPath[] = "./certs/client-cert.pem";
  38612. XMEMSET(buf, 0, sizeof(buf));
  38613. ExpectNotNull(method = TLSv1_2_client_method());
  38614. ExpectNotNull(ctx = SSL_CTX_new(method));
  38615. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38616. SSL_CTX_set_verify_depth(ctx, 4);
  38617. SSL_CTX_set_options(ctx, flags);
  38618. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38619. WOLFSSL_SUCCESS);
  38620. ExpectNotNull(ssl = SSL_new(ctx));
  38621. /* SSL_get_ciphers returns a stack of all configured ciphers
  38622. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38623. */
  38624. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38625. /* loop through the amount of supportedCiphers */
  38626. numCiphers = sk_num(supportedCiphers);
  38627. for (i = 0; i < numCiphers; ++i) {
  38628. int j;
  38629. /* sk_value increments "sk->data.cipher->cipherOffset".
  38630. * wolfSSL_sk_CIPHER_description sets the description for
  38631. * the cipher based on the provided offset.
  38632. */
  38633. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38634. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38635. }
  38636. /* Search cipher description string for "unknown" descriptor */
  38637. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38638. int k = 0;
  38639. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38640. test_str[k] = badStr[k];
  38641. j++;
  38642. k++;
  38643. }
  38644. }
  38645. /* Fail if test_str == badStr == "unknown" */
  38646. ExpectStrNE(test_str,badStr);
  38647. }
  38648. SSL_free(ssl);
  38649. SSL_CTX_free(ctx);
  38650. #endif
  38651. return EXPECT_RESULT();
  38652. }
  38653. static int test_wolfSSL_get_ciphers_compat(void)
  38654. {
  38655. EXPECT_DECLS;
  38656. #if !defined(NO_RSA)
  38657. const SSL_METHOD *method = NULL;
  38658. const char certPath[] = "./certs/client-cert.pem";
  38659. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38660. SSL_CTX *ctx = NULL;
  38661. WOLFSSL *ssl = NULL;
  38662. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38663. ExpectNotNull(method = SSLv23_client_method());
  38664. ExpectNotNull(ctx = SSL_CTX_new(method));
  38665. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38666. SSL_CTX_set_verify_depth(ctx, 4);
  38667. SSL_CTX_set_options(ctx, flags);
  38668. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38669. WOLFSSL_SUCCESS);
  38670. ExpectNotNull(ssl = SSL_new(ctx));
  38671. /* Test Bad NULL input */
  38672. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  38673. /* Test for Good input */
  38674. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38675. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38676. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38677. SSL_free(ssl);
  38678. SSL_CTX_free(ctx);
  38679. #endif
  38680. return EXPECT_RESULT();
  38681. }
  38682. static int test_wolfSSL_X509_PUBKEY_get(void)
  38683. {
  38684. EXPECT_DECLS;
  38685. WOLFSSL_X509_PUBKEY pubkey;
  38686. WOLFSSL_X509_PUBKEY* key;
  38687. WOLFSSL_EVP_PKEY evpkey ;
  38688. WOLFSSL_EVP_PKEY* evpPkey;
  38689. WOLFSSL_EVP_PKEY* retEvpPkey;
  38690. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38691. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38692. key = &pubkey;
  38693. evpPkey = &evpkey;
  38694. evpPkey->type = WOLFSSL_SUCCESS;
  38695. key->pkey = evpPkey;
  38696. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38697. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38698. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38699. key->pkey = NULL;
  38700. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38701. return EXPECT_RESULT();
  38702. }
  38703. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38704. {
  38705. EXPECT_DECLS;
  38706. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38707. DSA *dsa = NULL;
  38708. DSA *setDsa = NULL;
  38709. EVP_PKEY *pkey = NULL;
  38710. EVP_PKEY *set1Pkey = NULL;
  38711. SHA_CTX sha;
  38712. byte signature[DSA_SIG_SIZE];
  38713. byte hash[WC_SHA_DIGEST_SIZE];
  38714. word32 bytes;
  38715. int answer;
  38716. #ifdef USE_CERT_BUFFERS_1024
  38717. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38718. int dsaKeySz = sizeof_dsa_key_der_1024;
  38719. byte tmp[ONEK_BUF];
  38720. XMEMSET(tmp, 0, sizeof(tmp));
  38721. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38722. bytes = dsaKeySz;
  38723. #elif defined(USE_CERT_BUFFERS_2048)
  38724. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38725. int dsaKeySz = sizeof_dsa_key_der_2048;
  38726. byte tmp[TWOK_BUF];
  38727. XMEMSET(tmp, 0, sizeof(tmp));
  38728. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38729. bytes = dsaKeySz;
  38730. #else
  38731. byte tmp[TWOK_BUF];
  38732. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38733. int dsaKeySz;
  38734. XFILE fp = XBADFILE;
  38735. XMEMSET(tmp, 0, sizeof(tmp));
  38736. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  38737. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  38738. if (fp != XBADFILE)
  38739. XFCLOSE(fp);
  38740. #endif /* END USE_CERT_BUFFERS_1024 */
  38741. /* Create hash to later Sign and Verify */
  38742. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38743. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38744. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38745. /* Initialize pkey with der format dsa key */
  38746. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  38747. (long)dsaKeySz));
  38748. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38749. /* Should Fail: NULL argument */
  38750. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38751. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38752. /* Should Pass: Initialized pkey argument */
  38753. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38754. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38755. #ifdef USE_CERT_BUFFERS_1024
  38756. ExpectIntEQ(DSA_bits(dsa), 1024);
  38757. #else
  38758. ExpectIntEQ(DSA_bits(dsa), 2048);
  38759. #endif
  38760. /* Sign */
  38761. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38762. /* Verify. */
  38763. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38764. WOLFSSL_SUCCESS);
  38765. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38766. /* Should Fail: set1Pkey not initialized */
  38767. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38768. /* Initialize set1Pkey */
  38769. set1Pkey = EVP_PKEY_new();
  38770. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38771. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38772. WOLFSSL_SUCCESS);
  38773. /* Should Pass: set dsa into set1Pkey */
  38774. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38775. DSA_free(dsa);
  38776. DSA_free(setDsa);
  38777. EVP_PKEY_free(pkey);
  38778. EVP_PKEY_free(set1Pkey);
  38779. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38780. return EXPECT_RESULT();
  38781. } /* END test_EVP_PKEY_set1_get1_DSA */
  38782. static int test_wolfSSL_DSA_generate_parameters(void)
  38783. {
  38784. EXPECT_DECLS;
  38785. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38786. !defined(HAVE_FIPS)
  38787. DSA *dsa = NULL;
  38788. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  38789. NULL));
  38790. DSA_free(dsa);
  38791. #endif
  38792. return EXPECT_RESULT();
  38793. }
  38794. static int test_wolfSSL_DSA_SIG(void)
  38795. {
  38796. EXPECT_DECLS;
  38797. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38798. !defined(HAVE_FIPS)
  38799. DSA *dsa = NULL;
  38800. DSA *dsa2 = NULL;
  38801. DSA_SIG *sig = NULL;
  38802. const BIGNUM *p = NULL;
  38803. const BIGNUM *q = NULL;
  38804. const BIGNUM *g = NULL;
  38805. const BIGNUM *pub = NULL;
  38806. const BIGNUM *priv = NULL;
  38807. BIGNUM *dup_p = NULL;
  38808. BIGNUM *dup_q = NULL;
  38809. BIGNUM *dup_g = NULL;
  38810. BIGNUM *dup_pub = NULL;
  38811. BIGNUM *dup_priv = NULL;
  38812. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38813. ExpectNotNull(dsa = DSA_new());
  38814. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  38815. NULL), 1);
  38816. ExpectIntEQ(DSA_generate_key(dsa), 1);
  38817. DSA_get0_pqg(dsa, &p, &q, &g);
  38818. DSA_get0_key(dsa, &pub, &priv);
  38819. ExpectNotNull(dup_p = BN_dup(p));
  38820. ExpectNotNull(dup_q = BN_dup(q));
  38821. ExpectNotNull(dup_g = BN_dup(g));
  38822. ExpectNotNull(dup_pub = BN_dup(pub));
  38823. ExpectNotNull(dup_priv = BN_dup(priv));
  38824. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38825. ExpectNotNull(dsa2 = DSA_new());
  38826. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  38827. if (EXPECT_FAIL()) {
  38828. BN_free(dup_p);
  38829. BN_free(dup_q);
  38830. BN_free(dup_g);
  38831. }
  38832. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  38833. if (EXPECT_FAIL()) {
  38834. BN_free(dup_pub);
  38835. BN_free(dup_priv);
  38836. }
  38837. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38838. DSA_free(dsa);
  38839. DSA_free(dsa2);
  38840. DSA_SIG_free(sig);
  38841. #endif
  38842. return EXPECT_RESULT();
  38843. }
  38844. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38845. {
  38846. EXPECT_DECLS;
  38847. #ifdef HAVE_ECC
  38848. WOLFSSL_EC_KEY* ecKey = NULL;
  38849. WOLFSSL_EC_KEY* ecGet1 = NULL;
  38850. EVP_PKEY* pkey = NULL;
  38851. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  38852. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38853. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38854. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38855. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38856. /* Should fail since ecKey is empty */
  38857. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38858. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38859. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38860. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38861. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38862. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38863. wolfSSL_EC_KEY_free(ecKey);
  38864. wolfSSL_EC_KEY_free(ecGet1);
  38865. EVP_PKEY_free(pkey);
  38866. #endif /* HAVE_ECC */
  38867. return EXPECT_RESULT();
  38868. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38869. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38870. {
  38871. EXPECT_DECLS;
  38872. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38873. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38874. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38875. DH *dh = NULL;
  38876. DH *setDh = NULL;
  38877. EVP_PKEY *pkey = NULL;
  38878. XFILE f = XBADFILE;
  38879. unsigned char buf[4096];
  38880. const unsigned char* pt = buf;
  38881. const char* dh2048 = "./certs/dh2048.der";
  38882. long len = 0;
  38883. int code = -1;
  38884. XMEMSET(buf, 0, sizeof(buf));
  38885. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  38886. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  38887. if (f != XBADFILE)
  38888. XFCLOSE(f);
  38889. /* Load dh2048.der into DH with internal format */
  38890. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38891. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38892. ExpectIntEQ(code, 0);
  38893. code = -1;
  38894. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38895. /* Set DH into PKEY */
  38896. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38897. /* Get DH from PKEY */
  38898. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38899. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38900. ExpectIntEQ(code, 0);
  38901. EVP_PKEY_free(pkey);
  38902. DH_free(setDh);
  38903. DH_free(dh);
  38904. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38905. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38906. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38907. return EXPECT_RESULT();
  38908. } /* END test_EVP_PKEY_set1_get1_DH */
  38909. static int test_wolfSSL_CTX_ctrl(void)
  38910. {
  38911. EXPECT_DECLS;
  38912. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38913. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38914. char caFile[] = "./certs/client-ca.pem";
  38915. char clientFile[] = "./certs/client-cert.pem";
  38916. SSL_CTX* ctx = NULL;
  38917. X509* x509 = NULL;
  38918. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38919. byte buf[6000];
  38920. char file[] = "./certs/dsaparams.pem";
  38921. XFILE f = XBADFILE;
  38922. int bytes;
  38923. BIO* bio = NULL;
  38924. DSA* dsa = NULL;
  38925. DH* dh = NULL;
  38926. #endif
  38927. #ifdef HAVE_ECC
  38928. WOLFSSL_EC_KEY* ecKey = NULL;
  38929. #endif
  38930. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38931. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  38932. WOLFSSL_FILETYPE_PEM));
  38933. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38934. if (EXPECT_FAIL()) {
  38935. wolfSSL_X509_free(x509);
  38936. }
  38937. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  38938. WOLFSSL_FILETYPE_PEM));
  38939. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38940. /* Initialize DH */
  38941. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  38942. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  38943. if (f != XBADFILE)
  38944. XFCLOSE(f);
  38945. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  38946. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  38947. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  38948. #endif
  38949. #ifdef HAVE_ECC
  38950. /* Initialize WOLFSSL_EC_KEY */
  38951. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  38952. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38953. #endif
  38954. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38955. /* additional test of getting EVP_PKEY key size from X509
  38956. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38957. * allowed with user RSA */
  38958. {
  38959. EVP_PKEY* pkey = NULL;
  38960. #if defined(HAVE_ECC)
  38961. X509* ecX509 = NULL;
  38962. #endif /* HAVE_ECC */
  38963. ExpectNotNull(pkey = X509_get_pubkey(x509));
  38964. /* current RSA key is 2048 bit (256 bytes) */
  38965. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  38966. EVP_PKEY_free(pkey);
  38967. pkey = NULL;
  38968. #if defined(HAVE_ECC)
  38969. #if defined(USE_CERT_BUFFERS_256)
  38970. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38971. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38972. SSL_FILETYPE_ASN1));
  38973. #else
  38974. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38975. cliEccCertFile, SSL_FILETYPE_PEM));
  38976. #endif
  38977. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  38978. /* current ECC key is 256 bit (32 bytes) */
  38979. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  38980. X509_free(ecX509);
  38981. EVP_PKEY_free(pkey);
  38982. #endif /* HAVE_ECC */
  38983. }
  38984. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38985. /* Tests should fail with passed in NULL pointer */
  38986. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  38987. SSL_FAILURE);
  38988. #if !defined(NO_DH) && !defined(NO_DSA)
  38989. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  38990. SSL_FAILURE);
  38991. #endif
  38992. #ifdef HAVE_ECC
  38993. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  38994. SSL_FAILURE);
  38995. #endif
  38996. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38997. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38998. */
  38999. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  39000. SSL_SUCCESS);
  39001. if (EXPECT_FAIL()) {
  39002. wolfSSL_X509_free(x509);
  39003. }
  39004. /* Test with SSL_CTRL_OPTIONS
  39005. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  39006. */
  39007. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  39008. NULL) == SSL_OP_NO_TLSv1);
  39009. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  39010. /* Test with SSL_CTRL_SET_TMP_DH
  39011. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  39012. */
  39013. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  39014. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  39015. SSL_SUCCESS);
  39016. #endif
  39017. /* Test with SSL_CTRL_SET_TMP_ECDH
  39018. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  39019. */
  39020. #ifdef HAVE_ECC
  39021. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  39022. SSL_SUCCESS);
  39023. #endif
  39024. #ifdef WOLFSSL_ENCRYPTED_KEYS
  39025. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  39026. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  39027. #endif
  39028. /* Test for min/max proto */
  39029. #ifndef WOLFSSL_NO_TLS12
  39030. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  39031. 0, NULL), SSL_SUCCESS);
  39032. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  39033. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  39034. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  39035. #endif
  39036. #ifdef WOLFSSL_TLS13
  39037. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39038. 0, NULL), SSL_SUCCESS);
  39039. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39040. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  39041. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  39042. #ifndef WOLFSSL_NO_TLS12
  39043. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  39044. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  39045. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  39046. #endif
  39047. #endif
  39048. /* Cleanup and Pass */
  39049. #if !defined(NO_DH) && !defined(NO_DSA)
  39050. #ifndef NO_BIO
  39051. BIO_free(bio);
  39052. DSA_free(dsa);
  39053. DH_free(dh);
  39054. #endif
  39055. #endif
  39056. #ifdef HAVE_ECC
  39057. wolfSSL_EC_KEY_free(ecKey);
  39058. #endif
  39059. SSL_CTX_free(ctx);
  39060. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39061. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  39062. return EXPECT_RESULT();
  39063. }
  39064. static int test_wolfSSL_EVP_PKEY_assign(void)
  39065. {
  39066. EXPECT_DECLS;
  39067. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  39068. int type;
  39069. WOLFSSL_EVP_PKEY* pkey = NULL;
  39070. #ifndef NO_RSA
  39071. WOLFSSL_RSA* rsa = NULL;
  39072. #endif
  39073. #ifndef NO_DSA
  39074. WOLFSSL_DSA* dsa = NULL;
  39075. #endif
  39076. #ifdef HAVE_ECC
  39077. WOLFSSL_EC_KEY* ecKey = NULL;
  39078. #endif
  39079. #ifndef NO_RSA
  39080. type = EVP_PKEY_RSA;
  39081. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39082. ExpectNotNull(rsa = wolfSSL_RSA_new());
  39083. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  39084. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  39085. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  39086. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  39087. if (EXPECT_FAIL()) {
  39088. wolfSSL_RSA_free(rsa);
  39089. }
  39090. wolfSSL_EVP_PKEY_free(pkey);
  39091. pkey = NULL;
  39092. #endif /* NO_RSA */
  39093. #ifndef NO_DSA
  39094. type = EVP_PKEY_DSA;
  39095. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39096. ExpectNotNull(dsa = wolfSSL_DSA_new());
  39097. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  39098. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  39099. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  39100. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  39101. if (EXPECT_FAIL()) {
  39102. wolfSSL_DSA_free(dsa);
  39103. }
  39104. wolfSSL_EVP_PKEY_free(pkey);
  39105. pkey = NULL;
  39106. #endif /* NO_DSA */
  39107. #ifdef HAVE_ECC
  39108. type = EVP_PKEY_EC;
  39109. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39110. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  39111. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  39112. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  39113. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  39114. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  39115. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  39116. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  39117. if (EXPECT_FAIL()) {
  39118. wolfSSL_EC_KEY_free(ecKey);
  39119. }
  39120. wolfSSL_EVP_PKEY_free(pkey);
  39121. pkey = NULL;
  39122. #endif /* HAVE_ECC */
  39123. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  39124. return EXPECT_RESULT();
  39125. }
  39126. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39127. {
  39128. EXPECT_DECLS;
  39129. #if !defined(NO_DH) && \
  39130. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39131. XFILE f = XBADFILE;
  39132. unsigned char buf[4096];
  39133. const unsigned char* pt = buf;
  39134. const char* params1 = "./certs/dh2048.der";
  39135. long len = 0;
  39136. WOLFSSL_DH* dh = NULL;
  39137. WOLFSSL_EVP_PKEY* pkey = NULL;
  39138. XMEMSET(buf, 0, sizeof(buf));
  39139. /* Load DH parameters DER. */
  39140. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  39141. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  39142. if (f != XBADFILE)
  39143. XFCLOSE(f);
  39144. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39145. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39146. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39147. /* Bad cases */
  39148. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39149. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39150. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39151. /* Good case */
  39152. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39153. if (EXPECT_FAIL()) {
  39154. wolfSSL_DH_free(dh);
  39155. }
  39156. EVP_PKEY_free(pkey);
  39157. #endif
  39158. return EXPECT_RESULT();
  39159. }
  39160. static int test_wolfSSL_EVP_PKEY_base_id(void)
  39161. {
  39162. EXPECT_DECLS;
  39163. WOLFSSL_EVP_PKEY* pkey = NULL;
  39164. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39165. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  39166. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  39167. EVP_PKEY_free(pkey);
  39168. return EXPECT_RESULT();
  39169. }
  39170. static int test_wolfSSL_EVP_PKEY_id(void)
  39171. {
  39172. EXPECT_DECLS;
  39173. WOLFSSL_EVP_PKEY* pkey = NULL;
  39174. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39175. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  39176. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  39177. EVP_PKEY_free(pkey);
  39178. return EXPECT_RESULT();
  39179. }
  39180. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  39181. {
  39182. EXPECT_DECLS;
  39183. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  39184. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  39185. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  39186. ECC_MIN_KEY_SZ <= 256)
  39187. EVP_PKEY_CTX* ctx = NULL;
  39188. EVP_PKEY* pkey = NULL;
  39189. /* Test error conditions. */
  39190. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  39191. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39192. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  39193. #ifndef NO_RSA
  39194. EVP_PKEY_CTX_free(ctx);
  39195. /* Parameter generation for RSA not supported yet. */
  39196. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  39197. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  39198. #endif
  39199. #ifdef HAVE_ECC
  39200. EVP_PKEY_CTX_free(ctx);
  39201. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39202. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  39203. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  39204. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  39205. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  39206. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  39207. WOLFSSL_SUCCESS);
  39208. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39209. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39210. #endif
  39211. EVP_PKEY_CTX_free(ctx);
  39212. EVP_PKEY_free(pkey);
  39213. #endif
  39214. return EXPECT_RESULT();
  39215. }
  39216. static int test_wolfSSL_EVP_PKEY_keygen(void)
  39217. {
  39218. EXPECT_DECLS;
  39219. WOLFSSL_EVP_PKEY* pkey = NULL;
  39220. EVP_PKEY_CTX* ctx = NULL;
  39221. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39222. WOLFSSL_EVP_PKEY* params = NULL;
  39223. DH* dh = NULL;
  39224. const BIGNUM* pubkey = NULL;
  39225. const BIGNUM* privkey = NULL;
  39226. ASN1_INTEGER* asn1int = NULL;
  39227. unsigned int length = 0;
  39228. byte* derBuffer = NULL;
  39229. #endif
  39230. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39231. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39232. /* Bad cases */
  39233. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  39234. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  39235. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  39236. /* Good case */
  39237. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  39238. EVP_PKEY_CTX_free(ctx);
  39239. ctx = NULL;
  39240. EVP_PKEY_free(pkey);
  39241. pkey = NULL;
  39242. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39243. /* Test DH keygen */
  39244. {
  39245. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  39246. ExpectNotNull(dh = DH_get_2048_256());
  39247. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39248. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  39249. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39250. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39251. DH_free(dh);
  39252. dh = NULL;
  39253. EVP_PKEY_CTX_free(ctx);
  39254. EVP_PKEY_free(params);
  39255. /* try exporting generated key to DER, to verify */
  39256. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  39257. DH_get0_key(dh, &pubkey, &privkey);
  39258. ExpectNotNull(pubkey);
  39259. ExpectNotNull(privkey);
  39260. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  39261. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  39262. ASN1_INTEGER_free(asn1int);
  39263. DH_free(dh);
  39264. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39265. EVP_PKEY_free(pkey);
  39266. }
  39267. #endif
  39268. return EXPECT_RESULT();
  39269. }
  39270. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  39271. {
  39272. EXPECT_DECLS;
  39273. WOLFSSL_EVP_PKEY* pkey = NULL;
  39274. EVP_PKEY_CTX *ctx = NULL;
  39275. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39276. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39277. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39278. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  39279. EVP_PKEY_CTX_free(ctx);
  39280. EVP_PKEY_free(pkey);
  39281. return EXPECT_RESULT();
  39282. }
  39283. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  39284. {
  39285. EXPECT_DECLS;
  39286. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  39287. WOLFSSL_EVP_PKEY* pkey = NULL;
  39288. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39289. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  39290. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  39291. EVP_PKEY_free(pkey);
  39292. #endif
  39293. return EXPECT_RESULT();
  39294. }
  39295. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  39296. {
  39297. EXPECT_DECLS;
  39298. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  39299. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  39300. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  39301. !defined(NO_FILESYSTEM)
  39302. WOLFSSL_EVP_PKEY* params = NULL;
  39303. WOLFSSL_EVP_PKEY* copy = NULL;
  39304. DH* dh = NULL;
  39305. BIGNUM* p1;
  39306. BIGNUM* g1;
  39307. BIGNUM* q1;
  39308. BIGNUM* p2;
  39309. BIGNUM* g2;
  39310. BIGNUM* q2;
  39311. /* create DH with DH_get_2048_256 params */
  39312. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  39313. ExpectNotNull(dh = DH_get_2048_256());
  39314. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39315. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  39316. (const BIGNUM**)&q1,
  39317. (const BIGNUM**)&g1);
  39318. DH_free(dh);
  39319. dh = NULL;
  39320. /* create DH with random generated DH params */
  39321. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  39322. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  39323. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  39324. DH_free(dh);
  39325. dh = NULL;
  39326. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  39327. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  39328. ExpectNotNull(dh->p);
  39329. ExpectNotNull(dh->g);
  39330. ExpectNotNull(dh->q);
  39331. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  39332. (const BIGNUM**)&q2,
  39333. (const BIGNUM**)&g2);
  39334. ExpectIntEQ(BN_cmp(p1, p2), 0);
  39335. ExpectIntEQ(BN_cmp(q1, q2), 0);
  39336. ExpectIntEQ(BN_cmp(g1, g2), 0);
  39337. DH_free(dh);
  39338. EVP_PKEY_free(copy);
  39339. EVP_PKEY_free(params);
  39340. #endif
  39341. return EXPECT_RESULT();
  39342. }
  39343. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39344. {
  39345. EXPECT_DECLS;
  39346. WOLFSSL_EVP_PKEY* pkey = NULL;
  39347. EVP_PKEY_CTX* ctx = NULL;
  39348. int bits = 2048;
  39349. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39350. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39351. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39352. WOLFSSL_SUCCESS);
  39353. EVP_PKEY_CTX_free(ctx);
  39354. EVP_PKEY_free(pkey);
  39355. return EXPECT_RESULT();
  39356. }
  39357. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39358. {
  39359. EXPECT_DECLS;
  39360. /* This is large enough to be used for all key sizes */
  39361. byte key[AES_256_KEY_SIZE] = {0};
  39362. byte iv[AES_BLOCK_SIZE] = {0};
  39363. int i;
  39364. int nids[] = {
  39365. #ifdef HAVE_AES_CBC
  39366. NID_aes_128_cbc,
  39367. #endif
  39368. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39369. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39370. #ifdef HAVE_AESGCM
  39371. NID_aes_128_gcm,
  39372. #endif
  39373. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39374. #ifdef WOLFSSL_AES_COUNTER
  39375. NID_aes_128_ctr,
  39376. #endif
  39377. #ifndef NO_DES3
  39378. NID_des_cbc,
  39379. NID_des_ede3_cbc,
  39380. #endif
  39381. };
  39382. int iv_lengths[] = {
  39383. #ifdef HAVE_AES_CBC
  39384. AES_BLOCK_SIZE,
  39385. #endif
  39386. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39387. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39388. #ifdef HAVE_AESGCM
  39389. GCM_NONCE_MID_SZ,
  39390. #endif
  39391. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39392. #ifdef WOLFSSL_AES_COUNTER
  39393. AES_BLOCK_SIZE,
  39394. #endif
  39395. #ifndef NO_DES3
  39396. DES_BLOCK_SIZE,
  39397. DES_BLOCK_SIZE,
  39398. #endif
  39399. };
  39400. int nidsLen = (sizeof(nids)/sizeof(int));
  39401. for (i = 0; i < nidsLen; i++) {
  39402. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  39403. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  39404. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39405. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39406. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39407. EVP_CIPHER_CTX_free(ctx);
  39408. }
  39409. return EXPECT_RESULT();
  39410. }
  39411. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39412. {
  39413. EXPECT_DECLS;
  39414. byte key[AES_256_KEY_SIZE] = {0};
  39415. byte iv[AES_BLOCK_SIZE] = {0};
  39416. int i;
  39417. int nids[] = {
  39418. #ifdef HAVE_AES_CBC
  39419. NID_aes_128_cbc,
  39420. NID_aes_256_cbc,
  39421. #endif
  39422. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39423. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39424. #ifdef HAVE_AESGCM
  39425. NID_aes_128_gcm,
  39426. NID_aes_256_gcm,
  39427. #endif
  39428. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39429. #ifdef WOLFSSL_AES_COUNTER
  39430. NID_aes_128_ctr,
  39431. NID_aes_256_ctr,
  39432. #endif
  39433. #ifndef NO_DES3
  39434. NID_des_cbc,
  39435. NID_des_ede3_cbc,
  39436. #endif
  39437. };
  39438. int key_lengths[] = {
  39439. #ifdef HAVE_AES_CBC
  39440. AES_128_KEY_SIZE,
  39441. AES_256_KEY_SIZE,
  39442. #endif
  39443. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39444. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39445. #ifdef HAVE_AESGCM
  39446. AES_128_KEY_SIZE,
  39447. AES_256_KEY_SIZE,
  39448. #endif
  39449. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39450. #ifdef WOLFSSL_AES_COUNTER
  39451. AES_128_KEY_SIZE,
  39452. AES_256_KEY_SIZE,
  39453. #endif
  39454. #ifndef NO_DES3
  39455. DES_KEY_SIZE,
  39456. DES3_KEY_SIZE,
  39457. #endif
  39458. };
  39459. int nidsLen = (sizeof(nids)/sizeof(int));
  39460. for (i = 0; i < nidsLen; i++) {
  39461. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  39462. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  39463. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39464. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39465. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  39466. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  39467. WOLFSSL_SUCCESS);
  39468. EVP_CIPHER_CTX_free(ctx);
  39469. }
  39470. return EXPECT_RESULT();
  39471. }
  39472. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39473. {
  39474. EXPECT_DECLS;
  39475. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39476. int ivLen, keyLen;
  39477. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39478. #ifdef HAVE_AESGCM
  39479. byte key[AES_128_KEY_SIZE] = {0};
  39480. byte iv[AES_BLOCK_SIZE] = {0};
  39481. const EVP_CIPHER *init = EVP_aes_128_gcm();
  39482. #else
  39483. byte key[DES3_KEY_SIZE] = {0};
  39484. byte iv[DES_BLOCK_SIZE] = {0};
  39485. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39486. #endif
  39487. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39488. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39489. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39490. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39491. /* Bad cases */
  39492. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  39493. WOLFSSL_FAILURE);
  39494. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  39495. WOLFSSL_FAILURE);
  39496. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39497. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39498. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  39499. WOLFSSL_FAILURE);
  39500. /* Good case */
  39501. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39502. EVP_CIPHER_CTX_free(ctx);
  39503. #endif
  39504. return EXPECT_RESULT();
  39505. }
  39506. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39507. {
  39508. EXPECT_DECLS;
  39509. WOLFSSL_ENGINE* e = NULL;
  39510. int id = 0;
  39511. EVP_PKEY_CTX *ctx = NULL;
  39512. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39513. EVP_PKEY_CTX_free(ctx);
  39514. return EXPECT_RESULT();
  39515. }
  39516. static int test_wolfSSL_EVP_rc4(void)
  39517. {
  39518. EXPECT_DECLS;
  39519. #if !defined(NO_RC4)
  39520. ExpectNotNull(wolfSSL_EVP_rc4());
  39521. #endif
  39522. return EXPECT_RESULT();
  39523. }
  39524. static int test_wolfSSL_EVP_enc_null(void)
  39525. {
  39526. EXPECT_DECLS;
  39527. ExpectNotNull(wolfSSL_EVP_enc_null());
  39528. return EXPECT_RESULT();
  39529. }
  39530. static int test_wolfSSL_EVP_rc2_cbc(void)
  39531. {
  39532. EXPECT_DECLS;
  39533. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39534. ExpectNull(wolfSSL_EVP_rc2_cbc());
  39535. #endif
  39536. return EXPECT_RESULT();
  39537. }
  39538. static int test_wolfSSL_EVP_mdc2(void)
  39539. {
  39540. EXPECT_DECLS;
  39541. #if !defined(NO_WOLFSSL_STUB)
  39542. ExpectNull(wolfSSL_EVP_mdc2());
  39543. #endif
  39544. return EXPECT_RESULT();
  39545. }
  39546. static int test_wolfSSL_EVP_md4(void)
  39547. {
  39548. EXPECT_DECLS;
  39549. #if !defined(NO_MD4)
  39550. ExpectNotNull(wolfSSL_EVP_md4());
  39551. #endif
  39552. return EXPECT_RESULT();
  39553. }
  39554. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39555. {
  39556. EXPECT_DECLS;
  39557. #ifdef HAVE_AESGCM
  39558. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  39559. #endif
  39560. return EXPECT_RESULT();
  39561. }
  39562. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39563. {
  39564. EXPECT_DECLS;
  39565. #ifdef HAVE_AESGCM
  39566. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  39567. #endif
  39568. return EXPECT_RESULT();
  39569. }
  39570. static int test_wolfSSL_EVP_aes_256_ccm(void)
  39571. {
  39572. EXPECT_DECLS;
  39573. #ifdef HAVE_AESCCM
  39574. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  39575. #endif
  39576. return EXPECT_RESULT();
  39577. }
  39578. static int test_wolfSSL_EVP_aes_192_ccm(void)
  39579. {
  39580. EXPECT_DECLS;
  39581. #ifdef HAVE_AESCCM
  39582. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  39583. #endif
  39584. return EXPECT_RESULT();
  39585. }
  39586. static int test_wolfSSL_EVP_aes_128_ccm(void)
  39587. {
  39588. EXPECT_DECLS;
  39589. #ifdef HAVE_AESCCM
  39590. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  39591. #endif
  39592. return EXPECT_RESULT();
  39593. }
  39594. static int test_wolfSSL_EVP_ripemd160(void)
  39595. {
  39596. EXPECT_DECLS;
  39597. #if !defined(NO_WOLFSSL_STUB)
  39598. ExpectNull(wolfSSL_EVP_ripemd160());
  39599. #endif
  39600. return EXPECT_RESULT();
  39601. }
  39602. static int test_wolfSSL_EVP_get_digestbynid(void)
  39603. {
  39604. EXPECT_DECLS;
  39605. #ifndef NO_MD5
  39606. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39607. #endif
  39608. #ifndef NO_SHA
  39609. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39610. #endif
  39611. #ifndef NO_SHA256
  39612. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  39613. #endif
  39614. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  39615. return EXPECT_RESULT();
  39616. }
  39617. static int test_wolfSSL_EVP_MD_nid(void)
  39618. {
  39619. EXPECT_DECLS;
  39620. #ifndef NO_MD5
  39621. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39622. #endif
  39623. #ifndef NO_SHA
  39624. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39625. #endif
  39626. #ifndef NO_SHA256
  39627. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39628. #endif
  39629. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  39630. return EXPECT_RESULT();
  39631. }
  39632. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39633. {
  39634. EXPECT_DECLS;
  39635. #if defined(HAVE_ECC)
  39636. WOLFSSL_EVP_PKEY* pkey = NULL;
  39637. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  39638. ExpectNotNull(pkey = EVP_PKEY_new());
  39639. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  39640. EVP_PKEY_free(pkey);
  39641. #endif
  39642. return EXPECT_RESULT();
  39643. }
  39644. static int test_wolfSSL_EVP_X_STATE(void)
  39645. {
  39646. EXPECT_DECLS;
  39647. #if !defined(NO_DES3) && !defined(NO_RC4)
  39648. byte key[DES3_KEY_SIZE] = {0};
  39649. byte iv[DES_IV_SIZE] = {0};
  39650. EVP_CIPHER_CTX *ctx = NULL;
  39651. const EVP_CIPHER *init = NULL;
  39652. /* Bad test cases */
  39653. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39654. ExpectNotNull(init = EVP_des_ede3_cbc());
  39655. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39656. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39657. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  39658. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  39659. EVP_CIPHER_CTX_free(ctx);
  39660. ctx = NULL;
  39661. /* Good test case */
  39662. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39663. ExpectNotNull(init = wolfSSL_EVP_rc4());
  39664. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39665. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39666. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  39667. EVP_CIPHER_CTX_free(ctx);
  39668. #endif
  39669. return EXPECT_RESULT();
  39670. }
  39671. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39672. {
  39673. EXPECT_DECLS;
  39674. #if !defined(NO_DES3) && !defined(NO_RC4)
  39675. byte key[DES3_KEY_SIZE] = {0};
  39676. byte iv[DES_IV_SIZE] = {0};
  39677. EVP_CIPHER_CTX *ctx = NULL;
  39678. const EVP_CIPHER *init = NULL;
  39679. /* Bad test cases */
  39680. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39681. ExpectNotNull(init = EVP_des_ede3_cbc());
  39682. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39683. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39684. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39685. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39686. EVP_CIPHER_CTX_free(ctx);
  39687. ctx = NULL;
  39688. /* Good test case */
  39689. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39690. ExpectNotNull(init = wolfSSL_EVP_rc4());
  39691. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39692. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39693. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39694. EVP_CIPHER_CTX_free(ctx);
  39695. #endif
  39696. return EXPECT_RESULT();
  39697. }
  39698. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39699. {
  39700. EXPECT_DECLS;
  39701. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  39702. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  39703. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  39704. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  39705. #ifdef HAVE_AES_CBC
  39706. #ifdef WOLFSSL_AES_128
  39707. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39708. #endif
  39709. #ifdef WOLFSSL_AES_192
  39710. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39711. #endif
  39712. #ifdef WOLFSSL_AES_256
  39713. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39714. #endif
  39715. #endif
  39716. #ifdef HAVE_AESGCM
  39717. #ifdef WOLFSSL_AES_128
  39718. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39719. #endif
  39720. #ifdef WOLFSSL_AES_192
  39721. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39722. #endif
  39723. #ifdef WOLFSSL_AES_256
  39724. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39725. #endif
  39726. #endif
  39727. #ifdef HAVE_AESCCM
  39728. #ifdef WOLFSSL_AES_128
  39729. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  39730. #endif
  39731. #ifdef WOLFSSL_AES_192
  39732. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  39733. #endif
  39734. #ifdef WOLFSSL_AES_256
  39735. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  39736. #endif
  39737. #endif
  39738. #ifdef WOLFSSL_AES_COUNTER
  39739. #ifdef WOLFSSL_AES_128
  39740. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39741. #endif
  39742. #ifdef WOLFSSL_AES_192
  39743. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39744. #endif
  39745. #ifdef WOLFSSL_AES_256
  39746. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39747. #endif
  39748. #endif
  39749. #ifdef HAVE_AES_ECB
  39750. #ifdef WOLFSSL_AES_128
  39751. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39752. #endif
  39753. #ifdef WOLFSSL_AES_192
  39754. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39755. #endif
  39756. #ifdef WOLFSSL_AES_256
  39757. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39758. #endif
  39759. #endif
  39760. #ifdef WOLFSSL_AES_OFB
  39761. #ifdef WOLFSSL_AES_128
  39762. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39763. #endif
  39764. #ifdef WOLFSSL_AES_192
  39765. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39766. #endif
  39767. #ifdef WOLFSSL_AES_256
  39768. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39769. #endif
  39770. #endif
  39771. #ifndef NO_RC4
  39772. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39773. #endif
  39774. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39775. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39776. #endif
  39777. #endif
  39778. #ifdef WOLFSSL_SM4_ECB
  39779. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  39780. #endif
  39781. #ifdef WOLFSSL_SM4_CBC
  39782. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  39783. #endif
  39784. #ifdef WOLFSSL_SM4_CTR
  39785. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  39786. #endif
  39787. #ifdef WOLFSSL_SM4_GCM
  39788. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  39789. #endif
  39790. #ifdef WOLFSSL_SM4_CCM
  39791. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  39792. #endif
  39793. return EXPECT_RESULT();
  39794. }
  39795. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39796. {
  39797. EXPECT_DECLS;
  39798. int nids[] = {
  39799. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39800. #ifdef WOLFSSL_AES_128
  39801. NID_aes_128_cbc,
  39802. #endif
  39803. #ifdef WOLFSSL_AES_192
  39804. NID_aes_192_cbc,
  39805. #endif
  39806. #ifdef WOLFSSL_AES_256
  39807. NID_aes_256_cbc,
  39808. #endif
  39809. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39810. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39811. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39812. #ifdef HAVE_AESGCM
  39813. #ifdef WOLFSSL_AES_128
  39814. NID_aes_128_gcm,
  39815. #endif
  39816. #ifdef WOLFSSL_AES_192
  39817. NID_aes_192_gcm,
  39818. #endif
  39819. #ifdef WOLFSSL_AES_256
  39820. NID_aes_256_gcm,
  39821. #endif
  39822. #endif /* HAVE_AESGCM */
  39823. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39824. #ifdef WOLFSSL_AES_COUNTER
  39825. #ifdef WOLFSSL_AES_128
  39826. NID_aes_128_ctr,
  39827. #endif
  39828. #ifdef WOLFSSL_AES_192
  39829. NID_aes_192_ctr,
  39830. #endif
  39831. #ifdef WOLFSSL_AES_256
  39832. NID_aes_256_ctr,
  39833. #endif
  39834. #endif
  39835. #ifndef NO_DES3
  39836. NID_des_cbc,
  39837. NID_des_ede3_cbc,
  39838. #endif
  39839. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39840. NID_chacha20_poly1305,
  39841. #endif
  39842. };
  39843. int iv_lengths[] = {
  39844. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39845. #ifdef WOLFSSL_AES_128
  39846. AES_BLOCK_SIZE,
  39847. #endif
  39848. #ifdef WOLFSSL_AES_192
  39849. AES_BLOCK_SIZE,
  39850. #endif
  39851. #ifdef WOLFSSL_AES_256
  39852. AES_BLOCK_SIZE,
  39853. #endif
  39854. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39855. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39856. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39857. #ifdef HAVE_AESGCM
  39858. #ifdef WOLFSSL_AES_128
  39859. GCM_NONCE_MID_SZ,
  39860. #endif
  39861. #ifdef WOLFSSL_AES_192
  39862. GCM_NONCE_MID_SZ,
  39863. #endif
  39864. #ifdef WOLFSSL_AES_256
  39865. GCM_NONCE_MID_SZ,
  39866. #endif
  39867. #endif /* HAVE_AESGCM */
  39868. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39869. #ifdef WOLFSSL_AES_COUNTER
  39870. #ifdef WOLFSSL_AES_128
  39871. AES_BLOCK_SIZE,
  39872. #endif
  39873. #ifdef WOLFSSL_AES_192
  39874. AES_BLOCK_SIZE,
  39875. #endif
  39876. #ifdef WOLFSSL_AES_256
  39877. AES_BLOCK_SIZE,
  39878. #endif
  39879. #endif
  39880. #ifndef NO_DES3
  39881. DES_BLOCK_SIZE,
  39882. DES_BLOCK_SIZE,
  39883. #endif
  39884. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39885. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39886. #endif
  39887. };
  39888. int i;
  39889. int nidsLen = (sizeof(nids)/sizeof(int));
  39890. for (i = 0; i < nidsLen; i++) {
  39891. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  39892. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39893. }
  39894. return EXPECT_RESULT();
  39895. }
  39896. static int test_wolfSSL_EVP_SignInit_ex(void)
  39897. {
  39898. EXPECT_DECLS;
  39899. WOLFSSL_EVP_MD_CTX mdCtx;
  39900. WOLFSSL_ENGINE* e = 0;
  39901. const EVP_MD* md = EVP_sha256();
  39902. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39903. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39904. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39905. return EXPECT_RESULT();
  39906. }
  39907. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39908. {
  39909. EXPECT_DECLS;
  39910. #if !defined(NO_SHA256)
  39911. WOLFSSL_EVP_MD_CTX mdCtx;
  39912. unsigned int s = 0;
  39913. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39914. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39915. /* Bad Case */
  39916. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39917. (HAVE_FIPS_VERSION > 2))
  39918. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39919. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39920. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39921. #else
  39922. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39923. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39924. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39925. #endif
  39926. /* Good Case */
  39927. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39928. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  39929. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39930. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39931. #endif
  39932. return EXPECT_RESULT();
  39933. }
  39934. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39935. {
  39936. EXPECT_DECLS;
  39937. #if defined(OPENSSL_EXTRA)
  39938. EVP_PKEY* pkey = NULL;
  39939. EVP_PKEY_CTX* ctx = NULL;
  39940. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39941. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39942. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39943. /* void */
  39944. EVP_PKEY_CTX_free(ctx);
  39945. #else
  39946. /* int */
  39947. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39948. #endif
  39949. EVP_PKEY_free(pkey);
  39950. #endif
  39951. return EXPECT_RESULT();
  39952. }
  39953. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39954. {
  39955. EXPECT_DECLS;
  39956. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39957. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39958. DH *dh = NULL;
  39959. DH *setDh = NULL;
  39960. EVP_PKEY *pkey = NULL;
  39961. EVP_PKEY_CTX* ctx = NULL;
  39962. FILE* f = NULL;
  39963. unsigned char buf[512];
  39964. const unsigned char* pt = buf;
  39965. const char* dh2048 = "./certs/dh2048.der";
  39966. long len = 0;
  39967. int code = -1;
  39968. XMEMSET(buf, 0, sizeof(buf));
  39969. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  39970. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  39971. if (f != XBADFILE)
  39972. XFCLOSE(f);
  39973. /* Load dh2048.der into DH with internal format */
  39974. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39975. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39976. ExpectIntEQ(code, 0);
  39977. code = -1;
  39978. pkey = wolfSSL_EVP_PKEY_new();
  39979. /* Set DH into PKEY */
  39980. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39981. /* create ctx from pkey */
  39982. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39983. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39984. /* TODO: more invalid cases */
  39985. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  39986. EVP_PKEY_CTX_free(ctx);
  39987. EVP_PKEY_free(pkey);
  39988. DH_free(setDh);
  39989. DH_free(dh);
  39990. #endif
  39991. #endif
  39992. return EXPECT_RESULT();
  39993. }
  39994. static int test_wolfSSL_EVP_BytesToKey(void)
  39995. {
  39996. EXPECT_DECLS;
  39997. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39998. byte key[AES_BLOCK_SIZE] = {0};
  39999. byte iv[AES_BLOCK_SIZE] = {0};
  40000. int count = 0;
  40001. const EVP_MD* md = EVP_sha256();
  40002. const EVP_CIPHER *type;
  40003. const unsigned char *salt = (unsigned char *)"salt1234";
  40004. int sz = 5;
  40005. const byte data[] = {
  40006. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  40007. 0x72,0x6c,0x64
  40008. };
  40009. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  40010. /* Bad cases */
  40011. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  40012. 0);
  40013. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  40014. 16);
  40015. md = "2";
  40016. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  40017. WOLFSSL_FAILURE);
  40018. /* Good case */
  40019. md = EVP_sha256();
  40020. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  40021. 16);
  40022. #endif
  40023. return EXPECT_RESULT();
  40024. }
  40025. static int test_evp_cipher_aes_gcm(void)
  40026. {
  40027. EXPECT_DECLS;
  40028. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  40029. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  40030. (HAVE_FIPS_VERSION >= 2)))
  40031. /*
  40032. * This test checks data at various points in the encrypt/decrypt process
  40033. * against known values produced using the same test with OpenSSL. This
  40034. * interop testing is critical for verifying the correctness of our
  40035. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  40036. * a flow supported by OpenSSL that uses the control command
  40037. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  40038. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  40039. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  40040. * wolfSSL OpenSSH clients because there was a bug in this flow that
  40041. * happened to "cancel out" if both sides of the connection had the bug.
  40042. */
  40043. enum {
  40044. NUM_ENCRYPTIONS = 3,
  40045. AAD_SIZE = 4
  40046. };
  40047. byte plainText1[] = {
  40048. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  40049. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  40050. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  40051. };
  40052. byte plainText2[] = {
  40053. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  40054. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  40055. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  40056. };
  40057. byte plainText3[] = {
  40058. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  40059. 0x5b, 0x57, 0x10
  40060. };
  40061. byte* plainTexts[NUM_ENCRYPTIONS] = {
  40062. plainText1,
  40063. plainText2,
  40064. plainText3
  40065. };
  40066. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  40067. sizeof(plainText1),
  40068. sizeof(plainText2),
  40069. sizeof(plainText3)
  40070. };
  40071. byte aad1[AAD_SIZE] = {
  40072. 0x00, 0x00, 0x00, 0x01
  40073. };
  40074. byte aad2[AAD_SIZE] = {
  40075. 0x00, 0x00, 0x00, 0x10
  40076. };
  40077. byte aad3[AAD_SIZE] = {
  40078. 0x00, 0x00, 0x01, 0x00
  40079. };
  40080. byte* aads[NUM_ENCRYPTIONS] = {
  40081. aad1,
  40082. aad2,
  40083. aad3
  40084. };
  40085. const byte iv[GCM_NONCE_MID_SZ] = {
  40086. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  40087. };
  40088. byte currentIv[GCM_NONCE_MID_SZ];
  40089. const byte key[] = {
  40090. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  40091. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  40092. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  40093. };
  40094. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  40095. {
  40096. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40097. 0xEF
  40098. },
  40099. {
  40100. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40101. 0xF0
  40102. },
  40103. {
  40104. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  40105. 0xF1
  40106. }
  40107. };
  40108. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  40109. {
  40110. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  40111. 0x3D, 0x32, 0x18, 0x34, 0xA9
  40112. },
  40113. {
  40114. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  40115. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  40116. },
  40117. {
  40118. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  40119. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  40120. }
  40121. };
  40122. const byte expCipherText1[] = {
  40123. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  40124. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  40125. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  40126. };
  40127. const byte expCipherText2[] = {
  40128. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  40129. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  40130. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  40131. };
  40132. const byte expCipherText3[] = {
  40133. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  40134. 0x80, 0x5E, 0x6B,
  40135. };
  40136. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  40137. expCipherText1,
  40138. expCipherText2,
  40139. expCipherText3
  40140. };
  40141. byte* cipherText = NULL;
  40142. byte* calcPlainText = NULL;
  40143. byte tag[AES_BLOCK_SIZE];
  40144. EVP_CIPHER_CTX* encCtx = NULL;
  40145. EVP_CIPHER_CTX* decCtx = NULL;
  40146. int i, j, outl;
  40147. /****************************************************/
  40148. for (i = 0; i < 3; ++i) {
  40149. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  40150. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  40151. /* First iteration, set key before IV. */
  40152. if (i == 0) {
  40153. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  40154. SSL_SUCCESS);
  40155. /*
  40156. * The call to EVP_CipherInit below (with NULL key) should clear the
  40157. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  40158. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  40159. * behavior.
  40160. */
  40161. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40162. (void*)iv), SSL_SUCCESS);
  40163. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  40164. SSL_SUCCESS);
  40165. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40166. currentIv), SSL_FAILURE);
  40167. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  40168. SSL_SUCCESS);
  40169. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  40170. SSL_SUCCESS);
  40171. }
  40172. /* Second iteration, IV before key. */
  40173. else {
  40174. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  40175. SSL_SUCCESS);
  40176. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  40177. SSL_SUCCESS);
  40178. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  40179. SSL_SUCCESS);
  40180. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  40181. SSL_SUCCESS);
  40182. }
  40183. /*
  40184. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  40185. * been issued first.
  40186. */
  40187. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40188. currentIv), SSL_FAILURE);
  40189. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40190. (void*)iv), SSL_SUCCESS);
  40191. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40192. (void*)iv), SSL_SUCCESS);
  40193. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  40194. /*************** Encrypt ***************/
  40195. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40196. currentIv), SSL_SUCCESS);
  40197. /* Check current IV against expected. */
  40198. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40199. /* Add AAD. */
  40200. if (i == 2) {
  40201. /* Test streaming API. */
  40202. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  40203. AAD_SIZE), SSL_SUCCESS);
  40204. }
  40205. else {
  40206. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  40207. AAD_SIZE);
  40208. }
  40209. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40210. DYNAMIC_TYPE_TMP_BUFFER));
  40211. /* Encrypt plaintext. */
  40212. if (i == 2) {
  40213. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  40214. plainTexts[j], plainTextSzs[j]),
  40215. SSL_SUCCESS);
  40216. }
  40217. else {
  40218. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  40219. plainTextSzs[j]), plainTextSzs[j]);
  40220. }
  40221. if (i == 2) {
  40222. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  40223. SSL_SUCCESS);
  40224. }
  40225. else {
  40226. /*
  40227. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  40228. * akin to calling EVP_CipherFinal.
  40229. */
  40230. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  40231. }
  40232. /* Check ciphertext against expected. */
  40233. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  40234. 0);
  40235. /* Get and check tag against expected. */
  40236. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  40237. sizeof(tag), tag), SSL_SUCCESS);
  40238. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  40239. /*************** Decrypt ***************/
  40240. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40241. currentIv), SSL_SUCCESS);
  40242. /* Check current IV against expected. */
  40243. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40244. /* Add AAD. */
  40245. if (i == 2) {
  40246. /* Test streaming API. */
  40247. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  40248. AAD_SIZE), SSL_SUCCESS);
  40249. }
  40250. else {
  40251. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  40252. AAD_SIZE);
  40253. }
  40254. /* Set expected tag. */
  40255. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  40256. sizeof(tag), tag), SSL_SUCCESS);
  40257. /* Decrypt ciphertext. */
  40258. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40259. DYNAMIC_TYPE_TMP_BUFFER));
  40260. if (i == 2) {
  40261. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  40262. cipherText, plainTextSzs[j]),
  40263. SSL_SUCCESS);
  40264. }
  40265. else {
  40266. /* This first EVP_Cipher call will check the tag, too. */
  40267. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  40268. plainTextSzs[j]), plainTextSzs[j]);
  40269. }
  40270. if (i == 2) {
  40271. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  40272. SSL_SUCCESS);
  40273. }
  40274. else {
  40275. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  40276. }
  40277. /* Check plaintext against expected. */
  40278. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  40279. 0);
  40280. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40281. cipherText = NULL;
  40282. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40283. calcPlainText = NULL;
  40284. }
  40285. EVP_CIPHER_CTX_free(encCtx);
  40286. encCtx = NULL;
  40287. EVP_CIPHER_CTX_free(decCtx);
  40288. decCtx = NULL;
  40289. }
  40290. #endif
  40291. return EXPECT_RESULT();
  40292. }
  40293. static int test_wolfSSL_OBJ_ln(void)
  40294. {
  40295. EXPECT_DECLS;
  40296. const int nid_set[] = {
  40297. NID_commonName,
  40298. NID_serialNumber,
  40299. NID_countryName,
  40300. NID_localityName,
  40301. NID_stateOrProvinceName,
  40302. NID_organizationName,
  40303. NID_organizationalUnitName,
  40304. NID_domainComponent,
  40305. NID_businessCategory,
  40306. NID_jurisdictionCountryName,
  40307. NID_jurisdictionStateOrProvinceName,
  40308. NID_emailAddress
  40309. };
  40310. const char* ln_set[] = {
  40311. "commonName",
  40312. "serialNumber",
  40313. "countryName",
  40314. "localityName",
  40315. "stateOrProvinceName",
  40316. "organizationName",
  40317. "organizationalUnitName",
  40318. "domainComponent",
  40319. "businessCategory",
  40320. "jurisdictionCountryName",
  40321. "jurisdictionStateOrProvinceName",
  40322. "emailAddress",
  40323. };
  40324. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  40325. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  40326. #ifdef HAVE_ECC
  40327. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40328. {
  40329. EC_builtin_curve r[27];
  40330. size_t nCurves = sizeof(r) / sizeof(r[0]);
  40331. nCurves = EC_get_builtin_curves(r, nCurves);
  40332. for (i = 0; i < nCurves; i++) {
  40333. /* skip ECC_CURVE_INVALID */
  40334. if (r[i].nid != ECC_CURVE_INVALID) {
  40335. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  40336. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  40337. }
  40338. }
  40339. }
  40340. #endif
  40341. #endif
  40342. for (i = 0; i < maxIdx; i++) {
  40343. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  40344. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  40345. }
  40346. return EXPECT_RESULT();
  40347. }
  40348. static int test_wolfSSL_OBJ_sn(void)
  40349. {
  40350. EXPECT_DECLS;
  40351. int i = 0, maxIdx = 7;
  40352. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  40353. NID_stateOrProvinceName,NID_organizationName,
  40354. NID_organizationalUnitName,NID_emailAddress};
  40355. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  40356. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  40357. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  40358. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  40359. WOLFSSL_EMAIL_ADDR};
  40360. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  40361. for (i = 0; i < maxIdx; i++) {
  40362. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  40363. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  40364. }
  40365. return EXPECT_RESULT();
  40366. }
  40367. #if !defined(NO_BIO)
  40368. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  40369. {
  40370. return lh_strhash(s[3]);
  40371. }
  40372. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  40373. {
  40374. return XSTRCMP(a[3], b[3]);
  40375. }
  40376. #endif
  40377. static int test_wolfSSL_TXT_DB(void)
  40378. {
  40379. EXPECT_DECLS;
  40380. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40381. BIO *bio = NULL;
  40382. TXT_DB *db = NULL;
  40383. const int columns = 6;
  40384. const char *fields[6] = {
  40385. "V",
  40386. "320926161116Z",
  40387. "",
  40388. "12BD",
  40389. "unknown",
  40390. "/CN=rsa doe",
  40391. };
  40392. char** fields_copy = NULL;
  40393. /* Test read */
  40394. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  40395. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40396. ExpectNotNull(db = TXT_DB_read(bio, columns));
  40397. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40398. DYNAMIC_TYPE_OPENSSL));
  40399. if (fields_copy != NULL) {
  40400. XMEMCPY(fields_copy, fields, sizeof(fields));
  40401. }
  40402. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40403. if (EXPECT_FAIL()) {
  40404. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  40405. }
  40406. BIO_free(bio);
  40407. bio = NULL;
  40408. /* Test write */
  40409. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40410. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  40411. BIO_free(bio);
  40412. /* Test index */
  40413. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40414. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40415. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40416. fields[3] = "12DA";
  40417. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40418. fields[3] = "FFFF";
  40419. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40420. fields[3] = "";
  40421. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40422. TXT_DB_free(db);
  40423. #endif
  40424. return EXPECT_RESULT();
  40425. }
  40426. static int test_wolfSSL_NCONF(void)
  40427. {
  40428. EXPECT_DECLS;
  40429. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40430. const char* confFile = "./tests/NCONF_test.cnf";
  40431. CONF* conf = NULL;
  40432. long eline = 0;
  40433. long num = 0;
  40434. ExpectNotNull(conf = NCONF_new(NULL));
  40435. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40436. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40437. ExpectIntEQ(num, 1234);
  40438. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40439. ExpectIntEQ(num, 4321);
  40440. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40441. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40442. "./test-dir/file1");
  40443. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40444. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40445. NCONF_free(conf);
  40446. #endif
  40447. return EXPECT_RESULT();
  40448. }
  40449. #endif /* OPENSSL_ALL */
  40450. static int test_wolfSSL_X509V3_EXT_get(void) {
  40451. EXPECT_DECLS;
  40452. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40453. XFILE f = XBADFILE;
  40454. int numOfExt =0;
  40455. int extNid = 0;
  40456. int i = 0;
  40457. WOLFSSL_X509* x509 = NULL;
  40458. WOLFSSL_X509_EXTENSION* ext = NULL;
  40459. const WOLFSSL_v3_ext_method* method = NULL;
  40460. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40461. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40462. if (f != XBADFILE)
  40463. XFCLOSE(f);
  40464. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  40465. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40466. for (i = 0; i < numOfExt; i++) {
  40467. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40468. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  40469. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40470. ExpectIntEQ(method->ext_nid, extNid);
  40471. }
  40472. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  40473. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40474. wolfSSL_X509_free(x509);
  40475. #endif
  40476. return EXPECT_RESULT();
  40477. }
  40478. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40479. {
  40480. EXPECT_DECLS;
  40481. #ifdef OPENSSL_ALL
  40482. const char *ext_names[] = {
  40483. "subjectKeyIdentifier",
  40484. "authorityKeyIdentifier",
  40485. "subjectAltName",
  40486. "keyUsage",
  40487. "extendedKeyUsage",
  40488. };
  40489. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40490. int ext_nids[] = {
  40491. NID_subject_key_identifier,
  40492. NID_authority_key_identifier,
  40493. NID_subject_alt_name,
  40494. NID_key_usage,
  40495. NID_ext_key_usage,
  40496. };
  40497. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40498. const char *ext_values[] = {
  40499. "hash",
  40500. "hash",
  40501. "DNS:example.com, IP:127.0.0.1",
  40502. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  40503. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  40504. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  40505. "OCSPSigning",
  40506. };
  40507. size_t i;
  40508. X509_EXTENSION* ext = NULL;
  40509. X509* x509 = NULL;
  40510. unsigned int keyUsageFlags;
  40511. unsigned int extKeyUsageFlags;
  40512. ExpectNotNull(x509 = X509_new());
  40513. /* keyUsage / extKeyUsage should match string above */
  40514. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40515. | KU_NON_REPUDIATION
  40516. | KU_KEY_ENCIPHERMENT
  40517. | KU_DATA_ENCIPHERMENT
  40518. | KU_KEY_AGREEMENT
  40519. | KU_KEY_CERT_SIGN
  40520. | KU_CRL_SIGN
  40521. | KU_ENCIPHER_ONLY
  40522. | KU_DECIPHER_ONLY;
  40523. extKeyUsageFlags = XKU_SSL_CLIENT
  40524. | XKU_SSL_SERVER
  40525. | XKU_CODE_SIGN
  40526. | XKU_SMIME
  40527. | XKU_TIMESTAMP
  40528. | XKU_OCSP_SIGN;
  40529. for (i = 0; i < ext_names_count; i++) {
  40530. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  40531. ext_values[i]));
  40532. X509_EXTENSION_free(ext);
  40533. ext = NULL;
  40534. }
  40535. for (i = 0; i < ext_nids_count; i++) {
  40536. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  40537. ext_values[i]));
  40538. X509_EXTENSION_free(ext);
  40539. ext = NULL;
  40540. }
  40541. /* Test adding extension to X509 */
  40542. for (i = 0; i < ext_nids_count; i++) {
  40543. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  40544. ext_values[i]));
  40545. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40546. if (ext_nids[i] == NID_key_usage) {
  40547. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40548. }
  40549. else if (ext_nids[i] == NID_ext_key_usage) {
  40550. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40551. }
  40552. X509_EXTENSION_free(ext);
  40553. ext = NULL;
  40554. }
  40555. X509_free(x509);
  40556. #endif
  40557. return EXPECT_RESULT();
  40558. }
  40559. static int test_wolfSSL_X509V3_EXT(void) {
  40560. EXPECT_DECLS;
  40561. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40562. XFILE f = XBADFILE;
  40563. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  40564. char* str = NULL;
  40565. unsigned char* data = NULL;
  40566. const WOLFSSL_v3_ext_method* method = NULL;
  40567. WOLFSSL_X509* x509 = NULL;
  40568. WOLFSSL_X509_EXTENSION* ext = NULL;
  40569. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  40570. WOLFSSL_ASN1_OBJECT *obj = NULL;
  40571. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  40572. WOLFSSL_ASN1_STRING* asn1str = NULL;
  40573. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  40574. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  40575. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  40576. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  40577. WOLFSSL_GENERAL_NAME* gn = NULL;
  40578. /* Check NULL argument */
  40579. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40580. /* Using OCSP cert with X509V3 extensions */
  40581. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40582. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40583. if (f != XBADFILE)
  40584. XFCLOSE(f);
  40585. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40586. /* Basic Constraints */
  40587. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40588. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40589. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40590. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40591. ExpectIntEQ(bc->ca, 1);
  40592. ExpectNull(bc->pathlen);
  40593. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40594. bc = NULL;
  40595. i++;
  40596. /* Subject Key Identifier */
  40597. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40598. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40599. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40600. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40601. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40602. asn1str));
  40603. X509_EXTENSION_free(ext2);
  40604. ext2 = NULL;
  40605. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40606. ExpectNotNull(method->i2s);
  40607. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40608. wolfSSL_ASN1_STRING_free(asn1str);
  40609. asn1str = NULL;
  40610. if (str != NULL) {
  40611. actual = strcmp(str,
  40612. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40613. }
  40614. ExpectIntEQ(actual, 0);
  40615. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40616. str = NULL;
  40617. i++;
  40618. /* Authority Key Identifier */
  40619. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40620. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40621. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40622. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  40623. ext));
  40624. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40625. ExpectNotNull(asn1str = aKeyId->keyid);
  40626. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  40627. asn1str));
  40628. asn1str = NULL;
  40629. if (str != NULL) {
  40630. actual = strcmp(str,
  40631. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40632. }
  40633. ExpectIntEQ(actual, 0);
  40634. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40635. str = NULL;
  40636. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40637. aKeyId = NULL;
  40638. i++;
  40639. /* Key Usage */
  40640. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40641. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40642. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40643. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40644. #if defined(WOLFSSL_QT)
  40645. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40646. #else
  40647. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40648. #endif
  40649. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40650. if (data != NULL) {
  40651. #ifdef BIG_ENDIAN_ORDER
  40652. actual = data[1];
  40653. #else
  40654. actual = data[0];
  40655. #endif
  40656. }
  40657. ExpectIntEQ(actual, expected);
  40658. wolfSSL_ASN1_STRING_free(asn1str);
  40659. asn1str = NULL;
  40660. #if 1
  40661. i++;
  40662. /* Authority Info Access */
  40663. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40664. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40665. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40666. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40667. ext));
  40668. #if defined(WOLFSSL_QT)
  40669. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40670. #else
  40671. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40672. #endif
  40673. /* URI entry is an ACCESS_DESCRIPTION type */
  40674. #if defined(WOLFSSL_QT)
  40675. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40676. #else
  40677. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40678. #endif
  40679. ExpectNotNull(adObj = ad->method);
  40680. /* Make sure nid is OCSP */
  40681. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40682. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40683. ExpectNotNull(gn = ad->location);
  40684. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40685. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40686. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40687. #if defined(WOLFSSL_QT)
  40688. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  40689. #else
  40690. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  40691. #endif
  40692. if (str != NULL) {
  40693. actual = strcmp(str, "http://127.0.0.1:22220");
  40694. }
  40695. ExpectIntEQ(actual, 0);
  40696. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40697. aia = NULL;
  40698. #else
  40699. (void) aia; (void) ad; (void) adObj; (void) gn;
  40700. #endif
  40701. wolfSSL_X509_free(x509);
  40702. #endif
  40703. return EXPECT_RESULT();
  40704. }
  40705. static int test_wolfSSL_X509_get_extension_flags(void)
  40706. {
  40707. EXPECT_DECLS;
  40708. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40709. XFILE f = XBADFILE;
  40710. X509* x509 = NULL;
  40711. unsigned int extFlags;
  40712. unsigned int keyUsageFlags;
  40713. unsigned int extKeyUsageFlags;
  40714. /* client-int-cert.pem has the following extension flags. */
  40715. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40716. /* and the following key usage flags. */
  40717. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40718. | KU_NON_REPUDIATION
  40719. | KU_KEY_ENCIPHERMENT;
  40720. /* and the following extended key usage flags. */
  40721. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40722. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  40723. XBADFILE);
  40724. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40725. if (f != XBADFILE) {
  40726. XFCLOSE(f);
  40727. f = XBADFILE;
  40728. }
  40729. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  40730. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40731. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40732. X509_free(x509);
  40733. x509 = NULL;
  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. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  40741. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40742. if (f != XBADFILE)
  40743. XFCLOSE(f);
  40744. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  40745. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40746. X509_free(x509);
  40747. #endif /* OPENSSL_ALL */
  40748. return EXPECT_RESULT();
  40749. }
  40750. static int test_wolfSSL_X509_get_ext(void)
  40751. {
  40752. EXPECT_DECLS;
  40753. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40754. int ret = 0;
  40755. XFILE f = XBADFILE;
  40756. WOLFSSL_X509* x509 = NULL;
  40757. WOLFSSL_X509_EXTENSION* foundExtension;
  40758. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40759. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40760. if (f != XBADFILE)
  40761. XFCLOSE(f);
  40762. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40763. /* wolfSSL_X509_get_ext() valid input */
  40764. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40765. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  40766. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40767. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40768. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  40769. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40770. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40771. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  40772. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40773. wolfSSL_X509_free(x509);
  40774. #endif
  40775. return EXPECT_RESULT();
  40776. }
  40777. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40778. {
  40779. EXPECT_DECLS;
  40780. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40781. int rc;
  40782. XFILE f = XBADFILE;
  40783. WOLFSSL_X509* x509 = NULL;
  40784. ASN1_OBJECT* obj = NULL;
  40785. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40786. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40787. if (f != XBADFILE)
  40788. XFCLOSE(f);
  40789. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40790. -1), 0);
  40791. /* Start search from last location (should fail) */
  40792. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40793. rc), -1);
  40794. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40795. -2), -1);
  40796. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  40797. -1), -1);
  40798. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  40799. /* NID_ext_key_usage, check also its nid and oid */
  40800. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  40801. -1);
  40802. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  40803. x509, rc)));
  40804. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  40805. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40806. wolfSSL_X509_free(x509);
  40807. #endif
  40808. return EXPECT_RESULT();
  40809. }
  40810. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40811. {
  40812. EXPECT_DECLS;
  40813. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40814. int rc;
  40815. XFILE f = XBADFILE;
  40816. WOLFSSL_X509* x509 = NULL;
  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. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40824. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40825. if (f != XBADFILE)
  40826. XFCLOSE(f);
  40827. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  40828. ExpectNotNull(ext = X509_get_ext(x509, rc));
  40829. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  40830. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40831. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  40832. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40833. X509_free(x509);
  40834. #endif
  40835. return EXPECT_RESULT();
  40836. }
  40837. static int test_wolfSSL_X509_EXTENSION_new(void)
  40838. {
  40839. EXPECT_DECLS;
  40840. #if defined (OPENSSL_ALL)
  40841. WOLFSSL_X509_EXTENSION* ext = NULL;
  40842. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40843. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40844. wolfSSL_X509_EXTENSION_free(ext);
  40845. #endif
  40846. return EXPECT_RESULT();
  40847. }
  40848. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40849. {
  40850. EXPECT_DECLS;
  40851. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40852. WOLFSSL_X509* x509 = NULL;
  40853. WOLFSSL_X509_EXTENSION* ext = NULL;
  40854. WOLFSSL_ASN1_OBJECT* o = NULL;
  40855. XFILE file = XBADFILE;
  40856. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40857. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40858. if (file != XBADFILE)
  40859. XFCLOSE(file);
  40860. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  40861. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40862. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  40863. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40864. ExpectIntEQ(o->nid, 128);
  40865. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  40866. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40867. wolfSSL_X509_free(x509);
  40868. #endif
  40869. return EXPECT_RESULT();
  40870. }
  40871. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40872. {
  40873. EXPECT_DECLS;
  40874. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40875. WOLFSSL_X509* x509 = NULL;
  40876. WOLFSSL_X509_EXTENSION* ext;
  40877. WOLFSSL_ASN1_STRING* str;
  40878. XFILE file = XBADFILE;
  40879. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40880. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40881. if (file != XBADFILE)
  40882. XFCLOSE(file);
  40883. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40884. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  40885. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40886. wolfSSL_X509_free(x509);
  40887. #endif
  40888. return EXPECT_RESULT();
  40889. }
  40890. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40891. {
  40892. EXPECT_DECLS;
  40893. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40894. WOLFSSL_X509* x509 = NULL;
  40895. WOLFSSL_X509_EXTENSION* ext;
  40896. XFILE file = XBADFILE;
  40897. int crit;
  40898. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40899. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40900. if (file != XBADFILE)
  40901. XFCLOSE(file);
  40902. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40903. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  40904. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  40905. wolfSSL_X509_free(x509);
  40906. #endif
  40907. return EXPECT_RESULT();
  40908. }
  40909. static int test_wolfSSL_X509V3_EXT_print(void)
  40910. {
  40911. EXPECT_DECLS;
  40912. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40913. !defined(NO_RSA)
  40914. {
  40915. XFILE f = XBADFILE;
  40916. WOLFSSL_X509* x509 = NULL;
  40917. X509_EXTENSION * ext = NULL;
  40918. int loc;
  40919. BIO *bio = NULL;
  40920. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40921. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40922. if (f != XBADFILE)
  40923. fclose(f);
  40924. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40925. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40926. NID_basic_constraints, -1), -1);
  40927. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40928. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40929. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40930. NID_subject_key_identifier, -1), -1);
  40931. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40932. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40933. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40934. NID_authority_key_identifier, -1), -1);
  40935. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40936. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40937. wolfSSL_BIO_free(bio);
  40938. wolfSSL_X509_free(x509);
  40939. }
  40940. {
  40941. X509 *x509 = NULL;
  40942. BIO *bio = NULL;
  40943. X509_EXTENSION *ext;
  40944. unsigned int i;
  40945. unsigned int idx;
  40946. /* Some NIDs to test with */
  40947. int nids[] = {
  40948. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40949. * string, which messes up X509V3_EXT_print */
  40950. /* NID_ext_key_usage, */
  40951. NID_subject_alt_name,
  40952. };
  40953. int* n;
  40954. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  40955. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40956. WOLFSSL_FILETYPE_PEM));
  40957. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  40958. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40959. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40960. * update the index */
  40961. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40962. ExpectNotNull(ext = X509_get_ext(x509, idx));
  40963. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40964. ExpectIntGT(fprintf(stderr, "\n"), 0);
  40965. }
  40966. BIO_free(bio);
  40967. X509_free(x509);
  40968. }
  40969. #endif
  40970. return EXPECT_RESULT();
  40971. }
  40972. static int test_wolfSSL_X509_cmp(void)
  40973. {
  40974. EXPECT_DECLS;
  40975. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40976. XFILE file1 = XBADFILE;
  40977. XFILE file2 = XBADFILE;
  40978. WOLFSSL_X509* cert1 = NULL;
  40979. WOLFSSL_X509* cert2 = NULL;
  40980. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40981. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  40982. XBADFILE);
  40983. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40984. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40985. if (file1 != XBADFILE)
  40986. fclose(file1);
  40987. if (file2 != XBADFILE)
  40988. fclose(file2);
  40989. /* wolfSSL_X509_cmp() testing matching certs */
  40990. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40991. /* wolfSSL_X509_cmp() testing mismatched certs */
  40992. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40993. /* wolfSSL_X509_cmp() testing NULL, valid args */
  40994. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40995. /* wolfSSL_X509_cmp() testing valid, NULL args */
  40996. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40997. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  40998. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40999. wolfSSL_X509_free(cert1);
  41000. wolfSSL_X509_free(cert2);
  41001. #endif
  41002. return EXPECT_RESULT();
  41003. }
  41004. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  41005. {
  41006. EXPECT_DECLS;
  41007. #if defined(OPENSSL_ALL)
  41008. EVP_PKEY* pkey;
  41009. pkey = EVP_PKEY_new();
  41010. ExpectNotNull(pkey);
  41011. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  41012. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  41013. EVP_PKEY_free(pkey);
  41014. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  41015. EVP_PKEY_free(pkey);
  41016. EVP_PKEY_free(pkey);
  41017. #endif
  41018. return EXPECT_RESULT();
  41019. }
  41020. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  41021. {
  41022. EXPECT_DECLS;
  41023. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  41024. EVP_PKEY* pkey = NULL;
  41025. const unsigned char* p;
  41026. unsigned char *der = NULL;
  41027. unsigned char *tmp = NULL;
  41028. int derLen;
  41029. p = client_keypub_der_2048;
  41030. /* Check that key can be successfully decoded. */
  41031. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  41032. sizeof_client_keypub_der_2048));
  41033. /* Check that key can be successfully encoded. */
  41034. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  41035. /* Ensure that the encoded version matches the original. */
  41036. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  41037. ExpectIntEQ(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. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  41042. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  41043. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  41044. ExpectTrue(der + derLen == tmp);
  41045. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41046. EVP_PKEY_free(pkey);
  41047. #endif
  41048. return EXPECT_RESULT();
  41049. }
  41050. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  41051. {
  41052. EXPECT_DECLS;
  41053. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  41054. !defined(NO_ASN) && !defined(NO_PWDBASED)
  41055. EVP_PKEY* pkey = NULL;
  41056. const unsigned char* p;
  41057. unsigned char *der = NULL;
  41058. unsigned char *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 = NULL;
  41064. EC_KEY* ephemeral_key = NULL;
  41065. const EC_POINT* h;
  41066. /* Generate an x963 key pair and get public part into pub_buf */
  41067. ExpectNotNull(ctx = BN_CTX_new());
  41068. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  41069. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  41070. NID_X9_62_prime256v1));
  41071. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  41072. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  41073. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  41074. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  41075. /* Prepare the EVP_PKEY */
  41076. ExpectNotNull(pkey = EVP_PKEY_new());
  41077. p = pub_buf;
  41078. /* Check that key can be successfully decoded. */
  41079. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  41080. pub_len));
  41081. /* Check that key can be successfully encoded. */
  41082. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  41083. /* Ensure that the encoded version matches the original. */
  41084. ExpectIntEQ(derLen, pub_len);
  41085. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  41086. /* Do same test except with pre-allocated buffer to ensure the der pointer
  41087. * is advanced. */
  41088. tmp = der;
  41089. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  41090. ExpectIntEQ(derLen, pub_len);
  41091. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  41092. ExpectTrue(der + derLen == tmp);
  41093. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41094. EVP_PKEY_free(pkey);
  41095. EC_KEY_free(ephemeral_key);
  41096. EC_GROUP_free(curve);
  41097. #endif
  41098. return EXPECT_RESULT();
  41099. }
  41100. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  41101. {
  41102. EXPECT_DECLS;
  41103. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  41104. DSA* dsa = NULL;
  41105. byte derIn[] = {
  41106. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  41107. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  41108. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  41109. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  41110. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  41111. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  41112. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  41113. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  41114. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  41115. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  41116. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  41117. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  41118. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  41119. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  41120. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  41121. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  41122. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  41123. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  41124. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  41125. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  41126. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  41127. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  41128. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  41129. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  41130. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  41131. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  41132. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  41133. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  41134. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  41135. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  41136. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  41137. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  41138. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  41139. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  41140. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  41141. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  41142. 0x1e, 0xde, 0xc4
  41143. };
  41144. int derInLen = sizeof(derIn);
  41145. byte* derOut = NULL;
  41146. int derOutLen;
  41147. byte* p = derIn;
  41148. /* Check that params can be successfully decoded. */
  41149. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  41150. /* Check that params can be successfully encoded. */
  41151. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  41152. /* Ensure that the encoded version matches the original. */
  41153. ExpectIntEQ(derInLen, derOutLen);
  41154. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  41155. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41156. DSA_free(dsa);
  41157. #endif
  41158. return EXPECT_RESULT();
  41159. }
  41160. static int test_wolfSSL_i2d_PrivateKey(void)
  41161. {
  41162. EXPECT_DECLS;
  41163. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  41164. !defined(NO_ASN) && !defined(NO_PWDBASED)
  41165. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41166. {
  41167. EVP_PKEY* pkey = NULL;
  41168. const unsigned char* server_key =
  41169. (const unsigned char*)server_key_der_2048;
  41170. unsigned char buf[FOURK_BUF];
  41171. unsigned char* pt = NULL;
  41172. int bufSz;
  41173. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  41174. (long)sizeof_server_key_der_2048));
  41175. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  41176. pt = buf;
  41177. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  41178. ExpectIntNE((pt - buf), 0);
  41179. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  41180. EVP_PKEY_free(pkey);
  41181. }
  41182. #endif
  41183. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41184. {
  41185. EVP_PKEY* pkey = NULL;
  41186. const unsigned char* client_key =
  41187. (const unsigned char*)ecc_clikey_der_256;
  41188. unsigned char buf[FOURK_BUF];
  41189. unsigned char* pt = NULL;
  41190. int bufSz;
  41191. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  41192. (long)sizeof_ecc_clikey_der_256)));
  41193. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  41194. pt = buf;
  41195. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  41196. ExpectIntNE((pt - buf), 0);
  41197. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  41198. EVP_PKEY_free(pkey);
  41199. }
  41200. #endif
  41201. #endif
  41202. return EXPECT_RESULT();
  41203. }
  41204. static int test_wolfSSL_OCSP_id_get0_info(void)
  41205. {
  41206. EXPECT_DECLS;
  41207. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  41208. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41209. X509* cert = NULL;
  41210. X509* issuer = NULL;
  41211. OCSP_CERTID* id = NULL;
  41212. OCSP_CERTID* id2 = NULL;
  41213. ASN1_STRING* name = NULL;
  41214. ASN1_OBJECT* pmd = NULL;
  41215. ASN1_STRING* keyHash = NULL;
  41216. ASN1_INTEGER* serial = NULL;
  41217. ASN1_INTEGER* x509Int;
  41218. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  41219. SSL_FILETYPE_PEM));
  41220. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  41221. SSL_FILETYPE_PEM));
  41222. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  41223. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  41224. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  41225. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  41226. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  41227. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  41228. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  41229. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  41230. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  41231. ExpectNotNull(serial);
  41232. /* compare serial number to one in cert, should be equal */
  41233. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  41234. ExpectIntEQ(x509Int->length, serial->length);
  41235. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  41236. /* test OCSP_id_cmp */
  41237. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  41238. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  41239. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  41240. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  41241. if (id != NULL) {
  41242. id->issuerHash[0] = ~id->issuerHash[0];
  41243. }
  41244. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  41245. OCSP_CERTID_free(id);
  41246. OCSP_CERTID_free(id2);
  41247. X509_free(cert); /* free's x509Int */
  41248. X509_free(issuer);
  41249. #endif
  41250. return EXPECT_RESULT();
  41251. }
  41252. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  41253. {
  41254. EXPECT_DECLS;
  41255. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41256. WOLFSSL_OCSP_CERTID certId;
  41257. byte* targetBuffer = NULL;
  41258. byte* p;
  41259. /* OCSP CertID bytes taken from PCAP */
  41260. byte rawCertId[] = {
  41261. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41262. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41263. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41264. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41265. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41266. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41267. 0xcf, 0xbc, 0x91
  41268. };
  41269. int ret = 0;
  41270. int i;
  41271. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  41272. certId.rawCertId = rawCertId;
  41273. certId.rawCertIdSize = sizeof(rawCertId);
  41274. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  41275. DYNAMIC_TYPE_TMP_BUFFER));
  41276. p = targetBuffer;
  41277. /* Function returns the size of the encoded data. */
  41278. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  41279. /* If target buffer is not null, function increments targetBuffer to point
  41280. * just past the end of the encoded data. */
  41281. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  41282. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  41283. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  41284. }
  41285. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41286. targetBuffer = NULL;
  41287. /* If target buffer is null, function allocates memory for a buffer and
  41288. * copies the encoded data into it. targetBuffer then points to the start of
  41289. * this newly allocate buffer. */
  41290. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  41291. sizeof(rawCertId));
  41292. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  41293. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  41294. }
  41295. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  41296. #endif
  41297. return EXPECT_RESULT();
  41298. }
  41299. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  41300. {
  41301. EXPECT_DECLS;
  41302. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41303. WOLFSSL_OCSP_CERTID* certId;
  41304. WOLFSSL_OCSP_CERTID* certIdGood;
  41305. WOLFSSL_OCSP_CERTID* certIdBad;
  41306. const unsigned char* rawCertIdPtr;
  41307. const unsigned char rawCertId[] = {
  41308. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41309. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41310. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41311. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41312. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41313. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41314. 0xcf, 0xbc, 0x91
  41315. };
  41316. rawCertIdPtr = &rawCertId[0];
  41317. /* If the cert ID is NULL the function should allocate it and copy the
  41318. * data to it. */
  41319. certId = NULL;
  41320. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  41321. sizeof(rawCertId)));
  41322. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41323. if (certId != NULL) {
  41324. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41325. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  41326. }
  41327. /* If the cert ID is not NULL the function will just copy the data to it. */
  41328. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  41329. DYNAMIC_TYPE_TMP_BUFFER));
  41330. ExpectNotNull(certId);
  41331. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  41332. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  41333. rawCertIdPtr = &rawCertId[0];
  41334. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  41335. sizeof(rawCertId)));
  41336. ExpectPtrEq(certIdGood, certId);
  41337. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41338. if (certId != NULL) {
  41339. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41340. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41341. certId = NULL;
  41342. }
  41343. /* The below tests should fail when passed bad parameters. NULL should
  41344. * always be returned. */
  41345. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  41346. sizeof(rawCertId)));
  41347. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  41348. sizeof(rawCertId)));
  41349. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  41350. #endif
  41351. return EXPECT_RESULT();
  41352. }
  41353. static int test_wolfSSL_OCSP_id_cmp(void)
  41354. {
  41355. EXPECT_DECLS;
  41356. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41357. OCSP_CERTID id1;
  41358. OCSP_CERTID id2;
  41359. XMEMSET(&id1, 0, sizeof(id1));
  41360. XMEMSET(&id2, 0, sizeof(id2));
  41361. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  41362. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  41363. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  41364. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  41365. #endif
  41366. return EXPECT_RESULT();
  41367. }
  41368. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  41369. {
  41370. EXPECT_DECLS;
  41371. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41372. WOLFSSL_OCSP_SINGLERESP single;
  41373. const WOLFSSL_OCSP_CERTID* certId;
  41374. XMEMSET(&single, 0, sizeof(single));
  41375. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  41376. ExpectPtrEq(&single, certId);
  41377. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  41378. #endif
  41379. return EXPECT_RESULT();
  41380. }
  41381. static int test_wolfSSL_OCSP_single_get0_status(void)
  41382. {
  41383. EXPECT_DECLS;
  41384. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41385. WOLFSSL_OCSP_SINGLERESP single;
  41386. CertStatus certStatus;
  41387. WOLFSSL_ASN1_TIME* thisDate;
  41388. WOLFSSL_ASN1_TIME* nextDate;
  41389. int ret, i;
  41390. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41391. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  41392. /* Fill the date fields with some dummy data. */
  41393. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  41394. certStatus.thisDateParsed.data[i] = i;
  41395. certStatus.nextDateParsed.data[i] = i;
  41396. }
  41397. certStatus.status = CERT_GOOD;
  41398. single.status = &certStatus;
  41399. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  41400. &nextDate);
  41401. ExpectIntEQ(ret, CERT_GOOD);
  41402. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  41403. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  41404. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  41405. CERT_GOOD);
  41406. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  41407. NULL), CERT_GOOD);
  41408. #endif
  41409. return EXPECT_RESULT();
  41410. }
  41411. static int test_wolfSSL_OCSP_resp_count(void)
  41412. {
  41413. EXPECT_DECLS;
  41414. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41415. WOLFSSL_OCSP_BASICRESP basicResp;
  41416. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41417. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41418. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41419. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41420. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41421. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  41422. basicResp.single = &singleRespOne;
  41423. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  41424. singleRespOne.next = &singleRespTwo;
  41425. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  41426. #endif
  41427. return EXPECT_RESULT();
  41428. }
  41429. static int test_wolfSSL_OCSP_resp_get0(void)
  41430. {
  41431. EXPECT_DECLS;
  41432. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41433. WOLFSSL_OCSP_BASICRESP basicResp;
  41434. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41435. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41436. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41437. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41438. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41439. basicResp.single = &singleRespOne;
  41440. singleRespOne.next = &singleRespTwo;
  41441. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  41442. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  41443. #endif
  41444. return EXPECT_RESULT();
  41445. }
  41446. static int test_wolfSSL_EVP_PKEY_derive(void)
  41447. {
  41448. EXPECT_DECLS;
  41449. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41450. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41451. EVP_PKEY_CTX *ctx = NULL;
  41452. unsigned char *skey = NULL;
  41453. size_t skeylen;
  41454. EVP_PKEY *pkey = NULL;
  41455. EVP_PKEY *peerkey = NULL;
  41456. const unsigned char* key;
  41457. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41458. /* DH */
  41459. key = dh_key_der_2048;
  41460. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41461. sizeof_dh_key_der_2048)));
  41462. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41463. key = dh_key_der_2048;
  41464. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41465. sizeof_dh_key_der_2048)));
  41466. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41467. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41468. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41469. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41470. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41471. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  41472. DYNAMIC_TYPE_OPENSSL));
  41473. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41474. EVP_PKEY_CTX_free(ctx);
  41475. ctx = NULL;
  41476. EVP_PKEY_free(peerkey);
  41477. peerkey = NULL;
  41478. EVP_PKEY_free(pkey);
  41479. pkey = NULL;
  41480. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41481. skey = NULL;
  41482. #endif
  41483. #ifdef HAVE_ECC
  41484. /* ECDH */
  41485. key = ecc_clikey_der_256;
  41486. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41487. sizeof_ecc_clikey_der_256)));
  41488. key = ecc_clikeypub_der_256;
  41489. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41490. sizeof_ecc_clikeypub_der_256)));
  41491. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41492. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41493. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41494. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41495. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  41496. DYNAMIC_TYPE_OPENSSL));
  41497. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41498. EVP_PKEY_CTX_free(ctx);
  41499. EVP_PKEY_free(peerkey);
  41500. EVP_PKEY_free(pkey);
  41501. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41502. #endif /* HAVE_ECC */
  41503. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41504. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41505. return EXPECT_RESULT();
  41506. }
  41507. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41508. {
  41509. EXPECT_DECLS;
  41510. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41511. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41512. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41513. int ret;
  41514. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41515. int pwdlen = sizeof(pwd);
  41516. const byte salt[] = {'N','a','C','l'};
  41517. int saltlen = sizeof(salt);
  41518. byte key[80];
  41519. word64 numOvr32 = (word64)INT32_MAX + 1;
  41520. /* expected derived key for N:16, r:1, p:1 */
  41521. const byte expectedKey[] = {
  41522. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41523. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41524. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41525. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41526. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41527. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41528. 0xE7, 0xE9, 0xC0, 0x9A};
  41529. /* N r p mx key keylen */
  41530. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41531. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  41532. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41533. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  41534. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41535. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  41536. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41537. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  41538. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41539. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  41540. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41541. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  41542. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41543. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  41544. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41545. ExpectIntEQ(ret, 1); /* should succeed */
  41546. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41547. key, 64);
  41548. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41549. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41550. key, 64);
  41551. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41552. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41553. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41554. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41555. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41556. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41557. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41558. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41559. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41560. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41561. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41562. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41563. ExpectIntEQ(ret, 1);
  41564. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41565. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41566. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41567. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41568. return EXPECT_RESULT();
  41569. }
  41570. static int test_no_op_functions(void)
  41571. {
  41572. EXPECT_DECLS;
  41573. #if defined(OPENSSL_EXTRA)
  41574. /* this makes sure wolfSSL can compile and run these no-op functions */
  41575. SSL_load_error_strings();
  41576. ENGINE_load_builtin_engines();
  41577. OpenSSL_add_all_ciphers();
  41578. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  41579. #endif
  41580. return EXPECT_RESULT();
  41581. }
  41582. static int test_wolfSSL_CRYPTO_memcmp(void)
  41583. {
  41584. EXPECT_DECLS;
  41585. #ifdef OPENSSL_EXTRA
  41586. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41587. "implementation of TLS/SSL for embedded devices to the cloud.";
  41588. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41589. "implementation of TLS/SSL for embedded devices to the cloud.";
  41590. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41591. "implementation of TLS/SSL for embedded devices to the cloud!";
  41592. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41593. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41594. #endif
  41595. return EXPECT_RESULT();
  41596. }
  41597. /*----------------------------------------------------------------------------*
  41598. | wolfCrypt ASN
  41599. *----------------------------------------------------------------------------*/
  41600. static int test_wc_CreateEncryptedPKCS8Key(void)
  41601. {
  41602. EXPECT_DECLS;
  41603. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41604. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41605. WC_RNG rng;
  41606. byte* encKey = NULL;
  41607. word32 encKeySz = 0;
  41608. word32 decKeySz = 0;
  41609. const char password[] = "Lorem ipsum dolor sit amet";
  41610. word32 passwordSz = (word32)XSTRLEN(password);
  41611. word32 tradIdx = 0;
  41612. XMEMSET(&rng, 0, sizeof(WC_RNG));
  41613. ExpectIntEQ(wc_InitRng(&rng), 0);
  41614. /* Call with NULL for out buffer to get necessary length. */
  41615. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41616. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41617. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41618. LENGTH_ONLY_E);
  41619. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41620. DYNAMIC_TYPE_TMP_BUFFER));
  41621. /* Call with the allocated out buffer. */
  41622. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41623. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41624. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41625. 0);
  41626. /* Decrypt the encrypted PKCS8 key we just made. */
  41627. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41628. passwordSz)), 0);
  41629. /* encKey now holds the decrypted key (decrypted in place). */
  41630. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41631. /* Check that the decrypted key matches the key prior to encryption. */
  41632. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41633. sizeof_server_key_der_2048), 0);
  41634. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41635. wc_FreeRng(&rng);
  41636. #endif
  41637. return EXPECT_RESULT();
  41638. }
  41639. static int test_wc_GetPkcs8TraditionalOffset(void)
  41640. {
  41641. EXPECT_DECLS;
  41642. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41643. int length;
  41644. int derSz = 0;
  41645. word32 inOutIdx;
  41646. const char* path = "./certs/server-keyPkcs8.der";
  41647. XFILE file = XBADFILE;
  41648. byte der[2048];
  41649. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  41650. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  41651. if (file != XBADFILE)
  41652. XFCLOSE(file);
  41653. /* valid case */
  41654. inOutIdx = 0;
  41655. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41656. 0);
  41657. /* inOutIdx > sz */
  41658. inOutIdx = 4000;
  41659. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41660. BAD_FUNC_ARG);
  41661. /* null input */
  41662. inOutIdx = 0;
  41663. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  41664. BAD_FUNC_ARG);
  41665. /* invalid input, fill buffer with 1's */
  41666. XMEMSET(der, 1, sizeof(der));
  41667. inOutIdx = 0;
  41668. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41669. ASN_PARSE_E);
  41670. #endif /* NO_ASN */
  41671. return EXPECT_RESULT();
  41672. }
  41673. static int test_wc_SetSubjectRaw(void)
  41674. {
  41675. EXPECT_DECLS;
  41676. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41677. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41678. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41679. WOLFSSL_X509* x509 = NULL;
  41680. int peerCertSz;
  41681. const byte* peerCertBuf = NULL;
  41682. Cert forgedCert;
  41683. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41684. WOLFSSL_FILETYPE_ASN1));
  41685. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41686. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41687. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41688. wolfSSL_FreeX509(x509);
  41689. #endif
  41690. return EXPECT_RESULT();
  41691. }
  41692. static int test_wc_GetSubjectRaw(void)
  41693. {
  41694. EXPECT_DECLS;
  41695. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41696. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41697. Cert cert;
  41698. byte *subjectRaw;
  41699. ExpectIntEQ(0, wc_InitCert(&cert));
  41700. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41701. #endif
  41702. return EXPECT_RESULT();
  41703. }
  41704. static int test_wc_SetIssuerRaw(void)
  41705. {
  41706. EXPECT_DECLS;
  41707. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41708. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41709. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41710. WOLFSSL_X509* x509 = NULL;
  41711. int peerCertSz;
  41712. const byte* peerCertBuf;
  41713. Cert forgedCert;
  41714. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41715. WOLFSSL_FILETYPE_ASN1));
  41716. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41717. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41718. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41719. wolfSSL_FreeX509(x509);
  41720. #endif
  41721. return EXPECT_RESULT();
  41722. }
  41723. static int test_wc_SetIssueBuffer(void)
  41724. {
  41725. EXPECT_DECLS;
  41726. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41727. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41728. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41729. WOLFSSL_X509* x509 = NULL;
  41730. int peerCertSz;
  41731. const byte* peerCertBuf;
  41732. Cert forgedCert;
  41733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41734. WOLFSSL_FILETYPE_ASN1));
  41735. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41736. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41737. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41738. wolfSSL_FreeX509(x509);
  41739. #endif
  41740. return EXPECT_RESULT();
  41741. }
  41742. /*
  41743. * Testing wc_SetSubjectKeyId
  41744. */
  41745. static int test_wc_SetSubjectKeyId(void)
  41746. {
  41747. EXPECT_DECLS;
  41748. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41749. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41750. Cert cert;
  41751. const char* file = "certs/ecc-client-keyPub.pem";
  41752. ExpectIntEQ(0, wc_InitCert(&cert));
  41753. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41754. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41755. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41756. #endif
  41757. return EXPECT_RESULT();
  41758. } /* END test_wc_SetSubjectKeyId */
  41759. /*
  41760. * Testing wc_SetSubject
  41761. */
  41762. static int test_wc_SetSubject(void)
  41763. {
  41764. EXPECT_DECLS;
  41765. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41766. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41767. Cert cert;
  41768. const char* file = "./certs/ca-ecc-cert.pem";
  41769. ExpectIntEQ(0, wc_InitCert(&cert));
  41770. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  41771. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41772. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41773. #endif
  41774. return EXPECT_RESULT();
  41775. } /* END test_wc_SetSubject */
  41776. static int test_CheckCertSignature(void)
  41777. {
  41778. EXPECT_DECLS;
  41779. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41780. WOLFSSL_CERT_MANAGER* cm = NULL;
  41781. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41782. XFILE fp = XBADFILE;
  41783. byte cert[4096];
  41784. int certSz;
  41785. #endif
  41786. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41787. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41788. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41789. #ifndef NO_RSA
  41790. #ifdef USE_CERT_BUFFERS_1024
  41791. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41792. sizeof_server_cert_der_1024, NULL, cm));
  41793. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41794. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41795. WOLFSSL_FILETYPE_ASN1));
  41796. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41797. sizeof_server_cert_der_1024, NULL, cm));
  41798. #elif defined(USE_CERT_BUFFERS_2048)
  41799. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41800. sizeof_server_cert_der_2048, NULL, cm));
  41801. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41802. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41803. WOLFSSL_FILETYPE_ASN1));
  41804. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41805. sizeof_server_cert_der_2048, NULL, cm));
  41806. #endif
  41807. #endif
  41808. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41809. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41810. sizeof_serv_ecc_der_256, NULL, cm));
  41811. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41812. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41813. WOLFSSL_FILETYPE_ASN1));
  41814. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41815. NULL, cm));
  41816. #endif
  41817. #if !defined(NO_FILESYSTEM)
  41818. wolfSSL_CertManagerFree(cm);
  41819. cm = NULL;
  41820. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41821. #ifndef NO_RSA
  41822. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  41823. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41824. if (fp != XBADFILE) {
  41825. XFCLOSE(fp);
  41826. fp = XBADFILE;
  41827. }
  41828. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41829. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41830. "./certs/ca-cert.pem", NULL));
  41831. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41832. #endif
  41833. #ifdef HAVE_ECC
  41834. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  41835. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41836. if (fp != XBADFILE) {
  41837. XFCLOSE(fp);
  41838. fp = XBADFILE;
  41839. }
  41840. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41841. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41842. "./certs/ca-ecc-cert.pem", NULL));
  41843. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41844. #endif
  41845. #endif
  41846. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41847. (void)fp;
  41848. (void)cert;
  41849. (void)certSz;
  41850. #endif
  41851. wolfSSL_CertManagerFree(cm);
  41852. #endif
  41853. return EXPECT_RESULT();
  41854. }
  41855. static int test_wc_ParseCert(void)
  41856. {
  41857. EXPECT_DECLS;
  41858. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41859. DecodedCert decodedCert;
  41860. const byte* rawCert = client_cert_der_2048;
  41861. const int rawCertSize = sizeof_client_cert_der_2048;
  41862. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41863. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41864. #ifndef IGNORE_NAME_CONSTRAINTS
  41865. /* check that the subjects emailAddress was not put in the alt name list */
  41866. ExpectNotNull(decodedCert.subjectEmail);
  41867. ExpectNull(decodedCert.altEmailNames);
  41868. #endif
  41869. wc_FreeDecodedCert(&decodedCert);
  41870. #endif
  41871. return EXPECT_RESULT();
  41872. }
  41873. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  41874. * the API safely errors out on badly-formed ASN input.
  41875. * NOTE: Test not compatible with released FIPS implementations!
  41876. */
  41877. static int test_wc_ParseCert_Error(void)
  41878. {
  41879. EXPECT_DECLS;
  41880. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  41881. (!defined(HAVE_FIPS) || \
  41882. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41883. DecodedCert decodedCert;
  41884. int i;
  41885. /* Certificate data */
  41886. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  41887. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41888. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41889. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  41890. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  41891. /* Test data */
  41892. const struct testStruct {
  41893. const byte* c;
  41894. const int cSz;
  41895. const int expRet;
  41896. } t[] = {
  41897. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  41898. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  41899. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  41900. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  41901. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  41902. };
  41903. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  41904. for (i = 0; i < tSz; i++) {
  41905. WOLFSSL_MSG_EX("i == %d", i);
  41906. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  41907. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  41908. wc_FreeDecodedCert(&decodedCert);
  41909. }
  41910. #endif
  41911. return EXPECT_RESULT();
  41912. }
  41913. static int test_MakeCertWithPathLen(void)
  41914. {
  41915. EXPECT_DECLS;
  41916. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  41917. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41918. const byte expectedPathLen = 7;
  41919. Cert cert;
  41920. DecodedCert decodedCert;
  41921. byte der[FOURK_BUF];
  41922. int derSize = 0;
  41923. WC_RNG rng;
  41924. ecc_key key;
  41925. int ret;
  41926. XMEMSET(&rng, 0, sizeof(WC_RNG));
  41927. XMEMSET(&key, 0, sizeof(ecc_key));
  41928. XMEMSET(&cert, 0, sizeof(Cert));
  41929. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  41930. ExpectIntEQ(wc_InitRng(&rng), 0);
  41931. ExpectIntEQ(wc_ecc_init(&key), 0);
  41932. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41933. ExpectIntEQ(wc_InitCert(&cert), 0);
  41934. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41935. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41936. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41937. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41938. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41939. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  41940. CTC_NAME_SIZE);
  41941. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  41942. CTC_NAME_SIZE);
  41943. cert.selfSigned = 1;
  41944. cert.isCA = 1;
  41945. cert.pathLen = expectedPathLen;
  41946. cert.pathLenSet = 1;
  41947. cert.sigType = CTC_SHA256wECDSA;
  41948. #ifdef WOLFSSL_CERT_EXT
  41949. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41950. #endif
  41951. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41952. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  41953. FOURK_BUF, NULL, &key, &rng), 0);
  41954. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41955. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41956. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  41957. wc_FreeDecodedCert(&decodedCert);
  41958. ret = wc_ecc_free(&key);
  41959. ExpectIntEQ(ret, 0);
  41960. ret = wc_FreeRng(&rng);
  41961. ExpectIntEQ(ret, 0);
  41962. #endif
  41963. return EXPECT_RESULT();
  41964. }
  41965. /*----------------------------------------------------------------------------*
  41966. | wolfCrypt ECC
  41967. *----------------------------------------------------------------------------*/
  41968. static int test_wc_ecc_get_curve_size_from_name(void)
  41969. {
  41970. EXPECT_DECLS;
  41971. #ifdef HAVE_ECC
  41972. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41973. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  41974. #endif
  41975. /* invalid case */
  41976. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  41977. /* NULL input */
  41978. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  41979. #endif /* HAVE_ECC */
  41980. return EXPECT_RESULT();
  41981. }
  41982. static int test_wc_ecc_get_curve_id_from_name(void)
  41983. {
  41984. EXPECT_DECLS;
  41985. #ifdef HAVE_ECC
  41986. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41987. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  41988. ECC_SECP256R1);
  41989. #endif
  41990. /* invalid case */
  41991. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  41992. /* NULL input */
  41993. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  41994. #endif /* HAVE_ECC */
  41995. return EXPECT_RESULT();
  41996. }
  41997. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41998. !defined(HAVE_SELFTEST) && \
  41999. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  42000. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  42001. {
  42002. EXPECT_DECLS;
  42003. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  42004. ecc_key* key;
  42005. const ecc_set_type* params = NULL;
  42006. int ret;
  42007. #endif
  42008. WOLFSSL_EC_KEY *ecKey = NULL;
  42009. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  42010. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  42011. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  42012. if (EXPECT_SUCCESS()) {
  42013. ret = EC_KEY_generate_key(ecKey);
  42014. } else
  42015. ret = 0;
  42016. if (ret == 1) {
  42017. /* normal test */
  42018. key = (ecc_key*)ecKey->internal;
  42019. if (key != NULL) {
  42020. params = key->dp;
  42021. }
  42022. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  42023. ECC_SECP256R1);
  42024. }
  42025. #endif
  42026. /* invalid case, NULL input*/
  42027. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  42028. wolfSSL_EC_KEY_free(ecKey);
  42029. return EXPECT_RESULT();
  42030. }
  42031. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  42032. static int test_wc_ecc_get_curve_id_from_params(void)
  42033. {
  42034. EXPECT_DECLS;
  42035. #ifdef HAVE_ECC
  42036. const byte prime[] =
  42037. {
  42038. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42039. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42040. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42041. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  42042. };
  42043. const byte primeInvalid[] =
  42044. {
  42045. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42047. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42048. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  42049. };
  42050. const byte Af[] =
  42051. {
  42052. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  42053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  42054. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  42055. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  42056. };
  42057. const byte Bf[] =
  42058. {
  42059. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  42060. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  42061. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  42062. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  42063. };
  42064. const byte order[] =
  42065. {
  42066. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  42067. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  42068. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  42069. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  42070. };
  42071. const byte Gx[] =
  42072. {
  42073. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  42074. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  42075. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  42076. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  42077. };
  42078. const byte Gy[] =
  42079. {
  42080. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  42081. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  42082. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  42083. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  42084. };
  42085. int cofactor = 1;
  42086. int fieldSize = 256;
  42087. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  42088. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  42089. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  42090. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  42091. ECC_SECP256R1);
  42092. #endif
  42093. /* invalid case, fieldSize = 0 */
  42094. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  42095. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42096. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  42097. /* invalid case, NULL prime */
  42098. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  42099. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42100. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  42101. /* invalid case, invalid prime */
  42102. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  42103. primeInvalid, sizeof(primeInvalid),
  42104. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  42105. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  42106. #endif
  42107. return EXPECT_RESULT();
  42108. }
  42109. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  42110. {
  42111. EXPECT_DECLS;
  42112. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42113. !defined(HAVE_FAST_RSA)
  42114. WOLFSSL_RSA* rsa = NULL;
  42115. WOLFSSL_EVP_PKEY* pkey = NULL;
  42116. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  42117. const char* in = "What is easy to do is easy not to do.";
  42118. size_t inlen = XSTRLEN(in);
  42119. size_t outEncLen = 0;
  42120. byte* outEnc = NULL;
  42121. byte* outDec = NULL;
  42122. size_t outDecLen = 0;
  42123. size_t rsaKeySz = 2048/8; /* Bytes */
  42124. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42125. byte* inTmp = NULL;
  42126. byte* outEncTmp = NULL;
  42127. byte* outDecTmp = NULL;
  42128. #endif
  42129. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42130. DYNAMIC_TYPE_TMP_BUFFER));
  42131. if (outEnc != NULL) {
  42132. XMEMSET(outEnc, 0, rsaKeySz);
  42133. }
  42134. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42135. DYNAMIC_TYPE_TMP_BUFFER));
  42136. if (outDec != NULL) {
  42137. XMEMSET(outDec, 0, rsaKeySz);
  42138. }
  42139. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  42140. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42141. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42142. if (EXPECT_FAIL()) {
  42143. RSA_free(rsa);
  42144. }
  42145. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42146. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  42147. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42148. WOLFSSL_SUCCESS);
  42149. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  42150. since ctx uses it.*/
  42151. ExpectIntEQ(pkey->ref.count, 2);
  42152. EVP_PKEY_free(pkey);
  42153. ExpectIntEQ(pkey->ref.count, 1);
  42154. /* Encrypt data */
  42155. /* Check that we can get the required output buffer length by passing in a
  42156. * NULL output buffer. */
  42157. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  42158. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42159. ExpectIntEQ(rsaKeySz, outEncLen);
  42160. /* Now do the actual encryption. */
  42161. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  42162. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42163. /* Decrypt data */
  42164. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42165. /* Check that we can get the required output buffer length by passing in a
  42166. * NULL output buffer. */
  42167. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  42168. WOLFSSL_SUCCESS);
  42169. ExpectIntEQ(rsaKeySz, outDecLen);
  42170. /* Now do the actual decryption. */
  42171. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  42172. WOLFSSL_SUCCESS);
  42173. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  42174. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42175. /* The input length must be the same size as the RSA key.*/
  42176. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42177. DYNAMIC_TYPE_TMP_BUFFER));
  42178. if (inTmp != NULL) {
  42179. XMEMSET(inTmp, 9, rsaKeySz);
  42180. }
  42181. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42182. DYNAMIC_TYPE_TMP_BUFFER));
  42183. if (outEncTmp != NULL) {
  42184. XMEMSET(outEncTmp, 0, rsaKeySz);
  42185. }
  42186. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42187. DYNAMIC_TYPE_TMP_BUFFER));
  42188. if (outDecTmp != NULL) {
  42189. XMEMSET(outDecTmp, 0, rsaKeySz);
  42190. }
  42191. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  42192. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42193. WOLFSSL_SUCCESS);
  42194. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  42195. WOLFSSL_SUCCESS);
  42196. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42197. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  42198. outEncLen), WOLFSSL_SUCCESS);
  42199. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  42200. #endif
  42201. EVP_PKEY_CTX_free(ctx);
  42202. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42203. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42204. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42205. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42206. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42207. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42208. #endif
  42209. #endif
  42210. return EXPECT_RESULT();
  42211. }
  42212. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42213. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42214. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42215. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42216. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42217. #endif
  42218. #endif
  42219. #endif
  42220. #if defined(OPENSSL_EXTRA)
  42221. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42222. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42223. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42224. #endif
  42225. #endif
  42226. #endif
  42227. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42228. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42229. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42230. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42231. #endif
  42232. #endif
  42233. #endif
  42234. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42235. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  42236. {
  42237. EXPECT_DECLS;
  42238. #if defined(OPENSSL_EXTRA)
  42239. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42240. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42241. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42242. WOLFSSL_RSA* rsa = NULL;
  42243. #endif
  42244. #endif
  42245. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42246. WOLFSSL_DSA* dsa = NULL;
  42247. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42248. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42249. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42250. WOLFSSL_EC_KEY* ecKey = NULL;
  42251. #endif
  42252. #endif
  42253. WOLFSSL_EVP_PKEY* pkey = NULL;
  42254. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  42255. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  42256. const char* in = "What is easy to do is easy not to do.";
  42257. size_t inlen = XSTRLEN(in);
  42258. byte hash[SHA256_DIGEST_LENGTH] = {0};
  42259. byte zero[SHA256_DIGEST_LENGTH] = {0};
  42260. SHA256_CTX c;
  42261. byte* sig = NULL;
  42262. byte* sigVerify = NULL;
  42263. size_t siglen;
  42264. size_t siglenOnlyLen;
  42265. size_t keySz = 2048/8; /* Bytes */
  42266. ExpectNotNull(sig =
  42267. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42268. ExpectNotNull(sigVerify =
  42269. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42270. siglen = keySz;
  42271. ExpectNotNull(XMEMSET(sig, 0, keySz));
  42272. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  42273. /* Generate hash */
  42274. SHA256_Init(&c);
  42275. SHA256_Update(&c, in, inlen);
  42276. SHA256_Final(hash, &c);
  42277. #ifdef WOLFSSL_SMALL_STACK_CACHE
  42278. /* workaround for small stack cache case */
  42279. wc_Sha256Free((wc_Sha256*)&c);
  42280. #endif
  42281. /* Generate key */
  42282. ExpectNotNull(pkey = EVP_PKEY_new());
  42283. switch (keyType) {
  42284. case EVP_PKEY_RSA:
  42285. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42286. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42287. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42288. {
  42289. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  42290. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42291. }
  42292. #endif
  42293. #endif
  42294. break;
  42295. case EVP_PKEY_DSA:
  42296. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42297. ExpectNotNull(dsa = DSA_new());
  42298. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  42299. NULL, 0, NULL, NULL, NULL), 1);
  42300. ExpectIntEQ(DSA_generate_key(dsa), 1);
  42301. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  42302. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42303. break;
  42304. case EVP_PKEY_EC:
  42305. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42306. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42307. {
  42308. ExpectNotNull(ecKey = EC_KEY_new());
  42309. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  42310. ExpectIntEQ(
  42311. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42312. if (EXPECT_FAIL()) {
  42313. EC_KEY_free(ecKey);
  42314. }
  42315. }
  42316. #endif
  42317. #endif
  42318. break;
  42319. }
  42320. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42321. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42322. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42323. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42324. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42325. if (keyType == EVP_PKEY_RSA)
  42326. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42327. WOLFSSL_SUCCESS);
  42328. #endif
  42329. #endif
  42330. /* Check returning only length */
  42331. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  42332. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42333. ExpectIntGT(siglenOnlyLen, 0);
  42334. /* Sign data */
  42335. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  42336. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42337. ExpectIntGE(siglenOnlyLen, siglen);
  42338. /* Verify signature */
  42339. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  42340. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42341. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42342. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42343. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42344. if (keyType == EVP_PKEY_RSA)
  42345. ExpectIntEQ(
  42346. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  42347. WOLFSSL_SUCCESS);
  42348. #endif
  42349. #endif
  42350. ExpectIntEQ(EVP_PKEY_verify(
  42351. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  42352. WOLFSSL_SUCCESS);
  42353. ExpectIntEQ(EVP_PKEY_verify(
  42354. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  42355. WOLFSSL_FAILURE);
  42356. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42357. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42358. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42359. if (keyType == EVP_PKEY_RSA) {
  42360. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  42361. /* Try RSA sign/verify with no padding. */
  42362. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42363. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42364. WOLFSSL_SUCCESS);
  42365. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42366. siglen), WOLFSSL_SUCCESS);
  42367. ExpectIntGE(siglenOnlyLen, siglen);
  42368. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42369. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42370. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  42371. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42372. siglen), WOLFSSL_SUCCESS);
  42373. #endif
  42374. /* Wrong padding schemes. */
  42375. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42376. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  42377. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42378. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42379. siglen), WOLFSSL_SUCCESS);
  42380. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42381. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42382. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42383. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42384. siglen), WOLFSSL_SUCCESS);
  42385. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42386. WOLFSSL_SUCCESS);
  42387. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42388. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  42389. }
  42390. #endif
  42391. #endif
  42392. /* error cases */
  42393. siglen = keySz; /* Reset because sig size may vary slightly */
  42394. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  42395. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42396. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  42397. WOLFSSL_SUCCESS);
  42398. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  42399. WOLFSSL_SUCCESS);
  42400. EVP_PKEY_free(pkey);
  42401. pkey = NULL;
  42402. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42403. DSA_free(dsa);
  42404. dsa = NULL;
  42405. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42406. EVP_PKEY_CTX_free(ctx_verify);
  42407. ctx_verify = NULL;
  42408. EVP_PKEY_CTX_free(ctx);
  42409. ctx = NULL;
  42410. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42411. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42412. #endif /* OPENSSL_EXTRA */
  42413. return EXPECT_RESULT();
  42414. }
  42415. #endif
  42416. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  42417. {
  42418. EXPECT_DECLS;
  42419. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42420. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42421. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42422. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  42423. #endif
  42424. #endif
  42425. return EXPECT_RESULT();
  42426. }
  42427. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  42428. {
  42429. EXPECT_DECLS;
  42430. #if defined(OPENSSL_EXTRA)
  42431. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42432. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  42433. #endif
  42434. #endif
  42435. return EXPECT_RESULT();
  42436. }
  42437. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  42438. {
  42439. EXPECT_DECLS;
  42440. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42441. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42442. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  42443. #endif
  42444. #endif
  42445. return EXPECT_RESULT();
  42446. }
  42447. static int test_EVP_PKEY_rsa(void)
  42448. {
  42449. EXPECT_DECLS;
  42450. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42451. WOLFSSL_RSA* rsa = NULL;
  42452. WOLFSSL_EVP_PKEY* pkey = NULL;
  42453. ExpectNotNull(rsa = wolfSSL_RSA_new());
  42454. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42455. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  42456. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  42457. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42458. if (EXPECT_FAIL()) {
  42459. wolfSSL_RSA_free(rsa);
  42460. }
  42461. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  42462. wolfSSL_EVP_PKEY_free(pkey);
  42463. #endif
  42464. return EXPECT_RESULT();
  42465. }
  42466. static int test_EVP_PKEY_ec(void)
  42467. {
  42468. EXPECT_DECLS;
  42469. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42470. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42471. WOLFSSL_EC_KEY* ecKey = NULL;
  42472. WOLFSSL_EVP_PKEY* pkey = NULL;
  42473. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42474. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42475. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42476. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42477. /* Should fail since ecKey is empty */
  42478. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42479. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42480. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42481. if (EXPECT_FAIL()) {
  42482. wolfSSL_EC_KEY_free(ecKey);
  42483. }
  42484. wolfSSL_EVP_PKEY_free(pkey);
  42485. #endif
  42486. #endif
  42487. return EXPECT_RESULT();
  42488. }
  42489. static int test_EVP_PKEY_cmp(void)
  42490. {
  42491. EXPECT_DECLS;
  42492. #if defined(OPENSSL_EXTRA)
  42493. EVP_PKEY *a = NULL;
  42494. EVP_PKEY *b = NULL;
  42495. const unsigned char *in;
  42496. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42497. in = client_key_der_2048;
  42498. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42499. &in, (long)sizeof_client_key_der_2048));
  42500. in = client_key_der_2048;
  42501. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42502. &in, (long)sizeof_client_key_der_2048));
  42503. /* Test success case RSA */
  42504. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42505. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  42506. #else
  42507. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42508. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42509. EVP_PKEY_free(b);
  42510. b = NULL;
  42511. EVP_PKEY_free(a);
  42512. a = NULL;
  42513. #endif
  42514. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42515. in = ecc_clikey_der_256;
  42516. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42517. &in, (long)sizeof_ecc_clikey_der_256));
  42518. in = ecc_clikey_der_256;
  42519. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42520. &in, (long)sizeof_ecc_clikey_der_256));
  42521. /* Test success case ECC */
  42522. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42523. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  42524. #else
  42525. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42526. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42527. EVP_PKEY_free(b);
  42528. b = NULL;
  42529. EVP_PKEY_free(a);
  42530. a = NULL;
  42531. #endif
  42532. /* Test failure cases */
  42533. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  42534. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42535. in = client_key_der_2048;
  42536. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42537. &in, (long)sizeof_client_key_der_2048));
  42538. in = ecc_clikey_der_256;
  42539. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42540. &in, (long)sizeof_ecc_clikey_der_256));
  42541. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42542. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  42543. #else
  42544. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  42545. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42546. EVP_PKEY_free(b);
  42547. b = NULL;
  42548. EVP_PKEY_free(a);
  42549. a = NULL;
  42550. #endif
  42551. /* invalid or empty failure cases */
  42552. a = EVP_PKEY_new();
  42553. b = EVP_PKEY_new();
  42554. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42555. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42556. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42557. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42558. #ifdef NO_RSA
  42559. /* Type check will fail since RSA is the default EVP key type */
  42560. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  42561. #else
  42562. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42563. #endif
  42564. #else
  42565. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42566. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42567. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42568. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  42569. #endif
  42570. EVP_PKEY_free(b);
  42571. EVP_PKEY_free(a);
  42572. (void)in;
  42573. #endif
  42574. return EXPECT_RESULT();
  42575. }
  42576. static int test_ERR_load_crypto_strings(void)
  42577. {
  42578. #if defined(OPENSSL_ALL)
  42579. ERR_load_crypto_strings();
  42580. return TEST_SUCCESS;
  42581. #else
  42582. return TEST_SKIPPED;
  42583. #endif
  42584. }
  42585. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42586. static void free_x509(X509* x)
  42587. {
  42588. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42589. }
  42590. #endif
  42591. static int test_sk_X509(void)
  42592. {
  42593. EXPECT_DECLS;
  42594. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42595. {
  42596. STACK_OF(X509)* s = NULL;
  42597. ExpectNotNull(s = sk_X509_new_null());
  42598. ExpectIntEQ(sk_X509_num(s), 0);
  42599. sk_X509_pop_free(s, NULL);
  42600. ExpectNotNull(s = sk_X509_new_null());
  42601. ExpectIntEQ(sk_X509_num(s), 0);
  42602. sk_X509_pop_free(s, NULL);
  42603. ExpectNotNull(s = sk_X509_new_null());
  42604. sk_X509_push(s, (X509*)1);
  42605. ExpectIntEQ(sk_X509_num(s), 1);
  42606. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42607. sk_X509_push(s, (X509*)2);
  42608. ExpectIntEQ(sk_X509_num(s), 2);
  42609. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42610. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42611. sk_X509_push(s, (X509*)2);
  42612. sk_X509_pop_free(s, free_x509);
  42613. }
  42614. {
  42615. /* Push a list of 10 X509s onto stack, then verify that
  42616. * value(), push(), shift(), and pop() behave as expected. */
  42617. STACK_OF(X509)* s = NULL;
  42618. X509* xList[10];
  42619. int i = 0;
  42620. const int len = (sizeof(xList) / sizeof(xList[0]));
  42621. for (i = 0; i < len; ++i) {
  42622. xList[i] = NULL;
  42623. ExpectNotNull(xList[i] = X509_new());
  42624. }
  42625. /* test push, pop, and free */
  42626. ExpectNotNull(s = sk_X509_new_null());
  42627. for (i = 0; i < len; ++i) {
  42628. sk_X509_push(s, xList[i]);
  42629. ExpectIntEQ(sk_X509_num(s), i + 1);
  42630. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42631. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42632. }
  42633. /* pop returns and removes last pushed on stack, which is index 0
  42634. * in sk_x509_value */
  42635. for (i = 0; i < len; ++i) {
  42636. X509 * x = sk_X509_value(s, 0);
  42637. X509 * y = sk_X509_pop(s);
  42638. X509 * z = xList[len - 1 - i];
  42639. ExpectIntEQ((x == y), 1);
  42640. ExpectIntEQ((x == z), 1);
  42641. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  42642. }
  42643. sk_free(s);
  42644. s = NULL;
  42645. /* test push, shift, and free */
  42646. ExpectNotNull(s = sk_X509_new_null());
  42647. for (i = 0; i < len; ++i) {
  42648. sk_X509_push(s, xList[i]);
  42649. ExpectIntEQ(sk_X509_num(s), i + 1);
  42650. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42651. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42652. }
  42653. /* shift returns and removes first pushed on stack, which is index i
  42654. * in sk_x509_value() */
  42655. for (i = 0; i < len; ++i) {
  42656. X509 * x = sk_X509_value(s, len - 1 - i);
  42657. X509 * y = sk_X509_shift(s);
  42658. X509 * z = xList[i];
  42659. ExpectIntEQ((x == y), 1);
  42660. ExpectIntEQ((x == z), 1);
  42661. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  42662. }
  42663. sk_free(s);
  42664. for (i = 0; i < len; ++i)
  42665. X509_free(xList[i]);
  42666. }
  42667. #endif
  42668. return EXPECT_RESULT();
  42669. }
  42670. static int test_sk_X509_CRL(void)
  42671. {
  42672. EXPECT_DECLS;
  42673. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42674. X509_CRL* crl = NULL;
  42675. XFILE fp = XBADFILE;
  42676. STACK_OF(X509_CRL)* s = NULL;
  42677. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  42678. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42679. NULL, NULL));
  42680. if (fp != XBADFILE)
  42681. XFCLOSE(fp);
  42682. ExpectNotNull(s = sk_X509_CRL_new());
  42683. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  42684. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  42685. if (EXPECT_FAIL()) {
  42686. X509_CRL_free(crl);
  42687. }
  42688. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  42689. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  42690. sk_X509_CRL_free(s);
  42691. #endif
  42692. return EXPECT_RESULT();
  42693. }
  42694. static int test_X509_get_signature_nid(void)
  42695. {
  42696. EXPECT_DECLS;
  42697. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42698. X509* x509 = NULL;
  42699. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  42700. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42701. SSL_FILETYPE_PEM));
  42702. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42703. X509_free(x509);
  42704. #endif
  42705. return EXPECT_RESULT();
  42706. }
  42707. static int test_X509_REQ(void)
  42708. {
  42709. EXPECT_DECLS;
  42710. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42711. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42712. X509_NAME* name = NULL;
  42713. #ifndef NO_RSA
  42714. X509_NAME* subject = NULL;
  42715. #endif
  42716. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42717. X509_REQ* req = NULL;
  42718. EVP_PKEY* priv = NULL;
  42719. EVP_PKEY* pub = NULL;
  42720. unsigned char* der = NULL;
  42721. int len;
  42722. #endif
  42723. #ifndef NO_RSA
  42724. EVP_MD_CTX *mctx = NULL;
  42725. EVP_PKEY_CTX *pkctx = NULL;
  42726. #ifdef USE_CERT_BUFFERS_1024
  42727. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42728. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42729. #elif defined(USE_CERT_BUFFERS_2048)
  42730. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42731. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42732. #endif
  42733. #endif
  42734. #ifdef HAVE_ECC
  42735. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42736. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42737. #endif
  42738. ExpectNotNull(name = X509_NAME_new());
  42739. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42740. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  42741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42742. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  42743. #ifndef NO_RSA
  42744. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42745. (long)sizeof_client_key_der_2048));
  42746. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42747. (long)sizeof_client_keypub_der_2048));
  42748. ExpectNotNull(req = X509_REQ_new());
  42749. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42750. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42751. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42752. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42753. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42754. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42755. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42756. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42757. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42758. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42759. len = i2d_X509_REQ(req, &der);
  42760. DEBUG_WRITE_DER(der, len, "req.der");
  42761. #ifdef USE_CERT_BUFFERS_1024
  42762. ExpectIntEQ(len, 381);
  42763. #else
  42764. ExpectIntEQ(len, 643);
  42765. #endif
  42766. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42767. der = NULL;
  42768. mctx = EVP_MD_CTX_new();
  42769. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  42770. WOLFSSL_SUCCESS);
  42771. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42772. EVP_MD_CTX_free(mctx);
  42773. mctx = NULL;
  42774. X509_REQ_free(NULL);
  42775. X509_REQ_free(req);
  42776. req = NULL;
  42777. /* Test getting the subject from a newly created X509_REQ */
  42778. ExpectNotNull(req = X509_REQ_new());
  42779. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  42780. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42781. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42782. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42783. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42784. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42785. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42786. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42787. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42788. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42789. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42790. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42791. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42792. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42793. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42794. len = i2d_X509_REQ(req, &der);
  42795. DEBUG_WRITE_DER(der, len, "req2.der");
  42796. #ifdef USE_CERT_BUFFERS_1024
  42797. ExpectIntEQ(len, 435);
  42798. #else
  42799. ExpectIntEQ(len, 696);
  42800. #endif
  42801. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42802. der = NULL;
  42803. EVP_PKEY_free(pub);
  42804. pub = NULL;
  42805. EVP_PKEY_free(priv);
  42806. priv = NULL;
  42807. X509_REQ_free(req);
  42808. req = NULL;
  42809. #endif
  42810. #ifdef HAVE_ECC
  42811. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42812. sizeof_ecc_clikey_der_256));
  42813. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42814. sizeof_ecc_clikeypub_der_256));
  42815. ExpectNotNull(req = X509_REQ_new());
  42816. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42817. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42818. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42819. /* Signature is random and may be shorter or longer. */
  42820. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42821. ExpectIntLE(len, 253);
  42822. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42823. X509_REQ_free(req);
  42824. EVP_PKEY_free(pub);
  42825. EVP_PKEY_free(priv);
  42826. #ifdef FP_ECC
  42827. wc_ecc_fp_free();
  42828. #endif
  42829. #endif /* HAVE_ECC */
  42830. X509_NAME_free(name);
  42831. #endif
  42832. return EXPECT_RESULT();
  42833. }
  42834. static int test_wolfssl_PKCS7(void)
  42835. {
  42836. EXPECT_DECLS;
  42837. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42838. !defined(NO_RSA)
  42839. PKCS7* pkcs7 = NULL;
  42840. byte data[FOURK_BUF];
  42841. word32 len = sizeof(data);
  42842. const byte* p = data;
  42843. byte content[] = "Test data to encode.";
  42844. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42845. BIO* bio = NULL;
  42846. byte key[sizeof(client_key_der_2048)];
  42847. word32 keySz = (word32)sizeof(key);
  42848. byte* out = NULL;
  42849. #endif
  42850. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  42851. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  42852. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42853. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42854. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42855. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42856. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42857. PKCS7_free(pkcs7);
  42858. pkcs7 = NULL;
  42859. /* fail case, without PKCS7_NOVERIFY */
  42860. p = data;
  42861. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42862. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42863. 0), WOLFSSL_FAILURE);
  42864. PKCS7_free(pkcs7);
  42865. pkcs7 = NULL;
  42866. /* success case, with PKCS7_NOVERIFY */
  42867. p = data;
  42868. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42869. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42870. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42871. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42872. /* test i2d */
  42873. XMEMCPY(key, client_key_der_2048, keySz);
  42874. if (pkcs7 != NULL) {
  42875. pkcs7->privateKey = key;
  42876. pkcs7->privateKeySz = (word32)sizeof(key);
  42877. pkcs7->encryptOID = RSAk;
  42878. #ifdef NO_SHA
  42879. pkcs7->hashOID = SHA256h;
  42880. #else
  42881. pkcs7->hashOID = SHAh;
  42882. #endif
  42883. }
  42884. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42885. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42886. #ifndef NO_ASN_TIME
  42887. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42888. #else
  42889. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  42890. #endif
  42891. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42892. BIO_free(bio);
  42893. #endif
  42894. PKCS7_free(NULL);
  42895. PKCS7_free(pkcs7);
  42896. #endif
  42897. return EXPECT_RESULT();
  42898. }
  42899. static int test_wolfSSL_PKCS7_sign(void)
  42900. {
  42901. EXPECT_DECLS;
  42902. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42903. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42904. PKCS7* p7 = NULL;
  42905. PKCS7* p7Ver = NULL;
  42906. byte* out = NULL;
  42907. byte* tmpPtr = NULL;
  42908. int outLen = 0;
  42909. int flags = 0;
  42910. byte data[] = "Test data to encode.";
  42911. const char* cert = "./certs/server-cert.pem";
  42912. const char* key = "./certs/server-key.pem";
  42913. const char* ca = "./certs/ca-cert.pem";
  42914. WOLFSSL_BIO* certBio = NULL;
  42915. WOLFSSL_BIO* keyBio = NULL;
  42916. WOLFSSL_BIO* caBio = NULL;
  42917. WOLFSSL_BIO* inBio = NULL;
  42918. X509* signCert = NULL;
  42919. EVP_PKEY* signKey = NULL;
  42920. X509* caCert = NULL;
  42921. X509_STORE* store = NULL;
  42922. /* read signer cert/key into BIO */
  42923. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  42924. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  42925. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42926. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42927. /* read CA cert into store (for verify) */
  42928. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  42929. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42930. ExpectNotNull(store = X509_STORE_new());
  42931. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42932. /* data to be signed into BIO */
  42933. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42934. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42935. /* PKCS7_sign, bad args: signer NULL */
  42936. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42937. /* PKCS7_sign, bad args: signer key NULL */
  42938. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42939. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42940. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42941. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42942. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42943. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42944. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42945. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42946. {
  42947. flags = PKCS7_BINARY;
  42948. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42949. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42950. /* verify with d2i_PKCS7 */
  42951. tmpPtr = out;
  42952. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42953. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42954. PKCS7_free(p7Ver);
  42955. p7Ver = NULL;
  42956. /* verify with wc_PKCS7_VerifySignedData */
  42957. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42958. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42959. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42960. /* compare the signer found to expected signer */
  42961. ExpectIntNE(p7Ver->verifyCertSz, 0);
  42962. tmpPtr = NULL;
  42963. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42964. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42965. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42966. tmpPtr = NULL;
  42967. wc_PKCS7_Free(p7Ver);
  42968. p7Ver = NULL;
  42969. ExpectNotNull(out);
  42970. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42971. out = NULL;
  42972. PKCS7_free(p7);
  42973. p7 = NULL;
  42974. }
  42975. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42976. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42977. {
  42978. /* re-populate input BIO, may have been consumed */
  42979. BIO_free(inBio);
  42980. inBio = NULL;
  42981. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42982. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42983. flags = PKCS7_BINARY | PKCS7_STREAM;
  42984. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42985. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42986. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42987. /* PKCS7_final, bad args: PKCS7 null */
  42988. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42989. /* PKCS7_final, bad args: PKCS7 null */
  42990. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42991. tmpPtr = out;
  42992. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42993. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42994. PKCS7_free(p7Ver);
  42995. p7Ver = NULL;
  42996. ExpectNotNull(out);
  42997. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42998. out = NULL;
  42999. PKCS7_free(p7);
  43000. p7 = NULL;
  43001. }
  43002. /* TEST SUCCESS: Detached, not streaming, not MIME */
  43003. {
  43004. /* re-populate input BIO, may have been consumed */
  43005. BIO_free(inBio);
  43006. inBio = NULL;
  43007. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43008. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43009. flags = PKCS7_BINARY | PKCS7_DETACHED;
  43010. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43011. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  43012. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  43013. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  43014. if (p7Ver != NULL) {
  43015. p7Ver->content = data;
  43016. p7Ver->contentSz = sizeof(data);
  43017. }
  43018. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  43019. wc_PKCS7_Free(p7Ver);
  43020. p7Ver = NULL;
  43021. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  43022. * yet support detached content */
  43023. tmpPtr = out;
  43024. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  43025. PKCS7_free(p7Ver);
  43026. p7Ver = NULL;
  43027. ExpectNotNull(out);
  43028. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43029. out = NULL;
  43030. PKCS7_free(p7);
  43031. p7 = NULL;
  43032. }
  43033. /* TEST SUCCESS: Detached, streaming, not MIME */
  43034. {
  43035. /* re-populate input BIO, may have been consumed */
  43036. BIO_free(inBio);
  43037. inBio = NULL;
  43038. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43039. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43040. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  43041. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43042. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  43043. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  43044. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  43045. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  43046. if (p7Ver != NULL) {
  43047. p7Ver->content = data;
  43048. p7Ver->contentSz = sizeof(data);
  43049. }
  43050. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  43051. wc_PKCS7_Free(p7Ver);
  43052. p7Ver = NULL;
  43053. ExpectNotNull(out);
  43054. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43055. PKCS7_free(p7);
  43056. p7 = NULL;
  43057. }
  43058. X509_STORE_free(store);
  43059. X509_free(caCert);
  43060. X509_free(signCert);
  43061. EVP_PKEY_free(signKey);
  43062. BIO_free(inBio);
  43063. BIO_free(keyBio);
  43064. BIO_free(certBio);
  43065. BIO_free(caBio);
  43066. #endif
  43067. return EXPECT_RESULT();
  43068. }
  43069. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  43070. {
  43071. EXPECT_DECLS;
  43072. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  43073. PKCS7_SIGNED* pkcs7 = NULL;
  43074. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  43075. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  43076. PKCS7_SIGNED_free(pkcs7);
  43077. #endif
  43078. return EXPECT_RESULT();
  43079. }
  43080. #ifndef NO_BIO
  43081. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  43082. {
  43083. EXPECT_DECLS;
  43084. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  43085. PKCS7* pkcs7 = NULL;
  43086. BIO* bio = NULL;
  43087. const byte* cert_buf = NULL;
  43088. int ret = 0;
  43089. WC_RNG rng;
  43090. const byte data[] = { /* Hello World */
  43091. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  43092. 0x72,0x6c,0x64
  43093. };
  43094. #ifndef NO_RSA
  43095. #if defined(USE_CERT_BUFFERS_2048)
  43096. byte key[sizeof(client_key_der_2048)];
  43097. byte cert[sizeof(client_cert_der_2048)];
  43098. word32 keySz = (word32)sizeof(key);
  43099. word32 certSz = (word32)sizeof(cert);
  43100. XMEMSET(key, 0, keySz);
  43101. XMEMSET(cert, 0, certSz);
  43102. XMEMCPY(key, client_key_der_2048, keySz);
  43103. XMEMCPY(cert, client_cert_der_2048, certSz);
  43104. #elif defined(USE_CERT_BUFFERS_1024)
  43105. byte key[sizeof_client_key_der_1024];
  43106. byte cert[sizeof(sizeof_client_cert_der_1024)];
  43107. word32 keySz = (word32)sizeof(key);
  43108. word32 certSz = (word32)sizeof(cert);
  43109. XMEMSET(key, 0, keySz);
  43110. XMEMSET(cert, 0, certSz);
  43111. XMEMCPY(key, client_key_der_1024, keySz);
  43112. XMEMCPY(cert, client_cert_der_1024, certSz);
  43113. #else
  43114. unsigned char cert[ONEK_BUF];
  43115. unsigned char key[ONEK_BUF];
  43116. XFILE fp = XBADFILE;
  43117. int certSz;
  43118. int keySz;
  43119. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  43120. XBADFILE);
  43121. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  43122. fp), 0);
  43123. if (fp != XBADFILE) {
  43124. XFCLOSE(fp);
  43125. fp = XBADFILE;
  43126. }
  43127. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  43128. XBADFILE);
  43129. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  43130. 0);
  43131. if (fp != XBADFILE) {
  43132. XFCLOSE(fp);
  43133. fp = XBADFILE;
  43134. }
  43135. #endif
  43136. #elif defined(HAVE_ECC)
  43137. #if defined(USE_CERT_BUFFERS_256)
  43138. unsigned char cert[sizeof(cliecc_cert_der_256)];
  43139. unsigned char key[sizeof(ecc_clikey_der_256)];
  43140. int certSz = (int)sizeof(cert);
  43141. int keySz = (int)sizeof(key);
  43142. XMEMSET(cert, 0, certSz);
  43143. XMEMSET(key, 0, keySz);
  43144. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  43145. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  43146. #else
  43147. unsigned char cert[ONEK_BUF];
  43148. unsigned char key[ONEK_BUF];
  43149. XFILE fp = XBADFILE;
  43150. int certSz, keySz;
  43151. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  43152. XBADFILE);
  43153. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  43154. fp), 0);
  43155. if (fp != XBADFILE) {
  43156. XFCLOSE(fp);
  43157. fp = XBADFILE;
  43158. }
  43159. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  43160. XBADFILE);
  43161. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  43162. 0);
  43163. if (fp != XBADFILE) {
  43164. XFCLOSE(fp);
  43165. fp = XBADFILE;
  43166. }
  43167. #endif
  43168. #else
  43169. #error PKCS7 requires ECC or RSA
  43170. #endif
  43171. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  43172. /* initialize with DER encoded cert */
  43173. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  43174. /* init rng */
  43175. XMEMSET(&rng, 0, sizeof(WC_RNG));
  43176. ExpectIntEQ(wc_InitRng(&rng), 0);
  43177. if (pkcs7 != NULL) {
  43178. pkcs7->rng = &rng;
  43179. pkcs7->content = (byte*)data; /* not used for ex */
  43180. pkcs7->contentSz = (word32)sizeof(data);
  43181. pkcs7->contentOID = SIGNED_DATA;
  43182. pkcs7->privateKey = key;
  43183. pkcs7->privateKeySz = (word32)sizeof(key);
  43184. pkcs7->encryptOID = RSAk;
  43185. #ifdef NO_SHA
  43186. pkcs7->hashOID = SHA256h;
  43187. #else
  43188. pkcs7->hashOID = SHAh;
  43189. #endif
  43190. pkcs7->signedAttribs = NULL;
  43191. pkcs7->signedAttribsSz = 0;
  43192. }
  43193. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43194. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  43195. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  43196. /* Read PKCS#7 PEM from BIO */
  43197. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  43198. ExpectIntGE(ret, 0);
  43199. BIO_free(bio);
  43200. wc_PKCS7_Free(pkcs7);
  43201. wc_FreeRng(&rng);
  43202. #endif
  43203. return EXPECT_RESULT();
  43204. }
  43205. #ifdef HAVE_SMIME
  43206. static int test_wolfSSL_SMIME_read_PKCS7(void)
  43207. {
  43208. EXPECT_DECLS;
  43209. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  43210. !defined(NO_RSA)
  43211. PKCS7* pkcs7 = NULL;
  43212. BIO* bio = NULL;
  43213. BIO* bcont = NULL;
  43214. BIO* out = NULL;
  43215. const byte* outBuf = NULL;
  43216. int outBufLen = 0;
  43217. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  43218. XFILE smimeTestFile = XBADFILE;
  43219. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  43220. XBADFILE);
  43221. /* smime-test.p7s */
  43222. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  43223. ExpectNotNull(bio);
  43224. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43225. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43226. ExpectNotNull(pkcs7);
  43227. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43228. PKCS7_NOVERIFY), SSL_SUCCESS);
  43229. XFCLOSE(smimeTestFile);
  43230. if (bcont) BIO_free(bcont);
  43231. bcont = NULL;
  43232. wolfSSL_PKCS7_free(pkcs7);
  43233. pkcs7 = NULL;
  43234. /* smime-test-multipart.p7s */
  43235. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  43236. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43237. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43238. ExpectNotNull(pkcs7);
  43239. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43240. PKCS7_NOVERIFY), SSL_SUCCESS);
  43241. XFCLOSE(smimeTestFile);
  43242. if (bcont) BIO_free(bcont);
  43243. bcont = NULL;
  43244. wolfSSL_PKCS7_free(pkcs7);
  43245. pkcs7 = NULL;
  43246. /* smime-test-multipart-badsig.p7s */
  43247. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  43248. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43249. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43250. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  43251. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43252. PKCS7_NOVERIFY), SSL_FAILURE);
  43253. XFCLOSE(smimeTestFile);
  43254. if (bcont) BIO_free(bcont);
  43255. bcont = NULL;
  43256. wolfSSL_PKCS7_free(pkcs7);
  43257. pkcs7 = NULL;
  43258. /* smime-test-canon.p7s */
  43259. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43260. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43261. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43262. ExpectNotNull(pkcs7);
  43263. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43264. PKCS7_NOVERIFY), SSL_SUCCESS);
  43265. XFCLOSE(smimeTestFile);
  43266. if (bcont) BIO_free(bcont);
  43267. bcont = NULL;
  43268. wolfSSL_PKCS7_free(pkcs7);
  43269. pkcs7 = NULL;
  43270. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  43271. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43272. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43273. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43274. ExpectNotNull(pkcs7);
  43275. out = wolfSSL_BIO_new(BIO_s_mem());
  43276. ExpectNotNull(out);
  43277. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  43278. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  43279. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  43280. /* Content-Type should not show up at beginning of output buffer */
  43281. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  43282. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  43283. BIO_free(out);
  43284. BIO_free(bio);
  43285. if (bcont) BIO_free(bcont);
  43286. wolfSSL_PKCS7_free(pkcs7);
  43287. #endif
  43288. return EXPECT_RESULT();
  43289. }
  43290. static int test_wolfSSL_SMIME_write_PKCS7(void)
  43291. {
  43292. EXPECT_DECLS;
  43293. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  43294. PKCS7* p7 = NULL;
  43295. PKCS7* p7Ver = NULL;
  43296. int flags = 0;
  43297. byte data[] = "Test data to encode.";
  43298. const char* cert = "./certs/server-cert.pem";
  43299. const char* key = "./certs/server-key.pem";
  43300. const char* ca = "./certs/ca-cert.pem";
  43301. WOLFSSL_BIO* certBio = NULL;
  43302. WOLFSSL_BIO* keyBio = NULL;
  43303. WOLFSSL_BIO* caBio = NULL;
  43304. WOLFSSL_BIO* inBio = NULL;
  43305. WOLFSSL_BIO* outBio = NULL;
  43306. WOLFSSL_BIO* content = NULL;
  43307. X509* signCert = NULL;
  43308. EVP_PKEY* signKey = NULL;
  43309. X509* caCert = NULL;
  43310. X509_STORE* store = NULL;
  43311. /* read signer cert/key into BIO */
  43312. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  43313. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  43314. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  43315. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  43316. /* read CA cert into store (for verify) */
  43317. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  43318. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  43319. ExpectNotNull(store = X509_STORE_new());
  43320. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  43321. /* generate and verify SMIME: not detached */
  43322. {
  43323. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43324. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43325. flags = PKCS7_STREAM;
  43326. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43327. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43328. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43329. /* bad arg: out NULL */
  43330. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  43331. /* bad arg: pkcs7 NULL */
  43332. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  43333. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43334. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43335. BIO_free(content);
  43336. content = NULL;
  43337. BIO_free(inBio);
  43338. inBio = NULL;
  43339. BIO_free(outBio);
  43340. outBio = NULL;
  43341. PKCS7_free(p7Ver);
  43342. p7Ver = NULL;
  43343. PKCS7_free(p7);
  43344. p7 = NULL;
  43345. }
  43346. /* generate and verify SMIME: not detached, add Content-Type */
  43347. {
  43348. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43349. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43350. flags = PKCS7_STREAM | PKCS7_TEXT;
  43351. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43352. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43353. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43354. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43355. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43356. BIO_free(content);
  43357. content = NULL;
  43358. BIO_free(inBio);
  43359. inBio = NULL;
  43360. BIO_free(outBio);
  43361. outBio = NULL;
  43362. PKCS7_free(p7Ver);
  43363. p7Ver = NULL;
  43364. PKCS7_free(p7);
  43365. p7 = NULL;
  43366. }
  43367. /* generate and verify SMIME: detached */
  43368. {
  43369. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43370. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43371. flags = PKCS7_DETACHED | PKCS7_STREAM;
  43372. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43373. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43374. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43375. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43376. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43377. BIO_free(content);
  43378. content = NULL;
  43379. BIO_free(inBio);
  43380. inBio = NULL;
  43381. BIO_free(outBio);
  43382. outBio = NULL;
  43383. PKCS7_free(p7Ver);
  43384. p7Ver = NULL;
  43385. PKCS7_free(p7);
  43386. p7 = NULL;
  43387. }
  43388. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  43389. {
  43390. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43391. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43392. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  43393. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43394. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43395. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43396. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43397. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43398. BIO_free(content);
  43399. content = NULL;
  43400. BIO_free(inBio);
  43401. inBio = NULL;
  43402. BIO_free(outBio);
  43403. outBio = NULL;
  43404. PKCS7_free(p7Ver);
  43405. p7Ver = NULL;
  43406. PKCS7_free(p7);
  43407. p7 = NULL;
  43408. }
  43409. X509_STORE_free(store);
  43410. X509_free(caCert);
  43411. X509_free(signCert);
  43412. EVP_PKEY_free(signKey);
  43413. BIO_free(keyBio);
  43414. BIO_free(certBio);
  43415. BIO_free(caBio);
  43416. #endif
  43417. return EXPECT_RESULT();
  43418. }
  43419. #endif /* HAVE_SMIME */
  43420. #endif /* !NO_BIO */
  43421. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  43422. * returns 0) */
  43423. static int test_X509_STORE_No_SSL_CTX(void)
  43424. {
  43425. EXPECT_DECLS;
  43426. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  43427. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43428. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  43429. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43430. defined(HAVE_CRL) && !defined(NO_RSA)
  43431. X509_STORE * store = NULL;
  43432. X509_STORE_CTX * storeCtx = NULL;
  43433. X509_CRL * crl = NULL;
  43434. X509 * ca = NULL;
  43435. X509 * cert = NULL;
  43436. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43437. const char srvCert[] = "./certs/server-cert.pem";
  43438. const char caCert[] = "./certs/ca-cert.pem";
  43439. const char caDir[] = "./certs/crl/hash_pem";
  43440. XFILE fp = XBADFILE;
  43441. X509_LOOKUP * lookup = NULL;
  43442. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43443. /* Set up store with CA */
  43444. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43445. SSL_FILETYPE_PEM)));
  43446. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43447. /* Add CRL lookup directory to store
  43448. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  43449. * of crl.pem */
  43450. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43451. X509_LOOKUP_hash_dir())));
  43452. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  43453. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  43454. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43455. SSL_SUCCESS);
  43456. /* Add CRL to store NOT containing the verified certificate, which
  43457. * forces use of the CRL lookup directory */
  43458. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43459. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43460. NULL, NULL));
  43461. if (fp != XBADFILE)
  43462. XFCLOSE(fp);
  43463. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43464. /* Create verification context outside of an SSL session */
  43465. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43466. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43467. SSL_FILETYPE_PEM)));
  43468. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43469. /* Perform verification, which should NOT indicate CRL missing due to the
  43470. * store CM's X509 store pointer being NULL */
  43471. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43472. X509_CRL_free(crl);
  43473. X509_STORE_free(store);
  43474. X509_STORE_CTX_free(storeCtx);
  43475. X509_free(cert);
  43476. X509_free(ca);
  43477. #endif
  43478. return EXPECT_RESULT();
  43479. }
  43480. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  43481. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  43482. static int test_X509_LOOKUP_add_dir(void)
  43483. {
  43484. EXPECT_DECLS;
  43485. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  43486. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43487. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  43488. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43489. defined(HAVE_CRL) && !defined(NO_RSA)
  43490. X509_STORE * store = NULL;
  43491. X509_STORE_CTX * storeCtx = NULL;
  43492. X509_CRL * crl = NULL;
  43493. X509 * ca = NULL;
  43494. X509 * cert = NULL;
  43495. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43496. const char srvCert[] = "./certs/server-cert.pem";
  43497. const char caCert[] = "./certs/ca-cert.pem";
  43498. const char caDir[] = "./certs/crl/hash_der";
  43499. XFILE fp = XBADFILE;
  43500. X509_LOOKUP * lookup = NULL;
  43501. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43502. /* Set up store with CA */
  43503. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43504. SSL_FILETYPE_PEM)));
  43505. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43506. /* Add CRL lookup directory to store.
  43507. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  43508. * of crl.der */
  43509. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43510. X509_LOOKUP_hash_dir())));
  43511. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  43512. SSL_SUCCESS);
  43513. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43514. SSL_SUCCESS);
  43515. /* Add CRL to store NOT containing the verified certificate, which
  43516. * forces use of the CRL lookup directory */
  43517. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43518. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43519. NULL, NULL));
  43520. if (fp != XBADFILE) {
  43521. XFCLOSE(fp);
  43522. fp = XBADFILE;
  43523. }
  43524. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43525. /* Create verification context outside of an SSL session */
  43526. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43527. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43528. SSL_FILETYPE_PEM)));
  43529. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43530. /* Perform verification, which should NOT return CRL missing */
  43531. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43532. X509_CRL_free(crl);
  43533. crl = NULL;
  43534. X509_STORE_free(store);
  43535. store = NULL;
  43536. X509_STORE_CTX_free(storeCtx);
  43537. storeCtx = NULL;
  43538. X509_free(cert);
  43539. cert = NULL;
  43540. X509_free(ca);
  43541. ca = NULL;
  43542. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  43543. * We should get CRL_MISSING at the end, because the lookup
  43544. * dir has only ASN1 CRLs. */
  43545. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43546. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43547. SSL_FILETYPE_PEM)));
  43548. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43549. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43550. X509_LOOKUP_hash_dir())));
  43551. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  43552. SSL_SUCCESS);
  43553. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43554. SSL_SUCCESS);
  43555. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43556. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43557. NULL, NULL));
  43558. if (fp != XBADFILE) {
  43559. XFCLOSE(fp);
  43560. fp = XBADFILE;
  43561. }
  43562. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43563. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43564. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43565. SSL_FILETYPE_PEM)));
  43566. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43567. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  43568. * in dir containing only ASN1/DER. */
  43569. ExpectIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  43570. X509_CRL_free(crl);
  43571. X509_STORE_free(store);
  43572. X509_STORE_CTX_free(storeCtx);
  43573. X509_free(cert);
  43574. X509_free(ca);
  43575. #endif
  43576. return EXPECT_RESULT();
  43577. }
  43578. /*----------------------------------------------------------------------------*
  43579. | Certificate Failure Checks
  43580. *----------------------------------------------------------------------------*/
  43581. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  43582. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  43583. #if !defined(NO_RSA) || defined(HAVE_ECC)
  43584. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  43585. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  43586. int type)
  43587. {
  43588. int ret;
  43589. WOLFSSL_CERT_MANAGER* cm = NULL;
  43590. switch (type) {
  43591. case TESTING_RSA:
  43592. #ifdef NO_RSA
  43593. fprintf(stderr, "RSA disabled, skipping test\n");
  43594. return ASN_SIG_CONFIRM_E;
  43595. #else
  43596. break;
  43597. #endif
  43598. case TESTING_ECC:
  43599. #ifndef HAVE_ECC
  43600. fprintf(stderr, "ECC disabled, skipping test\n");
  43601. return ASN_SIG_CONFIRM_E;
  43602. #else
  43603. break;
  43604. #endif
  43605. default:
  43606. fprintf(stderr, "Bad function argument\n");
  43607. return BAD_FUNC_ARG;
  43608. }
  43609. cm = wolfSSL_CertManagerNew();
  43610. if (cm == NULL) {
  43611. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  43612. return -1;
  43613. }
  43614. #ifndef NO_FILESYSTEM
  43615. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  43616. if (ret != WOLFSSL_SUCCESS) {
  43617. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  43618. wolfSSL_CertManagerFree(cm);
  43619. return ret;
  43620. }
  43621. #else
  43622. (void)ca;
  43623. #endif
  43624. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  43625. WOLFSSL_FILETYPE_ASN1);
  43626. /* Let ExpectIntEQ handle return code */
  43627. wolfSSL_CertManagerFree(cm);
  43628. return ret;
  43629. }
  43630. #endif
  43631. #if !defined(NO_FILESYSTEM)
  43632. static int test_RsaSigFailure_cm(void)
  43633. {
  43634. EXPECT_DECLS;
  43635. #ifndef NO_RSA
  43636. const char* ca_cert = "./certs/ca-cert.pem";
  43637. const char* server_cert = "./certs/server-cert.der";
  43638. byte* cert_buf = NULL;
  43639. size_t cert_sz = 0;
  43640. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  43641. if (cert_buf != NULL) {
  43642. /* corrupt DER - invert last byte, which is signature */
  43643. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43644. /* test bad cert */
  43645. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43646. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  43647. WOLFSSL_FATAL_ERROR);
  43648. #else
  43649. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  43650. ASN_SIG_CONFIRM_E);
  43651. #endif
  43652. }
  43653. /* load_file() uses malloc. */
  43654. if (cert_buf != NULL) {
  43655. free(cert_buf);
  43656. }
  43657. #endif /* !NO_RSA */
  43658. return EXPECT_RESULT();
  43659. }
  43660. static int test_EccSigFailure_cm(void)
  43661. {
  43662. EXPECT_DECLS;
  43663. #ifdef HAVE_ECC
  43664. /* self-signed ECC cert, so use server cert as CA */
  43665. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43666. const char* server_cert = "./certs/server-ecc.der";
  43667. byte* cert_buf = NULL;
  43668. size_t cert_sz = 0;
  43669. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  43670. if (cert_buf != NULL) {
  43671. /* corrupt DER - invert last byte, which is signature */
  43672. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43673. /* test bad cert */
  43674. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43675. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  43676. WOLFSSL_FATAL_ERROR);
  43677. #else
  43678. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  43679. ASN_SIG_CONFIRM_E);
  43680. #endif
  43681. }
  43682. /* load_file() uses malloc. */
  43683. if (cert_buf != NULL) {
  43684. free(cert_buf);
  43685. }
  43686. #ifdef FP_ECC
  43687. wc_ecc_fp_free();
  43688. #endif
  43689. #endif /* HAVE_ECC */
  43690. return EXPECT_RESULT();
  43691. }
  43692. #endif /* !NO_FILESYSTEM */
  43693. #endif /* NO_CERTS */
  43694. #ifdef WOLFSSL_TLS13
  43695. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43696. #ifdef WC_SHA384_DIGEST_SIZE
  43697. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43698. #else
  43699. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43700. #endif
  43701. #endif
  43702. #ifdef WOLFSSL_EARLY_DATA
  43703. static const char earlyData[] = "Early Data";
  43704. static char earlyDataBuffer[1];
  43705. #endif
  43706. static int test_tls13_apis(void)
  43707. {
  43708. EXPECT_DECLS;
  43709. int ret;
  43710. #ifndef WOLFSSL_NO_TLS12
  43711. #ifndef NO_WOLFSSL_CLIENT
  43712. WOLFSSL_CTX* clientTls12Ctx = NULL;
  43713. WOLFSSL* clientTls12Ssl = NULL;
  43714. #endif
  43715. #ifndef NO_WOLFSSL_SERVER
  43716. WOLFSSL_CTX* serverTls12Ctx = NULL;
  43717. WOLFSSL* serverTls12Ssl = NULL;
  43718. #endif
  43719. #endif
  43720. #ifndef NO_WOLFSSL_CLIENT
  43721. WOLFSSL_CTX* clientCtx = NULL;
  43722. WOLFSSL* clientSsl = NULL;
  43723. #endif
  43724. #ifndef NO_WOLFSSL_SERVER
  43725. WOLFSSL_CTX* serverCtx = NULL;
  43726. WOLFSSL* serverSsl = NULL;
  43727. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43728. const char* ourCert = svrCertFile;
  43729. const char* ourKey = svrKeyFile;
  43730. #endif
  43731. #endif
  43732. int required;
  43733. #ifdef WOLFSSL_EARLY_DATA
  43734. int outSz;
  43735. #endif
  43736. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43737. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43738. #ifdef HAVE_PQC
  43739. WOLFSSL_KYBER_LEVEL1
  43740. #else
  43741. WOLFSSL_ECC_SECP256R1
  43742. #endif
  43743. };
  43744. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43745. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43746. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43747. int numGroups = 2;
  43748. #endif
  43749. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43750. char groupList[] =
  43751. #ifndef NO_ECC_SECP
  43752. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43753. "P-521:"
  43754. #endif
  43755. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43756. "P-384:"
  43757. #endif
  43758. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43759. "P-256"
  43760. #ifdef HAVE_PQC
  43761. ":P256_KYBER_LEVEL1"
  43762. #endif
  43763. #endif
  43764. #endif /* !defined(NO_ECC_SECP) */
  43765. #ifdef HAVE_PQC
  43766. ":KYBER_LEVEL1"
  43767. #endif
  43768. "";
  43769. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43770. (void)ret;
  43771. #ifndef WOLFSSL_NO_TLS12
  43772. #ifndef NO_WOLFSSL_CLIENT
  43773. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43774. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43775. #endif
  43776. #ifndef NO_WOLFSSL_SERVER
  43777. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43778. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43779. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43780. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  43781. WOLFSSL_FILETYPE_PEM);
  43782. #endif
  43783. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43784. #endif
  43785. #endif
  43786. #ifndef NO_WOLFSSL_CLIENT
  43787. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43788. clientSsl = wolfSSL_new(clientCtx);
  43789. #endif
  43790. #ifndef NO_WOLFSSL_SERVER
  43791. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43792. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43793. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43794. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43795. #endif
  43796. serverSsl = wolfSSL_new(serverCtx);
  43797. ExpectNotNull(serverSsl);
  43798. #endif
  43799. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43800. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43801. #ifndef NO_WOLFSSL_CLIENT
  43802. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43803. #endif
  43804. #ifndef NO_WOLFSSL_SERVER
  43805. #ifndef WOLFSSL_NO_TLS12
  43806. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  43807. BAD_FUNC_ARG);
  43808. #endif
  43809. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43810. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43811. WOLFSSL_SUCCESS);
  43812. #endif
  43813. #endif
  43814. #ifdef HAVE_SUPPORTED_CURVES
  43815. #ifdef HAVE_ECC
  43816. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  43817. BAD_FUNC_ARG);
  43818. #ifndef NO_WOLFSSL_SERVER
  43819. do {
  43820. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43821. #ifdef WOLFSSL_ASYNC_CRYPT
  43822. if (ret == WC_PENDING_E)
  43823. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43824. #endif
  43825. }
  43826. while (ret == WC_PENDING_E);
  43827. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43828. #endif
  43829. #ifndef NO_WOLFSSL_CLIENT
  43830. #ifndef WOLFSSL_NO_TLS12
  43831. do {
  43832. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43833. #ifdef WOLFSSL_ASYNC_CRYPT
  43834. if (ret == WC_PENDING_E)
  43835. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43836. #endif
  43837. }
  43838. while (ret == WC_PENDING_E);
  43839. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43840. #endif
  43841. do {
  43842. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43843. #ifdef WOLFSSL_ASYNC_CRYPT
  43844. if (ret == WC_PENDING_E)
  43845. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43846. #endif
  43847. }
  43848. while (ret == WC_PENDING_E);
  43849. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43850. #endif
  43851. #elif defined(HAVE_CURVE25519)
  43852. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43853. #ifndef NO_WOLFSSL_SERVER
  43854. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43855. WOLFSSL_SUCCESS);
  43856. #endif
  43857. #ifndef NO_WOLFSSL_CLIENT
  43858. #ifndef WOLFSSL_NO_TLS12
  43859. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43860. WOLFSSL_SUCCESS);
  43861. #endif
  43862. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43863. WOLFSSL_SUCCESS);
  43864. #endif
  43865. #elif defined(HAVE_CURVE448)
  43866. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43867. #ifndef NO_WOLFSSL_SERVER
  43868. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43869. WOLFSSL_SUCCESS);
  43870. #endif
  43871. #ifndef NO_WOLFSSL_CLIENT
  43872. #ifndef WOLFSSL_NO_TLS12
  43873. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43874. WOLFSSL_SUCCESS);
  43875. #endif
  43876. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43877. WOLFSSL_SUCCESS);
  43878. #endif
  43879. #else
  43880. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  43881. BAD_FUNC_ARG);
  43882. #ifndef NO_WOLFSSL_CLIENT
  43883. #ifndef WOLFSSL_NO_TLS12
  43884. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43885. NOT_COMPILED_IN);
  43886. #endif
  43887. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43888. NOT_COMPILED_IN);
  43889. #endif
  43890. #endif
  43891. #if defined(HAVE_PQC)
  43892. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43893. #ifndef NO_WOLFSSL_SERVER
  43894. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43895. WOLFSSL_SUCCESS);
  43896. #endif
  43897. #ifndef NO_WOLFSSL_CLIENT
  43898. #ifndef WOLFSSL_NO_TLS12
  43899. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43900. BAD_FUNC_ARG);
  43901. #endif
  43902. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43903. WOLFSSL_SUCCESS);
  43904. #endif
  43905. #endif
  43906. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43907. #ifndef NO_WOLFSSL_SERVER
  43908. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43909. #endif
  43910. #ifndef NO_WOLFSSL_CLIENT
  43911. #ifndef WOLFSSL_NO_TLS12
  43912. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43913. #endif
  43914. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43915. #endif
  43916. #endif /* HAVE_SUPPORTED_CURVES */
  43917. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43918. #ifndef NO_WOLFSSL_CLIENT
  43919. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43920. #endif
  43921. #ifndef NO_WOLFSSL_SERVER
  43922. #ifndef WOLFSSL_NO_TLS12
  43923. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43924. #endif
  43925. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43926. #endif
  43927. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43928. #ifndef NO_WOLFSSL_CLIENT
  43929. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43930. #endif
  43931. #ifndef NO_WOLFSSL_SERVER
  43932. #ifndef WOLFSSL_NO_TLS12
  43933. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43934. #endif
  43935. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43936. #endif
  43937. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43938. #ifndef NO_WOLFSSL_CLIENT
  43939. #ifndef WOLFSSL_NO_TLS12
  43940. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43941. #endif
  43942. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43943. #endif
  43944. #ifndef NO_WOLFSSL_SERVER
  43945. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43946. #endif
  43947. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43948. #ifndef NO_WOLFSSL_CLIENT
  43949. #ifndef WOLFSSL_NO_TLS12
  43950. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43951. #endif
  43952. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43953. #endif
  43954. #ifndef NO_WOLFSSL_SERVER
  43955. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43956. #endif
  43957. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43958. #ifndef NO_WOLFSSL_CLIENT
  43959. #ifndef WOLFSSL_NO_TLS12
  43960. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43961. #endif
  43962. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43963. #endif
  43964. #ifndef NO_WOLFSSL_SERVER
  43965. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43966. #endif
  43967. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43968. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43969. #ifndef NO_WOLFSSL_CLIENT
  43970. #ifndef WOLFSSL_NO_TLS12
  43971. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43972. BAD_FUNC_ARG);
  43973. #endif
  43974. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43975. #endif
  43976. #ifndef NO_WOLFSSL_SERVER
  43977. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43978. #endif
  43979. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43980. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43981. #ifndef NO_WOLFSSL_SERVER
  43982. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43983. #endif
  43984. #ifndef NO_WOLFSSL_CLIENT
  43985. #ifndef WOLFSSL_NO_TLS12
  43986. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43987. BAD_FUNC_ARG);
  43988. #endif
  43989. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43990. #endif
  43991. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43992. #ifndef NO_WOLFSSL_SERVER
  43993. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43994. #endif
  43995. #ifndef NO_WOLFSSL_CLIENT
  43996. #ifndef WOLFSSL_NO_TLS12
  43997. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43998. BAD_FUNC_ARG);
  43999. #endif
  44000. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  44001. #endif
  44002. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  44003. #ifndef NO_WOLFSSL_CLIENT
  44004. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  44005. #endif
  44006. #ifndef NO_WOLFSSL_SERVER
  44007. #ifndef WOLFSSL_NO_TLS12
  44008. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  44009. BAD_FUNC_ARG);
  44010. #endif
  44011. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  44012. #endif
  44013. #endif
  44014. #ifdef HAVE_ECC
  44015. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  44016. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  44017. #ifndef NO_WOLFSSL_SERVER
  44018. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  44019. #endif
  44020. #ifndef NO_WOLFSSL_CLIENT
  44021. #ifndef WOLFSSL_NO_TLS12
  44022. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  44023. #endif
  44024. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  44025. #endif
  44026. #endif
  44027. #ifdef HAVE_SUPPORTED_CURVES
  44028. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  44029. #ifndef NO_WOLFSSL_CLIENT
  44030. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  44031. #endif
  44032. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  44033. #ifndef NO_WOLFSSL_CLIENT
  44034. #ifndef WOLFSSL_NO_TLS12
  44035. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  44036. BAD_FUNC_ARG);
  44037. #endif
  44038. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  44039. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  44040. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  44041. WOLFSSL_SUCCESS);
  44042. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  44043. BAD_FUNC_ARG);
  44044. #endif
  44045. #ifndef NO_WOLFSSL_SERVER
  44046. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  44047. WOLFSSL_SUCCESS);
  44048. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  44049. BAD_FUNC_ARG);
  44050. #endif
  44051. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  44052. #ifndef NO_WOLFSSL_CLIENT
  44053. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  44054. #endif
  44055. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  44056. #ifndef NO_WOLFSSL_CLIENT
  44057. #ifndef WOLFSSL_NO_TLS12
  44058. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  44059. BAD_FUNC_ARG);
  44060. #endif
  44061. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  44062. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  44063. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  44064. WOLFSSL_SUCCESS);
  44065. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  44066. BAD_FUNC_ARG);
  44067. #endif
  44068. #ifndef NO_WOLFSSL_SERVER
  44069. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  44070. WOLFSSL_SUCCESS);
  44071. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  44072. BAD_FUNC_ARG);
  44073. #endif
  44074. #ifdef OPENSSL_EXTRA
  44075. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  44076. #ifndef NO_WOLFSSL_CLIENT
  44077. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  44078. WOLFSSL_FAILURE);
  44079. #endif
  44080. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  44081. WOLFSSL_FAILURE);
  44082. #ifndef NO_WOLFSSL_CLIENT
  44083. #ifndef WOLFSSL_NO_TLS12
  44084. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  44085. WOLFSSL_FAILURE);
  44086. #endif
  44087. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  44088. WOLFSSL_SUCCESS);
  44089. #endif
  44090. #ifndef NO_WOLFSSL_SERVER
  44091. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  44092. WOLFSSL_SUCCESS);
  44093. #endif
  44094. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  44095. #ifndef NO_WOLFSSL_CLIENT
  44096. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  44097. #endif
  44098. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  44099. #ifndef NO_WOLFSSL_CLIENT
  44100. #ifndef WOLFSSL_NO_TLS12
  44101. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  44102. WOLFSSL_FAILURE);
  44103. #endif
  44104. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  44105. WOLFSSL_SUCCESS);
  44106. #endif
  44107. #ifndef NO_WOLFSSL_SERVER
  44108. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  44109. WOLFSSL_SUCCESS);
  44110. #endif
  44111. #endif /* OPENSSL_EXTRA */
  44112. #endif /* HAVE_SUPPORTED_CURVES */
  44113. #endif /* HAVE_ECC */
  44114. #ifdef WOLFSSL_EARLY_DATA
  44115. #ifndef OPENSSL_EXTRA
  44116. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44117. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  44118. #else
  44119. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44120. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  44121. #endif
  44122. #ifndef NO_WOLFSSL_CLIENT
  44123. #ifndef OPENSSL_EXTRA
  44124. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  44125. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  44126. #else
  44127. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  44128. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  44129. #endif
  44130. #endif
  44131. #ifndef NO_WOLFSSL_SERVER
  44132. #ifndef WOLFSSL_NO_TLS12
  44133. #ifndef OPENSSL_EXTRA
  44134. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  44135. BAD_FUNC_ARG);
  44136. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  44137. #else
  44138. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  44139. BAD_FUNC_ARG);
  44140. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  44141. #endif
  44142. #endif
  44143. #ifndef OPENSSL_EXTRA
  44144. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  44145. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  44146. WOLFSSL_SUCCESS);
  44147. #else
  44148. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  44149. #endif
  44150. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  44151. #else
  44152. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  44153. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  44154. #endif
  44155. #endif
  44156. #ifndef OPENSSL_EXTRA
  44157. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44158. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  44159. #else
  44160. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44161. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  44162. #endif
  44163. #ifndef NO_WOLFSSL_CLIENT
  44164. #ifndef OPENSSL_EXTRA
  44165. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  44166. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  44167. #else
  44168. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  44169. #endif
  44170. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  44171. #else
  44172. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  44173. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  44174. #endif
  44175. #endif
  44176. #ifndef NO_WOLFSSL_SERVER
  44177. #ifndef WOLFSSL_NO_TLS12
  44178. #ifndef OPENSSL_EXTRA
  44179. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  44180. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  44181. #else
  44182. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  44183. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  44184. #endif
  44185. #endif
  44186. #ifndef OPENSSL_EXTRA
  44187. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  44188. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  44189. #else
  44190. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  44191. #endif
  44192. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  44193. #else
  44194. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  44195. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  44196. #endif
  44197. #endif
  44198. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  44199. &outSz), BAD_FUNC_ARG);
  44200. #ifndef NO_WOLFSSL_CLIENT
  44201. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  44202. &outSz), BAD_FUNC_ARG);
  44203. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  44204. BAD_FUNC_ARG);
  44205. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44206. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  44207. #endif
  44208. #ifndef NO_WOLFSSL_SERVER
  44209. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  44210. sizeof(earlyData), &outSz), SIDE_ERROR);
  44211. #endif
  44212. #ifndef NO_WOLFSSL_CLIENT
  44213. #ifndef WOLFSSL_NO_TLS12
  44214. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  44215. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  44216. #endif
  44217. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44218. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  44219. #endif
  44220. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  44221. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44222. #ifndef NO_WOLFSSL_SERVER
  44223. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  44224. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44225. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  44226. &outSz), BAD_FUNC_ARG);
  44227. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44228. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  44229. #endif
  44230. #ifndef NO_WOLFSSL_CLIENT
  44231. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  44232. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  44233. #endif
  44234. #ifndef NO_WOLFSSL_SERVER
  44235. #ifndef WOLFSSL_NO_TLS12
  44236. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  44237. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44238. #endif
  44239. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44240. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  44241. #endif
  44242. #endif
  44243. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  44244. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  44245. #endif
  44246. #ifndef NO_WOLFSSL_SERVER
  44247. wolfSSL_free(serverSsl);
  44248. wolfSSL_CTX_free(serverCtx);
  44249. #endif
  44250. #ifndef NO_WOLFSSL_CLIENT
  44251. wolfSSL_free(clientSsl);
  44252. wolfSSL_CTX_free(clientCtx);
  44253. #endif
  44254. #ifndef WOLFSSL_NO_TLS12
  44255. #ifndef NO_WOLFSSL_SERVER
  44256. wolfSSL_free(serverTls12Ssl);
  44257. wolfSSL_CTX_free(serverTls12Ctx);
  44258. #endif
  44259. #ifndef NO_WOLFSSL_CLIENT
  44260. wolfSSL_free(clientTls12Ssl);
  44261. wolfSSL_CTX_free(clientTls12Ctx);
  44262. #endif
  44263. #endif
  44264. return EXPECT_RESULT();
  44265. }
  44266. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44267. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44268. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44269. /* Called when writing. */
  44270. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44271. {
  44272. (void)ssl;
  44273. (void)buf;
  44274. (void)sz;
  44275. (void)ctx;
  44276. /* Force error return from wolfSSL_accept_TLSv13(). */
  44277. return WANT_WRITE;
  44278. }
  44279. /* Called when reading. */
  44280. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44281. {
  44282. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  44283. int len = (int)msg->length;
  44284. (void)ssl;
  44285. (void)sz;
  44286. /* Pass back as much of message as will fit in buffer. */
  44287. if (len > sz)
  44288. len = sz;
  44289. XMEMCPY(buf, msg->buffer, len);
  44290. /* Move over returned data. */
  44291. msg->buffer += len;
  44292. msg->length -= len;
  44293. /* Amount actually copied. */
  44294. return len;
  44295. }
  44296. #endif
  44297. static int test_tls13_cipher_suites(void)
  44298. {
  44299. EXPECT_DECLS;
  44300. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44301. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44302. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44303. WOLFSSL_CTX* ctx = NULL;
  44304. WOLFSSL *ssl = NULL;
  44305. int i;
  44306. byte clientHello[] = {
  44307. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  44308. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  44309. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  44310. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  44311. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  44312. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  44313. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  44314. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  44315. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44316. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  44317. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  44318. 0x13, 0x01,
  44319. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  44320. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  44321. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  44322. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  44323. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  44324. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  44325. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  44326. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  44327. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  44328. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  44329. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  44330. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  44331. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  44332. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  44333. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  44334. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  44335. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  44336. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  44337. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  44338. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  44339. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  44340. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  44341. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  44342. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  44343. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  44344. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  44345. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  44346. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  44347. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  44348. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  44349. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  44350. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  44351. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  44352. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  44353. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  44354. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44355. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44356. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  44357. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  44358. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  44359. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  44360. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  44361. };
  44362. WOLFSSL_BUFFER_INFO msg;
  44363. /* Offset into ClientHello message data of first cipher suite. */
  44364. const int csOff = 78;
  44365. /* Server cipher list. */
  44366. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  44367. /* Suite list with duplicates. */
  44368. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  44369. "TLS13-AES128-GCM-SHA256:"
  44370. "TLS13-AES256-GCM-SHA384:"
  44371. "TLS13-AES256-GCM-SHA384:"
  44372. "TLS13-AES128-GCM-SHA256";
  44373. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44374. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44375. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44376. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44377. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44378. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  44379. #endif
  44380. /* Set up wolfSSL context. */
  44381. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  44382. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  44383. WOLFSSL_FILETYPE_PEM));
  44384. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  44385. WOLFSSL_FILETYPE_PEM));
  44386. /* Read from 'msg'. */
  44387. wolfSSL_SetIORecv(ctx, CsRecv);
  44388. /* No where to send to - dummy sender. */
  44389. wolfSSL_SetIOSend(ctx, CsSend);
  44390. /* Test cipher suite list with many copies of a cipher suite. */
  44391. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44392. msg.buffer = clientHello;
  44393. msg.length = (unsigned int)sizeof(clientHello);
  44394. wolfSSL_SetIOReadCtx(ssl, &msg);
  44395. /* Force server to have as many occurrences of same cipher suite as
  44396. * possible. */
  44397. if (ssl != NULL) {
  44398. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  44399. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  44400. for (i = 0; i < suites->suiteSz; i += 2) {
  44401. suites->suites[i + 0] = TLS13_BYTE;
  44402. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  44403. }
  44404. }
  44405. /* Test multiple occurrences of same cipher suite. */
  44406. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44407. wolfSSL_free(ssl);
  44408. ssl = NULL;
  44409. /* Set client order opposite to server order:
  44410. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  44411. clientHello[csOff + 0] = TLS13_BYTE;
  44412. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  44413. clientHello[csOff + 2] = TLS13_BYTE;
  44414. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  44415. /* Test server order negotiation. */
  44416. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44417. msg.buffer = clientHello;
  44418. msg.length = (unsigned int)sizeof(clientHello);
  44419. wolfSSL_SetIOReadCtx(ssl, &msg);
  44420. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44421. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44422. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  44423. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44424. /* Check refined order - server order. */
  44425. ExpectIntEQ(ssl->suites->suiteSz, 4);
  44426. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44427. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44428. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44429. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44430. wolfSSL_free(ssl);
  44431. ssl = NULL;
  44432. /* Test client order negotiation. */
  44433. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44434. msg.buffer = clientHello;
  44435. msg.length = (unsigned int)sizeof(clientHello);
  44436. wolfSSL_SetIOReadCtx(ssl, &msg);
  44437. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44438. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44439. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  44440. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  44441. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44442. /* Check refined order - client order. */
  44443. ExpectIntEQ(ssl->suites->suiteSz, 4);
  44444. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44445. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44446. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44447. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44448. wolfSSL_free(ssl);
  44449. ssl = NULL;
  44450. /* Check duplicate detection is working. */
  44451. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  44452. ExpectIntEQ(ctx->suites->suiteSz, 4);
  44453. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44454. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44455. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44456. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44457. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44458. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  44459. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  44460. ExpectIntEQ(ctx->suites->suiteSz, 4);
  44461. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44462. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44463. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44464. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44465. #endif
  44466. wolfSSL_CTX_free(ctx);
  44467. #endif
  44468. return EXPECT_RESULT();
  44469. }
  44470. #endif
  44471. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  44472. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  44473. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44474. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  44475. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  44476. const unsigned char* priv, unsigned int privSz,
  44477. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  44478. unsigned char* out, unsigned int* outlen,
  44479. void* ctx)
  44480. {
  44481. int result;
  44482. /* Test fail when context associated with WOLFSSL is NULL */
  44483. if (ctx == NULL) {
  44484. return -1;
  44485. }
  44486. (void)ssl;
  44487. /* return 0 on success */
  44488. PRIVATE_KEY_UNLOCK();
  44489. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  44490. PRIVATE_KEY_LOCK();
  44491. return result;
  44492. }
  44493. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  44494. EXPECT_DECLS;
  44495. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  44496. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  44497. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  44498. WOLFSSL_SUCCESS);
  44499. #endif
  44500. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44501. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  44502. WOLFSSL_SUCCESS);
  44503. #endif
  44504. return EXPECT_RESULT();
  44505. }
  44506. static int test_dh_ssl_setup(WOLFSSL* ssl)
  44507. {
  44508. EXPECT_DECLS;
  44509. static int dh_test_ctx = 1;
  44510. int ret;
  44511. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  44512. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  44513. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44514. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44515. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  44516. }
  44517. return EXPECT_RESULT();
  44518. }
  44519. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  44520. {
  44521. EXPECT_DECLS;
  44522. int ret;
  44523. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  44524. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  44525. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44526. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44527. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  44528. }
  44529. return EXPECT_RESULT();
  44530. }
  44531. #endif
  44532. static int test_DhCallbacks(void)
  44533. {
  44534. EXPECT_DECLS;
  44535. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  44536. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44537. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  44538. WOLFSSL_CTX *ctx = NULL;
  44539. WOLFSSL *ssl = NULL;
  44540. int test;
  44541. test_ssl_cbf func_cb_client;
  44542. test_ssl_cbf func_cb_server;
  44543. /* Test that DH callback APIs work. */
  44544. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44545. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  44546. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  44547. /* load client ca cert */
  44548. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  44549. WOLFSSL_SUCCESS);
  44550. /* test with NULL arguments */
  44551. wolfSSL_SetDhAgreeCtx(NULL, &test);
  44552. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  44553. /* test success case */
  44554. test = 1;
  44555. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44556. wolfSSL_SetDhAgreeCtx(ssl, &test);
  44557. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  44558. wolfSSL_free(ssl);
  44559. wolfSSL_CTX_free(ctx);
  44560. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  44561. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  44562. /* set callbacks to use DH functions */
  44563. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44564. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  44565. func_cb_client.method = wolfTLSv1_2_client_method;
  44566. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44567. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  44568. func_cb_server.method = wolfTLSv1_2_server_method;
  44569. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  44570. &func_cb_server, NULL), TEST_SUCCESS);
  44571. /* Test fail */
  44572. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  44573. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  44574. /* set callbacks to use DH functions */
  44575. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44576. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  44577. func_cb_client.method = wolfTLSv1_2_client_method;
  44578. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44579. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  44580. func_cb_server.method = wolfTLSv1_2_server_method;
  44581. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  44582. &func_cb_server, NULL), TEST_FAIL);
  44583. #endif
  44584. return EXPECT_RESULT();
  44585. }
  44586. #endif /* HAVE_PK_CALLBACKS */
  44587. #ifdef HAVE_HASHDRBG
  44588. #ifdef TEST_RESEED_INTERVAL
  44589. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44590. {
  44591. EXPECT_DECLS;
  44592. int i;
  44593. WC_RNG rng;
  44594. byte key[32];
  44595. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44596. ExpectIntEQ(wc_InitRng(&rng), 0);
  44597. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44598. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  44599. }
  44600. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44601. return EXPECT_RESULT();
  44602. }
  44603. #endif /* TEST_RESEED_INTERVAL */
  44604. static int test_wc_RNG_GenerateBlock(void)
  44605. {
  44606. EXPECT_DECLS;
  44607. int i;
  44608. WC_RNG rng;
  44609. byte key[32];
  44610. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44611. ExpectIntEQ(wc_InitRng(&rng), 0);
  44612. for (i = 0; i < 10; i++) {
  44613. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  44614. }
  44615. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44616. return EXPECT_RESULT();
  44617. }
  44618. #endif /* HAVE_HASHDRBG */
  44619. /*
  44620. * Testing get_rand_digit
  44621. */
  44622. static int test_get_rand_digit(void)
  44623. {
  44624. EXPECT_DECLS;
  44625. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44626. WC_RNG rng;
  44627. mp_digit d;
  44628. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44629. ExpectIntEQ(wc_InitRng(&rng), 0);
  44630. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  44631. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  44632. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  44633. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  44634. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44635. #endif
  44636. return EXPECT_RESULT();
  44637. } /* End test_get_rand_digit*/
  44638. /*
  44639. * Testing get_digit_count
  44640. */
  44641. static int test_get_digit_count(void)
  44642. {
  44643. EXPECT_DECLS;
  44644. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44645. mp_int a;
  44646. XMEMSET(&a, 0, sizeof(mp_int));
  44647. ExpectIntEQ(mp_init(&a), 0);
  44648. ExpectIntEQ(get_digit_count(NULL), 0);
  44649. ExpectIntEQ(get_digit_count(&a), 0);
  44650. mp_clear(&a);
  44651. #endif
  44652. return EXPECT_RESULT();
  44653. } /* End test_get_digit_count*/
  44654. /*
  44655. * Testing mp_cond_copy
  44656. */
  44657. static int test_mp_cond_copy(void)
  44658. {
  44659. EXPECT_DECLS;
  44660. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44661. defined(WOLFSSL_PUBLIC_MP)
  44662. mp_int a;
  44663. mp_int b;
  44664. int copy = 0;
  44665. XMEMSET(&a, 0, sizeof(mp_int));
  44666. XMEMSET(&b, 0, sizeof(mp_int));
  44667. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44668. ExpectIntEQ(mp_init(&b), MP_OKAY);
  44669. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  44670. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  44671. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  44672. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  44673. mp_clear(&a);
  44674. mp_clear(&b);
  44675. #endif
  44676. return EXPECT_RESULT();
  44677. } /* End test_mp_cond_copy*/
  44678. /*
  44679. * Testing mp_rand
  44680. */
  44681. static int test_mp_rand(void)
  44682. {
  44683. EXPECT_DECLS;
  44684. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44685. mp_int a;
  44686. WC_RNG rng;
  44687. int digits = 1;
  44688. XMEMSET(&a, 0, sizeof(mp_int));
  44689. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44690. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44691. ExpectIntEQ(wc_InitRng(&rng), 0);
  44692. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  44693. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  44694. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  44695. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  44696. mp_clear(&a);
  44697. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44698. #endif
  44699. return EXPECT_RESULT();
  44700. } /* End test_mp_rand*/
  44701. /*
  44702. * Testing get_digit
  44703. */
  44704. static int test_get_digit(void)
  44705. {
  44706. EXPECT_DECLS;
  44707. #if defined(WOLFSSL_PUBLIC_MP)
  44708. mp_int a;
  44709. int n = 0;
  44710. XMEMSET(&a, 0, sizeof(mp_int));
  44711. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44712. ExpectIntEQ(get_digit(NULL, n), 0);
  44713. ExpectIntEQ(get_digit(&a, n), 0);
  44714. mp_clear(&a);
  44715. #endif
  44716. return EXPECT_RESULT();
  44717. } /* End test_get_digit*/
  44718. /*
  44719. * Testing wc_export_int
  44720. */
  44721. static int test_wc_export_int(void)
  44722. {
  44723. EXPECT_DECLS;
  44724. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44725. defined(WOLFSSL_PUBLIC_MP)
  44726. mp_int mp;
  44727. byte buf[32];
  44728. word32 keySz = (word32)sizeof(buf);
  44729. word32 len = (word32)sizeof(buf);
  44730. XMEMSET(&mp, 0, sizeof(mp_int));
  44731. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  44732. ExpectIntEQ(mp_set(&mp, 1234), 0);
  44733. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  44734. BAD_FUNC_ARG);
  44735. len = sizeof(buf)-1;
  44736. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  44737. BUFFER_E);
  44738. len = sizeof(buf);
  44739. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  44740. len = 4; /* test input too small */
  44741. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  44742. len = sizeof(buf);
  44743. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  44744. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44745. ExpectIntEQ(len, 5);
  44746. mp_clear(&mp);
  44747. #endif
  44748. return EXPECT_RESULT();
  44749. } /* End test_wc_export_int*/
  44750. static int test_wc_InitRngNonce(void)
  44751. {
  44752. EXPECT_DECLS;
  44753. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44754. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44755. HAVE_FIPS_VERSION >= 2))
  44756. WC_RNG rng;
  44757. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44758. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44759. word32 nonceSz = sizeof(nonce);
  44760. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  44761. ExpectIntEQ(wc_FreeRng(&rng), 0);
  44762. #endif
  44763. return EXPECT_RESULT();
  44764. } /* End test_wc_InitRngNonce*/
  44765. /*
  44766. * Testing wc_InitRngNonce_ex
  44767. */
  44768. static int test_wc_InitRngNonce_ex(void)
  44769. {
  44770. EXPECT_DECLS;
  44771. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44772. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44773. HAVE_FIPS_VERSION >= 2))
  44774. WC_RNG rng;
  44775. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44776. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44777. word32 nonceSz = sizeof(nonce);
  44778. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  44779. 0);
  44780. ExpectIntEQ(wc_FreeRng(&rng), 0);
  44781. #endif
  44782. return EXPECT_RESULT();
  44783. } /* End test_wc_InitRngNonce_ex */
  44784. static int test_wolfSSL_X509_CRL(void)
  44785. {
  44786. EXPECT_DECLS;
  44787. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44788. X509_CRL *crl = NULL;
  44789. char pem[][100] = {
  44790. "./certs/crl/crl.pem",
  44791. "./certs/crl/crl2.pem",
  44792. "./certs/crl/caEccCrl.pem",
  44793. "./certs/crl/eccCliCRL.pem",
  44794. "./certs/crl/eccSrvCRL.pem",
  44795. ""
  44796. };
  44797. #ifndef NO_BIO
  44798. BIO *bio = NULL;
  44799. #endif
  44800. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44801. char der[][100] = {
  44802. "./certs/crl/crl.der",
  44803. "./certs/crl/crl2.der",
  44804. ""};
  44805. #endif
  44806. XFILE fp = XBADFILE;
  44807. int i;
  44808. for (i = 0; pem[i][0] != '\0'; i++)
  44809. {
  44810. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  44811. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  44812. NULL, NULL));
  44813. ExpectNotNull(crl);
  44814. X509_CRL_free(crl);
  44815. if (fp != XBADFILE) {
  44816. XFCLOSE(fp);
  44817. fp = XBADFILE;
  44818. }
  44819. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  44820. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  44821. NULL));
  44822. if (EXPECT_FAIL()) {
  44823. crl = NULL;
  44824. }
  44825. ExpectNotNull(crl);
  44826. X509_CRL_free(crl);
  44827. crl = NULL;
  44828. if (fp != XBADFILE) {
  44829. XFCLOSE(fp);
  44830. fp = XBADFILE;
  44831. }
  44832. }
  44833. #ifndef NO_BIO
  44834. for (i = 0; pem[i][0] != '\0'; i++)
  44835. {
  44836. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  44837. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44838. X509_CRL_free(crl);
  44839. crl = NULL;
  44840. BIO_free(bio);
  44841. bio = NULL;
  44842. }
  44843. #endif
  44844. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44845. for (i = 0; der[i][0] != '\0'; i++) {
  44846. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  44847. ExpectTrue((fp != XBADFILE));
  44848. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44849. ExpectNotNull(crl);
  44850. X509_CRL_free(crl);
  44851. if (fp != XBADFILE) {
  44852. XFCLOSE(fp);
  44853. fp = XBADFILE;
  44854. }
  44855. fp = XFOPEN(der[i], "rb");
  44856. ExpectTrue((fp != XBADFILE));
  44857. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44858. if (EXPECT_FAIL()) {
  44859. crl = NULL;
  44860. }
  44861. ExpectNotNull(crl);
  44862. X509_CRL_free(crl);
  44863. crl = NULL;
  44864. if (fp != XBADFILE) {
  44865. XFCLOSE(fp);
  44866. fp = XBADFILE;
  44867. }
  44868. }
  44869. #endif
  44870. #endif
  44871. return EXPECT_RESULT();
  44872. }
  44873. static int test_wolfSSL_X509_load_crl_file(void)
  44874. {
  44875. EXPECT_DECLS;
  44876. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44877. !defined(NO_RSA) && !defined(NO_BIO)
  44878. int i;
  44879. char pem[][100] = {
  44880. "./certs/crl/crl.pem",
  44881. "./certs/crl/crl2.pem",
  44882. "./certs/crl/caEccCrl.pem",
  44883. "./certs/crl/eccCliCRL.pem",
  44884. "./certs/crl/eccSrvCRL.pem",
  44885. ""
  44886. };
  44887. char der[][100] = {
  44888. "./certs/crl/crl.der",
  44889. "./certs/crl/crl2.der",
  44890. ""
  44891. };
  44892. WOLFSSL_X509_STORE* store = NULL;
  44893. WOLFSSL_X509_LOOKUP* lookup = NULL;
  44894. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44895. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44896. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44897. X509_FILETYPE_PEM), 1);
  44898. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44899. X509_FILETYPE_PEM), 1);
  44900. if (store) {
  44901. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44902. WOLFSSL_FILETYPE_PEM), 1);
  44903. /* since store hasn't yet known the revoked cert*/
  44904. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44905. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  44906. }
  44907. for (i = 0; pem[i][0] != '\0'; i++) {
  44908. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  44909. 1);
  44910. }
  44911. if (store) {
  44912. /* since store knows crl list */
  44913. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44914. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  44915. CRL_CERT_REVOKED);
  44916. }
  44917. /* once feeing store */
  44918. X509_STORE_free(store);
  44919. store = NULL;
  44920. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44921. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44922. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44923. X509_FILETYPE_PEM), 1);
  44924. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44925. X509_FILETYPE_PEM), 1);
  44926. if (store) {
  44927. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44928. WOLFSSL_FILETYPE_PEM), 1);
  44929. /* since store hasn't yet known the revoked cert*/
  44930. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44931. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  44932. }
  44933. for (i = 0; der[i][0] != '\0'; i++) {
  44934. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  44935. 1);
  44936. }
  44937. if (store) {
  44938. /* since store knows crl list */
  44939. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44940. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  44941. CRL_CERT_REVOKED);
  44942. }
  44943. /* test for incorrect parameter */
  44944. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44945. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44946. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44947. X509_STORE_free(store);
  44948. store = NULL;
  44949. #endif
  44950. return EXPECT_RESULT();
  44951. }
  44952. static int test_wolfSSL_d2i_X509_REQ(void)
  44953. {
  44954. EXPECT_DECLS;
  44955. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44956. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44957. !defined(WOLFSSL_SP_MATH)
  44958. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  44959. * were generated by libest
  44960. * ./certs/csr.attr.der contains sample attributes
  44961. * ./certs/csr.ext.der contains sample extensions */
  44962. const char* csrFile = "./certs/csr.signed.der";
  44963. const char* csrPopFile = "./certs/csr.attr.der";
  44964. const char* csrExtFile = "./certs/csr.ext.der";
  44965. /* ./certs/csr.dsa.pem is generated using
  44966. * openssl req -newkey dsa:certs/dsaparams.pem \
  44967. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44968. * -outform PEM
  44969. * with the passphrase "wolfSSL"
  44970. */
  44971. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44972. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44973. XFILE f = XBADFILE;
  44974. #endif
  44975. BIO* bio = NULL;
  44976. X509* req = NULL;
  44977. EVP_PKEY *pub_key = NULL;
  44978. {
  44979. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  44980. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  44981. /*
  44982. * Extract the public key from the CSR
  44983. */
  44984. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  44985. /*
  44986. * Verify the signature in the CSR
  44987. */
  44988. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44989. X509_free(req);
  44990. req = NULL;
  44991. BIO_free(bio);
  44992. bio = NULL;
  44993. EVP_PKEY_free(pub_key);
  44994. pub_key = NULL;
  44995. }
  44996. {
  44997. #ifdef OPENSSL_ALL
  44998. X509_ATTRIBUTE* attr;
  44999. ASN1_TYPE *at;
  45000. #endif
  45001. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  45002. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  45003. /*
  45004. * Extract the public key from the CSR
  45005. */
  45006. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  45007. /*
  45008. * Verify the signature in the CSR
  45009. */
  45010. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  45011. #ifdef OPENSSL_ALL
  45012. /*
  45013. * Obtain the challenge password from the CSR
  45014. */
  45015. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  45016. -1), 1);
  45017. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  45018. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  45019. ExpectNotNull(at->value.asn1_string);
  45020. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  45021. "2xIE+qqp/rhyTXP+");
  45022. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  45023. #endif
  45024. X509_free(req);
  45025. req = NULL;
  45026. BIO_free(bio);
  45027. bio = NULL;
  45028. EVP_PKEY_free(pub_key);
  45029. pub_key = NULL;
  45030. }
  45031. {
  45032. #ifdef OPENSSL_ALL
  45033. X509_ATTRIBUTE* attr;
  45034. ASN1_TYPE *at;
  45035. STACK_OF(X509_EXTENSION) *exts = NULL;
  45036. #endif
  45037. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  45038. /* This CSR contains an Extension Request attribute so
  45039. * we test extension parsing in a CSR attribute here. */
  45040. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  45041. /*
  45042. * Extract the public key from the CSR
  45043. */
  45044. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  45045. /*
  45046. * Verify the signature in the CSR
  45047. */
  45048. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  45049. #ifdef OPENSSL_ALL
  45050. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  45051. req));
  45052. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  45053. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  45054. /*
  45055. * Obtain the challenge password from the CSR
  45056. */
  45057. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  45058. -1), 0);
  45059. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  45060. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  45061. ExpectNotNull(at->value.asn1_string);
  45062. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  45063. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  45064. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  45065. #endif
  45066. X509_free(req);
  45067. req = NULL;
  45068. BIO_free(bio);
  45069. bio = NULL;
  45070. EVP_PKEY_free(pub_key);
  45071. pub_key = NULL;
  45072. }
  45073. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  45074. {
  45075. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  45076. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  45077. /*
  45078. * Extract the public key from the CSR
  45079. */
  45080. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  45081. /*
  45082. * Verify the signature in the CSR
  45083. */
  45084. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  45085. X509_free(req);
  45086. req = NULL;
  45087. BIO_free(bio);
  45088. /* Run the same test, but with a file pointer instead of a BIO.
  45089. * (PEM_read_X509_REQ)*/
  45090. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  45091. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  45092. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  45093. X509_free(req);
  45094. EVP_PKEY_free(pub_key);
  45095. }
  45096. #endif /* !NO_DSA && !HAVE_SELFTEST */
  45097. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  45098. return EXPECT_RESULT();
  45099. }
  45100. static int test_wolfSSL_PEM_read_X509(void)
  45101. {
  45102. EXPECT_DECLS;
  45103. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  45104. !defined(NO_RSA)
  45105. X509 *x509 = NULL;
  45106. XFILE fp = XBADFILE;
  45107. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45108. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  45109. X509_free(x509);
  45110. if (fp != XBADFILE)
  45111. XFCLOSE(fp);
  45112. #endif
  45113. return EXPECT_RESULT();
  45114. }
  45115. static int test_wolfSSL_PEM_read(void)
  45116. {
  45117. EXPECT_DECLS;
  45118. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45119. const char* filename = "./certs/server-keyEnc.pem";
  45120. XFILE fp = XBADFILE;
  45121. char* name = NULL;
  45122. char* header = NULL;
  45123. byte* data = NULL;
  45124. long len;
  45125. EVP_CIPHER_INFO cipher;
  45126. WOLFSSL_BIO* bio = NULL;
  45127. byte* fileData = NULL;
  45128. size_t fileDataSz = 0;
  45129. byte* out;
  45130. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  45131. /* Fail cases. */
  45132. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  45133. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  45134. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  45135. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  45136. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  45137. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  45138. ExpectIntGT(XSTRLEN(header), 0);
  45139. ExpectIntGT(len, 0);
  45140. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  45141. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  45142. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  45143. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  45144. DYNAMIC_TYPE_TMP_BUFFER));
  45145. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  45146. if (fp != XBADFILE) {
  45147. XFCLOSE(fp);
  45148. fp = XBADFILE;
  45149. }
  45150. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45151. /* Fail cases. */
  45152. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  45153. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  45154. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  45155. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  45156. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45157. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45158. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45159. /* Fail cases. */
  45160. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  45161. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  45162. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  45163. #ifndef NO_DES3
  45164. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  45165. #endif
  45166. /* Fail cases. */
  45167. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  45168. (void*)"yassl123"), WOLFSSL_FAILURE);
  45169. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  45170. (void*)"yassl123"), WOLFSSL_FAILURE);
  45171. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  45172. (void*)"yassl123"), WOLFSSL_FAILURE);
  45173. #if !defined(NO_DES3) && !defined(NO_MD5)
  45174. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  45175. (void*)"yassl123"), WOLFSSL_SUCCESS);
  45176. #endif
  45177. BIO_free(bio);
  45178. bio = NULL;
  45179. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45180. fileData = NULL;
  45181. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45182. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45183. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45184. name = NULL;
  45185. header = NULL;
  45186. data = NULL;
  45187. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  45188. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  45189. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  45190. ExpectIntEQ(XSTRLEN(header), 0);
  45191. ExpectIntGT(len, 0);
  45192. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  45193. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  45194. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  45195. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  45196. DYNAMIC_TYPE_TMP_BUFFER));
  45197. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  45198. if (fp != XBADFILE)
  45199. XFCLOSE(fp);
  45200. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45201. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45202. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45203. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45204. BIO_free(bio);
  45205. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45206. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45207. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45208. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45209. #endif
  45210. return EXPECT_RESULT();
  45211. }
  45212. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  45213. {
  45214. EXPECT_DECLS;
  45215. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45216. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45217. const byte iv[12] = { 0 };
  45218. const byte key[16] = { 0 };
  45219. const byte cleartext[16] = { 0 };
  45220. const byte aad[] = {
  45221. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  45222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  45223. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  45224. 0x4f
  45225. };
  45226. byte out1Part[16];
  45227. byte outTag1Part[16];
  45228. byte out2Part[16];
  45229. byte outTag2Part[16];
  45230. byte decryptBuf[16];
  45231. int len = 0;
  45232. int tlen;
  45233. EVP_CIPHER_CTX* ctx = NULL;
  45234. /* ENCRYPT */
  45235. /* Send AAD and data in 1 part */
  45236. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45237. tlen = 0;
  45238. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45239. 1);
  45240. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45241. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45242. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  45243. sizeof(cleartext)), 1);
  45244. tlen += len;
  45245. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  45246. tlen += len;
  45247. ExpectIntEQ(tlen, sizeof(cleartext));
  45248. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45249. outTag1Part), 1);
  45250. EVP_CIPHER_CTX_free(ctx);
  45251. ctx = NULL;
  45252. /* DECRYPT */
  45253. /* Send AAD and data in 1 part */
  45254. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45255. tlen = 0;
  45256. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45257. 1);
  45258. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45259. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45260. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  45261. sizeof(cleartext)), 1);
  45262. tlen += len;
  45263. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45264. outTag1Part), 1);
  45265. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  45266. tlen += len;
  45267. ExpectIntEQ(tlen, sizeof(cleartext));
  45268. EVP_CIPHER_CTX_free(ctx);
  45269. ctx = NULL;
  45270. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45271. /* ENCRYPT */
  45272. /* Send AAD and data in 2 parts */
  45273. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45274. tlen = 0;
  45275. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45276. 1);
  45277. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45278. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45279. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45280. 1);
  45281. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  45282. tlen += len;
  45283. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  45284. sizeof(cleartext) - 1), 1);
  45285. tlen += len;
  45286. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  45287. tlen += len;
  45288. ExpectIntEQ(tlen, sizeof(cleartext));
  45289. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45290. outTag2Part), 1);
  45291. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  45292. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  45293. EVP_CIPHER_CTX_free(ctx);
  45294. ctx = NULL;
  45295. /* DECRYPT */
  45296. /* Send AAD and data in 2 parts */
  45297. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45298. tlen = 0;
  45299. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45300. 1);
  45301. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45302. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45303. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45304. 1);
  45305. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  45306. tlen += len;
  45307. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  45308. sizeof(cleartext) - 1), 1);
  45309. tlen += len;
  45310. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45311. outTag1Part), 1);
  45312. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  45313. tlen += len;
  45314. ExpectIntEQ(tlen, sizeof(cleartext));
  45315. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45316. /* Test AAD reuse */
  45317. EVP_CIPHER_CTX_free(ctx);
  45318. #endif
  45319. return EXPECT_RESULT();
  45320. }
  45321. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  45322. {
  45323. EXPECT_DECLS;
  45324. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45325. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45326. /* Zero length plain text */
  45327. byte key[] = {
  45328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45330. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45331. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45332. }; /* align */
  45333. byte iv[] = {
  45334. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45335. }; /* align */
  45336. byte plaintxt[1];
  45337. int ivSz = 12;
  45338. int plaintxtSz = 0;
  45339. unsigned char tag[16];
  45340. unsigned char tag_kat[] = {
  45341. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  45342. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  45343. };
  45344. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45345. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45346. int ciphertxtSz = 0;
  45347. int decryptedtxtSz = 0;
  45348. int len = 0;
  45349. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45350. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45351. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  45352. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45353. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45354. plaintxtSz));
  45355. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45356. ciphertxtSz += len;
  45357. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  45358. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45359. ExpectIntEQ(0, ciphertxtSz);
  45360. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  45361. EVP_CIPHER_CTX_init(de);
  45362. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  45363. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45364. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45365. decryptedtxtSz = len;
  45366. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  45367. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45368. decryptedtxtSz += len;
  45369. ExpectIntEQ(0, decryptedtxtSz);
  45370. EVP_CIPHER_CTX_free(en);
  45371. EVP_CIPHER_CTX_free(de);
  45372. #endif
  45373. return EXPECT_RESULT();
  45374. }
  45375. static int test_wolfssl_EVP_aes_gcm(void)
  45376. {
  45377. EXPECT_DECLS;
  45378. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45379. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45380. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45381. byte *key = (byte*)"01234567890123456789012345678901";
  45382. /* A 128 bit IV */
  45383. byte *iv = (byte*)"0123456789012345";
  45384. int ivSz = AES_BLOCK_SIZE;
  45385. /* Message to be encrypted */
  45386. byte *plaintxt = (byte*)"for things to change you have to change";
  45387. /* Additional non-confidential data */
  45388. byte *aad = (byte*)"Don't spend major time on minor things.";
  45389. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45390. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45391. int aadSz = (int)XSTRLEN((char*)aad);
  45392. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45393. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45394. int ciphertxtSz = 0;
  45395. int decryptedtxtSz = 0;
  45396. int len = 0;
  45397. int i = 0;
  45398. EVP_CIPHER_CTX en[2];
  45399. EVP_CIPHER_CTX de[2];
  45400. for (i = 0; i < 2; i++) {
  45401. EVP_CIPHER_CTX_init(&en[i]);
  45402. if (i == 0) {
  45403. /* Default uses 96-bits IV length */
  45404. #ifdef WOLFSSL_AES_128
  45405. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  45406. key, iv));
  45407. #elif defined(WOLFSSL_AES_192)
  45408. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  45409. key, iv));
  45410. #elif defined(WOLFSSL_AES_256)
  45411. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  45412. key, iv));
  45413. #endif
  45414. }
  45415. else {
  45416. #ifdef WOLFSSL_AES_128
  45417. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  45418. NULL, NULL));
  45419. #elif defined(WOLFSSL_AES_192)
  45420. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  45421. NULL, NULL));
  45422. #elif defined(WOLFSSL_AES_256)
  45423. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  45424. NULL, NULL));
  45425. #endif
  45426. /* non-default must to set the IV length first */
  45427. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  45428. ivSz, NULL));
  45429. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45430. }
  45431. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45432. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  45433. plaintxtSz));
  45434. ciphertxtSz = len;
  45435. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45436. ciphertxtSz += len;
  45437. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  45438. AES_BLOCK_SIZE, tag));
  45439. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45440. EVP_CIPHER_CTX_init(&de[i]);
  45441. if (i == 0) {
  45442. /* Default uses 96-bits IV length */
  45443. #ifdef WOLFSSL_AES_128
  45444. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  45445. key, iv));
  45446. #elif defined(WOLFSSL_AES_192)
  45447. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  45448. key, iv));
  45449. #elif defined(WOLFSSL_AES_256)
  45450. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  45451. key, iv));
  45452. #endif
  45453. }
  45454. else {
  45455. #ifdef WOLFSSL_AES_128
  45456. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  45457. NULL, NULL));
  45458. #elif defined(WOLFSSL_AES_192)
  45459. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  45460. NULL, NULL));
  45461. #elif defined(WOLFSSL_AES_256)
  45462. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  45463. NULL, NULL));
  45464. #endif
  45465. /* non-default must to set the IV length first */
  45466. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  45467. ivSz, NULL));
  45468. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45469. }
  45470. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45471. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45472. ciphertxtSz));
  45473. decryptedtxtSz = len;
  45474. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  45475. AES_BLOCK_SIZE, tag));
  45476. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45477. decryptedtxtSz += len;
  45478. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  45479. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45480. /* modify tag*/
  45481. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45482. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45483. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  45484. AES_BLOCK_SIZE, tag));
  45485. /* fail due to wrong tag */
  45486. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45487. ciphertxtSz));
  45488. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45489. ExpectIntEQ(0, len);
  45490. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45491. }
  45492. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45493. return EXPECT_RESULT();
  45494. }
  45495. static int test_wolfssl_EVP_aria_gcm(void)
  45496. {
  45497. int res = TEST_SKIPPED;
  45498. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  45499. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45500. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45501. byte *key = (byte*)"01234567890123456789012345678901";
  45502. /* A 128 bit IV */
  45503. byte *iv = (byte*)"0123456789012345";
  45504. int ivSz = ARIA_BLOCK_SIZE;
  45505. /* Message to be encrypted */
  45506. const int plaintxtSz = 40;
  45507. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  45508. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  45509. /* Additional non-confidential data */
  45510. byte *aad = (byte*)"Don't spend major time on minor things.";
  45511. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  45512. int aadSz = (int)XSTRLEN((char*)aad);
  45513. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  45514. byte decryptedtxt[plaintxtSz];
  45515. int ciphertxtSz = 0;
  45516. int decryptedtxtSz = 0;
  45517. int len = 0;
  45518. int i = 0;
  45519. #define TEST_ARIA_GCM_COUNT 6
  45520. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  45521. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  45522. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  45523. EVP_CIPHER_CTX_init(&en[i]);
  45524. switch (i) {
  45525. case 0:
  45526. /* Default uses 96-bits IV length */
  45527. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  45528. break;
  45529. case 1:
  45530. /* Default uses 96-bits IV length */
  45531. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  45532. break;
  45533. case 2:
  45534. /* Default uses 96-bits IV length */
  45535. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  45536. break;
  45537. case 3:
  45538. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  45539. /* non-default must to set the IV length first */
  45540. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45541. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45542. break;
  45543. case 4:
  45544. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  45545. /* non-default must to set the IV length first */
  45546. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45547. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45548. break;
  45549. case 5:
  45550. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  45551. /* non-default must to set the IV length first */
  45552. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45553. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45554. break;
  45555. }
  45556. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  45557. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45558. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  45559. ciphertxtSz = len;
  45560. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45561. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  45562. ciphertxtSz += len;
  45563. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  45564. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45565. EVP_CIPHER_CTX_init(&de[i]);
  45566. switch (i) {
  45567. case 0:
  45568. /* Default uses 96-bits IV length */
  45569. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  45570. break;
  45571. case 1:
  45572. /* Default uses 96-bits IV length */
  45573. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  45574. break;
  45575. case 2:
  45576. /* Default uses 96-bits IV length */
  45577. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  45578. break;
  45579. case 3:
  45580. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  45581. /* non-default must to set the IV length first */
  45582. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45583. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45584. break;
  45585. case 4:
  45586. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  45587. /* non-default must to set the IV length first */
  45588. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45589. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45590. break;
  45591. case 5:
  45592. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  45593. /* non-default must to set the IV length first */
  45594. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45595. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45596. break;
  45597. }
  45598. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  45599. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45600. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45601. decryptedtxtSz = len;
  45602. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  45603. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45604. decryptedtxtSz += len;
  45605. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  45606. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45607. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  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], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  45612. /* fail due to wrong tag */
  45613. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45614. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45615. AssertIntEQ(0, len);
  45616. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45617. }
  45618. res = TEST_RES_CHECK(1);
  45619. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45620. return res;
  45621. }
  45622. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  45623. {
  45624. EXPECT_DECLS;
  45625. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45626. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45627. /* Zero length plain text */
  45628. byte key[] = {
  45629. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45630. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45631. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45632. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45633. }; /* align */
  45634. byte iv[] = {
  45635. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45636. }; /* align */
  45637. byte plaintxt[1];
  45638. int ivSz = 12;
  45639. int plaintxtSz = 0;
  45640. unsigned char tag[16];
  45641. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45642. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45643. int ciphertxtSz = 0;
  45644. int decryptedtxtSz = 0;
  45645. int len = 0;
  45646. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45647. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45648. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  45649. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45650. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45651. plaintxtSz));
  45652. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45653. ciphertxtSz += len;
  45654. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  45655. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45656. ExpectIntEQ(0, ciphertxtSz);
  45657. EVP_CIPHER_CTX_init(de);
  45658. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  45659. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45660. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45661. decryptedtxtSz = len;
  45662. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  45663. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45664. decryptedtxtSz += len;
  45665. ExpectIntEQ(0, decryptedtxtSz);
  45666. EVP_CIPHER_CTX_free(en);
  45667. EVP_CIPHER_CTX_free(de);
  45668. #endif
  45669. return EXPECT_RESULT();
  45670. }
  45671. static int test_wolfssl_EVP_aes_ccm(void)
  45672. {
  45673. EXPECT_DECLS;
  45674. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45675. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45676. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45677. byte *key = (byte*)"01234567890123456789012345678901";
  45678. /* A 128 bit IV */
  45679. byte *iv = (byte*)"0123456789012";
  45680. int ivSz = (int)XSTRLEN((char*)iv);
  45681. /* Message to be encrypted */
  45682. byte *plaintxt = (byte*)"for things to change you have to change";
  45683. /* Additional non-confidential data */
  45684. byte *aad = (byte*)"Don't spend major time on minor things.";
  45685. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45686. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45687. int aadSz = (int)XSTRLEN((char*)aad);
  45688. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45689. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45690. int ciphertxtSz = 0;
  45691. int decryptedtxtSz = 0;
  45692. int len = 0;
  45693. int i = 0;
  45694. int ret;
  45695. EVP_CIPHER_CTX en[2];
  45696. EVP_CIPHER_CTX de[2];
  45697. for (i = 0; i < 2; i++) {
  45698. EVP_CIPHER_CTX_init(&en[i]);
  45699. if (i == 0) {
  45700. /* Default uses 96-bits IV length */
  45701. #ifdef WOLFSSL_AES_128
  45702. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  45703. key, iv));
  45704. #elif defined(WOLFSSL_AES_192)
  45705. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  45706. key, iv));
  45707. #elif defined(WOLFSSL_AES_256)
  45708. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  45709. key, iv));
  45710. #endif
  45711. }
  45712. else {
  45713. #ifdef WOLFSSL_AES_128
  45714. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  45715. NULL, NULL));
  45716. #elif defined(WOLFSSL_AES_192)
  45717. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  45718. NULL, NULL));
  45719. #elif defined(WOLFSSL_AES_256)
  45720. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  45721. NULL, NULL));
  45722. #endif
  45723. /* non-default must to set the IV length first */
  45724. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  45725. ivSz, NULL));
  45726. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45727. }
  45728. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45729. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  45730. plaintxtSz));
  45731. ciphertxtSz = len;
  45732. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45733. ciphertxtSz += len;
  45734. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  45735. AES_BLOCK_SIZE, tag));
  45736. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  45737. ExpectIntEQ(ret, 1);
  45738. EVP_CIPHER_CTX_init(&de[i]);
  45739. if (i == 0) {
  45740. /* Default uses 96-bits IV length */
  45741. #ifdef WOLFSSL_AES_128
  45742. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  45743. key, iv));
  45744. #elif defined(WOLFSSL_AES_192)
  45745. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  45746. key, iv));
  45747. #elif defined(WOLFSSL_AES_256)
  45748. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  45749. key, iv));
  45750. #endif
  45751. }
  45752. else {
  45753. #ifdef WOLFSSL_AES_128
  45754. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  45755. NULL, NULL));
  45756. #elif defined(WOLFSSL_AES_192)
  45757. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  45758. NULL, NULL));
  45759. #elif defined(WOLFSSL_AES_256)
  45760. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  45761. NULL, NULL));
  45762. #endif
  45763. /* non-default must to set the IV length first */
  45764. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  45765. ivSz, NULL));
  45766. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45767. }
  45768. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45769. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45770. ciphertxtSz));
  45771. decryptedtxtSz = len;
  45772. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  45773. AES_BLOCK_SIZE, tag));
  45774. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45775. decryptedtxtSz += len;
  45776. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  45777. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45778. /* modify tag*/
  45779. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45780. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45781. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  45782. AES_BLOCK_SIZE, tag));
  45783. /* fail due to wrong tag */
  45784. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45785. ciphertxtSz));
  45786. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45787. ExpectIntEQ(0, len);
  45788. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  45789. ExpectIntEQ(ret, 1);
  45790. }
  45791. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  45792. return EXPECT_RESULT();
  45793. }
  45794. static int test_wolfssl_EVP_chacha20_poly1305(void)
  45795. {
  45796. EXPECT_DECLS;
  45797. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45798. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45799. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45800. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45801. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45802. byte cipherText[sizeof(plainText)];
  45803. byte decryptedText[sizeof(plainText)];
  45804. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45805. EVP_CIPHER_CTX* ctx = NULL;
  45806. int outSz;
  45807. /* Encrypt. */
  45808. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45809. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45810. NULL), WOLFSSL_SUCCESS);
  45811. /* Invalid IV length. */
  45812. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45813. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45814. /* Valid IV length. */
  45815. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45816. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45817. /* Invalid tag length. */
  45818. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45819. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45820. /* Valid tag length. */
  45821. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45822. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45823. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45824. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45825. WOLFSSL_SUCCESS);
  45826. ExpectIntEQ(outSz, sizeof(aad));
  45827. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45828. sizeof(plainText)), WOLFSSL_SUCCESS);
  45829. ExpectIntEQ(outSz, sizeof(plainText));
  45830. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45831. ExpectIntEQ(outSz, 0);
  45832. /* Invalid tag length. */
  45833. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45834. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45835. /* Valid tag length. */
  45836. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45837. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45838. EVP_CIPHER_CTX_free(ctx);
  45839. ctx = NULL;
  45840. /* Decrypt. */
  45841. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45842. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45843. NULL), WOLFSSL_SUCCESS);
  45844. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45845. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45846. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45847. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45848. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45849. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45850. WOLFSSL_SUCCESS);
  45851. ExpectIntEQ(outSz, sizeof(aad));
  45852. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45853. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45854. ExpectIntEQ(outSz, sizeof(cipherText));
  45855. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45856. WOLFSSL_SUCCESS);
  45857. ExpectIntEQ(outSz, 0);
  45858. EVP_CIPHER_CTX_free(ctx);
  45859. ctx = NULL;
  45860. /* Test partial Inits. CipherInit() allow setting of key and iv
  45861. * in separate calls. */
  45862. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45863. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45864. key, NULL, 1), WOLFSSL_SUCCESS);
  45865. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45866. WOLFSSL_SUCCESS);
  45867. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45868. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45869. ExpectIntEQ(outSz, sizeof(aad));
  45870. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45871. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45872. ExpectIntEQ(outSz, sizeof(cipherText));
  45873. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45874. WOLFSSL_SUCCESS);
  45875. ExpectIntEQ(outSz, 0);
  45876. EVP_CIPHER_CTX_free(ctx);
  45877. #endif
  45878. return EXPECT_RESULT();
  45879. }
  45880. static int test_wolfssl_EVP_chacha20(void)
  45881. {
  45882. EXPECT_DECLS;
  45883. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45884. byte key[CHACHA_MAX_KEY_SZ];
  45885. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45886. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45887. byte cipherText[sizeof(plainText)];
  45888. byte decryptedText[sizeof(plainText)];
  45889. EVP_CIPHER_CTX* ctx = NULL;
  45890. int outSz;
  45891. /* Encrypt. */
  45892. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45893. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45894. NULL), WOLFSSL_SUCCESS);
  45895. /* Any tag length must fail - not an AEAD cipher. */
  45896. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45897. 16, NULL), WOLFSSL_FAILURE);
  45898. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45899. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45900. sizeof(plainText)), WOLFSSL_SUCCESS);
  45901. ExpectIntEQ(outSz, sizeof(plainText));
  45902. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45903. ExpectIntEQ(outSz, 0);
  45904. EVP_CIPHER_CTX_free(ctx);
  45905. ctx = NULL;
  45906. /* Decrypt. */
  45907. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45908. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45909. NULL), WOLFSSL_SUCCESS);
  45910. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45911. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45912. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45913. ExpectIntEQ(outSz, sizeof(cipherText));
  45914. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45915. WOLFSSL_SUCCESS);
  45916. ExpectIntEQ(outSz, 0);
  45917. EVP_CIPHER_CTX_free(ctx);
  45918. ctx = NULL;
  45919. /* Test partial Inits. CipherInit() allow setting of key and iv
  45920. * in separate calls. */
  45921. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45922. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45923. key, NULL, 1), WOLFSSL_SUCCESS);
  45924. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45925. WOLFSSL_SUCCESS);
  45926. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45927. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45928. ExpectIntEQ(outSz, sizeof(cipherText));
  45929. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45930. WOLFSSL_SUCCESS);
  45931. ExpectIntEQ(outSz, 0);
  45932. EVP_CIPHER_CTX_free(ctx);
  45933. #endif
  45934. return EXPECT_RESULT();
  45935. }
  45936. static int test_wolfssl_EVP_sm4_ecb(void)
  45937. {
  45938. int res = TEST_SKIPPED;
  45939. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  45940. EXPECT_DECLS;
  45941. byte key[SM4_KEY_SIZE];
  45942. byte plainText[SM4_BLOCK_SIZE] = {
  45943. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  45944. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45945. };
  45946. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45947. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45948. EVP_CIPHER_CTX* ctx;
  45949. int outSz;
  45950. XMEMSET(key, 0, sizeof(key));
  45951. /* Encrypt. */
  45952. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45953. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  45954. WOLFSSL_SUCCESS);
  45955. /* Any tag length must fail - not an AEAD cipher. */
  45956. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  45957. WOLFSSL_FAILURE);
  45958. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  45959. WOLFSSL_SUCCESS);
  45960. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45961. sizeof(plainText)), WOLFSSL_SUCCESS);
  45962. ExpectIntEQ(outSz, sizeof(plainText));
  45963. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  45964. WOLFSSL_SUCCESS);
  45965. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  45966. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  45967. EVP_CIPHER_CTX_free(ctx);
  45968. /* Decrypt. */
  45969. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45970. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  45971. WOLFSSL_SUCCESS);
  45972. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  45973. WOLFSSL_SUCCESS);
  45974. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45975. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45976. ExpectIntEQ(outSz, sizeof(plainText));
  45977. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  45978. WOLFSSL_SUCCESS);
  45979. ExpectIntEQ(outSz, 0);
  45980. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45981. EVP_CIPHER_CTX_free(ctx);
  45982. res = EXPECT_RESULT();
  45983. #endif
  45984. return res;
  45985. }
  45986. static int test_wolfssl_EVP_sm4_cbc(void)
  45987. {
  45988. int res = TEST_SKIPPED;
  45989. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  45990. EXPECT_DECLS;
  45991. byte key[SM4_KEY_SIZE];
  45992. byte iv[SM4_BLOCK_SIZE];
  45993. byte plainText[SM4_BLOCK_SIZE] = {
  45994. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  45995. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45996. };
  45997. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45998. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45999. EVP_CIPHER_CTX* ctx;
  46000. int outSz;
  46001. XMEMSET(key, 0, sizeof(key));
  46002. XMEMSET(iv, 0, sizeof(iv));
  46003. /* Encrypt. */
  46004. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46005. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  46006. WOLFSSL_SUCCESS);
  46007. /* Any tag length must fail - not an AEAD cipher. */
  46008. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  46009. WOLFSSL_FAILURE);
  46010. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  46011. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  46012. sizeof(plainText)), WOLFSSL_SUCCESS);
  46013. ExpectIntEQ(outSz, sizeof(plainText));
  46014. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  46015. WOLFSSL_SUCCESS);
  46016. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  46017. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  46018. EVP_CIPHER_CTX_free(ctx);
  46019. /* Decrypt. */
  46020. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46021. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  46022. WOLFSSL_SUCCESS);
  46023. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  46024. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  46025. sizeof(cipherText)), WOLFSSL_SUCCESS);
  46026. ExpectIntEQ(outSz, sizeof(plainText));
  46027. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  46028. WOLFSSL_SUCCESS);
  46029. ExpectIntEQ(outSz, 0);
  46030. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  46031. EVP_CIPHER_CTX_free(ctx);
  46032. /* Test partial Inits. CipherInit() allow setting of key and iv
  46033. * in separate calls. */
  46034. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46035. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  46036. WOLFSSL_SUCCESS);
  46037. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  46038. WOLFSSL_SUCCESS);
  46039. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  46040. sizeof(cipherText)), WOLFSSL_SUCCESS);
  46041. ExpectIntEQ(outSz, sizeof(plainText));
  46042. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  46043. WOLFSSL_SUCCESS);
  46044. ExpectIntEQ(outSz, 0);
  46045. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  46046. EVP_CIPHER_CTX_free(ctx);
  46047. res = EXPECT_RESULT();
  46048. #endif
  46049. return res;
  46050. }
  46051. static int test_wolfssl_EVP_sm4_ctr(void)
  46052. {
  46053. int res = TEST_SKIPPED;
  46054. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  46055. EXPECT_DECLS;
  46056. byte key[SM4_KEY_SIZE];
  46057. byte iv[SM4_BLOCK_SIZE];
  46058. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  46059. byte cipherText[sizeof(plainText)];
  46060. byte decryptedText[sizeof(plainText)];
  46061. EVP_CIPHER_CTX* ctx;
  46062. int outSz;
  46063. XMEMSET(key, 0, sizeof(key));
  46064. XMEMSET(iv, 0, sizeof(iv));
  46065. /* Encrypt. */
  46066. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46067. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  46068. WOLFSSL_SUCCESS);
  46069. /* Any tag length must fail - not an AEAD cipher. */
  46070. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  46071. WOLFSSL_FAILURE);
  46072. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  46073. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  46074. sizeof(plainText)), WOLFSSL_SUCCESS);
  46075. ExpectIntEQ(outSz, sizeof(plainText));
  46076. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  46077. ExpectIntEQ(outSz, 0);
  46078. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  46079. EVP_CIPHER_CTX_free(ctx);
  46080. /* Decrypt. */
  46081. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46082. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  46083. WOLFSSL_SUCCESS);
  46084. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  46085. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  46086. sizeof(cipherText)), WOLFSSL_SUCCESS);
  46087. ExpectIntEQ(outSz, sizeof(cipherText));
  46088. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  46089. WOLFSSL_SUCCESS);
  46090. ExpectIntEQ(outSz, 0);
  46091. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  46092. EVP_CIPHER_CTX_free(ctx);
  46093. /* Test partial Inits. CipherInit() allow setting of key and iv
  46094. * in separate calls. */
  46095. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  46096. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  46097. WOLFSSL_SUCCESS);
  46098. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  46099. WOLFSSL_SUCCESS);
  46100. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  46101. sizeof(cipherText)), WOLFSSL_SUCCESS);
  46102. ExpectIntEQ(outSz, sizeof(cipherText));
  46103. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  46104. WOLFSSL_SUCCESS);
  46105. ExpectIntEQ(outSz, 0);
  46106. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  46107. EVP_CIPHER_CTX_free(ctx);
  46108. res = EXPECT_RESULT();
  46109. #endif
  46110. return res;
  46111. }
  46112. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  46113. {
  46114. int res = TEST_SKIPPED;
  46115. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  46116. /* Zero length plain text */
  46117. EXPECT_DECLS;
  46118. byte key[] = {
  46119. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46120. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46121. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46123. }; /* align */
  46124. byte iv[] = {
  46125. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46126. }; /* align */
  46127. byte plaintxt[1];
  46128. int ivSz = 12;
  46129. int plaintxtSz = 0;
  46130. unsigned char tag[16];
  46131. unsigned char tag_kat[16] = {
  46132. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  46133. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  46134. };
  46135. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46136. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46137. int ciphertxtSz = 0;
  46138. int decryptedtxtSz = 0;
  46139. int len = 0;
  46140. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  46141. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  46142. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  46143. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  46144. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  46145. plaintxtSz));
  46146. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  46147. ciphertxtSz += len;
  46148. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  46149. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  46150. ExpectIntEQ(0, ciphertxtSz);
  46151. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  46152. EVP_CIPHER_CTX_init(de);
  46153. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  46154. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  46155. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  46156. decryptedtxtSz = len;
  46157. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  46158. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  46159. decryptedtxtSz += len;
  46160. ExpectIntEQ(0, decryptedtxtSz);
  46161. EVP_CIPHER_CTX_free(en);
  46162. EVP_CIPHER_CTX_free(de);
  46163. res = EXPECT_RESULT();
  46164. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  46165. return res;
  46166. }
  46167. static int test_wolfssl_EVP_sm4_gcm(void)
  46168. {
  46169. int res = TEST_SKIPPED;
  46170. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  46171. EXPECT_DECLS;
  46172. byte *key = (byte*)"0123456789012345";
  46173. /* A 128 bit IV */
  46174. byte *iv = (byte*)"0123456789012345";
  46175. int ivSz = SM4_BLOCK_SIZE;
  46176. /* Message to be encrypted */
  46177. byte *plaintxt = (byte*)"for things to change you have to change";
  46178. /* Additional non-confidential data */
  46179. byte *aad = (byte*)"Don't spend major time on minor things.";
  46180. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  46181. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  46182. int aadSz = (int)XSTRLEN((char*)aad);
  46183. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46184. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46185. int ciphertxtSz = 0;
  46186. int decryptedtxtSz = 0;
  46187. int len = 0;
  46188. int i = 0;
  46189. EVP_CIPHER_CTX en[2];
  46190. EVP_CIPHER_CTX de[2];
  46191. for (i = 0; i < 2; i++) {
  46192. EVP_CIPHER_CTX_init(&en[i]);
  46193. if (i == 0) {
  46194. /* Default uses 96-bits IV length */
  46195. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  46196. iv));
  46197. }
  46198. else {
  46199. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  46200. NULL));
  46201. /* non-default must to set the IV length first */
  46202. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  46203. ivSz, NULL));
  46204. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  46205. }
  46206. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  46207. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  46208. plaintxtSz));
  46209. ciphertxtSz = len;
  46210. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  46211. ciphertxtSz += len;
  46212. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  46213. SM4_BLOCK_SIZE, tag));
  46214. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  46215. EVP_CIPHER_CTX_init(&de[i]);
  46216. if (i == 0) {
  46217. /* Default uses 96-bits IV length */
  46218. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  46219. iv));
  46220. }
  46221. else {
  46222. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  46223. NULL));
  46224. /* non-default must to set the IV length first */
  46225. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  46226. ivSz, NULL));
  46227. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  46228. }
  46229. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46230. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46231. ciphertxtSz));
  46232. decryptedtxtSz = len;
  46233. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  46234. SM4_BLOCK_SIZE, tag));
  46235. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46236. decryptedtxtSz += len;
  46237. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  46238. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  46239. /* modify tag*/
  46240. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  46241. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46242. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  46243. SM4_BLOCK_SIZE, tag));
  46244. /* fail due to wrong tag */
  46245. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46246. ciphertxtSz));
  46247. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46248. ExpectIntEQ(0, len);
  46249. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  46250. }
  46251. res = EXPECT_RESULT();
  46252. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  46253. return res;
  46254. }
  46255. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  46256. {
  46257. int res = TEST_SKIPPED;
  46258. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  46259. /* Zero length plain text */
  46260. EXPECT_DECLS;
  46261. byte key[] = {
  46262. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46263. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46264. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46265. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46266. }; /* align */
  46267. byte iv[] = {
  46268. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46269. }; /* align */
  46270. byte plaintxt[1];
  46271. int ivSz = 12;
  46272. int plaintxtSz = 0;
  46273. unsigned char tag[16];
  46274. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46275. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46276. int ciphertxtSz = 0;
  46277. int decryptedtxtSz = 0;
  46278. int len = 0;
  46279. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  46280. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  46281. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  46282. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  46283. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  46284. plaintxtSz));
  46285. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  46286. ciphertxtSz += len;
  46287. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  46288. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  46289. ExpectIntEQ(0, ciphertxtSz);
  46290. EVP_CIPHER_CTX_init(de);
  46291. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  46292. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  46293. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  46294. decryptedtxtSz = len;
  46295. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  46296. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  46297. decryptedtxtSz += len;
  46298. ExpectIntEQ(0, decryptedtxtSz);
  46299. EVP_CIPHER_CTX_free(en);
  46300. EVP_CIPHER_CTX_free(de);
  46301. res = EXPECT_RESULT();
  46302. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  46303. return res;
  46304. }
  46305. static int test_wolfssl_EVP_sm4_ccm(void)
  46306. {
  46307. int res = TEST_SKIPPED;
  46308. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  46309. EXPECT_DECLS;
  46310. byte *key = (byte*)"0123456789012345";
  46311. byte *iv = (byte*)"0123456789012";
  46312. int ivSz = (int)XSTRLEN((char*)iv);
  46313. /* Message to be encrypted */
  46314. byte *plaintxt = (byte*)"for things to change you have to change";
  46315. /* Additional non-confidential data */
  46316. byte *aad = (byte*)"Don't spend major time on minor things.";
  46317. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  46318. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  46319. int aadSz = (int)XSTRLEN((char*)aad);
  46320. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46321. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46322. int ciphertxtSz = 0;
  46323. int decryptedtxtSz = 0;
  46324. int len = 0;
  46325. int i = 0;
  46326. EVP_CIPHER_CTX en[2];
  46327. EVP_CIPHER_CTX de[2];
  46328. for (i = 0; i < 2; i++) {
  46329. EVP_CIPHER_CTX_init(&en[i]);
  46330. if (i == 0) {
  46331. /* Default uses 96-bits IV length */
  46332. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  46333. iv));
  46334. }
  46335. else {
  46336. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  46337. NULL));
  46338. /* non-default must to set the IV length first */
  46339. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  46340. ivSz, NULL));
  46341. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  46342. }
  46343. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  46344. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  46345. plaintxtSz));
  46346. ciphertxtSz = len;
  46347. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  46348. ciphertxtSz += len;
  46349. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  46350. SM4_BLOCK_SIZE, tag));
  46351. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  46352. EVP_CIPHER_CTX_init(&de[i]);
  46353. if (i == 0) {
  46354. /* Default uses 96-bits IV length */
  46355. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  46356. iv));
  46357. }
  46358. else {
  46359. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  46360. NULL));
  46361. /* non-default must to set the IV length first */
  46362. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  46363. ivSz, NULL));
  46364. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  46365. }
  46366. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46367. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46368. ciphertxtSz));
  46369. decryptedtxtSz = len;
  46370. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  46371. SM4_BLOCK_SIZE, tag));
  46372. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46373. decryptedtxtSz += len;
  46374. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  46375. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  46376. /* modify tag*/
  46377. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  46378. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46379. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  46380. SM4_BLOCK_SIZE, tag));
  46381. /* fail due to wrong tag */
  46382. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46383. ciphertxtSz));
  46384. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46385. ExpectIntEQ(0, len);
  46386. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  46387. }
  46388. res = EXPECT_RESULT();
  46389. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  46390. return res;
  46391. }
  46392. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  46393. {
  46394. EXPECT_DECLS;
  46395. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  46396. EVP_PKEY_CTX* ctx = NULL;
  46397. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  46398. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  46399. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  46400. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  46401. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  46402. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  46403. byte outKey[34];
  46404. size_t outKeySz = sizeof(outKey);
  46405. /* These expected outputs were gathered by running the same test below using
  46406. * OpenSSL. */
  46407. const byte extractAndExpand[] = {
  46408. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  46409. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  46410. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  46411. };
  46412. const byte extractOnly[] = {
  46413. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  46414. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  46415. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  46416. };
  46417. const byte expandOnly[] = {
  46418. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  46419. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  46420. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  46421. };
  46422. const byte extractAndExpandAddInfo[] = {
  46423. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  46424. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  46425. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  46426. };
  46427. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  46428. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  46429. /* NULL ctx. */
  46430. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46431. /* NULL md. */
  46432. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  46433. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  46434. /* NULL ctx. */
  46435. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  46436. WOLFSSL_FAILURE);
  46437. /* NULL salt is ok. */
  46438. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  46439. WOLFSSL_SUCCESS);
  46440. /* Salt length <= 0. */
  46441. /* Length 0 salt is ok. */
  46442. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  46443. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  46444. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  46445. WOLFSSL_SUCCESS);
  46446. /* NULL ctx. */
  46447. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  46448. WOLFSSL_FAILURE);
  46449. /* NULL key. */
  46450. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  46451. WOLFSSL_FAILURE);
  46452. /* Key length <= 0 */
  46453. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  46454. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  46455. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  46456. WOLFSSL_SUCCESS);
  46457. /* NULL ctx. */
  46458. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  46459. WOLFSSL_FAILURE);
  46460. /* NULL info is ok. */
  46461. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  46462. WOLFSSL_SUCCESS);
  46463. /* Info length <= 0 */
  46464. /* Length 0 info is ok. */
  46465. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  46466. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  46467. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  46468. WOLFSSL_SUCCESS);
  46469. /* NULL ctx. */
  46470. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  46471. WOLFSSL_FAILURE);
  46472. /* Extract and expand (default). */
  46473. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46474. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  46475. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  46476. /* Extract only. */
  46477. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  46478. WOLFSSL_SUCCESS);
  46479. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46480. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  46481. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  46482. outKeySz = sizeof(outKey);
  46483. /* Expand only. */
  46484. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  46485. WOLFSSL_SUCCESS);
  46486. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46487. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  46488. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  46489. outKeySz = sizeof(outKey);
  46490. /* Extract and expand with appended additional info. */
  46491. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  46492. WOLFSSL_SUCCESS);
  46493. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  46494. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  46495. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46496. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  46497. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  46498. EVP_PKEY_CTX_free(ctx);
  46499. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  46500. return EXPECT_RESULT();
  46501. }
  46502. #ifndef NO_BIO
  46503. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  46504. {
  46505. EXPECT_DECLS;
  46506. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46507. BIO* bio = NULL;
  46508. X509_INFO* info = NULL;
  46509. STACK_OF(X509_INFO)* sk = NULL;
  46510. char* subject = NULL;
  46511. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  46512. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  46513. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  46514. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  46515. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  46516. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  46517. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  46518. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  46519. /* using dereference to maintain testing for Apache port*/
  46520. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  46521. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  46522. 0, 0));
  46523. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  46524. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  46525. X509_INFO_free(info);
  46526. info = NULL;
  46527. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  46528. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  46529. 0, 0));
  46530. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  46531. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  46532. X509_INFO_free(info);
  46533. ExpectNull(info = sk_X509_INFO_pop(sk));
  46534. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  46535. BIO_free(bio);
  46536. #endif
  46537. return EXPECT_RESULT();
  46538. }
  46539. #endif /* !NO_BIO */
  46540. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  46541. {
  46542. EXPECT_DECLS;
  46543. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46544. X509 *x509 = NULL;
  46545. X509_NAME* name = NULL;
  46546. int idx = 0;
  46547. X509_NAME_ENTRY *ne = NULL;
  46548. ASN1_OBJECT *object = NULL;
  46549. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  46550. WOLFSSL_FILETYPE_PEM));
  46551. ExpectNotNull(name = X509_get_subject_name(x509));
  46552. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  46553. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  46554. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  46555. X509_free(x509);
  46556. #endif
  46557. return EXPECT_RESULT();
  46558. }
  46559. static int test_wolfSSL_X509_STORE_get1_certs(void)
  46560. {
  46561. EXPECT_DECLS;
  46562. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  46563. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46564. X509_STORE_CTX *storeCtx = NULL;
  46565. X509_STORE *store = NULL;
  46566. X509 *caX509 = NULL;
  46567. X509 *svrX509 = NULL;
  46568. X509_NAME *subject = NULL;
  46569. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  46570. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  46571. SSL_FILETYPE_PEM));
  46572. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  46573. SSL_FILETYPE_PEM)));
  46574. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  46575. ExpectNotNull(store = X509_STORE_new());
  46576. ExpectNotNull(subject = X509_get_subject_name(caX509));
  46577. /* Errors */
  46578. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  46579. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  46580. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  46581. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  46582. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  46583. SSL_SUCCESS);
  46584. /* Should find the cert */
  46585. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46586. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  46587. sk_X509_pop_free(certs, NULL);
  46588. certs = NULL;
  46589. /* Should not find the cert */
  46590. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  46591. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46592. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  46593. sk_X509_pop_free(certs, NULL);
  46594. certs = NULL;
  46595. X509_STORE_free(store);
  46596. X509_STORE_CTX_free(storeCtx);
  46597. X509_free(svrX509);
  46598. X509_free(caX509);
  46599. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  46600. return EXPECT_RESULT();
  46601. }
  46602. static int test_wolfSSL_dup_CA_list(void)
  46603. {
  46604. int res = TEST_SKIPPED;
  46605. #if defined(OPENSSL_ALL)
  46606. EXPECT_DECLS;
  46607. STACK_OF(X509_NAME) *originalStack = NULL;
  46608. STACK_OF(X509_NAME) *copyStack = NULL;
  46609. int originalCount = 0;
  46610. int copyCount = 0;
  46611. X509_NAME *name = NULL;
  46612. int i;
  46613. originalStack = sk_X509_NAME_new_null();
  46614. ExpectNotNull(originalStack);
  46615. for (i = 0; i < 3; i++) {
  46616. name = X509_NAME_new();
  46617. ExpectNotNull(name);
  46618. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  46619. }
  46620. copyStack = SSL_dup_CA_list(originalStack);
  46621. ExpectNotNull(copyStack);
  46622. originalCount = sk_X509_NAME_num(originalStack);
  46623. copyCount = sk_X509_NAME_num(copyStack);
  46624. AssertIntEQ(originalCount, copyCount);
  46625. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  46626. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  46627. originalStack = NULL;
  46628. copyStack = NULL;
  46629. res = EXPECT_RESULT();
  46630. #endif /* OPENSSL_ALL */
  46631. return res;
  46632. }
  46633. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  46634. * have default (hidden) visibility, and in the absence of visibility, it's
  46635. * benign to mask out the library implementation.
  46636. */
  46637. #define WOLFSSL_MISC_INCLUDED
  46638. #include <wolfcrypt/src/misc.c>
  46639. static int test_ForceZero(void)
  46640. {
  46641. EXPECT_DECLS;
  46642. unsigned char data[32];
  46643. unsigned int i, j, len;
  46644. /* Test case with 0 length */
  46645. ForceZero(data, 0);
  46646. /* Test ForceZero */
  46647. for (i = 0; i < sizeof(data); i++) {
  46648. for (len = 1; len < sizeof(data) - i; len++) {
  46649. for (j = 0; j < sizeof(data); j++)
  46650. data[j] = j + 1;
  46651. ForceZero(data + i, len);
  46652. for (j = 0; j < sizeof(data); j++) {
  46653. if (j < i || j >= i + len) {
  46654. ExpectIntNE(data[j], 0x00);
  46655. }
  46656. else {
  46657. ExpectIntEQ(data[j], 0x00);
  46658. }
  46659. }
  46660. }
  46661. }
  46662. return EXPECT_RESULT();
  46663. }
  46664. #ifndef NO_BIO
  46665. static int test_wolfSSL_X509_print(void)
  46666. {
  46667. EXPECT_DECLS;
  46668. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46669. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  46670. X509 *x509 = NULL;
  46671. BIO *bio = NULL;
  46672. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46673. const X509_ALGOR *cert_sig_alg;
  46674. #endif
  46675. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  46676. WOLFSSL_FILETYPE_PEM));
  46677. /* print to memory */
  46678. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46679. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46680. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  46681. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  46682. /* Will print IP address subject alt name. */
  46683. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  46684. #elif defined(NO_ASN_TIME)
  46685. /* Will print IP address subject alt name but not Validity. */
  46686. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  46687. #else
  46688. /* Will print IP address subject alt name. */
  46689. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  46690. #endif
  46691. #elif defined(NO_ASN_TIME)
  46692. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  46693. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  46694. #else
  46695. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  46696. #endif
  46697. BIO_free(bio);
  46698. bio = NULL;
  46699. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46700. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46701. /* Print signature */
  46702. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  46703. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  46704. #endif
  46705. /* print to stderr */
  46706. #if !defined(NO_WOLFSSL_DIR)
  46707. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46708. #endif
  46709. /* print again */
  46710. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  46711. X509_free(x509);
  46712. BIO_free(bio);
  46713. #endif
  46714. return EXPECT_RESULT();
  46715. }
  46716. static int test_wolfSSL_X509_CRL_print(void)
  46717. {
  46718. EXPECT_DECLS;
  46719. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  46720. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  46721. X509_CRL* crl = NULL;
  46722. BIO *bio = NULL;
  46723. XFILE fp = XBADFILE;
  46724. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  46725. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46726. NULL, NULL));
  46727. if (fp != XBADFILE)
  46728. XFCLOSE(fp);
  46729. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46730. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  46731. X509_CRL_free(crl);
  46732. BIO_free(bio);
  46733. #endif
  46734. return EXPECT_RESULT();
  46735. }
  46736. static int test_wolfSSL_BIO_get_len(void)
  46737. {
  46738. EXPECT_DECLS;
  46739. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  46740. BIO *bio = NULL;
  46741. const char txt[] = "Some example text to push to the BIO.";
  46742. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  46743. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  46744. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  46745. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  46746. BIO_free(bio);
  46747. bio = NULL;
  46748. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46749. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  46750. BIO_free(bio);
  46751. #endif
  46752. return EXPECT_RESULT();
  46753. }
  46754. #endif /* !NO_BIO */
  46755. static int test_wolfSSL_RSA(void)
  46756. {
  46757. EXPECT_DECLS;
  46758. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  46759. defined(WOLFSSL_KEY_GEN)
  46760. RSA* rsa = NULL;
  46761. const BIGNUM *n;
  46762. const BIGNUM *e;
  46763. const BIGNUM *d;
  46764. const BIGNUM *p;
  46765. const BIGNUM *q;
  46766. const BIGNUM *dmp1;
  46767. const BIGNUM *dmq1;
  46768. const BIGNUM *iqmp;
  46769. ExpectNotNull(rsa = RSA_new());
  46770. ExpectIntEQ(RSA_size(NULL), 0);
  46771. ExpectIntEQ(RSA_size(rsa), 0);
  46772. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  46773. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  46774. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  46775. #ifdef WOLFSSL_RSA_KEY_CHECK
  46776. ExpectIntEQ(RSA_check_key(rsa), 0);
  46777. #endif
  46778. RSA_free(rsa);
  46779. rsa = NULL;
  46780. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46781. ExpectIntEQ(RSA_size(rsa), 256);
  46782. #ifdef WOLFSSL_RSA_KEY_CHECK
  46783. ExpectIntEQ(RSA_check_key(NULL), 0);
  46784. ExpectIntEQ(RSA_check_key(rsa), 1);
  46785. #endif
  46786. /* sanity check */
  46787. ExpectIntEQ(RSA_bits(NULL), 0);
  46788. /* key */
  46789. ExpectIntEQ(RSA_bits(rsa), 2048);
  46790. RSA_get0_key(rsa, &n, &e, &d);
  46791. ExpectPtrEq(rsa->n, n);
  46792. ExpectPtrEq(rsa->e, e);
  46793. ExpectPtrEq(rsa->d, d);
  46794. n = NULL;
  46795. e = NULL;
  46796. d = NULL;
  46797. ExpectNotNull(n = BN_new());
  46798. ExpectNotNull(e = BN_new());
  46799. ExpectNotNull(d = BN_new());
  46800. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  46801. if (EXPECT_FAIL()) {
  46802. BN_free((BIGNUM*)n);
  46803. BN_free((BIGNUM*)e);
  46804. BN_free((BIGNUM*)d);
  46805. }
  46806. ExpectPtrEq(rsa->n, n);
  46807. ExpectPtrEq(rsa->e, e);
  46808. ExpectPtrEq(rsa->d, d);
  46809. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  46810. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  46811. /* crt_params */
  46812. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  46813. ExpectPtrEq(rsa->dmp1, dmp1);
  46814. ExpectPtrEq(rsa->dmq1, dmq1);
  46815. ExpectPtrEq(rsa->iqmp, iqmp);
  46816. dmp1 = NULL;
  46817. dmq1 = NULL;
  46818. iqmp = NULL;
  46819. ExpectNotNull(dmp1 = BN_new());
  46820. ExpectNotNull(dmq1 = BN_new());
  46821. ExpectNotNull(iqmp = BN_new());
  46822. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46823. (BIGNUM*)iqmp), 1);
  46824. if (EXPECT_FAIL()) {
  46825. BN_free((BIGNUM*)dmp1);
  46826. BN_free((BIGNUM*)dmq1);
  46827. BN_free((BIGNUM*)iqmp);
  46828. }
  46829. ExpectPtrEq(rsa->dmp1, dmp1);
  46830. ExpectPtrEq(rsa->dmq1, dmq1);
  46831. ExpectPtrEq(rsa->iqmp, iqmp);
  46832. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  46833. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46834. (BIGNUM*)iqmp), 0);
  46835. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  46836. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  46837. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  46838. ExpectNull(dmp1);
  46839. ExpectNull(dmq1);
  46840. ExpectNull(iqmp);
  46841. /* factors */
  46842. RSA_get0_factors(rsa, NULL, NULL);
  46843. RSA_get0_factors(rsa, &p, &q);
  46844. ExpectPtrEq(rsa->p, p);
  46845. ExpectPtrEq(rsa->q, q);
  46846. p = NULL;
  46847. q = NULL;
  46848. ExpectNotNull(p = BN_new());
  46849. ExpectNotNull(q = BN_new());
  46850. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  46851. if (EXPECT_FAIL()) {
  46852. BN_free((BIGNUM*)p);
  46853. BN_free((BIGNUM*)q);
  46854. }
  46855. ExpectPtrEq(rsa->p, p);
  46856. ExpectPtrEq(rsa->q, q);
  46857. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  46858. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  46859. RSA_get0_factors(NULL, NULL, NULL);
  46860. RSA_get0_factors(NULL, &p, &q);
  46861. ExpectNull(p);
  46862. ExpectNull(q);
  46863. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46864. ExpectIntEQ(RSA_bits(rsa), 21);
  46865. RSA_free(rsa);
  46866. rsa = NULL;
  46867. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46868. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46869. ExpectIntEQ(RSA_size(rsa), 384);
  46870. ExpectIntEQ(RSA_bits(rsa), 3072);
  46871. RSA_free(rsa);
  46872. rsa = NULL;
  46873. #endif
  46874. /* remove for now with odd key size until adjusting rsa key size check with
  46875. wc_MakeRsaKey()
  46876. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46877. RSA_free(rsa);
  46878. rsa = NULL;
  46879. */
  46880. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  46881. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46882. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46883. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  46884. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46885. {
  46886. byte buff[FOURK_BUF];
  46887. byte der[FOURK_BUF];
  46888. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46889. XFILE f = XBADFILE;
  46890. int bytes;
  46891. /* test loading encrypted RSA private pem w/o password */
  46892. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  46893. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  46894. if (f != XBADFILE)
  46895. XFCLOSE(f);
  46896. XMEMSET(der, 0, sizeof(der));
  46897. /* test that error value is returned with no password */
  46898. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  46899. 0);
  46900. }
  46901. #endif
  46902. #endif
  46903. return EXPECT_RESULT();
  46904. }
  46905. static int test_wolfSSL_RSA_DER(void)
  46906. {
  46907. EXPECT_DECLS;
  46908. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46909. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46910. RSA *rsa = NULL;
  46911. int i;
  46912. const unsigned char *buff = NULL;
  46913. unsigned char *newBuff = NULL;
  46914. struct tbl_s
  46915. {
  46916. const unsigned char *der;
  46917. int sz;
  46918. } tbl[] = {
  46919. #ifdef USE_CERT_BUFFERS_1024
  46920. {client_key_der_1024, sizeof_client_key_der_1024},
  46921. {server_key_der_1024, sizeof_server_key_der_1024},
  46922. #endif
  46923. #ifdef USE_CERT_BUFFERS_2048
  46924. {client_key_der_2048, sizeof_client_key_der_2048},
  46925. {server_key_der_2048, sizeof_server_key_der_2048},
  46926. #endif
  46927. {NULL, 0}
  46928. };
  46929. /* Public Key DER */
  46930. struct tbl_s pub[] = {
  46931. #ifdef USE_CERT_BUFFERS_1024
  46932. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46933. #endif
  46934. #ifdef USE_CERT_BUFFERS_2048
  46935. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46936. #endif
  46937. {NULL, 0}
  46938. };
  46939. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46940. buff = pub[0].der;
  46941. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46942. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46943. buff = tbl[0].der;
  46944. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46945. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46946. rsa = RSA_new();
  46947. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46948. RSA_free(rsa);
  46949. rsa = NULL;
  46950. for (i = 0; tbl[i].der != NULL; i++)
  46951. {
  46952. /* Passing in pointer results in pointer moving. */
  46953. buff = tbl[i].der;
  46954. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46955. ExpectNotNull(rsa);
  46956. RSA_free(rsa);
  46957. rsa = NULL;
  46958. }
  46959. for (i = 0; tbl[i].der != NULL; i++)
  46960. {
  46961. /* Passing in pointer results in pointer moving. */
  46962. buff = tbl[i].der;
  46963. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46964. ExpectNotNull(rsa);
  46965. RSA_free(rsa);
  46966. rsa = NULL;
  46967. }
  46968. for (i = 0; pub[i].der != NULL; i++)
  46969. {
  46970. buff = pub[i].der;
  46971. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46972. ExpectNotNull(rsa);
  46973. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46974. newBuff = NULL;
  46975. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46976. ExpectNotNull(newBuff);
  46977. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46978. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46979. RSA_free(rsa);
  46980. rsa = NULL;
  46981. }
  46982. #endif
  46983. return EXPECT_RESULT();
  46984. }
  46985. static int test_wolfSSL_RSA_print(void)
  46986. {
  46987. EXPECT_DECLS;
  46988. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46989. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46990. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  46991. BIO *bio = NULL;
  46992. WOLFSSL_RSA* rsa = NULL;
  46993. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46994. ExpectNotNull(rsa = RSA_new());
  46995. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  46996. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46997. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  46998. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  46999. /* Some very large number of indent spaces. */
  47000. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  47001. /* RSA is empty. */
  47002. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  47003. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  47004. RSA_free(rsa);
  47005. rsa = NULL;
  47006. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47007. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  47008. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  47009. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  47010. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  47011. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  47012. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  47013. BIO_free(bio);
  47014. RSA_free(rsa);
  47015. #endif
  47016. return EXPECT_RESULT();
  47017. }
  47018. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  47019. {
  47020. EXPECT_DECLS;
  47021. #ifndef NO_RSA
  47022. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  47023. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47024. RSA *rsa = NULL;
  47025. const unsigned char *derBuf = client_key_der_2048;
  47026. unsigned char em[256] = {0}; /* len = 2048/8 */
  47027. /* Random data simulating a hash */
  47028. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  47029. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  47030. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  47031. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  47032. };
  47033. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  47034. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  47035. RSA_PSS_SALTLEN_DIGEST), 0);
  47036. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  47037. RSA_PSS_SALTLEN_DIGEST), 0);
  47038. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  47039. RSA_PSS_SALTLEN_DIGEST), 0);
  47040. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  47041. RSA_PSS_SALTLEN_DIGEST), 0);
  47042. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  47043. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  47044. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  47045. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  47046. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  47047. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  47048. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  47049. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  47050. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  47051. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  47052. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  47053. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  47054. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  47055. RSA_PSS_SALTLEN_DIGEST), 1);
  47056. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  47057. RSA_PSS_SALTLEN_DIGEST), 1);
  47058. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  47059. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  47060. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  47061. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  47062. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  47063. RSA_PSS_SALTLEN_MAX), 1);
  47064. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  47065. RSA_PSS_SALTLEN_MAX), 1);
  47066. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  47067. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  47068. RSA_free(rsa);
  47069. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  47070. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  47071. #endif
  47072. return EXPECT_RESULT();
  47073. }
  47074. static int test_wolfSSL_RSA_sign_sha3(void)
  47075. {
  47076. EXPECT_DECLS;
  47077. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  47078. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  47079. RSA* rsa = NULL;
  47080. const unsigned char *derBuf = client_key_der_2048;
  47081. unsigned char sigRet[256] = {0};
  47082. unsigned int sigLen = sizeof(sigRet);
  47083. /* Random data simulating a hash */
  47084. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  47085. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  47086. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  47087. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  47088. };
  47089. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  47090. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  47091. rsa), 1);
  47092. RSA_free(rsa);
  47093. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  47094. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  47095. return EXPECT_RESULT();
  47096. }
  47097. static int test_wolfSSL_RSA_get0_key(void)
  47098. {
  47099. EXPECT_DECLS;
  47100. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  47101. RSA *rsa = NULL;
  47102. const BIGNUM* n = NULL;
  47103. const BIGNUM* e = NULL;
  47104. const BIGNUM* d = NULL;
  47105. const unsigned char* der;
  47106. int derSz;
  47107. #ifdef USE_CERT_BUFFERS_1024
  47108. der = client_key_der_1024;
  47109. derSz = sizeof_client_key_der_1024;
  47110. #elif defined(USE_CERT_BUFFERS_2048)
  47111. der = client_key_der_2048;
  47112. derSz = sizeof_client_key_der_2048;
  47113. #else
  47114. der = NULL;
  47115. derSz = 0;
  47116. #endif
  47117. if (der != NULL) {
  47118. RSA_get0_key(NULL, NULL, NULL, NULL);
  47119. RSA_get0_key(rsa, NULL, NULL, NULL);
  47120. RSA_get0_key(NULL, &n, &e, &d);
  47121. ExpectNull(n);
  47122. ExpectNull(e);
  47123. ExpectNull(d);
  47124. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  47125. ExpectNotNull(rsa);
  47126. RSA_get0_key(rsa, NULL, NULL, NULL);
  47127. RSA_get0_key(rsa, &n, NULL, NULL);
  47128. ExpectNotNull(n);
  47129. RSA_get0_key(rsa, NULL, &e, NULL);
  47130. ExpectNotNull(e);
  47131. RSA_get0_key(rsa, NULL, NULL, &d);
  47132. ExpectNotNull(d);
  47133. RSA_get0_key(rsa, &n, &e, &d);
  47134. ExpectNotNull(n);
  47135. ExpectNotNull(e);
  47136. ExpectNotNull(d);
  47137. RSA_free(rsa);
  47138. }
  47139. #endif
  47140. return EXPECT_RESULT();
  47141. }
  47142. static int test_wolfSSL_RSA_meth(void)
  47143. {
  47144. EXPECT_DECLS;
  47145. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47146. RSA *rsa = NULL;
  47147. RSA_METHOD *rsa_meth = NULL;
  47148. #ifdef WOLFSSL_KEY_GEN
  47149. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47150. RSA_free(rsa);
  47151. rsa = NULL;
  47152. #else
  47153. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47154. #endif
  47155. ExpectNotNull(RSA_get_default_method());
  47156. wolfSSL_RSA_meth_free(NULL);
  47157. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  47158. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  47159. RSA_METHOD_FLAG_NO_CHECK));
  47160. #ifndef NO_WOLFSSL_STUB
  47161. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  47162. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  47163. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  47164. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  47165. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  47166. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  47167. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  47168. #endif
  47169. ExpectIntEQ(RSA_flags(NULL), 0);
  47170. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  47171. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  47172. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  47173. ExpectNotNull(rsa = RSA_new());
  47174. /* No method set. */
  47175. ExpectIntEQ(RSA_flags(rsa), 0);
  47176. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47177. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47178. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47179. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  47180. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  47181. if (EXPECT_FAIL()) {
  47182. wolfSSL_RSA_meth_free(rsa_meth);
  47183. }
  47184. ExpectNull(RSA_get_method(NULL));
  47185. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  47186. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  47187. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47188. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47189. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  47190. RSA_METHOD_FLAG_NO_CHECK);
  47191. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47192. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47193. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  47194. /* rsa_meth is freed here */
  47195. RSA_free(rsa);
  47196. #endif
  47197. return EXPECT_RESULT();
  47198. }
  47199. static int test_wolfSSL_RSA_verify(void)
  47200. {
  47201. EXPECT_DECLS;
  47202. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  47203. !defined(NO_FILESYSTEM)
  47204. #ifndef NO_BIO
  47205. XFILE fp = XBADFILE;
  47206. RSA *pKey = NULL;
  47207. RSA *pubKey = NULL;
  47208. X509 *cert = NULL;
  47209. const char *text = "Hello wolfSSL !";
  47210. unsigned char hash[SHA256_DIGEST_LENGTH];
  47211. unsigned char signature[2048/8];
  47212. unsigned int signatureLength;
  47213. byte *buf = NULL;
  47214. BIO *bio = NULL;
  47215. SHA256_CTX c;
  47216. EVP_PKEY *evpPkey = NULL;
  47217. EVP_PKEY *evpPubkey = NULL;
  47218. size_t sz;
  47219. /* generate hash */
  47220. SHA256_Init(&c);
  47221. SHA256_Update(&c, text, strlen(text));
  47222. SHA256_Final(hash, &c);
  47223. #ifdef WOLFSSL_SMALL_STACK_CACHE
  47224. /* workaround for small stack cache case */
  47225. wc_Sha256Free((wc_Sha256*)&c);
  47226. #endif
  47227. /* read privete key file */
  47228. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  47229. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  47230. ExpectTrue((sz = XFTELL(fp)) > 0);
  47231. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  47232. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  47233. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  47234. if (fp != XBADFILE) {
  47235. XFCLOSE(fp);
  47236. fp = XBADFILE;
  47237. }
  47238. /* read private key and sign hash data */
  47239. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  47240. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  47241. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  47242. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  47243. signature, &signatureLength, pKey), SSL_SUCCESS);
  47244. /* read public key and verify signed data */
  47245. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  47246. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  47247. if (fp != XBADFILE)
  47248. XFCLOSE(fp);
  47249. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  47250. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  47251. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47252. signatureLength, pubKey), SSL_SUCCESS);
  47253. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  47254. signatureLength, NULL), SSL_FAILURE);
  47255. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  47256. signatureLength, pubKey), SSL_FAILURE);
  47257. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  47258. signatureLength, pubKey), SSL_FAILURE);
  47259. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47260. signatureLength, NULL), SSL_FAILURE);
  47261. RSA_free(pKey);
  47262. EVP_PKEY_free(evpPkey);
  47263. RSA_free(pubKey);
  47264. EVP_PKEY_free(evpPubkey);
  47265. X509_free(cert);
  47266. BIO_free(bio);
  47267. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  47268. #endif
  47269. #endif
  47270. return EXPECT_RESULT();
  47271. }
  47272. static int test_wolfSSL_RSA_sign(void)
  47273. {
  47274. EXPECT_DECLS;
  47275. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47276. RSA *rsa;
  47277. unsigned char hash[SHA256_DIGEST_LENGTH];
  47278. #ifdef USE_CERT_BUFFERS_1024
  47279. const unsigned char* privDer = client_key_der_1024;
  47280. size_t privDerSz = sizeof_client_key_der_1024;
  47281. const unsigned char* pubDer = client_keypub_der_1024;
  47282. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47283. unsigned char signature[1024/8];
  47284. #else
  47285. const unsigned char* privDer = client_key_der_2048;
  47286. size_t privDerSz = sizeof_client_key_der_2048;
  47287. const unsigned char* pubDer = client_keypub_der_2048;
  47288. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47289. unsigned char signature[2048/8];
  47290. #endif
  47291. unsigned int signatureLen;
  47292. const unsigned char* der;
  47293. XMEMSET(hash, 0, sizeof(hash));
  47294. der = privDer;
  47295. rsa = NULL;
  47296. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47297. /* Invalid parameters. */
  47298. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  47299. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  47300. &signatureLen, rsa), 0);
  47301. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  47302. &signatureLen, rsa), 0);
  47303. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  47304. &signatureLen, rsa), 0);
  47305. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47306. NULL, rsa), 0);
  47307. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47308. &signatureLen, NULL), 0);
  47309. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47310. &signatureLen, rsa), 1);
  47311. RSA_free(rsa);
  47312. der = pubDer;
  47313. rsa = NULL;
  47314. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47315. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47316. signatureLen, rsa), 1);
  47317. RSA_free(rsa);
  47318. #endif
  47319. return EXPECT_RESULT();
  47320. }
  47321. static int test_wolfSSL_RSA_sign_ex(void)
  47322. {
  47323. EXPECT_DECLS;
  47324. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47325. RSA *rsa = NULL;
  47326. unsigned char hash[SHA256_DIGEST_LENGTH];
  47327. #ifdef USE_CERT_BUFFERS_1024
  47328. const unsigned char* privDer = client_key_der_1024;
  47329. size_t privDerSz = sizeof_client_key_der_1024;
  47330. const unsigned char* pubDer = client_keypub_der_1024;
  47331. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47332. unsigned char signature[1024/8];
  47333. #else
  47334. const unsigned char* privDer = client_key_der_2048;
  47335. size_t privDerSz = sizeof_client_key_der_2048;
  47336. const unsigned char* pubDer = client_keypub_der_2048;
  47337. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47338. unsigned char signature[2048/8];
  47339. #endif
  47340. unsigned int signatureLen;
  47341. const unsigned char* der;
  47342. unsigned char encodedHash[51];
  47343. unsigned int encodedHashLen;
  47344. const unsigned char expEncHash[] = {
  47345. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  47346. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  47347. 0x00, 0x04, 0x20,
  47348. /* Hash data */
  47349. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47350. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47351. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47352. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47353. };
  47354. XMEMSET(hash, 0, sizeof(hash));
  47355. ExpectNotNull(rsa = wolfSSL_RSA_new());
  47356. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47357. &signatureLen, rsa, 1), 0);
  47358. wolfSSL_RSA_free(rsa);
  47359. der = privDer;
  47360. rsa = NULL;
  47361. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47362. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  47363. -1), 0);
  47364. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  47365. signature, &signatureLen, rsa, 1), 0);
  47366. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47367. &signatureLen, rsa, 1), 0);
  47368. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47369. &signatureLen, rsa, 1), 0);
  47370. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47371. NULL, rsa, 1), 0);
  47372. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47373. &signatureLen, NULL, 1), 0);
  47374. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47375. &signatureLen, rsa, -1), 0);
  47376. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47377. &signatureLen, rsa, 0), 0);
  47378. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47379. &signatureLen, rsa, 0), 0);
  47380. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47381. NULL, rsa, 0), 0);
  47382. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47383. &signatureLen, rsa, 1), 1);
  47384. /* Test returning encoded hash. */
  47385. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  47386. &encodedHashLen, rsa, 0), 1);
  47387. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  47388. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  47389. RSA_free(rsa);
  47390. der = pubDer;
  47391. rsa = NULL;
  47392. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47393. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47394. signatureLen, rsa), 1);
  47395. RSA_free(rsa);
  47396. #endif
  47397. return EXPECT_RESULT();
  47398. }
  47399. static int test_wolfSSL_RSA_public_decrypt(void)
  47400. {
  47401. EXPECT_DECLS;
  47402. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47403. RSA *rsa;
  47404. unsigned char msg[SHA256_DIGEST_LENGTH];
  47405. #ifdef USE_CERT_BUFFERS_1024
  47406. const unsigned char* pubDer = client_keypub_der_1024;
  47407. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47408. unsigned char decMsg[1024/8];
  47409. const unsigned char encMsg[] = {
  47410. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47411. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47412. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47413. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47414. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47415. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47416. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47417. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47418. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47419. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47420. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47421. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47422. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47423. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47424. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47425. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47426. };
  47427. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47428. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47429. defined(WC_RSA_NO_PADDING)
  47430. const unsigned char encMsgNoPad[] = {
  47431. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47432. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47433. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47434. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47435. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47436. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47437. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47438. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47439. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47440. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47441. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47442. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47443. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47444. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47445. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47446. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47447. };
  47448. #endif
  47449. #else
  47450. const unsigned char* pubDer = client_keypub_der_2048;
  47451. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47452. unsigned char decMsg[2048/8];
  47453. const unsigned char encMsg[] = {
  47454. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47455. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47456. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47457. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47458. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47459. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47460. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47461. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47462. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47463. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47464. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47465. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47466. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47467. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47468. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47469. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47470. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47471. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47472. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47473. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47474. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47475. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47476. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47477. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47478. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47479. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47480. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47481. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47482. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47483. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47484. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47485. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47486. };
  47487. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47488. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47489. defined(WC_RSA_NO_PADDING)
  47490. const unsigned char encMsgNoPad[] = {
  47491. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47492. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47493. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47494. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47495. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47496. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47497. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47498. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47499. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47500. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47501. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47502. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47503. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47504. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47505. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47506. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47507. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47508. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47509. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47510. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47511. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47512. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47513. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47514. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47515. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47516. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47517. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47518. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47519. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47520. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47521. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47522. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47523. };
  47524. #endif
  47525. #endif
  47526. const unsigned char* der;
  47527. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47528. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47529. defined(WC_RSA_NO_PADDING)
  47530. int i;
  47531. #endif
  47532. XMEMSET(msg, 0, sizeof(msg));
  47533. der = pubDer;
  47534. rsa = NULL;
  47535. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47536. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  47537. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  47538. RSA_PKCS1_PADDING), -1);
  47539. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  47540. RSA_PKCS1_PADDING), -1);
  47541. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47542. RSA_PKCS1_PADDING), -1);
  47543. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  47544. RSA_PKCS1_PADDING), -1);
  47545. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47546. RSA_PKCS1_PSS_PADDING), -1);
  47547. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47548. RSA_PKCS1_PADDING), 32);
  47549. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  47550. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47551. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47552. defined(WC_RSA_NO_PADDING)
  47553. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  47554. rsa, RSA_NO_PADDING), sizeof(decMsg));
  47555. /* Zeros before actual data. */
  47556. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  47557. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47558. }
  47559. /* Check actual data. */
  47560. XMEMSET(msg, 0x01, sizeof(msg));
  47561. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47562. #endif
  47563. RSA_free(rsa);
  47564. #endif
  47565. return EXPECT_RESULT();
  47566. }
  47567. static int test_wolfSSL_RSA_private_encrypt(void)
  47568. {
  47569. EXPECT_DECLS;
  47570. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47571. RSA *rsa;
  47572. unsigned char msg[SHA256_DIGEST_LENGTH];
  47573. #ifdef USE_CERT_BUFFERS_1024
  47574. const unsigned char* privDer = client_key_der_1024;
  47575. size_t privDerSz = sizeof_client_key_der_1024;
  47576. unsigned char encMsg[1024/8];
  47577. const unsigned char expEncMsg[] = {
  47578. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47579. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47580. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47581. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47582. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47583. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47584. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47585. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47586. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47587. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47588. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47589. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47590. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47591. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47592. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47593. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47594. };
  47595. #ifdef WC_RSA_NO_PADDING
  47596. const unsigned char expEncMsgNoPad[] = {
  47597. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47598. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47599. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47600. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47601. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47602. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47603. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47604. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47605. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47606. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47607. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47608. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47609. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47610. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47611. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47612. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47613. };
  47614. #endif
  47615. #else
  47616. const unsigned char* privDer = client_key_der_2048;
  47617. size_t privDerSz = sizeof_client_key_der_2048;
  47618. unsigned char encMsg[2048/8];
  47619. const unsigned char expEncMsg[] = {
  47620. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47621. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47622. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47623. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47624. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47625. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47626. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47627. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47628. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47629. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47630. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47631. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47632. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47633. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47634. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47635. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47636. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47637. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47638. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47639. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47640. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47641. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47642. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47643. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47644. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47645. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47646. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47647. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47648. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47649. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47650. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47651. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47652. };
  47653. #ifdef WC_RSA_NO_PADDING
  47654. const unsigned char expEncMsgNoPad[] = {
  47655. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47656. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47657. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47658. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47659. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47660. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47661. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47662. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47663. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47664. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47665. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47666. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47667. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47668. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47669. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47670. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47671. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47672. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47673. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47674. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47675. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47676. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47677. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47678. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47679. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47680. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47681. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47682. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47683. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47684. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47685. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47686. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47687. };
  47688. #endif
  47689. #endif
  47690. const unsigned char* der;
  47691. XMEMSET(msg, 0x00, sizeof(msg));
  47692. der = privDer;
  47693. rsa = NULL;
  47694. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47695. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  47696. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  47697. -1);
  47698. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47699. RSA_PKCS1_PADDING), -1);
  47700. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  47701. RSA_PKCS1_PADDING), -1);
  47702. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  47703. RSA_PKCS1_PADDING), -1);
  47704. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47705. RSA_PKCS1_PSS_PADDING), -1);
  47706. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47707. RSA_PKCS1_PADDING), sizeof(encMsg));
  47708. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  47709. #ifdef WC_RSA_NO_PADDING
  47710. /* Non-zero message. */
  47711. XMEMSET(msg, 0x01, sizeof(msg));
  47712. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47713. RSA_NO_PADDING), sizeof(encMsg));
  47714. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  47715. #endif
  47716. RSA_free(rsa);
  47717. #endif
  47718. return EXPECT_RESULT();
  47719. }
  47720. static int test_wolfSSL_RSA_public_encrypt(void)
  47721. {
  47722. EXPECT_DECLS;
  47723. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47724. RSA* rsa = NULL;
  47725. const unsigned char msg[2048/8] = { 0 };
  47726. unsigned char encMsg[2048/8];
  47727. ExpectNotNull(rsa = RSA_new());
  47728. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  47729. RSA_PKCS1_PADDING), -1);
  47730. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47731. RSA_PKCS1_PADDING), -1);
  47732. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  47733. RSA_PKCS1_PADDING), -1);
  47734. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  47735. RSA_PKCS1_PADDING), -1);
  47736. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47737. RSA_PKCS1_PSS_PADDING), -1);
  47738. /* Empty RSA key. */
  47739. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47740. RSA_PKCS1_PADDING), -1);
  47741. RSA_free(rsa);
  47742. #endif
  47743. return EXPECT_RESULT();
  47744. }
  47745. static int test_wolfSSL_RSA_private_decrypt(void)
  47746. {
  47747. EXPECT_DECLS;
  47748. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47749. RSA* rsa = NULL;
  47750. unsigned char msg[2048/8];
  47751. const unsigned char encMsg[2048/8] = { 0 };
  47752. ExpectNotNull(rsa = RSA_new());
  47753. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  47754. RSA_PKCS1_PADDING), -1);
  47755. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  47756. RSA_PKCS1_PADDING), -1);
  47757. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47758. RSA_PKCS1_PADDING), -1);
  47759. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  47760. RSA_PKCS1_PADDING), -1);
  47761. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47762. RSA_PKCS1_PSS_PADDING), -1);
  47763. /* Empty RSA key. */
  47764. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47765. RSA_PKCS1_PADDING), -1);
  47766. RSA_free(rsa);
  47767. #endif
  47768. return EXPECT_RESULT();
  47769. }
  47770. static int test_wolfSSL_RSA_GenAdd(void)
  47771. {
  47772. EXPECT_DECLS;
  47773. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47774. RSA *rsa;
  47775. #ifdef USE_CERT_BUFFERS_1024
  47776. const unsigned char* privDer = client_key_der_1024;
  47777. size_t privDerSz = sizeof_client_key_der_1024;
  47778. const unsigned char* pubDer = client_keypub_der_1024;
  47779. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47780. #else
  47781. const unsigned char* privDer = client_key_der_2048;
  47782. size_t privDerSz = sizeof_client_key_der_2048;
  47783. const unsigned char* pubDer = client_keypub_der_2048;
  47784. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47785. #endif
  47786. const unsigned char* der;
  47787. der = privDer;
  47788. rsa = NULL;
  47789. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47790. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  47791. #ifndef RSA_LOW_MEM
  47792. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  47793. #else
  47794. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  47795. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47796. #endif
  47797. RSA_free(rsa);
  47798. der = pubDer;
  47799. rsa = NULL;
  47800. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47801. /* Need private values. */
  47802. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47803. RSA_free(rsa);
  47804. #endif
  47805. return EXPECT_RESULT();
  47806. }
  47807. static int test_wolfSSL_RSA_blinding_on(void)
  47808. {
  47809. EXPECT_DECLS;
  47810. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  47811. RSA *rsa;
  47812. WOLFSSL_BN_CTX *bnCtx = NULL;
  47813. #ifdef USE_CERT_BUFFERS_1024
  47814. const unsigned char* privDer = client_key_der_1024;
  47815. size_t privDerSz = sizeof_client_key_der_1024;
  47816. #else
  47817. const unsigned char* privDer = client_key_der_2048;
  47818. size_t privDerSz = sizeof_client_key_der_2048;
  47819. #endif
  47820. const unsigned char* der;
  47821. der = privDer;
  47822. rsa = NULL;
  47823. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47824. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  47825. /* Does nothing so all parameters are valid. */
  47826. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  47827. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  47828. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  47829. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  47830. wolfSSL_BN_CTX_free(bnCtx);
  47831. RSA_free(rsa);
  47832. #endif
  47833. return EXPECT_RESULT();
  47834. }
  47835. static int test_wolfSSL_RSA_ex_data(void)
  47836. {
  47837. EXPECT_DECLS;
  47838. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  47839. RSA* rsa = NULL;
  47840. unsigned char data[1];
  47841. ExpectNotNull(rsa = RSA_new());
  47842. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  47843. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47844. #ifdef MAX_EX_DATA
  47845. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  47846. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  47847. #endif
  47848. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  47849. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  47850. #ifdef HAVE_EX_DATA
  47851. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  47852. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  47853. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  47854. #else
  47855. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  47856. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  47857. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47858. #endif
  47859. RSA_free(rsa);
  47860. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47861. return EXPECT_RESULT();
  47862. }
  47863. static int test_wolfSSL_RSA_LoadDer(void)
  47864. {
  47865. EXPECT_DECLS;
  47866. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47867. defined(OPENSSL_EXTRA_X509_SMALL))
  47868. RSA *rsa = NULL;
  47869. #ifdef USE_CERT_BUFFERS_1024
  47870. const unsigned char* privDer = client_key_der_1024;
  47871. size_t privDerSz = sizeof_client_key_der_1024;
  47872. #else
  47873. const unsigned char* privDer = client_key_der_2048;
  47874. size_t privDerSz = sizeof_client_key_der_2048;
  47875. #endif
  47876. ExpectNotNull(rsa = RSA_new());
  47877. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47878. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47879. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47880. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47881. RSA_free(rsa);
  47882. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47883. return EXPECT_RESULT();
  47884. }
  47885. /* Local API. */
  47886. static int test_wolfSSL_RSA_To_Der(void)
  47887. {
  47888. EXPECT_DECLS;
  47889. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47890. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47891. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47892. RSA* rsa;
  47893. #ifdef USE_CERT_BUFFERS_1024
  47894. const unsigned char* privDer = client_key_der_1024;
  47895. size_t privDerSz = sizeof_client_key_der_1024;
  47896. const unsigned char* pubDer = client_keypub_der_1024;
  47897. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47898. unsigned char out[sizeof(client_key_der_1024)];
  47899. #else
  47900. const unsigned char* privDer = client_key_der_2048;
  47901. size_t privDerSz = sizeof_client_key_der_2048;
  47902. const unsigned char* pubDer = client_keypub_der_2048;
  47903. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47904. unsigned char out[sizeof(client_key_der_2048)];
  47905. #endif
  47906. const unsigned char* der;
  47907. unsigned char* outDer = NULL;
  47908. der = privDer;
  47909. rsa = NULL;
  47910. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47911. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47912. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47913. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47914. outDer = out;
  47915. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47916. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47917. outDer = NULL;
  47918. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47919. ExpectNotNull(outDer);
  47920. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47921. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47922. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47923. outDer = out;
  47924. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47925. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47926. RSA_free(rsa);
  47927. ExpectNotNull(rsa = RSA_new());
  47928. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47929. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47930. RSA_free(rsa);
  47931. der = pubDer;
  47932. rsa = NULL;
  47933. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47934. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47935. RSA_free(rsa);
  47936. #endif
  47937. #endif
  47938. return EXPECT_RESULT();
  47939. }
  47940. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47941. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47942. {
  47943. EXPECT_DECLS;
  47944. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47945. XFILE file = XBADFILE;
  47946. const char* fname = "./certs/server-keyPub.pem";
  47947. RSA *rsa = NULL;
  47948. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47949. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  47950. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  47951. ExpectIntEQ(RSA_size(rsa), 256);
  47952. RSA_free(rsa);
  47953. if (file != XBADFILE)
  47954. XFCLOSE(file);
  47955. #endif
  47956. return EXPECT_RESULT();
  47957. }
  47958. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47959. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  47960. {
  47961. EXPECT_DECLS;
  47962. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  47963. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  47964. RSA* rsa = NULL;
  47965. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  47966. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  47967. /* Valid but stub so returns 0. */
  47968. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  47969. #endif
  47970. return EXPECT_RESULT();
  47971. }
  47972. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47973. {
  47974. EXPECT_DECLS;
  47975. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47976. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47977. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47978. RSA* rsa = NULL;
  47979. #ifdef USE_CERT_BUFFERS_1024
  47980. const unsigned char* privDer = client_key_der_1024;
  47981. size_t privDerSz = sizeof_client_key_der_1024;
  47982. #else
  47983. const unsigned char* privDer = client_key_der_2048;
  47984. size_t privDerSz = sizeof_client_key_der_2048;
  47985. #endif
  47986. const unsigned char* der;
  47987. #ifndef NO_AES
  47988. unsigned char passwd[] = "password";
  47989. #endif
  47990. ExpectNotNull(rsa = RSA_new());
  47991. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47992. NULL, NULL), 0);
  47993. RSA_free(rsa);
  47994. der = privDer;
  47995. rsa = NULL;
  47996. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47997. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47998. NULL, NULL), 0);
  47999. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  48000. NULL, NULL), 0);
  48001. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  48002. NULL, NULL), 1);
  48003. #ifndef NO_AES
  48004. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  48005. NULL, 0, NULL, NULL), 1);
  48006. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  48007. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  48008. #endif
  48009. RSA_free(rsa);
  48010. #endif
  48011. return EXPECT_RESULT();
  48012. }
  48013. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  48014. {
  48015. EXPECT_DECLS;
  48016. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  48017. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  48018. defined(WOLFSSL_DER_TO_PEM))
  48019. RSA* rsa = NULL;
  48020. #ifdef USE_CERT_BUFFERS_1024
  48021. const unsigned char* privDer = client_key_der_1024;
  48022. size_t privDerSz = sizeof_client_key_der_1024;
  48023. #else
  48024. const unsigned char* privDer = client_key_der_2048;
  48025. size_t privDerSz = sizeof_client_key_der_2048;
  48026. #endif
  48027. const unsigned char* der;
  48028. #ifndef NO_AES
  48029. unsigned char passwd[] = "password";
  48030. #endif
  48031. unsigned char* pem = NULL;
  48032. int plen;
  48033. ExpectNotNull(rsa = RSA_new());
  48034. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  48035. &plen), 0);
  48036. RSA_free(rsa);
  48037. der = privDer;
  48038. rsa = NULL;
  48039. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  48040. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  48041. &plen), 0);
  48042. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  48043. &plen), 0);
  48044. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  48045. NULL), 0);
  48046. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  48047. &plen), 1);
  48048. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  48049. pem = NULL;
  48050. #ifndef NO_AES
  48051. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  48052. NULL, 0, &pem, &plen), 1);
  48053. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  48054. pem = NULL;
  48055. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  48056. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  48057. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  48058. #endif
  48059. RSA_free(rsa);
  48060. #endif
  48061. return EXPECT_RESULT();
  48062. }
  48063. static int test_wolfSSL_DH(void)
  48064. {
  48065. EXPECT_DECLS;
  48066. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48067. DH *dh = NULL;
  48068. BIGNUM* p;
  48069. BIGNUM* q;
  48070. BIGNUM* g;
  48071. BIGNUM* pub = NULL;
  48072. BIGNUM* priv = NULL;
  48073. #if defined(OPENSSL_ALL)
  48074. #if !defined(HAVE_FIPS) || \
  48075. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  48076. FILE* f = NULL;
  48077. unsigned char buf[268];
  48078. const unsigned char* pt = buf;
  48079. long len = 0;
  48080. dh = NULL;
  48081. XMEMSET(buf, 0, sizeof(buf));
  48082. /* Test 2048 bit parameters */
  48083. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  48084. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48085. if (f != XBADFILE)
  48086. XFCLOSE(f);
  48087. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  48088. ExpectNotNull(dh->p);
  48089. ExpectNotNull(dh->g);
  48090. ExpectTrue(pt == buf);
  48091. ExpectIntEQ(DH_generate_key(dh), 1);
  48092. ExpectIntEQ(DH_generate_key(dh), 1);
  48093. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  48094. ExpectNotNull(pub = BN_new());
  48095. ExpectIntEQ(BN_set_word(pub, 1), 1);
  48096. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  48097. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  48098. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  48099. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  48100. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  48101. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  48102. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  48103. BN_free(pub);
  48104. pub = NULL;
  48105. DH_get0_pqg(dh, (const BIGNUM**)&p,
  48106. (const BIGNUM**)&q,
  48107. (const BIGNUM**)&g);
  48108. ExpectPtrEq(p, dh->p);
  48109. ExpectPtrEq(q, dh->q);
  48110. ExpectPtrEq(g, dh->g);
  48111. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  48112. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  48113. ExpectPtrEq(pub, dh->pub_key);
  48114. ExpectPtrEq(priv, dh->priv_key);
  48115. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  48116. ExpectPtrEq(pub, dh->pub_key);
  48117. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  48118. ExpectPtrEq(priv, dh->priv_key);
  48119. pub = NULL;
  48120. priv = NULL;
  48121. ExpectNotNull(pub = BN_new());
  48122. ExpectNotNull(priv = BN_new());
  48123. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  48124. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  48125. if (EXPECT_FAIL()) {
  48126. BN_free(pub);
  48127. BN_free(priv);
  48128. }
  48129. pub = NULL;
  48130. priv = NULL;
  48131. ExpectNotNull(pub = BN_new());
  48132. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  48133. if (EXPECT_FAIL()) {
  48134. BN_free(pub);
  48135. }
  48136. ExpectNotNull(priv = BN_new());
  48137. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  48138. if (EXPECT_FAIL()) {
  48139. BN_free(priv);
  48140. }
  48141. ExpectPtrEq(pub, dh->pub_key);
  48142. ExpectPtrEq(priv, dh->priv_key);
  48143. pub = NULL;
  48144. priv = NULL;
  48145. DH_free(dh);
  48146. dh = NULL;
  48147. ExpectNotNull(dh = DH_new());
  48148. p = NULL;
  48149. ExpectNotNull(p = BN_new());
  48150. ExpectIntEQ(BN_set_word(p, 1), 1);
  48151. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  48152. ExpectNotNull(pub = BN_new());
  48153. ExpectNotNull(priv = BN_new());
  48154. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  48155. if (EXPECT_FAIL()) {
  48156. BN_free(pub);
  48157. BN_free(priv);
  48158. }
  48159. pub = NULL;
  48160. priv = NULL;
  48161. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  48162. BN_free(p);
  48163. p = NULL;
  48164. DH_free(dh);
  48165. dh = NULL;
  48166. #ifdef WOLFSSL_KEY_GEN
  48167. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  48168. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  48169. DH_free(dh);
  48170. #endif
  48171. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  48172. #endif /* OPENSSL_ALL */
  48173. (void)dh;
  48174. (void)p;
  48175. (void)q;
  48176. (void)g;
  48177. (void)pub;
  48178. (void)priv;
  48179. ExpectNotNull(dh = wolfSSL_DH_new());
  48180. /* invalid parameters test */
  48181. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  48182. (const BIGNUM**)&q,
  48183. (const BIGNUM**)&g);
  48184. DH_get0_pqg(dh, NULL,
  48185. (const BIGNUM**)&q,
  48186. (const BIGNUM**)&g);
  48187. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  48188. DH_get0_pqg(dh, NULL, NULL, NULL);
  48189. DH_get0_pqg(dh, (const BIGNUM**)&p,
  48190. (const BIGNUM**)&q,
  48191. (const BIGNUM**)&g);
  48192. ExpectPtrEq(p, NULL);
  48193. ExpectPtrEq(q, NULL);
  48194. ExpectPtrEq(g, NULL);
  48195. DH_free(dh);
  48196. dh = NULL;
  48197. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  48198. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  48199. #if defined(OPENSSL_ALL) || \
  48200. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  48201. dh = wolfSSL_DH_new();
  48202. ExpectNotNull(dh);
  48203. p = wolfSSL_BN_new();
  48204. ExpectNotNull(p);
  48205. ExpectIntEQ(BN_set_word(p, 11), 1);
  48206. g = wolfSSL_BN_new();
  48207. ExpectNotNull(g);
  48208. ExpectIntEQ(BN_set_word(g, 2), 1);
  48209. q = wolfSSL_BN_new();
  48210. ExpectNotNull(q);
  48211. ExpectIntEQ(BN_set_word(q, 5), 1);
  48212. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  48213. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  48214. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  48215. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  48216. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  48217. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  48218. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  48219. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  48220. /* Don't need q. */
  48221. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48222. if (EXPECT_FAIL()) {
  48223. BN_free(p);
  48224. BN_free(g);
  48225. }
  48226. p = NULL;
  48227. g = NULL;
  48228. /* Setting again will free the p and g. */
  48229. wolfSSL_BN_free(q);
  48230. q = NULL;
  48231. DH_free(dh);
  48232. dh = NULL;
  48233. dh = wolfSSL_DH_new();
  48234. ExpectNotNull(dh);
  48235. p = wolfSSL_BN_new();
  48236. ExpectNotNull(p);
  48237. ExpectIntEQ(BN_set_word(p, 11), 1);
  48238. g = wolfSSL_BN_new();
  48239. ExpectNotNull(g);
  48240. ExpectIntEQ(BN_set_word(g, 2), 1);
  48241. q = wolfSSL_BN_new();
  48242. ExpectNotNull(q);
  48243. ExpectIntEQ(BN_set_word(q, 5), 1);
  48244. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  48245. /* p, q and g are now owned by dh - don't free. */
  48246. if (EXPECT_FAIL()) {
  48247. BN_free(p);
  48248. BN_free(q);
  48249. BN_free(g);
  48250. }
  48251. p = NULL;
  48252. q = NULL;
  48253. g = NULL;
  48254. p = wolfSSL_BN_new();
  48255. ExpectNotNull(p);
  48256. ExpectIntEQ(BN_set_word(p, 11), 1);
  48257. g = wolfSSL_BN_new();
  48258. ExpectNotNull(g);
  48259. ExpectIntEQ(BN_set_word(g, 2), 1);
  48260. q = wolfSSL_BN_new();
  48261. ExpectNotNull(q);
  48262. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  48263. if (EXPECT_FAIL()) {
  48264. BN_free(p);
  48265. }
  48266. p = NULL;
  48267. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  48268. if (EXPECT_FAIL()) {
  48269. BN_free(q);
  48270. }
  48271. q = NULL;
  48272. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  48273. if (EXPECT_FAIL()) {
  48274. BN_free(g);
  48275. }
  48276. g = NULL;
  48277. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  48278. /* p, q and g are now owned by dh - don't free. */
  48279. DH_free(dh);
  48280. dh = NULL;
  48281. ExpectIntEQ(DH_generate_key(NULL), 0);
  48282. ExpectNotNull(dh = DH_new());
  48283. ExpectIntEQ(DH_generate_key(dh), 0);
  48284. p = wolfSSL_BN_new();
  48285. ExpectNotNull(p);
  48286. ExpectIntEQ(BN_set_word(p, 0), 1);
  48287. g = wolfSSL_BN_new();
  48288. ExpectNotNull(g);
  48289. ExpectIntEQ(BN_set_word(g, 2), 1);
  48290. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48291. if (EXPECT_FAIL()) {
  48292. BN_free(p);
  48293. BN_free(g);
  48294. }
  48295. p = NULL;
  48296. g = NULL;
  48297. ExpectIntEQ(DH_generate_key(dh), 0);
  48298. DH_free(dh);
  48299. dh = NULL;
  48300. #endif
  48301. #endif
  48302. /* Test DH_up_ref() */
  48303. dh = wolfSSL_DH_new();
  48304. ExpectNotNull(dh);
  48305. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  48306. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  48307. DH_free(dh); /* decrease ref count */
  48308. DH_free(dh); /* free WOLFSSL_DH */
  48309. q = NULL;
  48310. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  48311. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  48312. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  48313. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  48314. #ifdef HAVE_FFDHE_2048
  48315. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48316. DH_free(dh);
  48317. q = NULL;
  48318. #endif
  48319. #ifdef HAVE_FFDHE_3072
  48320. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  48321. DH_free(dh);
  48322. q = NULL;
  48323. #endif
  48324. #ifdef HAVE_FFDHE_4096
  48325. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  48326. DH_free(dh);
  48327. q = NULL;
  48328. #endif
  48329. #else
  48330. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48331. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  48332. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  48333. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  48334. #endif /* OPENSSL_EXTRA && !NO_DH */
  48335. return EXPECT_RESULT();
  48336. }
  48337. static int test_wolfSSL_DH_dup(void)
  48338. {
  48339. EXPECT_DECLS;
  48340. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  48341. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  48342. defined(OPENSSL_EXTRA)
  48343. DH *dh = NULL;
  48344. DH *dhDup = NULL;
  48345. ExpectNotNull(dh = wolfSSL_DH_new());
  48346. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  48347. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  48348. #if defined(OPENSSL_ALL) || \
  48349. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  48350. {
  48351. WOLFSSL_BIGNUM* p = NULL;
  48352. WOLFSSL_BIGNUM* g = NULL;
  48353. ExpectNotNull(p = wolfSSL_BN_new());
  48354. ExpectNotNull(g = wolfSSL_BN_new());
  48355. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  48356. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  48357. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48358. if (EXPECT_FAIL()) {
  48359. wolfSSL_BN_free(p);
  48360. wolfSSL_BN_free(g);
  48361. }
  48362. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  48363. wolfSSL_DH_free(dhDup);
  48364. }
  48365. #endif
  48366. wolfSSL_DH_free(dh);
  48367. #endif
  48368. #endif
  48369. return EXPECT_RESULT();
  48370. }
  48371. static int test_wolfSSL_DH_check(void)
  48372. {
  48373. EXPECT_DECLS;
  48374. #ifdef OPENSSL_ALL
  48375. #ifndef NO_DH
  48376. #ifndef NO_BIO
  48377. #ifndef NO_DSA
  48378. byte buf[6000];
  48379. char file[] = "./certs/dsaparams.pem";
  48380. XFILE f = XBADFILE;
  48381. int bytes;
  48382. BIO* bio = NULL;
  48383. DSA* dsa = NULL;
  48384. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48385. static const byte dh2048[] = {
  48386. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48387. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48388. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48389. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48390. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48391. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48392. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48393. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48394. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48395. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48396. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48397. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48398. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48399. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48400. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48401. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48402. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48403. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48404. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48405. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48406. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48407. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48408. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48409. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48410. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48411. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48412. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48413. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48414. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48415. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48416. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48417. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48418. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48419. 0x93, 0x02, 0x01, 0x02
  48420. };
  48421. const byte* params;
  48422. #endif
  48423. DH* dh = NULL;
  48424. WOLFSSL_BIGNUM* p = NULL;
  48425. WOLFSSL_BIGNUM* g = NULL;
  48426. WOLFSSL_BIGNUM* pTmp = NULL;
  48427. WOLFSSL_BIGNUM* gTmp = NULL;
  48428. int codes = -1;
  48429. #ifndef NO_DSA
  48430. /* Initialize DH */
  48431. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  48432. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  48433. if (f != XBADFILE)
  48434. XFCLOSE(f);
  48435. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  48436. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  48437. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  48438. ExpectNotNull(dh);
  48439. BIO_free(bio);
  48440. DSA_free(dsa);
  48441. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48442. params = dh2048;
  48443. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  48444. (long)sizeof(dh2048)));
  48445. #else
  48446. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  48447. #endif
  48448. /* Test assumed to be valid dh.
  48449. * Should return WOLFSSL_SUCCESS
  48450. * codes should be 0
  48451. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  48452. */
  48453. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48454. ExpectIntEQ(codes, 0);
  48455. /* Test NULL dh: expected BAD_FUNC_ARG */
  48456. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  48457. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  48458. if (dh != NULL) {
  48459. pTmp = dh->p;
  48460. dh->p = NULL;
  48461. }
  48462. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48463. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48464. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48465. /* set dh->p back to normal so it won't fail on next tests */
  48466. if (dh != NULL) {
  48467. dh->p = pTmp;
  48468. pTmp = NULL;
  48469. }
  48470. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  48471. if (dh != NULL) {
  48472. gTmp = dh->g;
  48473. dh->g = NULL;
  48474. }
  48475. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48476. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48477. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  48478. if (dh != NULL) {
  48479. dh->g = gTmp;
  48480. gTmp = NULL;
  48481. }
  48482. /* Cleanup */
  48483. DH_free(dh);
  48484. dh = NULL;
  48485. dh = DH_new();
  48486. ExpectNotNull(dh);
  48487. /* Check empty DH. */
  48488. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48489. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48490. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  48491. /* Check non-prime valued p. */
  48492. ExpectNotNull(p = BN_new());
  48493. ExpectIntEQ(BN_set_word(p, 4), 1);
  48494. ExpectNotNull(g = BN_new());
  48495. ExpectIntEQ(BN_set_word(g, 2), 1);
  48496. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  48497. if (EXPECT_FAIL()) {
  48498. wolfSSL_BN_free(p);
  48499. wolfSSL_BN_free(g);
  48500. }
  48501. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48502. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48503. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48504. DH_free(dh);
  48505. #endif
  48506. #endif /* !NO_DH && !NO_DSA */
  48507. #endif
  48508. return EXPECT_RESULT();
  48509. }
  48510. static int test_wolfSSL_DH_prime(void)
  48511. {
  48512. EXPECT_DECLS;
  48513. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48514. WOLFSSL_BIGNUM* bn = NULL;
  48515. #if WOLFSSL_MAX_BN_BITS >= 768
  48516. WOLFSSL_BIGNUM* bn2 = NULL;
  48517. #endif
  48518. bn = wolfSSL_DH_768_prime(NULL);
  48519. #if WOLFSSL_MAX_BN_BITS >= 768
  48520. ExpectNotNull(bn);
  48521. bn2 = wolfSSL_DH_768_prime(bn);
  48522. ExpectNotNull(bn2);
  48523. ExpectTrue(bn == bn2);
  48524. wolfSSL_BN_free(bn);
  48525. bn = NULL;
  48526. #else
  48527. ExpectNull(bn);
  48528. #endif
  48529. bn = wolfSSL_DH_1024_prime(NULL);
  48530. #if WOLFSSL_MAX_BN_BITS >= 1024
  48531. ExpectNotNull(bn);
  48532. wolfSSL_BN_free(bn);
  48533. bn = NULL;
  48534. #else
  48535. ExpectNull(bn);
  48536. #endif
  48537. bn = wolfSSL_DH_2048_prime(NULL);
  48538. #if WOLFSSL_MAX_BN_BITS >= 2048
  48539. ExpectNotNull(bn);
  48540. wolfSSL_BN_free(bn);
  48541. bn = NULL;
  48542. #else
  48543. ExpectNull(bn);
  48544. #endif
  48545. bn = wolfSSL_DH_3072_prime(NULL);
  48546. #if WOLFSSL_MAX_BN_BITS >= 3072
  48547. ExpectNotNull(bn);
  48548. wolfSSL_BN_free(bn);
  48549. bn = NULL;
  48550. #else
  48551. ExpectNull(bn);
  48552. #endif
  48553. bn = wolfSSL_DH_4096_prime(NULL);
  48554. #if WOLFSSL_MAX_BN_BITS >= 4096
  48555. ExpectNotNull(bn);
  48556. wolfSSL_BN_free(bn);
  48557. bn = NULL;
  48558. #else
  48559. ExpectNull(bn);
  48560. #endif
  48561. bn = wolfSSL_DH_6144_prime(NULL);
  48562. #if WOLFSSL_MAX_BN_BITS >= 6144
  48563. ExpectNotNull(bn);
  48564. wolfSSL_BN_free(bn);
  48565. bn = NULL;
  48566. #else
  48567. ExpectNull(bn);
  48568. #endif
  48569. bn = wolfSSL_DH_8192_prime(NULL);
  48570. #if WOLFSSL_MAX_BN_BITS >= 8192
  48571. ExpectNotNull(bn);
  48572. wolfSSL_BN_free(bn);
  48573. bn = NULL;
  48574. #else
  48575. ExpectNull(bn);
  48576. #endif
  48577. #endif
  48578. return EXPECT_RESULT();
  48579. }
  48580. static int test_wolfSSL_DH_1536_prime(void)
  48581. {
  48582. EXPECT_DECLS;
  48583. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48584. BIGNUM* bn = NULL;
  48585. unsigned char bits[200];
  48586. int sz = 192; /* known binary size */
  48587. const byte expected[] = {
  48588. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48589. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  48590. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  48591. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  48592. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  48593. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  48594. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  48595. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  48596. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  48597. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  48598. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  48599. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  48600. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  48601. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  48602. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  48603. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  48604. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  48605. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  48606. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  48607. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  48608. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  48609. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  48610. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  48611. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48612. };
  48613. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  48614. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  48615. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  48616. BN_free(bn);
  48617. #endif
  48618. return EXPECT_RESULT();
  48619. }
  48620. static int test_wolfSSL_DH_get_2048_256(void)
  48621. {
  48622. EXPECT_DECLS;
  48623. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48624. WOLFSSL_DH* dh = NULL;
  48625. const WOLFSSL_BIGNUM* pBn;
  48626. const WOLFSSL_BIGNUM* gBn;
  48627. const WOLFSSL_BIGNUM* qBn;
  48628. const byte pExpected[] = {
  48629. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  48630. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  48631. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  48632. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  48633. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  48634. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  48635. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  48636. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  48637. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  48638. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  48639. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  48640. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  48641. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  48642. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  48643. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  48644. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  48645. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  48646. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  48647. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  48648. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  48649. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  48650. 0x1E, 0x1A, 0x15, 0x97
  48651. };
  48652. const byte gExpected[] = {
  48653. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  48654. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  48655. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  48656. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  48657. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  48658. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  48659. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  48660. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  48661. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  48662. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  48663. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  48664. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  48665. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  48666. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  48667. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  48668. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  48669. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  48670. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  48671. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  48672. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  48673. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  48674. 0x6C, 0xC4, 0x16, 0x59
  48675. };
  48676. const byte qExpected[] = {
  48677. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  48678. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  48679. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  48680. };
  48681. int pSz;
  48682. int qSz;
  48683. int gSz;
  48684. byte* pReturned = NULL;
  48685. byte* qReturned = NULL;
  48686. byte* gReturned = NULL;
  48687. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  48688. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  48689. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  48690. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  48691. DYNAMIC_TYPE_TMP_BUFFER));
  48692. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  48693. ExpectIntEQ(pSz, sizeof(pExpected));
  48694. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  48695. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  48696. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  48697. DYNAMIC_TYPE_TMP_BUFFER));
  48698. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  48699. ExpectIntEQ(qSz, sizeof(qExpected));
  48700. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  48701. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  48702. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  48703. DYNAMIC_TYPE_TMP_BUFFER));
  48704. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  48705. ExpectIntEQ(gSz, sizeof(gExpected));
  48706. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  48707. wolfSSL_DH_free(dh);
  48708. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48709. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48710. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48711. #endif
  48712. return EXPECT_RESULT();
  48713. }
  48714. static int test_wolfSSL_PEM_write_DHparams(void)
  48715. {
  48716. EXPECT_DECLS;
  48717. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  48718. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  48719. DH* dh = NULL;
  48720. BIO* bio = NULL;
  48721. XFILE fp = XBADFILE;
  48722. byte pem[2048];
  48723. int pemSz = 0;
  48724. const char expected[] =
  48725. "-----BEGIN DH PARAMETERS-----\n"
  48726. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  48727. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  48728. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  48729. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  48730. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  48731. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  48732. "-----END DH PARAMETERS-----\n";
  48733. const char badPem[] =
  48734. "-----BEGIN DH PARAMETERS-----\n"
  48735. "-----END DH PARAMETERS-----\n";
  48736. const char emptySeqPem[] =
  48737. "-----BEGIN DH PARAMETERS-----\n"
  48738. "MAA=\n"
  48739. "-----END DH PARAMETERS-----\n";
  48740. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  48741. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48742. if (fp != XBADFILE) {
  48743. XFCLOSE(fp);
  48744. fp = XBADFILE;
  48745. }
  48746. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  48747. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48748. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48749. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  48750. (int)sizeof(badPem));
  48751. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48752. BIO_free(bio);
  48753. bio = NULL;
  48754. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48755. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48756. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  48757. (int)sizeof(emptySeqPem));
  48758. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48759. BIO_free(bio);
  48760. bio = NULL;
  48761. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48762. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  48763. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48764. BIO_free(bio);
  48765. bio = NULL;
  48766. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  48767. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  48768. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  48769. DH_free(dh);
  48770. dh = NULL;
  48771. dh = wolfSSL_DH_new();
  48772. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  48773. if (fp != XBADFILE) {
  48774. XFCLOSE(fp);
  48775. fp = XBADFILE;
  48776. }
  48777. wolfSSL_DH_free(dh);
  48778. dh = NULL;
  48779. /* check results */
  48780. XMEMSET(pem, 0, sizeof(pem));
  48781. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  48782. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48783. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  48784. if (fp != XBADFILE)
  48785. XFCLOSE(fp);
  48786. #endif
  48787. return EXPECT_RESULT();
  48788. }
  48789. static int test_wolfSSL_d2i_DHparams(void)
  48790. {
  48791. EXPECT_DECLS;
  48792. #ifdef OPENSSL_ALL
  48793. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48794. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48795. XFILE f = XBADFILE;
  48796. unsigned char buf[4096];
  48797. const unsigned char* pt = buf;
  48798. #ifdef HAVE_FFDHE_2048
  48799. const char* params1 = "./certs/dh2048.der";
  48800. #endif
  48801. #ifdef HAVE_FFDHE_3072
  48802. const char* params2 = "./certs/dh3072.der";
  48803. #endif
  48804. long len = 0;
  48805. WOLFSSL_DH* dh = NULL;
  48806. XMEMSET(buf, 0, sizeof(buf));
  48807. /* Test 2048 bit parameters */
  48808. #ifdef HAVE_FFDHE_2048
  48809. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  48810. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48811. if (f != XBADFILE) {
  48812. XFCLOSE(f);
  48813. f = XBADFILE;
  48814. }
  48815. /* Valid case */
  48816. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48817. ExpectNotNull(dh->p);
  48818. ExpectNotNull(dh->g);
  48819. ExpectTrue(pt == buf);
  48820. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  48821. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  48822. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  48823. /* Invalid cases */
  48824. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48825. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48826. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  48827. DH_free(dh);
  48828. dh = NULL;
  48829. *buf = 0;
  48830. pt = buf;
  48831. #endif /* HAVE_FFDHE_2048 */
  48832. /* Test 3072 bit parameters */
  48833. #ifdef HAVE_FFDHE_3072
  48834. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  48835. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48836. if (f != XBADFILE) {
  48837. XFCLOSE(f);
  48838. f = XBADFILE;
  48839. }
  48840. /* Valid case */
  48841. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  48842. ExpectNotNull(dh->p);
  48843. ExpectNotNull(dh->g);
  48844. ExpectTrue(pt != buf);
  48845. ExpectIntEQ(DH_generate_key(dh), 1);
  48846. /* Invalid cases */
  48847. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48848. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48849. DH_free(dh);
  48850. dh = NULL;
  48851. #endif /* HAVE_FFDHE_3072 */
  48852. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48853. #endif /* !NO_DH */
  48854. #endif
  48855. return EXPECT_RESULT();
  48856. }
  48857. static int test_wolfSSL_DH_LoadDer(void)
  48858. {
  48859. EXPECT_DECLS;
  48860. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  48861. defined(OPENSSL_EXTRA)
  48862. static const byte dh2048[] = {
  48863. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48864. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48865. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48866. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48867. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48868. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48869. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48870. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48871. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48872. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48873. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48874. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48875. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48876. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48877. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48878. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48879. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48880. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48881. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48882. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48883. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48884. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48885. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48886. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48887. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48888. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48889. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48890. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48891. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48892. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48893. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48894. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48895. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48896. 0x93, 0x02, 0x01, 0x02
  48897. };
  48898. WOLFSSL_DH* dh = NULL;
  48899. ExpectNotNull(dh = wolfSSL_DH_new());
  48900. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  48901. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  48902. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  48903. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  48904. wolfSSL_DH_free(dh);
  48905. #endif
  48906. return EXPECT_RESULT();
  48907. }
  48908. static int test_wolfSSL_i2d_DHparams(void)
  48909. {
  48910. EXPECT_DECLS;
  48911. #ifdef OPENSSL_ALL
  48912. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48913. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48914. XFILE f = XBADFILE;
  48915. unsigned char buf[4096];
  48916. const unsigned char* pt;
  48917. unsigned char* pt2;
  48918. #ifdef HAVE_FFDHE_2048
  48919. const char* params1 = "./certs/dh2048.der";
  48920. #endif
  48921. #ifdef HAVE_FFDHE_3072
  48922. const char* params2 = "./certs/dh3072.der";
  48923. #endif
  48924. long len;
  48925. WOLFSSL_DH* dh = NULL;
  48926. /* Test 2048 bit parameters */
  48927. #ifdef HAVE_FFDHE_2048
  48928. pt = buf;
  48929. pt2 = buf;
  48930. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  48931. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48932. if (f != XBADFILE) {
  48933. XFCLOSE(f);
  48934. f = XBADFILE;
  48935. }
  48936. /* Valid case */
  48937. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48938. ExpectTrue(pt == buf);
  48939. ExpectIntEQ(DH_generate_key(dh), 1);
  48940. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  48941. /* Invalid case */
  48942. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48943. /* Return length only */
  48944. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  48945. DH_free(dh);
  48946. *buf = 0;
  48947. #endif
  48948. /* Test 3072 bit parameters */
  48949. #ifdef HAVE_FFDHE_3072
  48950. pt = buf;
  48951. pt2 = buf;
  48952. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  48953. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48954. if (f != XBADFILE) {
  48955. XFCLOSE(f);
  48956. f = XBADFILE;
  48957. }
  48958. /* Valid case */
  48959. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48960. ExpectTrue(pt == buf);
  48961. ExpectIntEQ(DH_generate_key(dh), 1);
  48962. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  48963. /* Invalid case */
  48964. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48965. /* Return length only */
  48966. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  48967. DH_free(dh);
  48968. #endif
  48969. dh = DH_new();
  48970. ExpectNotNull(dh);
  48971. pt2 = buf;
  48972. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48973. DH_free(dh);
  48974. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48975. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48976. #endif
  48977. return EXPECT_RESULT();
  48978. }
  48979. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  48980. /*----------------------------------------------------------------------------*
  48981. | EC
  48982. *----------------------------------------------------------------------------*/
  48983. static int test_wolfSSL_EC_GROUP(void)
  48984. {
  48985. EXPECT_DECLS;
  48986. #ifdef OPENSSL_EXTRA
  48987. EC_GROUP *group = NULL;
  48988. EC_GROUP *group2 = NULL;
  48989. EC_GROUP *group3 = NULL;
  48990. #ifndef HAVE_ECC_BRAINPOOL
  48991. EC_GROUP *group4 = NULL;
  48992. #endif
  48993. WOLFSSL_BIGNUM* order = NULL;
  48994. int group_bits;
  48995. int i;
  48996. static const int knownEccNids[] = {
  48997. NID_X9_62_prime192v1,
  48998. NID_X9_62_prime192v2,
  48999. NID_X9_62_prime192v3,
  49000. NID_X9_62_prime239v1,
  49001. NID_X9_62_prime239v2,
  49002. NID_X9_62_prime239v3,
  49003. NID_X9_62_prime256v1,
  49004. NID_secp112r1,
  49005. NID_secp112r2,
  49006. NID_secp128r1,
  49007. NID_secp128r2,
  49008. NID_secp160r1,
  49009. NID_secp160r2,
  49010. NID_secp224r1,
  49011. NID_secp384r1,
  49012. NID_secp521r1,
  49013. NID_secp160k1,
  49014. NID_secp192k1,
  49015. NID_secp224k1,
  49016. NID_secp256k1,
  49017. NID_brainpoolP160r1,
  49018. NID_brainpoolP192r1,
  49019. NID_brainpoolP224r1,
  49020. NID_brainpoolP256r1,
  49021. NID_brainpoolP320r1,
  49022. NID_brainpoolP384r1,
  49023. NID_brainpoolP512r1,
  49024. };
  49025. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  49026. static const int knownEccEnums[] = {
  49027. ECC_SECP192R1,
  49028. ECC_PRIME192V2,
  49029. ECC_PRIME192V3,
  49030. ECC_PRIME239V1,
  49031. ECC_PRIME239V2,
  49032. ECC_PRIME239V3,
  49033. ECC_SECP256R1,
  49034. ECC_SECP112R1,
  49035. ECC_SECP112R2,
  49036. ECC_SECP128R1,
  49037. ECC_SECP128R2,
  49038. ECC_SECP160R1,
  49039. ECC_SECP160R2,
  49040. ECC_SECP224R1,
  49041. ECC_SECP384R1,
  49042. ECC_SECP521R1,
  49043. ECC_SECP160K1,
  49044. ECC_SECP192K1,
  49045. ECC_SECP224K1,
  49046. ECC_SECP256K1,
  49047. ECC_BRAINPOOLP160R1,
  49048. ECC_BRAINPOOLP192R1,
  49049. ECC_BRAINPOOLP224R1,
  49050. ECC_BRAINPOOLP256R1,
  49051. ECC_BRAINPOOLP320R1,
  49052. ECC_BRAINPOOLP384R1,
  49053. ECC_BRAINPOOLP512R1,
  49054. };
  49055. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  49056. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49057. ExpectNotNull(group2 = EC_GROUP_dup(group));
  49058. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  49059. #ifndef HAVE_ECC_BRAINPOOL
  49060. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  49061. NID_brainpoolP256r1));
  49062. #endif
  49063. ExpectNull(EC_GROUP_dup(NULL));
  49064. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  49065. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  49066. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  49067. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  49068. #ifndef HAVE_ECC_BRAINPOOL
  49069. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  49070. #endif
  49071. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  49072. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  49073. ExpectNotNull(order = BN_new());
  49074. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  49075. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  49076. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  49077. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  49078. wolfSSL_BN_free(order);
  49079. ExpectNotNull(EC_GROUP_method_of(group));
  49080. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  49081. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  49082. NID_X9_62_prime_field);
  49083. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  49084. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  49085. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  49086. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  49087. #ifndef NO_WOLFSSL_STUB
  49088. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  49089. #endif
  49090. #ifndef HAVE_ECC_BRAINPOOL
  49091. EC_GROUP_free(group4);
  49092. #endif
  49093. EC_GROUP_free(group3);
  49094. EC_GROUP_free(group2);
  49095. EC_GROUP_free(group);
  49096. for (i = 0; i < knowEccNidsLen; i++) {
  49097. group = NULL;
  49098. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  49099. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  49100. EC_GROUP_free(group);
  49101. }
  49102. for (i = 0; i < knowEccEnumsLen; i++) {
  49103. group = NULL;
  49104. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  49105. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  49106. EC_GROUP_free(group);
  49107. }
  49108. #endif
  49109. return EXPECT_RESULT();
  49110. }
  49111. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  49112. {
  49113. EXPECT_DECLS;
  49114. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49115. EC_GROUP *group = NULL;
  49116. BIO* bio = NULL;
  49117. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  49118. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  49119. EC_GROUP *ret = NULL;
  49120. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  49121. "BgUrgQQAIg==\n"
  49122. "-----END EC PARAMETERS-----";
  49123. #endif
  49124. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  49125. "MAA=\n"
  49126. "-----END EC PARAMETERS-----";
  49127. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  49128. "BgA=\n"
  49129. "-----END EC PARAMETERS-----";
  49130. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  49131. "BgE=\n"
  49132. "-----END EC PARAMETERS-----";
  49133. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  49134. "BgE*\n"
  49135. "-----END EC PARAMETERS-----";
  49136. /* Test that first parameter, bio, being NULL fails. */
  49137. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  49138. /* Test that reading named parameters works. */
  49139. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  49140. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  49141. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49142. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  49143. BIO_free(bio);
  49144. bio = NULL;
  49145. EC_GROUP_free(group);
  49146. group = NULL;
  49147. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  49148. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  49149. /* Test that reusing group works. */
  49150. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  49151. sizeof(ec_nc_p384)));
  49152. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  49153. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  49154. BIO_free(bio);
  49155. bio = NULL;
  49156. EC_GROUP_free(group);
  49157. group = NULL;
  49158. /* Test that returning through group works. */
  49159. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  49160. sizeof(ec_nc_p384)));
  49161. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  49162. ExpectIntEQ(group == ret, 1);
  49163. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  49164. BIO_free(bio);
  49165. bio = NULL;
  49166. EC_GROUP_free(group);
  49167. group = NULL;
  49168. #endif
  49169. /* Test 0x30, 0x00 (not and object id) fails. */
  49170. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  49171. sizeof(ec_nc_bad_1)));
  49172. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49173. BIO_free(bio);
  49174. bio = NULL;
  49175. /* Test 0x06, 0x00 (empty object id) fails. */
  49176. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  49177. sizeof(ec_nc_bad_2)));
  49178. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49179. BIO_free(bio);
  49180. bio = NULL;
  49181. /* Test 0x06, 0x01 (badly formed object id) fails. */
  49182. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  49183. sizeof(ec_nc_bad_3)));
  49184. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49185. BIO_free(bio);
  49186. bio = NULL;
  49187. /* Test invalid PEM encoding - invalid character. */
  49188. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  49189. sizeof(ec_nc_bad_4)));
  49190. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49191. BIO_free(bio);
  49192. #endif
  49193. return EXPECT_RESULT();
  49194. }
  49195. static int test_wolfSSL_EC_POINT(void)
  49196. {
  49197. EXPECT_DECLS;
  49198. #if !defined(WOLFSSL_SP_MATH) && \
  49199. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  49200. #ifdef OPENSSL_EXTRA
  49201. BN_CTX* ctx = NULL;
  49202. EC_GROUP* group = NULL;
  49203. #ifndef HAVE_ECC_BRAINPOOL
  49204. EC_GROUP* group2 = NULL;
  49205. #endif
  49206. EC_POINT* Gxy = NULL;
  49207. EC_POINT* new_point = NULL;
  49208. EC_POINT* set_point = NULL;
  49209. EC_POINT* infinity = NULL;
  49210. BIGNUM* k = NULL;
  49211. BIGNUM* Gx = NULL;
  49212. BIGNUM* Gy = NULL;
  49213. BIGNUM* Gz = NULL;
  49214. BIGNUM* X = NULL;
  49215. BIGNUM* Y = NULL;
  49216. BIGNUM* set_point_bn = NULL;
  49217. char* hexStr = NULL;
  49218. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  49219. "17AF381913FF7A96314EA47055EA0FD0";
  49220. /* NISTP256R1 Gx/Gy */
  49221. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  49222. "77037D812DEB33A0F4A13945D898C296";
  49223. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  49224. "2BCE33576B315ECECBB6406837BF51F5";
  49225. #ifndef HAVE_SELFTEST
  49226. EC_POINT *tmp = NULL;
  49227. size_t bin_len;
  49228. unsigned int blen = 0;
  49229. unsigned char* buf = NULL;
  49230. unsigned char bufInf[1] = { 0x00 };
  49231. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  49232. "77037D812DEB33A0F4A13945D898C296"
  49233. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  49234. "2BCE33576B315ECECBB6406837BF51F5";
  49235. const unsigned char binUncompG[] = {
  49236. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49237. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49238. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49239. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49240. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49241. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49242. };
  49243. const unsigned char binUncompGBad[] = {
  49244. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49245. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49246. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49247. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49248. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49249. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49250. };
  49251. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  49252. "77037D812DEB33A0F4A13945D898C296";
  49253. #ifdef HAVE_COMP_KEY
  49254. const unsigned char binCompG[] = {
  49255. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49256. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49257. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49258. };
  49259. #endif
  49260. #endif
  49261. ExpectNotNull(ctx = BN_CTX_new());
  49262. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49263. #ifndef HAVE_ECC_BRAINPOOL
  49264. /* Used to make groups curve_idx == -1. */
  49265. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  49266. #endif
  49267. ExpectNull(EC_POINT_new(NULL));
  49268. ExpectNotNull(Gxy = EC_POINT_new(group));
  49269. ExpectNotNull(new_point = EC_POINT_new(group));
  49270. ExpectNotNull(set_point = EC_POINT_new(group));
  49271. ExpectNotNull(X = BN_new());
  49272. ExpectNotNull(Y = BN_new());
  49273. ExpectNotNull(set_point_bn = BN_new());
  49274. ExpectNotNull(infinity = EC_POINT_new(group));
  49275. /* load test values */
  49276. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  49277. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  49278. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  49279. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  49280. /* populate coordinates for input point */
  49281. if (Gxy != NULL) {
  49282. Gxy->X = Gx;
  49283. Gxy->Y = Gy;
  49284. Gxy->Z = Gz;
  49285. }
  49286. /* Test handling of NULL point. */
  49287. EC_POINT_clear_free(NULL);
  49288. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49289. NULL, NULL, ctx), 0);
  49290. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  49291. NULL, NULL, ctx), 0);
  49292. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  49293. NULL, NULL, ctx), 0);
  49294. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49295. X, NULL, ctx), 0);
  49296. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49297. NULL, Y, ctx), 0);
  49298. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  49299. X, Y, ctx), 0);
  49300. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  49301. X, Y, ctx), 0);
  49302. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  49303. NULL, Y, ctx), 0);
  49304. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  49305. X, NULL, ctx), 0);
  49306. /* Getting point at infinity returns an error. */
  49307. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  49308. X, Y, ctx), 0);
  49309. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  49310. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  49311. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49312. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  49313. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  49314. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  49315. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  49316. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  49317. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  49318. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  49319. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  49320. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  49321. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  49322. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  49323. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  49324. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  49325. /* perform point multiplication */
  49326. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  49327. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49328. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49329. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49330. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  49331. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49332. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49333. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49334. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  49335. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49336. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49337. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49338. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  49339. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  49340. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  49341. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  49342. /* Set point to something. */
  49343. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  49344. #else
  49345. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  49346. ctx), 1);
  49347. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49348. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49349. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49350. #endif
  49351. /* check if point X coordinate is zero */
  49352. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49353. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  49354. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  49355. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  49356. #endif
  49357. /* extract the coordinates from point */
  49358. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49359. ctx), WOLFSSL_SUCCESS);
  49360. /* check if point X coordinate is zero */
  49361. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  49362. /* set the same X and Y points in another object */
  49363. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  49364. ctx), WOLFSSL_SUCCESS);
  49365. /* compare points as they should be the same */
  49366. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  49367. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  49368. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  49369. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  49370. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  49371. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  49372. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  49373. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  49374. /* Test copying */
  49375. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  49376. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  49377. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  49378. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  49379. /* Test inverting */
  49380. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  49381. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  49382. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  49383. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  49384. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  49385. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  49386. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49387. {
  49388. EC_POINT* orig_point = NULL;
  49389. ExpectNotNull(orig_point = EC_POINT_new(group));
  49390. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  49391. 1);
  49392. /* new_point should be set_point inverted so adding it will revert
  49393. * the point back to set_point */
  49394. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  49395. NULL), 1);
  49396. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  49397. EC_POINT_free(orig_point);
  49398. }
  49399. #endif
  49400. /* Test getting affine converts from projective. */
  49401. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  49402. /* Force non-affine coordinates */
  49403. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49404. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  49405. if (new_point != NULL) {
  49406. new_point->inSet = 0;
  49407. }
  49408. /* extract the coordinates from point */
  49409. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49410. ctx), WOLFSSL_SUCCESS);
  49411. /* check if point ordinates have changed. */
  49412. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  49413. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  49414. /* Test check for infinity */
  49415. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  49416. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  49417. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  49418. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  49419. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  49420. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  49421. #else
  49422. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  49423. #endif
  49424. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  49425. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  49426. /* check bn2hex */
  49427. hexStr = BN_bn2hex(k);
  49428. ExpectStrEQ(hexStr, kTest);
  49429. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49430. BN_print_fp(stderr, k);
  49431. fprintf(stderr, "\n");
  49432. #endif
  49433. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49434. hexStr = BN_bn2hex(Gx);
  49435. ExpectStrEQ(hexStr, kGx);
  49436. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49437. BN_print_fp(stderr, Gx);
  49438. fprintf(stderr, "\n");
  49439. #endif
  49440. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49441. hexStr = BN_bn2hex(Gy);
  49442. ExpectStrEQ(hexStr, kGy);
  49443. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49444. BN_print_fp(stderr, Gy);
  49445. fprintf(stderr, "\n");
  49446. #endif
  49447. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49448. #ifndef HAVE_SELFTEST
  49449. /* Test point to hex */
  49450. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49451. ctx));
  49452. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49453. ctx));
  49454. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49455. ctx));
  49456. #ifndef HAVE_ECC_BRAINPOOL
  49457. /* Group not supported in wolfCrypt. */
  49458. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49459. ctx));
  49460. #endif
  49461. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  49462. ExpectNotNull(hexStr);
  49463. ExpectStrEQ(hexStr, uncompG);
  49464. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49465. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  49466. ExpectNotNull(hexStr);
  49467. ExpectStrEQ(hexStr, compG);
  49468. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49469. /* Test point to oct */
  49470. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49471. NULL, 0, ctx), 0);
  49472. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49473. NULL, 0, ctx), 0);
  49474. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49475. NULL, 0, ctx), 0);
  49476. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49477. NULL, 0, ctx);
  49478. ExpectIntEQ(bin_len, sizeof(binUncompG));
  49479. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49480. DYNAMIC_TYPE_ECC));
  49481. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49482. buf, bin_len, ctx), bin_len);
  49483. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49484. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49485. /* Infinity (x=0, y=0) encodes as '0x00'. */
  49486. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49487. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  49488. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49489. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  49490. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49491. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  49492. ExpectIntEQ(bufInf[0], 0);
  49493. wolfSSL_EC_POINT_dump(NULL, NULL);
  49494. /* Test point i2d */
  49495. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  49496. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  49497. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  49498. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  49499. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  49500. ExpectIntEQ(blen, sizeof(binUncompG));
  49501. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  49502. blen -= 1;
  49503. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  49504. blen += 1;
  49505. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  49506. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49507. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49508. #ifdef HAVE_COMP_KEY
  49509. /* Test point to oct compressed */
  49510. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  49511. 0, ctx);
  49512. ExpectIntEQ(bin_len, sizeof(binCompG));
  49513. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49514. DYNAMIC_TYPE_ECC));
  49515. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  49516. bin_len, ctx), bin_len);
  49517. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  49518. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49519. #endif
  49520. /* Test point BN */
  49521. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  49522. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49523. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  49524. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49525. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  49526. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49527. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  49528. /* Test oct to point */
  49529. ExpectNotNull(tmp = EC_POINT_new(group));
  49530. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  49531. ctx), 0);
  49532. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  49533. ctx), 0);
  49534. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  49535. ctx), 0);
  49536. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  49537. sizeof(binUncompGBad), ctx), 0);
  49538. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  49539. ctx), 1);
  49540. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49541. EC_POINT_free(tmp);
  49542. tmp = NULL;
  49543. /* Test setting BN ordinates. */
  49544. ExpectNotNull(tmp = EC_POINT_new(group));
  49545. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49546. NULL, ctx), 0);
  49547. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  49548. NULL, ctx), 0);
  49549. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  49550. NULL, ctx), 0);
  49551. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  49552. NULL, ctx), 0);
  49553. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49554. Gy, ctx), 0);
  49555. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  49556. ctx), 0);
  49557. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  49558. ctx), 0);
  49559. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  49560. Gy, ctx), 0);
  49561. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  49562. NULL, ctx), 0);
  49563. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  49564. ctx), 1);
  49565. EC_POINT_free(tmp);
  49566. tmp = NULL;
  49567. /* Test point d2i */
  49568. ExpectNotNull(tmp = EC_POINT_new(group));
  49569. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  49570. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  49571. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  49572. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  49573. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  49574. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  49575. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  49576. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  49577. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  49578. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49579. EC_POINT_free(tmp);
  49580. tmp = NULL;
  49581. #ifdef HAVE_COMP_KEY
  49582. /* Test oct compressed to point */
  49583. ExpectNotNull(tmp = EC_POINT_new(group));
  49584. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  49585. 1);
  49586. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49587. EC_POINT_free(tmp);
  49588. tmp = NULL;
  49589. /* Test point d2i - compressed */
  49590. ExpectNotNull(tmp = EC_POINT_new(group));
  49591. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  49592. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49593. EC_POINT_free(tmp);
  49594. tmp = NULL;
  49595. #endif
  49596. #endif
  49597. /* test BN_mod_add */
  49598. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49599. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  49600. 1);
  49601. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  49602. /* cleanup */
  49603. BN_free(X);
  49604. BN_free(Y);
  49605. BN_free(k);
  49606. BN_free(set_point_bn);
  49607. EC_POINT_free(infinity);
  49608. EC_POINT_free(new_point);
  49609. EC_POINT_free(set_point);
  49610. EC_POINT_clear_free(Gxy);
  49611. #ifndef HAVE_ECC_BRAINPOOL
  49612. EC_GROUP_free(group2);
  49613. #endif
  49614. EC_GROUP_free(group);
  49615. BN_CTX_free(ctx);
  49616. #endif
  49617. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  49618. return EXPECT_RESULT();
  49619. }
  49620. static int test_wolfSSL_SPAKE(void)
  49621. {
  49622. EXPECT_DECLS;
  49623. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  49624. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  49625. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49626. BIGNUM* x = NULL; /* kdc priv */
  49627. BIGNUM* y = NULL; /* client priv */
  49628. BIGNUM* w = NULL; /* shared value */
  49629. byte M_bytes[] = {
  49630. /* uncompressed */
  49631. 0x04,
  49632. /* x */
  49633. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  49634. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  49635. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  49636. /* y */
  49637. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  49638. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  49639. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  49640. };
  49641. EC_POINT* M = NULL; /* shared value */
  49642. byte N_bytes[] = {
  49643. /* uncompressed */
  49644. 0x04,
  49645. /* x */
  49646. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  49647. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  49648. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  49649. /* y */
  49650. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  49651. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  49652. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  49653. };
  49654. EC_POINT* N = NULL; /* shared value */
  49655. EC_POINT* T = NULL; /* kdc pub */
  49656. EC_POINT* tmp1 = NULL; /* kdc pub */
  49657. EC_POINT* tmp2 = NULL; /* kdc pub */
  49658. EC_POINT* S = NULL; /* client pub */
  49659. EC_POINT* client_secret = NULL;
  49660. EC_POINT* kdc_secret = NULL;
  49661. EC_GROUP* group = NULL;
  49662. BN_CTX* bn_ctx = NULL;
  49663. /* Values taken from a test run of Kerberos 5 */
  49664. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49665. ExpectNotNull(bn_ctx = BN_CTX_new());
  49666. ExpectNotNull(M = EC_POINT_new(group));
  49667. ExpectNotNull(N = EC_POINT_new(group));
  49668. ExpectNotNull(T = EC_POINT_new(group));
  49669. ExpectNotNull(tmp1 = EC_POINT_new(group));
  49670. ExpectNotNull(tmp2 = EC_POINT_new(group));
  49671. ExpectNotNull(S = EC_POINT_new(group));
  49672. ExpectNotNull(client_secret = EC_POINT_new(group));
  49673. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  49674. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  49675. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  49676. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  49677. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  49678. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  49679. "506AB395478F0AAB647752CF117B36250"), 1);
  49680. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  49681. 1);
  49682. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  49683. 1);
  49684. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  49685. /* kdc */
  49686. /* T=x*P+w*M */
  49687. /* All in one function call */
  49688. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  49689. /* Spread into separate calls */
  49690. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  49691. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  49692. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  49693. 1);
  49694. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  49695. /* client */
  49696. /* S=y*P+w*N */
  49697. /* All in one function call */
  49698. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  49699. /* Spread into separate calls */
  49700. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  49701. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  49702. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  49703. 1);
  49704. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  49705. /* K=y*(T-w*M) */
  49706. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  49707. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  49708. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  49709. 1);
  49710. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  49711. bn_ctx), 1);
  49712. /* kdc */
  49713. /* K=x*(S-w*N) */
  49714. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  49715. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  49716. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  49717. 1);
  49718. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  49719. 1);
  49720. /* kdc_secret == client_secret */
  49721. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  49722. BN_free(x);
  49723. BN_free(y);
  49724. BN_free(w);
  49725. EC_POINT_free(M);
  49726. EC_POINT_free(N);
  49727. EC_POINT_free(T);
  49728. EC_POINT_free(tmp1);
  49729. EC_POINT_free(tmp2);
  49730. EC_POINT_free(S);
  49731. EC_POINT_free(client_secret);
  49732. EC_POINT_free(kdc_secret);
  49733. EC_GROUP_free(group);
  49734. BN_CTX_free(bn_ctx);
  49735. #endif
  49736. return EXPECT_RESULT();
  49737. }
  49738. static int test_wolfSSL_EC_KEY_generate(void)
  49739. {
  49740. EXPECT_DECLS;
  49741. #ifdef OPENSSL_EXTRA
  49742. WOLFSSL_EC_KEY* key = NULL;
  49743. #ifndef HAVE_ECC_BRAINPOOL
  49744. WOLFSSL_EC_GROUP* group = NULL;
  49745. #endif
  49746. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49747. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  49748. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  49749. wolfSSL_EC_KEY_free(key);
  49750. key = NULL;
  49751. #ifndef HAVE_ECC_BRAINPOOL
  49752. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  49753. NID_brainpoolP256r1));
  49754. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  49755. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  49756. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  49757. wolfSSL_EC_KEY_free(key);
  49758. wolfSSL_EC_GROUP_free(group);
  49759. #endif
  49760. #endif
  49761. return EXPECT_RESULT();
  49762. }
  49763. static int test_EC_i2d(void)
  49764. {
  49765. EXPECT_DECLS;
  49766. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  49767. EC_KEY *key = NULL;
  49768. EC_KEY *copy = NULL;
  49769. int len = 0;
  49770. unsigned char *buf = NULL;
  49771. unsigned char *p = NULL;
  49772. const unsigned char *tmp = NULL;
  49773. const unsigned char octBad[] = {
  49774. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49775. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49776. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49777. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49778. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49779. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49780. };
  49781. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49782. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  49783. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  49784. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49785. DYNAMIC_TYPE_TMP_BUFFER));
  49786. p = buf;
  49787. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  49788. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  49789. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  49790. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  49791. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49792. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  49793. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  49794. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  49795. tmp = buf;
  49796. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  49797. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  49798. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49799. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  49800. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  49801. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  49802. tmp = buf;
  49803. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  49804. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  49805. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  49806. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  49807. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  49808. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  49809. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  49810. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  49811. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  49812. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  49813. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  49814. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  49815. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  49816. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49817. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  49818. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49819. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  49820. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49821. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  49822. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49823. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  49824. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  49825. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49826. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49827. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49828. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49829. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49830. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49831. buf = NULL;
  49832. buf = NULL;
  49833. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  49834. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49835. DYNAMIC_TYPE_TMP_BUFFER));
  49836. p = buf;
  49837. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  49838. p = NULL;
  49839. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  49840. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49841. p = NULL;
  49842. /* Bad point is also an invalid private key. */
  49843. tmp = octBad;
  49844. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  49845. tmp = buf;
  49846. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  49847. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49848. buf = NULL;
  49849. buf = NULL;
  49850. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  49851. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49852. DYNAMIC_TYPE_TMP_BUFFER));
  49853. p = buf;
  49854. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49855. p = NULL;
  49856. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49857. tmp = buf;
  49858. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  49859. tmp = octBad;
  49860. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  49861. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  49862. ExpectIntEQ(EC_KEY_check_key(key), 1);
  49863. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  49864. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  49865. EC_KEY_free(key);
  49866. EC_KEY_free(copy);
  49867. #endif
  49868. return EXPECT_RESULT();
  49869. }
  49870. static int test_wolfSSL_EC_curve(void)
  49871. {
  49872. EXPECT_DECLS;
  49873. #if defined(OPENSSL_EXTRA)
  49874. int nid = NID_secp160k1;
  49875. const char* nid_name = NULL;
  49876. ExpectNull(EC_curve_nid2nist(NID_sha256));
  49877. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  49878. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49879. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  49880. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  49881. #endif
  49882. return EXPECT_RESULT();
  49883. }
  49884. static int test_wolfSSL_EC_KEY_dup(void)
  49885. {
  49886. EXPECT_DECLS;
  49887. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49888. WOLFSSL_EC_KEY* ecKey = NULL;
  49889. WOLFSSL_EC_KEY* dupKey = NULL;
  49890. ecc_key* srcKey = NULL;
  49891. ecc_key* destKey = NULL;
  49892. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49893. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49894. /* Valid cases */
  49895. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49896. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  49897. /* Compare pubkey */
  49898. if (ecKey != NULL) {
  49899. srcKey = (ecc_key*)ecKey->internal;
  49900. }
  49901. if (dupKey != NULL) {
  49902. destKey = (ecc_key*)dupKey->internal;
  49903. }
  49904. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  49905. /* compare EC_GROUP */
  49906. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  49907. /* compare EC_POINT */
  49908. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  49909. dupKey->pub_key, NULL), MP_EQ);
  49910. /* compare BIGNUM */
  49911. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  49912. wolfSSL_EC_KEY_free(dupKey);
  49913. dupKey = NULL;
  49914. /* Invalid cases */
  49915. /* NULL key */
  49916. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  49917. /* NULL ecc_key */
  49918. if (ecKey != NULL) {
  49919. wc_ecc_free((ecc_key*)ecKey->internal);
  49920. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  49921. ecKey->internal = NULL; /* Set ecc_key to NULL */
  49922. }
  49923. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49924. wolfSSL_EC_KEY_free(ecKey);
  49925. ecKey = NULL;
  49926. wolfSSL_EC_KEY_free(dupKey);
  49927. dupKey = NULL;
  49928. /* NULL Group */
  49929. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49930. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49931. if (ecKey != NULL) {
  49932. wolfSSL_EC_GROUP_free(ecKey->group);
  49933. ecKey->group = NULL; /* Set group to NULL */
  49934. }
  49935. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49936. wolfSSL_EC_KEY_free(ecKey);
  49937. ecKey = NULL;
  49938. wolfSSL_EC_KEY_free(dupKey);
  49939. dupKey = NULL;
  49940. /* NULL public key */
  49941. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49942. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49943. if (ecKey != NULL) {
  49944. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  49945. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  49946. }
  49947. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49948. if (ecKey != NULL) {
  49949. wolfSSL_EC_POINT_free(ecKey->pub_key);
  49950. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  49951. }
  49952. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49953. wolfSSL_EC_KEY_free(ecKey);
  49954. ecKey = NULL;
  49955. wolfSSL_EC_KEY_free(dupKey);
  49956. dupKey = NULL;
  49957. /* NULL private key */
  49958. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49959. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49960. if (ecKey != NULL) {
  49961. wolfSSL_BN_free(ecKey->priv_key);
  49962. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  49963. }
  49964. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49965. wolfSSL_EC_KEY_free(ecKey);
  49966. ecKey = NULL;
  49967. wolfSSL_EC_KEY_free(dupKey);
  49968. dupKey = NULL;
  49969. /* Test EC_KEY_up_ref */
  49970. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49971. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  49972. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  49973. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  49974. /* reference count doesn't follow duplicate */
  49975. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49976. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  49977. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  49978. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  49979. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  49980. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  49981. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  49982. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  49983. #endif
  49984. return EXPECT_RESULT();
  49985. }
  49986. static int test_wolfSSL_EC_KEY_set_group(void)
  49987. {
  49988. EXPECT_DECLS;
  49989. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  49990. defined(OPENSSL_EXTRA)
  49991. EC_KEY *key = NULL;
  49992. EC_GROUP *group = NULL;
  49993. const EC_GROUP *group2 = NULL;
  49994. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49995. ExpectNotNull(key = EC_KEY_new());
  49996. ExpectNull(EC_KEY_get0_group(NULL));
  49997. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  49998. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  49999. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  50000. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  50001. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  50002. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  50003. EC_GROUP_free(group);
  50004. EC_KEY_free(key);
  50005. #endif
  50006. return EXPECT_RESULT();
  50007. }
  50008. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  50009. {
  50010. EXPECT_DECLS;
  50011. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50012. BIO* bio = NULL;
  50013. EC_KEY* key = NULL;
  50014. /* Error condition: NULL key. */
  50015. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  50016. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  50017. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  50018. /* Conversion form defaults to uncompressed. */
  50019. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  50020. #ifdef HAVE_COMP_KEY
  50021. /* Explicitly set to compressed. */
  50022. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  50023. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  50024. #else
  50025. /* Will still work just won't change anything. */
  50026. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  50027. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  50028. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  50029. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  50030. #endif
  50031. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  50032. BIO_free(bio);
  50033. EC_KEY_free(key);
  50034. #endif
  50035. return EXPECT_RESULT();
  50036. }
  50037. static int test_wolfSSL_EC_KEY_private_key(void)
  50038. {
  50039. EXPECT_DECLS;
  50040. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50041. WOLFSSL_EC_KEY* key = NULL;
  50042. WOLFSSL_BIGNUM* priv = NULL;
  50043. WOLFSSL_BIGNUM* priv2 = NULL;
  50044. WOLFSSL_BIGNUM* bn;
  50045. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50046. ExpectNotNull(priv = wolfSSL_BN_new());
  50047. ExpectNotNull(priv2 = wolfSSL_BN_new());
  50048. ExpectIntNE(BN_set_word(priv, 2), 0);
  50049. ExpectIntNE(BN_set_word(priv2, 2), 0);
  50050. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  50051. /* No private key set. */
  50052. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  50053. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  50054. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  50055. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  50056. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  50057. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  50058. ExpectPtrNE(bn, priv);
  50059. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  50060. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  50061. ExpectPtrNE(bn, priv2);
  50062. wolfSSL_BN_free(priv2);
  50063. wolfSSL_BN_free(priv);
  50064. wolfSSL_EC_KEY_free(key);
  50065. #endif
  50066. return EXPECT_RESULT();
  50067. }
  50068. static int test_wolfSSL_EC_KEY_public_key(void)
  50069. {
  50070. EXPECT_DECLS;
  50071. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  50072. WOLFSSL_EC_KEY* key = NULL;
  50073. WOLFSSL_EC_POINT* pub = NULL;
  50074. WOLFSSL_EC_POINT* point = NULL;
  50075. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50076. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  50077. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  50078. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  50079. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  50080. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  50081. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  50082. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  50083. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  50084. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  50085. ExpectPtrEq(point, pub);
  50086. wolfSSL_EC_KEY_free(key);
  50087. #endif
  50088. return EXPECT_RESULT();
  50089. }
  50090. static int test_wolfSSL_EC_KEY_print_fp(void)
  50091. {
  50092. EXPECT_DECLS;
  50093. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  50094. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  50095. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  50096. !defined(NO_STDIO_FILESYSTEM)
  50097. EC_KEY* key = NULL;
  50098. /* Bad file pointer. */
  50099. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  50100. /* NULL key. */
  50101. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  50102. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  50103. /* Negative indent. */
  50104. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  50105. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  50106. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  50107. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  50108. wolfSSL_EC_KEY_free(key);
  50109. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  50110. NID_X9_62_prime256v1)));
  50111. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  50112. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  50113. wolfSSL_EC_KEY_free(key);
  50114. #endif
  50115. return EXPECT_RESULT();
  50116. }
  50117. static int test_wolfSSL_EC_get_builtin_curves(void)
  50118. {
  50119. EXPECT_DECLS;
  50120. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  50121. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  50122. EC_builtin_curve* curves = NULL;
  50123. size_t crv_len = 0;
  50124. size_t i = 0;
  50125. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  50126. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  50127. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  50128. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  50129. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  50130. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  50131. if (curves[i].comment != NULL) {
  50132. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  50133. }
  50134. }
  50135. if (crv_len > 1) {
  50136. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  50137. }
  50138. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50139. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  50140. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  50141. return EXPECT_RESULT();
  50142. }
  50143. static int test_wolfSSL_ECDSA_SIG(void)
  50144. {
  50145. EXPECT_DECLS;
  50146. #ifdef OPENSSL_EXTRA
  50147. WOLFSSL_ECDSA_SIG* sig = NULL;
  50148. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  50149. WOLFSSL_BIGNUM* r = NULL;
  50150. WOLFSSL_BIGNUM* s = NULL;
  50151. const WOLFSSL_BIGNUM* r2 = NULL;
  50152. const WOLFSSL_BIGNUM* s2 = NULL;
  50153. const unsigned char* cp = NULL;
  50154. unsigned char* p = NULL;
  50155. unsigned char outSig[8];
  50156. unsigned char sigData[8] =
  50157. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  50158. unsigned char sigDataBad[8] =
  50159. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  50160. wolfSSL_ECDSA_SIG_free(NULL);
  50161. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  50162. ExpectNotNull(r = wolfSSL_BN_new());
  50163. ExpectNotNull(s = wolfSSL_BN_new());
  50164. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  50165. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  50166. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  50167. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  50168. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  50169. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  50170. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  50171. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  50172. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  50173. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  50174. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  50175. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  50176. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  50177. r2 = NULL;
  50178. s2 = NULL;
  50179. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  50180. ExpectNull(r2);
  50181. ExpectNull(s2);
  50182. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  50183. if (EXPECT_FAIL()) {
  50184. wolfSSL_BN_free(r);
  50185. wolfSSL_BN_free(s);
  50186. }
  50187. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  50188. ExpectPtrEq(r2, r);
  50189. ExpectPtrEq(s2, s);
  50190. r2 = NULL;
  50191. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  50192. ExpectPtrEq(r2, r);
  50193. s2 = NULL;
  50194. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  50195. ExpectPtrEq(s2, s);
  50196. /* r and s are freed when sig is freed. */
  50197. wolfSSL_ECDSA_SIG_free(sig);
  50198. sig = NULL;
  50199. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  50200. cp = sigDataBad;
  50201. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  50202. cp = sigData;
  50203. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  50204. ExpectIntEQ((cp == sigData + 8), 1);
  50205. cp = sigData;
  50206. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  50207. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  50208. ExpectIntEQ((sig == sig2), 1);
  50209. cp = outSig;
  50210. p = outSig;
  50211. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  50212. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  50213. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  50214. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  50215. ExpectIntEQ((p == outSig + 8), 1);
  50216. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  50217. wolfSSL_ECDSA_SIG_free(sig);
  50218. #endif
  50219. return EXPECT_RESULT();
  50220. }
  50221. static int test_ECDSA_size_sign(void)
  50222. {
  50223. EXPECT_DECLS;
  50224. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  50225. EC_KEY* key = NULL;
  50226. ECDSA_SIG* ecdsaSig = NULL;
  50227. int id;
  50228. byte hash[WC_MAX_DIGEST_SIZE];
  50229. byte hash2[WC_MAX_DIGEST_SIZE];
  50230. byte sig[ECC_MAX_SIG_SIZE];
  50231. unsigned int sigSz = sizeof(sig);
  50232. XMEMSET(hash, 123, sizeof(hash));
  50233. XMEMSET(hash2, 234, sizeof(hash2));
  50234. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  50235. ExpectIntEQ(id, ECC_SECP256R1);
  50236. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50237. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  50238. ExpectIntGE(ECDSA_size(NULL), 0);
  50239. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  50240. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  50241. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  50242. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  50243. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  50244. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  50245. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  50246. ExpectIntGE(ECDSA_size(key), sigSz);
  50247. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  50248. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  50249. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  50250. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  50251. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  50252. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  50253. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  50254. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  50255. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  50256. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  50257. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  50258. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  50259. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  50260. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  50261. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  50262. ECDSA_SIG_free(ecdsaSig);
  50263. EC_KEY_free(key);
  50264. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  50265. return EXPECT_RESULT();
  50266. }
  50267. static int test_ECDH_compute_key(void)
  50268. {
  50269. EXPECT_DECLS;
  50270. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  50271. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  50272. EC_KEY* key1 = NULL;
  50273. EC_KEY* key2 = NULL;
  50274. EC_POINT* pub1 = NULL;
  50275. EC_POINT* pub2 = NULL;
  50276. byte secret1[32];
  50277. byte secret2[32];
  50278. int i;
  50279. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50280. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  50281. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  50282. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50283. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  50284. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  50285. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  50286. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  50287. 0);
  50288. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  50289. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  50290. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  50291. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  50292. 0);
  50293. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  50294. 0);
  50295. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  50296. NULL), 0);
  50297. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  50298. sizeof(secret1));
  50299. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  50300. sizeof(secret2));
  50301. for (i = 0; i < (int)sizeof(secret1); i++) {
  50302. ExpectIntEQ(secret1[i], secret2[i]);
  50303. }
  50304. EC_KEY_free(key2);
  50305. EC_KEY_free(key1);
  50306. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  50307. * !WOLF_CRYPTO_CB_ONLY_ECC */
  50308. return EXPECT_RESULT();
  50309. }
  50310. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  50311. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  50312. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  50313. !defined(NO_ASN_TIME)
  50314. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  50315. int expectedDerSz)
  50316. {
  50317. EXPECT_DECLS;
  50318. X509* x509 = NULL;
  50319. BIGNUM* serial_number = NULL;
  50320. X509_NAME* name = NULL;
  50321. time_t epoch_off = 0;
  50322. ASN1_INTEGER* asn1_serial_number;
  50323. long not_before, not_after;
  50324. int derSz;
  50325. ExpectNotNull(x509 = X509_new());
  50326. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  50327. ExpectNotNull(serial_number = BN_new());
  50328. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  50329. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  50330. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  50331. /* version 3 */
  50332. ExpectIntNE(X509_set_version(x509, 2L), 0);
  50333. ExpectNotNull(name = X509_NAME_new());
  50334. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  50335. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  50336. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  50337. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  50338. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  50339. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  50340. not_before = (long)wc_Time(NULL);
  50341. not_after = not_before + (365 * 24 * 60 * 60);
  50342. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  50343. &epoch_off));
  50344. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  50345. &epoch_off));
  50346. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  50347. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  50348. ExpectIntGE(derSz, expectedDerSz);
  50349. BN_free(serial_number);
  50350. X509_NAME_free(name);
  50351. X509_free(x509);
  50352. return EXPECT_RESULT();
  50353. }
  50354. #endif
  50355. static int test_openssl_generate_key_and_cert(void)
  50356. {
  50357. EXPECT_DECLS;
  50358. #if defined(OPENSSL_EXTRA)
  50359. EVP_PKEY* pkey = NULL;
  50360. #ifdef HAVE_ECC
  50361. EC_KEY* ec_key = NULL;
  50362. #endif
  50363. #if !defined(NO_RSA)
  50364. int expectedDerSz;
  50365. int key_length = 2048;
  50366. BIGNUM* exponent = NULL;
  50367. RSA* rsa = NULL;
  50368. ExpectNotNull(pkey = EVP_PKEY_new());
  50369. ExpectNotNull(exponent = BN_new());
  50370. ExpectNotNull(rsa = RSA_new());
  50371. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  50372. #ifndef WOLFSSL_KEY_GEN
  50373. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  50374. #if defined(USE_CERT_BUFFERS_1024)
  50375. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  50376. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  50377. key_length = 1024;
  50378. #elif defined(USE_CERT_BUFFERS_2048)
  50379. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  50380. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  50381. #else
  50382. RSA_free(rsa);
  50383. rsa = NULL;
  50384. #endif
  50385. #else
  50386. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  50387. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  50388. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  50389. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  50390. #endif
  50391. if (rsa) {
  50392. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  50393. if (EXPECT_FAIL()) {
  50394. RSA_free(rsa);
  50395. }
  50396. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  50397. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  50398. expectedDerSz = 743;
  50399. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  50400. expectedDerSz), TEST_SUCCESS);
  50401. #endif
  50402. }
  50403. (void)expectedDerSz;
  50404. EVP_PKEY_free(pkey);
  50405. pkey = NULL;
  50406. BN_free(exponent);
  50407. #endif /* !NO_RSA */
  50408. #ifdef HAVE_ECC
  50409. ExpectNotNull(pkey = EVP_PKEY_new());
  50410. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50411. #ifndef NO_WOLFSSL_STUB
  50412. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  50413. #endif
  50414. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  50415. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  50416. if (EXPECT_FAIL()) {
  50417. EC_KEY_free(ec_key);
  50418. }
  50419. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  50420. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  50421. expectedDerSz = 344;
  50422. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  50423. TEST_SUCCESS);
  50424. #endif
  50425. EVP_PKEY_free(pkey);
  50426. #endif /* HAVE_ECC */
  50427. (void)pkey;
  50428. #endif /* OPENSSL_EXTRA */
  50429. return EXPECT_RESULT();
  50430. }
  50431. static int test_stubs_are_stubs(void)
  50432. {
  50433. EXPECT_DECLS;
  50434. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  50435. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  50436. WOLFSSL_CTX* ctx = NULL;
  50437. WOLFSSL_CTX* ctxN = NULL;
  50438. #ifndef NO_WOLFSSL_CLIENT
  50439. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50440. #elif !defined(NO_WOLFSSL_SERVER)
  50441. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50442. #endif
  50443. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  50444. ExpectIntEQ((int) x(z), 0)
  50445. /* test logic, all stubs return same result regardless of ctx being NULL
  50446. * as there are no sanity checks, it's just a stub! If at some
  50447. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  50448. * if invalid inputs are supplied. Test calling both
  50449. * with and without valid inputs, if a stub functionality remains unchanged.
  50450. */
  50451. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  50452. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  50453. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  50454. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  50455. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  50456. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  50457. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  50458. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  50459. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  50460. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  50461. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  50462. wolfSSL_CTX_free(ctx);
  50463. ctx = NULL;
  50464. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  50465. * !NO_WOLFSSL_SERVER) */
  50466. return EXPECT_RESULT();
  50467. }
  50468. static int test_CONF_modules_xxx(void)
  50469. {
  50470. int res = TEST_SKIPPED;
  50471. #if defined(OPENSSL_EXTRA)
  50472. CONF_modules_free();
  50473. CONF_modules_unload(0);
  50474. CONF_modules_unload(1);
  50475. CONF_modules_unload(-1);
  50476. res = TEST_SUCCESS;
  50477. #endif /* OPENSSL_EXTRA */
  50478. return res;
  50479. }
  50480. static int test_CRYPTO_set_dynlock_xxx(void)
  50481. {
  50482. int res = TEST_SKIPPED;
  50483. #if defined(OPENSSL_EXTRA)
  50484. CRYPTO_set_dynlock_create_callback(
  50485. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  50486. CRYPTO_set_dynlock_create_callback(
  50487. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  50488. CRYPTO_set_dynlock_destroy_callback(
  50489. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  50490. CRYPTO_set_dynlock_destroy_callback(
  50491. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  50492. CRYPTO_set_dynlock_lock_callback(
  50493. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  50494. CRYPTO_set_dynlock_lock_callback(
  50495. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  50496. res = TEST_SUCCESS;
  50497. #endif /* OPENSSL_EXTRA */
  50498. return res;
  50499. }
  50500. static int test_CRYPTO_THREADID_xxx(void)
  50501. {
  50502. EXPECT_DECLS;
  50503. #if defined(OPENSSL_EXTRA)
  50504. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  50505. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  50506. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  50507. #endif /* OPENSSL_EXTRA */
  50508. return EXPECT_RESULT();
  50509. }
  50510. static int test_ENGINE_cleanup(void)
  50511. {
  50512. int res = TEST_SKIPPED;
  50513. #if defined(OPENSSL_EXTRA)
  50514. ENGINE_cleanup();
  50515. res = TEST_SUCCESS;
  50516. #endif /* OPENSSL_EXTRA */
  50517. return res;
  50518. }
  50519. static int test_wolfSSL_CTX_LoadCRL(void)
  50520. {
  50521. EXPECT_DECLS;
  50522. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  50523. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  50524. WOLFSSL_CTX* ctx = NULL;
  50525. WOLFSSL* ssl = NULL;
  50526. const char* badPath = "dummypath";
  50527. const char* validPath = "./certs/crl";
  50528. const char* validFilePath = "./certs/crl/cliCrl.pem";
  50529. const char* issuerCert = "./certs/client-cert.pem";
  50530. int derType = WOLFSSL_FILETYPE_ASN1;
  50531. int pemType = WOLFSSL_FILETYPE_PEM;
  50532. #ifdef HAVE_CRL_MONITOR
  50533. int monitor = WOLFSSL_CRL_MONITOR;
  50534. #else
  50535. int monitor = 0;
  50536. #endif
  50537. WOLFSSL_CERT_MANAGER* cm = NULL;
  50538. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50539. BAD_FUNC_ARG)
  50540. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50541. NOT_COMPILED_IN)
  50542. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50543. WOLFSSL_SUCCESS)
  50544. #ifndef NO_WOLFSSL_CLIENT
  50545. #define NEW_CTX(ctx) AssertNotNull( \
  50546. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  50547. #elif !defined(NO_WOLFSSL_SERVER)
  50548. #define NEW_CTX(ctx) AssertNotNull( \
  50549. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  50550. #else
  50551. #define NEW_CTX(ctx) return
  50552. #endif
  50553. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50554. NEW_CTX(ctx);
  50555. #ifndef HAVE_CRL_MONITOR
  50556. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  50557. wolfSSL_CTX_free(ctx);
  50558. NEW_CTX(ctx);
  50559. #endif
  50560. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50561. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  50562. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  50563. wolfSSL_CTX_free(ctx);
  50564. ctx = NULL;
  50565. NEW_CTX(ctx);
  50566. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50567. WOLFSSL_SUCCESS);
  50568. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  50569. wolfSSL_CTX_free(ctx);
  50570. ctx = NULL;
  50571. NEW_CTX(ctx);
  50572. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50573. WOLFSSL_SUCCESS);
  50574. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50575. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  50576. wolfSSL_free(ssl);
  50577. ssl = NULL;
  50578. wolfSSL_CTX_free(ctx);
  50579. ctx = NULL;
  50580. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  50581. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  50582. WOLFSSL_SUCCESS);
  50583. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  50584. WOLFSSL_SUCCESS);
  50585. wolfSSL_CertManagerFree(cm);
  50586. #endif
  50587. return EXPECT_RESULT();
  50588. }
  50589. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  50590. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  50591. {
  50592. EXPECT_DECLS;
  50593. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  50594. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  50595. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50596. return EXPECT_RESULT();
  50597. }
  50598. #endif
  50599. static int test_multiple_crls_same_issuer(void)
  50600. {
  50601. EXPECT_DECLS;
  50602. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  50603. test_ssl_cbf client_cbs, server_cbs;
  50604. struct {
  50605. const char* server_cert;
  50606. const char* server_key;
  50607. } test_params[] = {
  50608. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  50609. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  50610. };
  50611. size_t i;
  50612. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  50613. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50614. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50615. server_cbs.certPemFile = test_params[i].server_cert;
  50616. server_cbs.keyPemFile = test_params[i].server_key;
  50617. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  50618. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  50619. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  50620. &server_cbs, NULL), TEST_FAIL);
  50621. }
  50622. #endif
  50623. return EXPECT_RESULT();
  50624. }
  50625. static int test_SetTmpEC_DHE_Sz(void)
  50626. {
  50627. EXPECT_DECLS;
  50628. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  50629. WOLFSSL_CTX *ctx = NULL;
  50630. WOLFSSL *ssl = NULL;
  50631. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50632. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  50633. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50634. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  50635. wolfSSL_free(ssl);
  50636. wolfSSL_CTX_free(ctx);
  50637. #endif
  50638. return EXPECT_RESULT();
  50639. }
  50640. static int test_wolfSSL_CTX_get0_privatekey(void)
  50641. {
  50642. EXPECT_DECLS;
  50643. #ifdef OPENSSL_ALL
  50644. WOLFSSL_CTX* ctx = NULL;
  50645. (void)ctx;
  50646. #ifndef NO_RSA
  50647. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50648. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50649. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50650. WOLFSSL_FILETYPE_PEM));
  50651. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50652. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50653. WOLFSSL_FILETYPE_PEM));
  50654. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  50655. wolfSSL_CTX_free(ctx);
  50656. ctx = NULL;
  50657. #endif
  50658. #ifdef HAVE_ECC
  50659. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50660. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50661. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  50662. WOLFSSL_FILETYPE_PEM));
  50663. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50664. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  50665. WOLFSSL_FILETYPE_PEM));
  50666. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  50667. wolfSSL_CTX_free(ctx);
  50668. #endif
  50669. #endif
  50670. return EXPECT_RESULT();
  50671. }
  50672. static int test_wolfSSL_dtls_set_mtu(void)
  50673. {
  50674. EXPECT_DECLS;
  50675. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  50676. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  50677. WOLFSSL_CTX* ctx = NULL;
  50678. WOLFSSL* ssl = NULL;
  50679. const char* testCertFile;
  50680. const char* testKeyFile;
  50681. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  50682. #ifndef NO_RSA
  50683. testCertFile = svrCertFile;
  50684. testKeyFile = svrKeyFile;
  50685. #elif defined(HAVE_ECC)
  50686. testCertFile = eccCertFile;
  50687. testKeyFile = eccKeyFile;
  50688. #endif
  50689. if (testCertFile != NULL && testKeyFile != NULL) {
  50690. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  50691. WOLFSSL_FILETYPE_PEM));
  50692. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50693. WOLFSSL_FILETYPE_PEM));
  50694. }
  50695. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50696. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50697. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50698. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  50699. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  50700. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  50701. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  50702. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  50703. wolfSSL_free(ssl);
  50704. wolfSSL_CTX_free(ctx);
  50705. #endif
  50706. return EXPECT_RESULT();
  50707. }
  50708. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50709. defined(WOLFSSL_DTLS)
  50710. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  50711. enum HandShakeType hsType, word16 length)
  50712. {
  50713. size_t idx = 0;
  50714. byte* l;
  50715. /* record layer */
  50716. /* handshake type */
  50717. out[idx++] = handshake;
  50718. /* protocol version */
  50719. out[idx++] = 0xfe;
  50720. out[idx++] = 0xfd; /* DTLS 1.2 */
  50721. /* epoch 0 */
  50722. XMEMSET(out + idx, 0, 2);
  50723. idx += 2;
  50724. /* sequence number */
  50725. XMEMSET(out + idx, 0, 6);
  50726. c32toa(seq, out + idx + 2);
  50727. idx += 6;
  50728. /* length in BE */
  50729. if (length)
  50730. c16toa(length, out + idx);
  50731. else
  50732. c16toa(outSz - idx - 2, out + idx);
  50733. idx += 2;
  50734. /* handshake layer */
  50735. /* handshake type */
  50736. out[idx++] = (byte)hsType;
  50737. /* length */
  50738. l = out + idx;
  50739. idx += 3;
  50740. /* message seq */
  50741. c16toa(0, out + idx);
  50742. idx += 2;
  50743. /* frag offset */
  50744. c32to24(0, out + idx);
  50745. idx += 3;
  50746. /* frag length */
  50747. c32to24((word32)outSz - (word32)idx - 3, l);
  50748. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  50749. idx += 3;
  50750. XMEMSET(out + idx, 0, outSz - idx);
  50751. }
  50752. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  50753. {
  50754. byte msg[] = "This is a msg for the client";
  50755. byte reply[40];
  50756. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50757. reply[sizeof(reply) - 1] = '\0';
  50758. fprintf(stderr, "Client message: %s\n", reply);
  50759. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50760. }
  50761. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  50762. {
  50763. byte ch[50];
  50764. int fd = wolfSSL_get_fd(ssl);
  50765. byte msg[] = "This is a msg for the server";
  50766. byte reply[40];
  50767. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  50768. /* Server should ignore this datagram */
  50769. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50770. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  50771. /* Server should ignore this datagram */
  50772. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50773. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50774. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50775. reply[sizeof(reply) - 1] = '\0';
  50776. fprintf(stderr, "Server response: %s\n", reply);
  50777. }
  50778. static int test_wolfSSL_dtls_plaintext(void)
  50779. {
  50780. callback_functions func_cb_client;
  50781. callback_functions func_cb_server;
  50782. size_t i;
  50783. struct test_params {
  50784. method_provider client_meth;
  50785. method_provider server_meth;
  50786. ssl_callback on_result_server;
  50787. ssl_callback on_result_client;
  50788. } params[] = {
  50789. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50790. test_wolfSSL_dtls_plaintext_server,
  50791. test_wolfSSL_dtls_plaintext_client},
  50792. };
  50793. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50794. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50795. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50796. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50797. func_cb_server.method = params[i].server_meth;
  50798. func_cb_client.method = params[i].client_meth;
  50799. func_cb_client.on_result = params[i].on_result_client;
  50800. func_cb_server.on_result = params[i].on_result_server;
  50801. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50802. if (!func_cb_client.return_code)
  50803. return TEST_FAIL;
  50804. if (!func_cb_server.return_code)
  50805. return TEST_FAIL;
  50806. }
  50807. return TEST_RES_CHECK(1);
  50808. }
  50809. #else
  50810. static int test_wolfSSL_dtls_plaintext(void) {
  50811. return TEST_SKIPPED;
  50812. }
  50813. #endif
  50814. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50815. defined(WOLFSSL_DTLS)
  50816. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  50817. {
  50818. byte b[1100]; /* buffer for the messages to send */
  50819. size_t idx = 0;
  50820. size_t seq_offset = 0;
  50821. size_t msg_offset = 0;
  50822. int i;
  50823. int fd = wolfSSL_get_fd(ssl);
  50824. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50825. word32 seq_number = 100; /* start high so server definitely reads this */
  50826. word16 msg_number = 50; /* start high so server has to buffer this */
  50827. AssertIntEQ(ret, 1);
  50828. /* Now let's start spamming the peer with fragments it needs to store */
  50829. XMEMSET(b, -1, sizeof(b));
  50830. /* record layer */
  50831. /* handshake type */
  50832. b[idx++] = 22;
  50833. /* protocol version */
  50834. b[idx++] = 0xfe;
  50835. b[idx++] = 0xfd; /* DTLS 1.2 */
  50836. /* epoch 0 */
  50837. XMEMSET(b + idx, 0, 2);
  50838. idx += 2;
  50839. /* sequence number */
  50840. XMEMSET(b + idx, 0, 6);
  50841. seq_offset = idx + 2; /* increment only the low 32 bits */
  50842. idx += 6;
  50843. /* static length in BE */
  50844. c16toa(42, b + idx);
  50845. idx += 2;
  50846. /* handshake layer */
  50847. /* cert type */
  50848. b[idx++] = 11;
  50849. /* length */
  50850. c32to24(1000, b + idx);
  50851. idx += 3;
  50852. /* message seq */
  50853. c16toa(0, b + idx);
  50854. msg_offset = idx;
  50855. idx += 2;
  50856. /* frag offset */
  50857. c32to24(500, b + idx);
  50858. idx += 3;
  50859. /* frag length */
  50860. c32to24(30, b + idx);
  50861. idx += 3;
  50862. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  50863. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  50864. seq_number++, msg_number++, i++) {
  50865. struct timespec delay;
  50866. XMEMSET(&delay, 0, sizeof(delay));
  50867. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50868. c32toa(seq_number, b + seq_offset);
  50869. c16toa(msg_number, b + msg_offset);
  50870. ret = (int)send(fd, b, 55, 0);
  50871. nanosleep(&delay, NULL);
  50872. }
  50873. }
  50874. #ifdef WOLFSSL_DTLS13
  50875. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  50876. {
  50877. const word16 sendCountMax = 100;
  50878. byte b[150]; /* buffer for the messages to send */
  50879. size_t idx = 0;
  50880. size_t msg_offset = 0;
  50881. int fd = wolfSSL_get_fd(ssl);
  50882. word16 msg_number = 10; /* start high so server has to buffer this */
  50883. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50884. AssertIntEQ(ret, 1);
  50885. /* Now let's start spamming the peer with fragments it needs to store */
  50886. XMEMSET(b, -1, sizeof(b));
  50887. /* handshake type */
  50888. b[idx++] = 11;
  50889. /* length */
  50890. c32to24(10000, b + idx);
  50891. idx += 3;
  50892. /* message_seq */
  50893. msg_offset = idx;
  50894. idx += 2;
  50895. /* fragment_offset */
  50896. c32to24(5000, b + idx);
  50897. idx += 3;
  50898. /* fragment_length */
  50899. c32to24(100, b + idx);
  50900. idx += 3;
  50901. /* fragment contents */
  50902. idx += 100;
  50903. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  50904. byte sendBuf[150];
  50905. int sendSz = sizeof(sendBuf);
  50906. struct timespec delay;
  50907. XMEMSET(&delay, 0, sizeof(delay));
  50908. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50909. c16toa(msg_number, b + msg_offset);
  50910. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  50911. (int)idx, handshake, 0, 0, 0);
  50912. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  50913. nanosleep(&delay, NULL);
  50914. }
  50915. }
  50916. #endif
  50917. static int test_wolfSSL_dtls_fragments(void)
  50918. {
  50919. EXPECT_DECLS;
  50920. callback_functions func_cb_client;
  50921. callback_functions func_cb_server;
  50922. size_t i;
  50923. struct test_params {
  50924. method_provider client_meth;
  50925. method_provider server_meth;
  50926. ssl_callback spammer;
  50927. } params[] = {
  50928. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50929. test_wolfSSL_dtls12_fragments_spammer},
  50930. #ifdef WOLFSSL_DTLS13
  50931. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  50932. test_wolfSSL_dtls13_fragments_spammer},
  50933. #endif
  50934. };
  50935. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50936. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50937. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50938. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50939. func_cb_server.method = params[i].server_meth;
  50940. func_cb_client.method = params[i].client_meth;
  50941. func_cb_client.ssl_ready = params[i].spammer;
  50942. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50943. ExpectFalse(func_cb_client.return_code);
  50944. ExpectFalse(func_cb_server.return_code);
  50945. /* The socket should be closed by the server resulting in a
  50946. * socket error, fatal error or reading a close notify alert */
  50947. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  50948. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  50949. func_cb_client.last_err != FATAL_ERROR) {
  50950. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  50951. }
  50952. /* Check the server returned an error indicating the msg buffer
  50953. * was full */
  50954. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  50955. if (EXPECT_FAIL())
  50956. break;
  50957. }
  50958. return EXPECT_RESULT();
  50959. }
  50960. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  50961. {
  50962. int fd, ret;
  50963. byte alert_msg[] = {
  50964. 0x15, /* alert type */
  50965. 0xfe, 0xfd, /* version */
  50966. 0x00, 0x00, /* epoch */
  50967. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  50968. 0x00, 0x02, /* length */
  50969. 0x02, /* level: fatal */
  50970. 0x46 /* protocol version */
  50971. };
  50972. fd = wolfSSL_get_fd(ssl);
  50973. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  50974. AssertIntGT(ret, 0);
  50975. }
  50976. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  50977. {
  50978. callback_functions client_cbs, server_cbs;
  50979. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50980. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50981. client_cbs.doUdp = server_cbs.doUdp = 1;
  50982. if (version12) {
  50983. client_cbs.method = wolfDTLSv1_2_client_method;
  50984. server_cbs.method = wolfDTLSv1_2_server_method;
  50985. }
  50986. else {
  50987. #ifdef WOLFSSL_DTLS13
  50988. client_cbs.method = wolfDTLSv1_3_client_method;
  50989. server_cbs.method = wolfDTLSv1_3_server_method;
  50990. #else
  50991. return TEST_SKIPPED;
  50992. #endif /* WOLFSSL_DTLS13 */
  50993. }
  50994. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  50995. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50996. if (!client_cbs.return_code)
  50997. return TEST_FAIL;
  50998. if (!server_cbs.return_code)
  50999. return TEST_FAIL;
  51000. return TEST_SUCCESS;
  51001. }
  51002. static int test_wolfSSL_ignore_alert_before_cookie(void)
  51003. {
  51004. int ret;
  51005. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  51006. if (ret != 0)
  51007. return ret;
  51008. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  51009. if (ret != 0)
  51010. return ret;
  51011. return 0;
  51012. }
  51013. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  51014. {
  51015. int ret;
  51016. int fd;
  51017. byte bad_msg[] = {
  51018. 0x17, /* app data */
  51019. 0xaa, 0xfd, /* bad version */
  51020. 0x00, 0x01, /* epoch 1 */
  51021. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  51022. 0x00, 0x26, /* length: 38 bytes */
  51023. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  51024. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  51025. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  51026. 0x03, 0x18, 0x72
  51027. };
  51028. fd = wolfSSL_get_fd(ssl);
  51029. AssertIntGE(fd, 0);
  51030. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  51031. AssertIntEQ(ret, sizeof(bad_msg));
  51032. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  51033. AssertIntEQ(ret, sizeof("badrecordtest"));
  51034. }
  51035. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  51036. {
  51037. byte buf[100];
  51038. int ret;
  51039. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  51040. AssertIntGT(ret, 0);
  51041. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  51042. }
  51043. static int _test_wolfSSL_dtls_bad_record(
  51044. method_provider client_method, method_provider server_method)
  51045. {
  51046. callback_functions client_cbs, server_cbs;
  51047. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  51048. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  51049. client_cbs.doUdp = server_cbs.doUdp = 1;
  51050. client_cbs.method = client_method;
  51051. server_cbs.method = server_method;
  51052. client_cbs.on_result = test_wolfSSL_send_bad_record;
  51053. server_cbs.on_result = test_wolfSSL_read_string;
  51054. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  51055. if (!client_cbs.return_code)
  51056. return TEST_FAIL;
  51057. if (!server_cbs.return_code)
  51058. return TEST_FAIL;
  51059. return TEST_SUCCESS;
  51060. }
  51061. static int test_wolfSSL_dtls_bad_record(void)
  51062. {
  51063. int ret;
  51064. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  51065. wolfDTLSv1_2_server_method);
  51066. #ifdef WOLFSSL_DTLS13
  51067. if (ret != TEST_SUCCESS)
  51068. return ret;
  51069. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  51070. wolfDTLSv1_3_server_method);
  51071. #else
  51072. return ret;
  51073. #endif /* WOLFSSL_DTLS13 */
  51074. }
  51075. #else
  51076. static int test_wolfSSL_dtls_fragments(void) {
  51077. return TEST_SKIPPED;
  51078. }
  51079. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  51080. return TEST_SKIPPED;
  51081. }
  51082. static int test_wolfSSL_dtls_bad_record(void) {
  51083. return TEST_SKIPPED;
  51084. }
  51085. #endif
  51086. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  51087. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51088. defined(HAVE_IO_TESTS_DEPENDENCIES)
  51089. static byte test_AEAD_fail_decryption = 0;
  51090. static byte test_AEAD_seq_num = 0;
  51091. static byte test_AEAD_done = 0;
  51092. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  51093. {
  51094. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  51095. if (ret > 0) {
  51096. if (test_AEAD_fail_decryption) {
  51097. /* Modify the packet to trigger a decryption failure */
  51098. buf[ret/2] ^= 0xFF;
  51099. if (test_AEAD_fail_decryption == 1)
  51100. test_AEAD_fail_decryption = 0;
  51101. }
  51102. }
  51103. (void)ctx;
  51104. return ret;
  51105. }
  51106. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  51107. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  51108. {
  51109. if (sendLimit)
  51110. w64Zero(sendLimit);
  51111. switch (ssl->specs.bulk_cipher_algorithm) {
  51112. case wolfssl_aes_gcm:
  51113. if (sendLimit)
  51114. *sendLimit = AEAD_AES_LIMIT;
  51115. FALL_THROUGH;
  51116. case wolfssl_chacha:
  51117. if (hardLimit)
  51118. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  51119. if (keyUpdateLimit)
  51120. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  51121. break;
  51122. case wolfssl_aes_ccm:
  51123. if (sendLimit)
  51124. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  51125. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  51126. if (hardLimit)
  51127. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  51128. if (keyUpdateLimit)
  51129. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  51130. }
  51131. else {
  51132. if (hardLimit)
  51133. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  51134. if (keyUpdateLimit)
  51135. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  51136. }
  51137. break;
  51138. default:
  51139. fprintf(stderr, "Unrecognized bulk cipher");
  51140. AssertFalse(1);
  51141. break;
  51142. }
  51143. }
  51144. static void test_AEAD_limit_client(WOLFSSL* ssl)
  51145. {
  51146. int ret;
  51147. int i;
  51148. int didReKey = 0;
  51149. char msgBuf[20];
  51150. w64wrapper hardLimit;
  51151. w64wrapper keyUpdateLimit;
  51152. w64wrapper counter;
  51153. w64wrapper sendLimit;
  51154. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  51155. w64Zero(&counter);
  51156. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  51157. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  51158. for (i = 0; i < 10; i++) {
  51159. /* Test some failed decryptions */
  51160. test_AEAD_fail_decryption = 1;
  51161. w64Increment(&counter);
  51162. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51163. /* Should succeed since decryption failures are dropped */
  51164. AssertIntGT(ret, 0);
  51165. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  51166. }
  51167. test_AEAD_fail_decryption = 1;
  51168. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  51169. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  51170. /* 100 read calls should be enough to complete the key update */
  51171. w64Zero(&counter);
  51172. for (i = 0; i < 100; i++) {
  51173. /* Key update should be sent and negotiated */
  51174. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51175. AssertIntGT(ret, 0);
  51176. /* Epoch after one key update is 4 */
  51177. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  51178. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  51179. didReKey = 1;
  51180. break;
  51181. }
  51182. }
  51183. AssertTrue(didReKey);
  51184. if (!w64IsZero(sendLimit)) {
  51185. /* Test the sending limit for AEAD ciphers */
  51186. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  51187. test_AEAD_seq_num = 1;
  51188. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  51189. AssertIntGT(ret, 0);
  51190. didReKey = 0;
  51191. w64Zero(&counter);
  51192. /* 100 read calls should be enough to complete the key update */
  51193. for (i = 0; i < 100; i++) {
  51194. /* Key update should be sent and negotiated */
  51195. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51196. AssertIntGT(ret, 0);
  51197. /* Epoch after another key update is 5 */
  51198. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  51199. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  51200. didReKey = 1;
  51201. break;
  51202. }
  51203. }
  51204. AssertTrue(didReKey);
  51205. }
  51206. test_AEAD_fail_decryption = 2;
  51207. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  51208. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  51209. /* Connection should fail with a DECRYPT_ERROR */
  51210. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51211. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  51212. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  51213. test_AEAD_done = 1;
  51214. }
  51215. int counter = 0;
  51216. static void test_AEAD_limit_server(WOLFSSL* ssl)
  51217. {
  51218. char msgBuf[] = "Sending data";
  51219. int ret = WOLFSSL_SUCCESS;
  51220. w64wrapper sendLimit;
  51221. SOCKET_T fd = wolfSSL_get_fd(ssl);
  51222. struct timespec delay;
  51223. XMEMSET(&delay, 0, sizeof(delay));
  51224. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  51225. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  51226. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  51227. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  51228. while (!test_AEAD_done && ret > 0) {
  51229. counter++;
  51230. if (test_AEAD_seq_num) {
  51231. /* We need to update the seq number so that we can understand the
  51232. * peer. Otherwise we will incorrectly interpret the seq number. */
  51233. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  51234. AssertNotNull(e);
  51235. e->nextPeerSeqNumber = sendLimit;
  51236. test_AEAD_seq_num = 0;
  51237. }
  51238. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51239. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  51240. nanosleep(&delay, NULL);
  51241. }
  51242. }
  51243. static int test_wolfSSL_dtls_AEAD_limit(void)
  51244. {
  51245. callback_functions func_cb_client;
  51246. callback_functions func_cb_server;
  51247. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  51248. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  51249. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  51250. func_cb_server.method = wolfDTLSv1_3_server_method;
  51251. func_cb_client.method = wolfDTLSv1_3_client_method;
  51252. func_cb_server.on_result = test_AEAD_limit_server;
  51253. func_cb_client.on_result = test_AEAD_limit_client;
  51254. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  51255. if (!func_cb_client.return_code)
  51256. return TEST_FAIL;
  51257. if (!func_cb_server.return_code)
  51258. return TEST_FAIL;
  51259. return TEST_SUCCESS;
  51260. }
  51261. #else
  51262. static int test_wolfSSL_dtls_AEAD_limit(void)
  51263. {
  51264. return TEST_SKIPPED;
  51265. }
  51266. #endif
  51267. #if defined(WOLFSSL_DTLS) && \
  51268. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  51269. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  51270. {
  51271. int fd, ret;
  51272. byte ch_msg[] = {
  51273. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51274. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51275. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  51276. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  51277. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  51278. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  51279. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  51280. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  51281. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  51282. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  51283. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  51284. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  51285. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  51286. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  51287. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  51288. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  51289. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  51290. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  51291. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  51292. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  51293. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  51294. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  51295. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  51296. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  51297. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  51298. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  51299. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  51300. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  51301. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  51302. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  51303. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  51304. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  51305. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  51306. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  51307. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  51308. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  51309. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  51310. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  51311. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  51312. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  51313. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  51314. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  51315. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  51316. 0x31, 0x68, 0x4c
  51317. };
  51318. fd = wolfSSL_get_fd(ssl);
  51319. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  51320. AssertIntGT(ret, 0);
  51321. /* consume the HRR otherwise handshake will fail */
  51322. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  51323. AssertIntGT(ret, 0);
  51324. }
  51325. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51326. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  51327. {
  51328. int fd, ret;
  51329. byte ch_msh_invalid_cookie[] = {
  51330. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  51331. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  51332. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  51333. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  51334. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  51335. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  51336. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  51337. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  51338. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  51339. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  51340. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  51341. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  51342. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  51343. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  51344. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51345. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51346. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51347. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51348. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51349. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  51350. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  51351. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  51352. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  51353. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  51354. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  51355. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  51356. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  51357. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  51358. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  51359. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  51360. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  51361. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  51362. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  51363. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  51364. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  51365. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  51366. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  51367. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  51368. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  51369. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  51370. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  51371. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  51372. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  51373. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  51374. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  51375. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  51376. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  51377. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  51378. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  51379. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  51380. 0x23, 0x00, 0x00
  51381. };
  51382. byte alert_reply[50];
  51383. byte expected_alert_reply[] = {
  51384. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  51385. 0x02, 0x02, 0x2f
  51386. };
  51387. fd = wolfSSL_get_fd(ssl);
  51388. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  51389. AssertIntGT(ret, 0);
  51390. /* should reply with an illegal_parameter reply */
  51391. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  51392. AssertIntEQ(ret, sizeof(expected_alert_reply));
  51393. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  51394. }
  51395. #endif
  51396. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  51397. {
  51398. #ifndef NO_MD5
  51399. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  51400. #elif !defined(NO_SHA)
  51401. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  51402. #elif !defined(NO_SHA256)
  51403. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  51404. #else
  51405. #error "We need a digest to hash the WOLFSSL object"
  51406. #endif
  51407. byte hashBuf[WC_MAX_DIGEST_SIZE];
  51408. wc_HashAlg hash;
  51409. const TLSX* exts = ssl->extensions;
  51410. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  51411. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  51412. * InitHandshakeHashes */
  51413. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  51414. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  51415. /* Following fields are not important to compare */
  51416. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  51417. sslCopy.buffers.inputBuffer.buffer = NULL;
  51418. sslCopy.buffers.inputBuffer.bufferSize = 0;
  51419. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  51420. sslCopy.buffers.inputBuffer.offset = 0;
  51421. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  51422. sslCopy.buffers.outputBuffer.buffer = NULL;
  51423. sslCopy.buffers.outputBuffer.bufferSize = 0;
  51424. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  51425. sslCopy.buffers.outputBuffer.offset = 0;
  51426. sslCopy.error = 0;
  51427. sslCopy.curSize = 0;
  51428. sslCopy.keys.curSeq_lo = 0;
  51429. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  51430. #ifdef WOLFSSL_DTLS13
  51431. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  51432. sslCopy.dtls13FastTimeout = 0;
  51433. #endif
  51434. sslCopy.keys.dtls_peer_handshake_number = 0;
  51435. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  51436. sslCopy.hsHashes = NULL;
  51437. #ifdef WOLFSSL_ASYNC_IO
  51438. #ifdef WOLFSSL_ASYNC_CRYPT
  51439. sslCopy.asyncDev = NULL;
  51440. #endif
  51441. sslCopy.async = NULL;
  51442. #endif
  51443. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  51444. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  51445. /* hash extension list */
  51446. while (exts != NULL) {
  51447. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  51448. exts = exts->next;
  51449. }
  51450. /* Hash suites */
  51451. if (sslCopy.suites != NULL) {
  51452. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  51453. sizeof(struct Suites)), 0);
  51454. }
  51455. /* Hash hsHashes */
  51456. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  51457. sizeof(*hsHashes)), 0);
  51458. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  51459. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  51460. return MakeWordFromHash(hashBuf);
  51461. }
  51462. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  51463. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  51464. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  51465. char *buf, int sz, void *ctx)
  51466. {
  51467. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  51468. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  51469. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  51470. }
  51471. else {
  51472. return WOLFSSL_CBIO_ERR_WANT_READ;
  51473. }
  51474. }
  51475. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  51476. {
  51477. /* Compare the ssl object before and after one ClientHello msg */
  51478. SOCKET_T fd = wolfSSL_get_fd(ssl);
  51479. int res;
  51480. int err;
  51481. word32 initHash;
  51482. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  51483. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  51484. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  51485. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  51486. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  51487. (void)initHash;
  51488. res = tcp_select(fd, 5);
  51489. /* We are expecting a msg. A timeout indicates failure. */
  51490. AssertIntEQ(res, TEST_RECV_READY);
  51491. res = wolfSSL_accept(ssl);
  51492. err = wolfSSL_get_error(ssl, res);
  51493. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  51494. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  51495. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  51496. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  51497. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  51498. }
  51499. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51500. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  51501. {
  51502. int ret;
  51503. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  51504. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  51505. test_wolfSSL_dtls_compare_stateless(ssl);
  51506. }
  51507. #endif
  51508. static int test_wolfSSL_dtls_stateless(void)
  51509. {
  51510. callback_functions client_cbs, server_cbs;
  51511. size_t i;
  51512. struct {
  51513. method_provider client_meth;
  51514. method_provider server_meth;
  51515. ssl_callback client_ssl_ready;
  51516. ssl_callback server_ssl_ready;
  51517. } test_params[] = {
  51518. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  51519. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  51520. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51521. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  51522. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  51523. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  51524. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  51525. #endif
  51526. };
  51527. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  51528. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  51529. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  51530. client_cbs.doUdp = server_cbs.doUdp = 1;
  51531. client_cbs.method = test_params[i].client_meth;
  51532. server_cbs.method = test_params[i].server_meth;
  51533. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  51534. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  51535. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  51536. if (!client_cbs.return_code)
  51537. return TEST_FAIL;
  51538. if (!server_cbs.return_code)
  51539. return TEST_FAIL;
  51540. }
  51541. return TEST_SUCCESS;
  51542. }
  51543. #else
  51544. static int test_wolfSSL_dtls_stateless(void)
  51545. {
  51546. return TEST_SKIPPED;
  51547. }
  51548. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  51549. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  51550. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  51551. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  51552. !defined(WOLFSSL_NO_CLIENT_AUTH))
  51553. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  51554. {
  51555. int ret;
  51556. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  51557. fprintf(stderr, "loading cert %s failed\n", certA);
  51558. fprintf(stderr, "Error: (%d): %s\n", ret,
  51559. wolfSSL_ERR_reason_error_string(ret));
  51560. return -1;
  51561. }
  51562. return 0;
  51563. }
  51564. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  51565. {
  51566. int ret;
  51567. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  51568. != WOLFSSL_SUCCESS) {
  51569. fprintf(stderr, "could not verify the cert: %s\n", certA);
  51570. fprintf(stderr, "Error: (%d): %s\n", ret,
  51571. wolfSSL_ERR_reason_error_string(ret));
  51572. return -1;
  51573. }
  51574. else {
  51575. fprintf(stderr, "successfully verified: %s\n", certA);
  51576. }
  51577. return 0;
  51578. }
  51579. #define LOAD_ONE_CA(a, b, c, d) \
  51580. do { \
  51581. (a) = load_ca_into_cm(c, d); \
  51582. if ((a) != 0) \
  51583. return (b); \
  51584. else \
  51585. (b)--; \
  51586. } while(0)
  51587. #define VERIFY_ONE_CERT(a, b, c, d) \
  51588. do { \
  51589. (a) = verify_cert_with_cm(c, d);\
  51590. if ((a) != 0) \
  51591. return (b); \
  51592. else \
  51593. (b)--; \
  51594. } while(0)
  51595. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  51596. {
  51597. int ret;
  51598. int i = -1;
  51599. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  51600. char chainGArr[9][50] = {"certs/ca-cert.pem",
  51601. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  51602. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  51603. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  51604. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  51605. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  51606. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  51607. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  51608. "certs/test-pathlen/chainG-entity.pem"};
  51609. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  51610. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  51611. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  51612. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  51613. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  51614. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  51615. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  51616. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  51617. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  51618. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  51619. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  51620. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  51621. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  51622. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  51623. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  51624. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  51625. /* test validating the entity twice, should have no effect on pathLen since
  51626. * entity/leaf cert */
  51627. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  51628. return ret;
  51629. }
  51630. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  51631. {
  51632. int ret;
  51633. int i = -1;
  51634. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  51635. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  51636. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  51637. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  51638. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  51639. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  51640. */
  51641. char chainHArr[6][50] = {"certs/ca-cert.pem",
  51642. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  51643. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  51644. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  51645. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  51646. "certs/test-pathlen/chainH-entity.pem"};
  51647. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  51648. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  51649. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  51650. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  51651. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  51652. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  51653. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  51654. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  51655. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  51656. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  51657. return ret;
  51658. }
  51659. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  51660. {
  51661. int ret;
  51662. int i = -1;
  51663. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  51664. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  51665. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  51666. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  51667. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  51668. */
  51669. char chainIArr[5][50] = {"certs/ca-cert.pem",
  51670. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  51671. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  51672. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  51673. "certs/test-pathlen/chainI-entity.pem"};
  51674. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  51675. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  51676. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  51677. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  51678. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  51679. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  51680. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  51681. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  51682. return ret;
  51683. }
  51684. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  51685. {
  51686. int ret;
  51687. int i = -1;
  51688. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  51689. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  51690. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  51691. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  51692. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  51693. */
  51694. char chainJArr[6][50] = {"certs/ca-cert.pem",
  51695. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  51696. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  51697. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  51698. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  51699. "certs/test-pathlen/chainJ-entity.pem"};
  51700. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  51701. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  51702. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  51703. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  51704. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  51705. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  51706. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  51707. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  51708. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  51709. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  51710. return ret;
  51711. }
  51712. static int test_various_pathlen_chains(void)
  51713. {
  51714. EXPECT_DECLS;
  51715. WOLFSSL_CERT_MANAGER* cm = NULL;
  51716. /* Test chain G (large chain with varying pathLens) */
  51717. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51718. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51719. ExpectIntEQ(test_chainG(cm), -1);
  51720. #else
  51721. ExpectIntEQ(test_chainG(cm), 0);
  51722. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51723. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51724. wolfSSL_CertManagerFree(cm);
  51725. /* end test chain G */
  51726. /* Test chain H (5 chain with same pathLens) */
  51727. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51728. ExpectIntLT(test_chainH(cm), 0);
  51729. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51730. wolfSSL_CertManagerFree(cm);
  51731. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51732. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51733. wolfSSL_CertManagerFree(cm);
  51734. /* end test chain H */
  51735. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  51736. * followed by 2 ICA's, should pass) */
  51737. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51738. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51739. ExpectIntEQ(test_chainI(cm), -1);
  51740. #else
  51741. ExpectIntEQ(test_chainI(cm), 0);
  51742. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51743. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51744. wolfSSL_CertManagerFree(cm);
  51745. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51746. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51747. wolfSSL_CertManagerFree(cm);
  51748. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  51749. * this time followed by 3 ICA's, should fail */
  51750. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51751. ExpectIntLT(test_chainJ(cm), 0);
  51752. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51753. wolfSSL_CertManagerFree(cm);
  51754. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51755. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51756. wolfSSL_CertManagerFree(cm);
  51757. return EXPECT_RESULT();
  51758. }
  51759. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  51760. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51761. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  51762. {
  51763. EXPECT_DECLS;
  51764. byte ekm[100] = {0};
  51765. (void)ctx;
  51766. /* Success Cases */
  51767. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51768. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  51769. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51770. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  51771. /* Use some random context */
  51772. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51773. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  51774. /* Failure cases */
  51775. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51776. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  51777. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51778. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  51779. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51780. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  51781. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51782. "extended master secret", XSTR_SIZEOF("extended master secret"),
  51783. NULL, 0, 0), 0);
  51784. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51785. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  51786. return EXPECT_RESULT();
  51787. }
  51788. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  51789. {
  51790. wolfSSL_KeepArrays(ssl);
  51791. return TEST_SUCCESS;
  51792. }
  51793. static int test_export_keying_material(void)
  51794. {
  51795. EXPECT_DECLS;
  51796. test_ssl_cbf serverCb;
  51797. test_ssl_cbf clientCb;
  51798. XMEMSET(&serverCb, 0, sizeof(serverCb));
  51799. XMEMSET(&clientCb, 0, sizeof(clientCb));
  51800. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  51801. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  51802. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  51803. return EXPECT_RESULT();
  51804. }
  51805. #endif /* HAVE_KEYING_MATERIAL */
  51806. static int test_wolfSSL_THREADID_hash(void)
  51807. {
  51808. EXPECT_DECLS;
  51809. #if defined(OPENSSL_EXTRA)
  51810. CRYPTO_THREADID id;
  51811. CRYPTO_THREADID_current(NULL);
  51812. /* Hash result is unsigned long. */
  51813. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  51814. XMEMSET(&id, 0, sizeof(id));
  51815. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  51816. #endif /* OPENSSL_EXTRA */
  51817. return EXPECT_RESULT();
  51818. }
  51819. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  51820. {
  51821. EXPECT_DECLS;
  51822. #if defined(OPENSSL_EXTRA)
  51823. WOLFSSL_CTX* ctx = NULL;
  51824. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  51825. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  51826. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  51827. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  51828. #endif /* OPENSSL_EXTRA */
  51829. return EXPECT_RESULT();
  51830. }
  51831. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  51832. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  51833. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  51834. {
  51835. EXPECT_DECLS;
  51836. callback_functions* callbacks = NULL;
  51837. WOLFSSL_CTX* ctx = NULL;
  51838. WOLFSSL* ssl = NULL;
  51839. SOCKET_T sfd = 0;
  51840. SOCKET_T cfd = 0;
  51841. word16 port;
  51842. char msg[] = "I hear you fa shizzle!";
  51843. int len = (int) XSTRLEN(msg);
  51844. char input[1024];
  51845. int ret;
  51846. int err = 0;
  51847. if (!args)
  51848. WOLFSSL_RETURN_FROM_THREAD(0);
  51849. ((func_args*)args)->return_code = TEST_FAIL;
  51850. callbacks = ((func_args*)args)->callbacks;
  51851. ctx = wolfSSL_CTX_new(callbacks->method());
  51852. #if defined(USE_WINDOWS_API)
  51853. port = ((func_args*)args)->signal->port;
  51854. #else
  51855. /* Let tcp_listen assign port */
  51856. port = 0;
  51857. #endif
  51858. #ifdef WOLFSSL_TIRTOS
  51859. fdOpenSession(Task_self());
  51860. #endif
  51861. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  51862. caCertFile, 0));
  51863. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  51864. svrCertFile, WOLFSSL_FILETYPE_PEM));
  51865. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  51866. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  51867. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  51868. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  51869. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  51870. #elif !defined(NO_DH)
  51871. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  51872. #endif
  51873. if (callbacks->ctx_ready)
  51874. callbacks->ctx_ready(ctx);
  51875. ssl = wolfSSL_new(ctx);
  51876. ExpectNotNull(ssl);
  51877. /* listen and accept */
  51878. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  51879. CloseSocket(sfd);
  51880. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  51881. if (callbacks->ssl_ready)
  51882. callbacks->ssl_ready(ssl);
  51883. if (EXPECT_SUCCESS()) {
  51884. do {
  51885. err = 0; /* Reset error */
  51886. ret = wolfSSL_accept(ssl);
  51887. if (ret != WOLFSSL_SUCCESS) {
  51888. err = wolfSSL_get_error(ssl, 0);
  51889. }
  51890. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51891. }
  51892. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51893. /* read and write data */
  51894. XMEMSET(input, 0, sizeof(input));
  51895. while (EXPECT_SUCCESS()) {
  51896. ret = wolfSSL_read(ssl, input, sizeof(input));
  51897. if (ret > 0) {
  51898. break;
  51899. }
  51900. else {
  51901. err = wolfSSL_get_error(ssl,ret);
  51902. if (err == WOLFSSL_ERROR_WANT_READ) {
  51903. continue;
  51904. }
  51905. break;
  51906. }
  51907. }
  51908. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  51909. do {
  51910. ret = wolfSSL_write(ssl, msg, len);
  51911. if (ret > 0) {
  51912. break;
  51913. }
  51914. } while (ret < 0);
  51915. }
  51916. /* bidirectional shutdown */
  51917. while (EXPECT_SUCCESS()) {
  51918. ret = wolfSSL_shutdown(ssl);
  51919. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  51920. if (ret == WOLFSSL_SUCCESS) {
  51921. break;
  51922. }
  51923. }
  51924. if (EXPECT_SUCCESS()) {
  51925. /* wait for the peer to disconnect the tcp connection */
  51926. do {
  51927. ret = wolfSSL_read(ssl, input, sizeof(input));
  51928. err = wolfSSL_get_error(ssl, ret);
  51929. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  51930. }
  51931. /* detect TCP disconnect */
  51932. ExpectIntLE(ret,WOLFSSL_FAILURE);
  51933. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  51934. ((func_args*)args)->return_code = EXPECT_RESULT();
  51935. wolfSSL_free(ssl);
  51936. wolfSSL_CTX_free(ctx);
  51937. CloseSocket(cfd);
  51938. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51939. wc_ecc_fp_free(); /* free per thread cache */
  51940. #endif
  51941. WOLFSSL_RETURN_FROM_THREAD(0);
  51942. }
  51943. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  51944. {
  51945. EXPECT_DECLS;
  51946. callback_functions* callbacks = NULL;
  51947. WOLFSSL_CTX* ctx = NULL;
  51948. WOLFSSL* ssl = NULL;
  51949. SOCKET_T sfd = 0;
  51950. char msg[] = "hello wolfssl server!";
  51951. int len = (int) XSTRLEN(msg);
  51952. char input[1024];
  51953. int idx;
  51954. int ret, err;
  51955. if (!args)
  51956. WOLFSSL_RETURN_FROM_THREAD(0);
  51957. ((func_args*)args)->return_code = TEST_FAIL;
  51958. callbacks = ((func_args*)args)->callbacks;
  51959. ctx = wolfSSL_CTX_new(callbacks->method());
  51960. #ifdef WOLFSSL_TIRTOS
  51961. fdOpenSession(Task_self());
  51962. #endif
  51963. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  51964. caCertFile, 0));
  51965. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  51966. cliCertFile, WOLFSSL_FILETYPE_PEM));
  51967. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  51968. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  51969. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  51970. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  51971. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  51972. if (EXPECT_SUCCESS()) {
  51973. do {
  51974. err = 0; /* Reset error */
  51975. ret = wolfSSL_connect(ssl);
  51976. if (ret != WOLFSSL_SUCCESS) {
  51977. err = wolfSSL_get_error(ssl, 0);
  51978. }
  51979. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51980. }
  51981. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  51982. if (EXPECT_SUCCESS()) {
  51983. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  51984. input[idx] = 0;
  51985. }
  51986. }
  51987. if (EXPECT_SUCCESS()) {
  51988. ret = wolfSSL_shutdown(ssl);
  51989. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  51990. ret = wolfSSL_shutdown(ssl);
  51991. }
  51992. }
  51993. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51994. ((func_args*)args)->return_code = EXPECT_RESULT();
  51995. wolfSSL_free(ssl);
  51996. wolfSSL_CTX_free(ctx);
  51997. CloseSocket(sfd);
  51998. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51999. wc_ecc_fp_free(); /* free per thread cache */
  52000. #endif
  52001. WOLFSSL_RETURN_FROM_THREAD(0);
  52002. }
  52003. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  52004. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  52005. /* This test is to check wolfSSL_read behaves as same as
  52006. * openSSL when it is called after SSL_shutdown completes.
  52007. */
  52008. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  52009. {
  52010. EXPECT_DECLS;
  52011. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  52012. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  52013. tcp_ready ready;
  52014. func_args client_args;
  52015. func_args server_args;
  52016. THREAD_TYPE serverThread;
  52017. THREAD_TYPE clientThread;
  52018. callback_functions server_cbf;
  52019. callback_functions client_cbf;
  52020. #ifdef WOLFSSL_TIRTOS
  52021. fdOpenSession(Task_self());
  52022. #endif
  52023. StartTCP();
  52024. InitTcpReady(&ready);
  52025. #if defined(USE_WINDOWS_API)
  52026. /* use RNG to get random port if using windows */
  52027. ready.port = GetRandomPort();
  52028. #endif
  52029. XMEMSET(&client_args, 0, sizeof(func_args));
  52030. XMEMSET(&server_args, 0, sizeof(func_args));
  52031. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  52032. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  52033. server_cbf.method = wolfTLSv1_2_server_method;
  52034. client_cbf.method = wolfTLSv1_2_client_method;
  52035. server_args.callbacks = &server_cbf;
  52036. client_args.callbacks = &client_cbf;
  52037. server_args.signal = &ready;
  52038. client_args.signal = &ready;
  52039. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  52040. wait_tcp_ready(&server_args);
  52041. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  52042. join_thread(clientThread);
  52043. join_thread(serverThread);
  52044. ExpectTrue(client_args.return_code);
  52045. ExpectTrue(server_args.return_code);
  52046. FreeTcpReady(&ready);
  52047. #endif
  52048. return EXPECT_RESULT();
  52049. }
  52050. static int test_wolfSSL_CTX_get_min_proto_version(void)
  52051. {
  52052. EXPECT_DECLS;
  52053. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  52054. WOLFSSL_CTX *ctx = NULL;
  52055. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52056. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  52057. WOLFSSL_SUCCESS);
  52058. #ifdef WOLFSSL_ALLOW_SSLV3
  52059. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  52060. #else
  52061. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  52062. #endif
  52063. wolfSSL_CTX_free(ctx);
  52064. ctx = NULL;
  52065. #ifdef WOLFSSL_ALLOW_TLSV10
  52066. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  52067. #else
  52068. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52069. #endif
  52070. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  52071. WOLFSSL_SUCCESS);
  52072. #ifdef WOLFSSL_ALLOW_TLSV10
  52073. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  52074. #else
  52075. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  52076. #endif
  52077. wolfSSL_CTX_free(ctx);
  52078. ctx = NULL;
  52079. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  52080. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  52081. WOLFSSL_SUCCESS);
  52082. #ifndef NO_OLD_TLS
  52083. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  52084. #else
  52085. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  52086. #endif
  52087. wolfSSL_CTX_free(ctx);
  52088. ctx = NULL;
  52089. #ifndef WOLFSSL_NO_TLS12
  52090. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  52091. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  52092. WOLFSSL_SUCCESS);
  52093. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  52094. wolfSSL_CTX_free(ctx);
  52095. ctx = NULL;
  52096. #endif
  52097. #ifdef WOLFSSL_TLS13
  52098. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  52099. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  52100. WOLFSSL_SUCCESS);
  52101. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  52102. wolfSSL_CTX_free(ctx);
  52103. ctx = NULL;
  52104. #endif
  52105. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  52106. return EXPECT_RESULT();
  52107. }
  52108. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52109. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52110. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52111. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  52112. static int test_wolfSSL_set_SSL_CTX(void)
  52113. {
  52114. EXPECT_DECLS;
  52115. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  52116. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  52117. !defined(NO_RSA)
  52118. WOLFSSL_CTX *ctx1 = NULL;
  52119. WOLFSSL_CTX *ctx2 = NULL;
  52120. WOLFSSL *ssl = NULL;
  52121. const byte *session_id1 = (const byte *)"CTX1";
  52122. const byte *session_id2 = (const byte *)"CTX2";
  52123. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  52124. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  52125. WOLFSSL_FILETYPE_PEM));
  52126. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  52127. WOLFSSL_FILETYPE_PEM));
  52128. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  52129. WOLFSSL_SUCCESS);
  52130. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  52131. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  52132. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  52133. WOLFSSL_SUCCESS);
  52134. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  52135. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  52136. WOLFSSL_FILETYPE_PEM));
  52137. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  52138. WOLFSSL_FILETYPE_PEM));
  52139. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  52140. WOLFSSL_SUCCESS);
  52141. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  52142. WOLFSSL_SUCCESS);
  52143. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  52144. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  52145. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  52146. WOLFSSL_SUCCESS);
  52147. #ifdef HAVE_SESSION_TICKET
  52148. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  52149. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  52150. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  52151. #endif
  52152. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  52153. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  52154. #ifdef WOLFSSL_INT_H
  52155. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  52156. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  52157. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  52158. #endif
  52159. #ifdef HAVE_SESSION_TICKET
  52160. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  52161. #endif
  52162. /* Set the ctx1 that has TLSv1.3 as max proto version */
  52163. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  52164. /* MUST not change proto versions of ssl */
  52165. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  52166. #ifdef HAVE_SESSION_TICKET
  52167. /* MUST not change */
  52168. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  52169. #endif
  52170. /* MUST change */
  52171. #ifdef WOLFSSL_INT_H
  52172. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  52173. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  52174. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  52175. #endif
  52176. wolfSSL_free(ssl);
  52177. wolfSSL_CTX_free(ctx1);
  52178. wolfSSL_CTX_free(ctx2);
  52179. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  52180. return EXPECT_RESULT();
  52181. }
  52182. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52183. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52184. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52185. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  52186. static int test_wolfSSL_security_level(void)
  52187. {
  52188. EXPECT_DECLS;
  52189. #if defined(OPENSSL_EXTRA)
  52190. SSL_CTX *ctx = NULL;
  52191. #ifdef WOLFSSL_TLS13
  52192. #ifdef NO_WOLFSSL_SERVER
  52193. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52194. #else
  52195. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52196. #endif
  52197. SSL_CTX_set_security_level(NULL, 1);
  52198. SSL_CTX_set_security_level(ctx, 1);
  52199. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  52200. /* Stub so nothing happens. */
  52201. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  52202. SSL_CTX_free(ctx);
  52203. #else
  52204. (void)ctx;
  52205. #endif
  52206. #endif
  52207. return EXPECT_RESULT();
  52208. }
  52209. static int test_wolfSSL_SSL_in_init(void)
  52210. {
  52211. EXPECT_DECLS;
  52212. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  52213. SSL_CTX* ctx = NULL;
  52214. SSL* ssl = NULL;
  52215. const char* testCertFile;
  52216. const char* testKeyFile;
  52217. #ifdef WOLFSSL_TLS13
  52218. #ifdef NO_WOLFSSL_SERVER
  52219. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52220. #else
  52221. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52222. #endif
  52223. #else
  52224. #ifdef NO_WOLFSSL_SERVER
  52225. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52226. #else
  52227. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52228. #endif
  52229. #endif
  52230. #ifndef NO_RSA
  52231. testCertFile = svrCertFile;
  52232. testKeyFile = svrKeyFile;
  52233. #elif defined(HAVE_ECC)
  52234. testCertFile = eccCertFile;
  52235. testKeyFile = eccKeyFile;
  52236. #else
  52237. testCertFile = NULL;
  52238. testKeyFile = NULL;
  52239. #endif
  52240. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  52241. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52242. SSL_FILETYPE_PEM));
  52243. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52244. SSL_FILETYPE_PEM));
  52245. }
  52246. ExpectNotNull(ssl = SSL_new(ctx));
  52247. ExpectIntEQ(SSL_in_init(ssl), 1);
  52248. SSL_CTX_free(ctx);
  52249. SSL_free(ssl);
  52250. #endif
  52251. return EXPECT_RESULT();
  52252. }
  52253. static int test_wolfSSL_CTX_set_timeout(void)
  52254. {
  52255. EXPECT_DECLS;
  52256. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  52257. int timeout;
  52258. WOLFSSL_CTX* ctx = NULL;
  52259. (void)timeout;
  52260. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52261. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  52262. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  52263. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  52264. */
  52265. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  52266. /* giving 0 as timeout value sets default timeout */
  52267. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  52268. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  52269. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  52270. #else
  52271. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  52272. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  52273. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  52274. #endif
  52275. wolfSSL_CTX_free(ctx);
  52276. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  52277. return EXPECT_RESULT();
  52278. }
  52279. static int test_wolfSSL_OpenSSL_version(void)
  52280. {
  52281. EXPECT_DECLS;
  52282. #if defined(OPENSSL_EXTRA)
  52283. const char* ver;
  52284. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52285. ExpectNotNull(ver = OpenSSL_version(0));
  52286. #else
  52287. ExpectNotNull(ver = OpenSSL_version());
  52288. #endif
  52289. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  52290. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  52291. #endif
  52292. return EXPECT_RESULT();
  52293. }
  52294. static int test_CONF_CTX_CMDLINE(void)
  52295. {
  52296. EXPECT_DECLS;
  52297. #if defined(OPENSSL_ALL)
  52298. SSL_CTX* ctx = NULL;
  52299. SSL_CONF_CTX* cctx = NULL;
  52300. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  52301. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52302. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  52303. /* set flags */
  52304. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  52305. WOLFSSL_CONF_FLAG_CMDLINE);
  52306. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  52307. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  52308. /* cmd invalid command */
  52309. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  52310. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  52311. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  52312. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  52313. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  52314. /* cmd Certificate and Private Key*/
  52315. {
  52316. #if !defined(NO_CERTS) && !defined(NO_RSA)
  52317. const char* ourCert = svrCertFile;
  52318. const char* ourKey = svrKeyFile;
  52319. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  52320. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  52321. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  52322. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  52323. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52324. #endif
  52325. }
  52326. /* cmd curves */
  52327. {
  52328. #if defined(HAVE_ECC)
  52329. const char* curve = "secp256r1";
  52330. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  52331. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  52332. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52333. #endif
  52334. }
  52335. /* cmd CipherString */
  52336. {
  52337. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  52338. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  52339. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  52340. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52341. }
  52342. /* cmd DH parameter */
  52343. {
  52344. #if !defined(NO_DH) && !defined(NO_BIO)
  52345. const char* ourdhcert = "./certs/dh2048.pem";
  52346. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  52347. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  52348. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52349. #endif
  52350. }
  52351. SSL_CTX_free(ctx);
  52352. SSL_CONF_CTX_free(cctx);
  52353. #endif /* OPENSSL_EXTRA */
  52354. return EXPECT_RESULT();
  52355. }
  52356. static int test_CONF_CTX_FILE(void)
  52357. {
  52358. EXPECT_DECLS;
  52359. #if defined(OPENSSL_ALL)
  52360. SSL_CTX* ctx = NULL;
  52361. SSL_CONF_CTX* cctx = NULL;
  52362. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  52363. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52364. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  52365. /* set flags */
  52366. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  52367. WOLFSSL_CONF_FLAG_FILE);
  52368. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  52369. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  52370. /* sanity check */
  52371. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  52372. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  52373. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  52374. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  52375. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  52376. /* cmd Certificate and Private Key*/
  52377. {
  52378. #if !defined(NO_CERTS) && !defined(NO_RSA)
  52379. const char* ourCert = svrCertFile;
  52380. const char* ourKey = svrKeyFile;
  52381. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  52382. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  52383. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  52384. WOLFSSL_SUCCESS);
  52385. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  52386. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52387. #endif
  52388. }
  52389. /* cmd curves */
  52390. {
  52391. #if defined(HAVE_ECC)
  52392. const char* curve = "secp256r1";
  52393. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  52394. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  52395. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52396. #endif
  52397. }
  52398. /* cmd CipherString */
  52399. {
  52400. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  52401. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  52402. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  52403. WOLFSSL_SUCCESS);
  52404. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52405. }
  52406. /* cmd DH parameter */
  52407. {
  52408. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  52409. const char* ourdhcert = "./certs/dh3072.pem";
  52410. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  52411. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  52412. WOLFSSL_SUCCESS);
  52413. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52414. #endif
  52415. }
  52416. SSL_CTX_free(ctx);
  52417. SSL_CONF_CTX_free(cctx);
  52418. #endif /* OPENSSL_EXTRA */
  52419. return EXPECT_RESULT();
  52420. }
  52421. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  52422. {
  52423. EXPECT_DECLS;
  52424. #ifdef HAVE_EX_DATA
  52425. int idx1, idx2;
  52426. /* test for unsupported class index */
  52427. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  52428. 0,NULL, NULL, NULL, NULL ), -1);
  52429. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  52430. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  52431. 0,NULL, NULL, NULL, NULL ), -1);
  52432. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  52433. 0,NULL, NULL, NULL, NULL ), -1);
  52434. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  52435. 0,NULL, NULL, NULL, NULL ), -1);
  52436. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  52437. 0,NULL, NULL, NULL, NULL ), -1);
  52438. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  52439. 0,NULL, NULL, NULL, NULL ), -1);
  52440. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  52441. 0,NULL, NULL, NULL, NULL ), -1);
  52442. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  52443. 0,NULL, NULL, NULL, NULL ), -1);
  52444. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  52445. 0,NULL, NULL, NULL, NULL ), -1);
  52446. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  52447. 0,NULL, NULL, NULL, NULL ), -1);
  52448. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  52449. 0,NULL, NULL, NULL, NULL ), -1);
  52450. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  52451. 0,NULL, NULL, NULL, NULL ), -1);
  52452. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  52453. 0,NULL, NULL, NULL, NULL ), -1);
  52454. /* test for supported class index */
  52455. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  52456. 0,NULL, NULL, NULL, NULL );
  52457. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  52458. 0,NULL, NULL, NULL, NULL );
  52459. ExpectIntNE(idx1, -1);
  52460. ExpectIntNE(idx2, -1);
  52461. ExpectIntNE(idx1, idx2);
  52462. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  52463. 0,NULL, NULL, NULL, NULL );
  52464. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  52465. 0,NULL, NULL, NULL, NULL );
  52466. ExpectIntNE(idx1, -1);
  52467. ExpectIntNE(idx2, -1);
  52468. ExpectIntNE(idx1, idx2);
  52469. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  52470. 0,NULL, NULL, NULL, NULL );
  52471. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  52472. 0,NULL, NULL, NULL, NULL );
  52473. ExpectIntNE(idx1, -1);
  52474. ExpectIntNE(idx2, -1);
  52475. ExpectIntNE(idx1, idx2);
  52476. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  52477. 0,NULL, NULL, NULL, NULL );
  52478. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  52479. 0,NULL, NULL, NULL, NULL );
  52480. ExpectIntNE(idx1, -1);
  52481. ExpectIntNE(idx2, -1);
  52482. ExpectIntNE(idx1, idx2);
  52483. #endif /* HAVE_EX_DATA */
  52484. return EXPECT_RESULT();
  52485. }
  52486. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  52487. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52488. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52489. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52490. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  52491. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  52492. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  52493. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  52494. #define SESSION_NEW_IDX_PTR "Testing"
  52495. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  52496. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  52497. {
  52498. AssertNotNull(p);
  52499. AssertNull(ptr);
  52500. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  52501. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  52502. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  52503. }
  52504. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  52505. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  52506. void* arg)
  52507. {
  52508. EXPECT_DECLS;
  52509. ExpectNotNull(out);
  52510. ExpectNotNull(in);
  52511. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  52512. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  52513. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  52514. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  52515. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  52516. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  52517. if (EXPECT_SUCCESS()) {
  52518. return SSL_SUCCESS;
  52519. }
  52520. else {
  52521. return SSL_FAILURE;
  52522. }
  52523. }
  52524. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  52525. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  52526. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  52527. {
  52528. EXPECT_DECLS;
  52529. ExpectNotNull(p);
  52530. ExpectNull(ptr);
  52531. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  52532. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  52533. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  52534. if (EXPECT_SUCCESS()) {
  52535. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  52536. }
  52537. }
  52538. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  52539. {
  52540. EXPECT_DECLS;
  52541. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  52542. (void*)SESSION_NEW_IDX_PTR,
  52543. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  52544. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  52545. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  52546. SSL_SESSION* s = SSL_SESSION_new();
  52547. SSL_SESSION* d = NULL;
  52548. ExpectNotNull(s);
  52549. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  52550. ExpectNotNull(d = SSL_SESSION_dup(s));
  52551. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  52552. SSL_SESSION_free(s);
  52553. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  52554. SSL_SESSION_free(d);
  52555. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  52556. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  52557. crypto_ex_cb_ctx_session = NULL;
  52558. return EXPECT_RESULT();
  52559. }
  52560. #else
  52561. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  52562. {
  52563. return TEST_SKIPPED;
  52564. }
  52565. #endif
  52566. static int test_wolfSSL_set_psk_use_session_callback(void)
  52567. {
  52568. EXPECT_DECLS;
  52569. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  52570. SSL_CTX* ctx = NULL;
  52571. SSL* ssl = NULL;
  52572. const char* testCertFile;
  52573. const char* testKeyFile;
  52574. #ifdef WOLFSSL_TLS13
  52575. #ifdef NO_WOLFSSL_SERVER
  52576. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52577. #else
  52578. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52579. #endif
  52580. #else
  52581. #ifdef NO_WOLFSSL_SERVER
  52582. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52583. #else
  52584. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52585. #endif
  52586. #endif
  52587. #ifndef NO_RSA
  52588. testCertFile = svrCertFile;
  52589. testKeyFile = svrKeyFile;
  52590. #elif defined(HAVE_ECC)
  52591. testCertFile = eccCertFile;
  52592. testKeyFile = eccKeyFile;
  52593. #else
  52594. testCertFile = NULL;
  52595. testKeyFile = NULL;
  52596. #endif
  52597. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  52598. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52599. SSL_FILETYPE_PEM));
  52600. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52601. SSL_FILETYPE_PEM));
  52602. }
  52603. ExpectNotNull(ssl = SSL_new(ctx));
  52604. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  52605. SSL_CTX_free(ctx);
  52606. SSL_free(ssl);
  52607. #endif
  52608. return EXPECT_RESULT();
  52609. }
  52610. static int test_wolfSSL_ERR_strings(void)
  52611. {
  52612. EXPECT_DECLS;
  52613. #if !defined(NO_ERROR_STRINGS)
  52614. const char* err1 = "unsupported cipher suite";
  52615. const char* err2 = "wolfSSL PEM routines";
  52616. const char* err = NULL;
  52617. (void)err;
  52618. (void)err1;
  52619. (void)err2;
  52620. #if defined(OPENSSL_EXTRA)
  52621. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  52622. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  52623. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  52624. ExpectIntEQ((*err == '\0'), 1);
  52625. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  52626. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  52627. #else
  52628. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  52629. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  52630. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  52631. ExpectIntEQ((*err == '\0'), 1);
  52632. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  52633. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  52634. ExpectIntEQ((*err == '\0'), 1);
  52635. #endif
  52636. #endif
  52637. return EXPECT_RESULT();
  52638. }
  52639. static int test_wolfSSL_EVP_shake128(void)
  52640. {
  52641. EXPECT_DECLS;
  52642. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  52643. defined(WOLFSSL_SHAKE128)
  52644. const EVP_MD* md = NULL;
  52645. ExpectNotNull(md = EVP_shake128());
  52646. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  52647. #endif
  52648. return EXPECT_RESULT();
  52649. }
  52650. static int test_wolfSSL_EVP_shake256(void)
  52651. {
  52652. EXPECT_DECLS;
  52653. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  52654. defined(WOLFSSL_SHAKE256)
  52655. const EVP_MD* md = NULL;
  52656. ExpectNotNull(md = EVP_shake256());
  52657. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  52658. #endif
  52659. return EXPECT_RESULT();
  52660. }
  52661. /*
  52662. * Testing EVP digest API with SM3
  52663. */
  52664. static int test_wolfSSL_EVP_sm3(void)
  52665. {
  52666. int res = TEST_SKIPPED;
  52667. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  52668. EXPECT_DECLS;
  52669. const EVP_MD* md = NULL;
  52670. EVP_MD_CTX* mdCtx = NULL;
  52671. byte data[WC_SM3_BLOCK_SIZE * 4];
  52672. byte hash[WC_SM3_DIGEST_SIZE];
  52673. byte calcHash[WC_SM3_DIGEST_SIZE];
  52674. byte expHash[WC_SM3_DIGEST_SIZE] = {
  52675. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  52676. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  52677. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  52678. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  52679. };
  52680. word32 chunk;
  52681. word32 i;
  52682. unsigned int sz;
  52683. int ret;
  52684. XMEMSET(data, 0, sizeof(data));
  52685. md = EVP_sm3();
  52686. ExpectTrue(md != NULL);
  52687. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  52688. mdCtx = EVP_MD_CTX_new();
  52689. ExpectTrue(mdCtx != NULL);
  52690. /* Invalid Parameters */
  52691. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  52692. /* Valid Parameters */
  52693. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  52694. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  52695. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  52696. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  52697. /* Valid Parameters */
  52698. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  52699. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  52700. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  52701. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  52702. WOLFSSL_SUCCESS);
  52703. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  52704. WOLFSSL_SUCCESS);
  52705. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  52706. WOLFSSL_SUCCESS);
  52707. /* Ensure too many bytes for lengths. */
  52708. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  52709. WOLFSSL_SUCCESS);
  52710. /* Invalid Parameters */
  52711. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  52712. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  52713. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  52714. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  52715. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  52716. /* Valid Parameters */
  52717. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  52718. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  52719. /* Chunk tests. */
  52720. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  52721. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  52722. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  52723. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  52724. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  52725. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  52726. WOLFSSL_SUCCESS);
  52727. }
  52728. if (i < (word32)sizeof(data)) {
  52729. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  52730. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  52731. }
  52732. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  52733. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  52734. }
  52735. /* Not testing when the low 32-bit length overflows. */
  52736. ret = EVP_MD_CTX_cleanup(mdCtx);
  52737. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  52738. wolfSSL_EVP_MD_CTX_free(mdCtx);
  52739. res = EXPECT_RESULT();
  52740. #endif
  52741. return res;
  52742. } /* END test_EVP_sm3 */
  52743. static int test_EVP_blake2(void)
  52744. {
  52745. EXPECT_DECLS;
  52746. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  52747. const EVP_MD* md = NULL;
  52748. (void)md;
  52749. #if defined(HAVE_BLAKE2)
  52750. ExpectNotNull(md = EVP_blake2b512());
  52751. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  52752. #endif
  52753. #if defined(HAVE_BLAKE2S)
  52754. ExpectNotNull(md = EVP_blake2s256());
  52755. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  52756. #endif
  52757. #endif
  52758. return EXPECT_RESULT();
  52759. }
  52760. #if defined(OPENSSL_EXTRA)
  52761. static void list_md_fn(const EVP_MD* m, const char* from,
  52762. const char* to, void* arg)
  52763. {
  52764. const char* mn;
  52765. BIO *bio;
  52766. (void) from;
  52767. (void) to;
  52768. (void) arg;
  52769. (void) mn;
  52770. (void) bio;
  52771. if (!m) {
  52772. /* alias */
  52773. AssertNull(m);
  52774. AssertNotNull(to);
  52775. }
  52776. else {
  52777. AssertNotNull(m);
  52778. AssertNull(to);
  52779. }
  52780. AssertNotNull(from);
  52781. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52782. mn = EVP_get_digestbyname(from);
  52783. /* print to stderr */
  52784. AssertNotNull(arg);
  52785. bio = BIO_new(BIO_s_file());
  52786. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52787. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  52788. BIO_free(bio);
  52789. #endif
  52790. }
  52791. #endif
  52792. static int test_EVP_MD_do_all(void)
  52793. {
  52794. int res = TEST_SKIPPED;
  52795. #if defined(OPENSSL_EXTRA)
  52796. EVP_MD_do_all(NULL, stderr);
  52797. EVP_MD_do_all(list_md_fn, stderr);
  52798. res = TEST_SUCCESS;
  52799. #endif
  52800. return res;
  52801. }
  52802. #if defined(OPENSSL_EXTRA)
  52803. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  52804. {
  52805. (void)arg;
  52806. (void)nm;
  52807. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  52808. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52809. /* print to stderr */
  52810. AssertNotNull(arg);
  52811. BIO *bio = BIO_new(BIO_s_file());
  52812. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52813. BIO_printf(bio, "%s\n", nm);
  52814. BIO_free(bio);
  52815. #endif
  52816. }
  52817. #endif
  52818. static int test_OBJ_NAME_do_all(void)
  52819. {
  52820. int res = TEST_SKIPPED;
  52821. #if defined(OPENSSL_EXTRA)
  52822. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  52823. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  52824. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  52825. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  52826. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  52827. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  52828. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  52829. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  52830. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  52831. res = TEST_SUCCESS;
  52832. #endif
  52833. return res;
  52834. }
  52835. static int test_SSL_CIPHER_get_xxx(void)
  52836. {
  52837. EXPECT_DECLS;
  52838. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  52839. !defined(NO_FILESYSTEM)
  52840. const SSL_CIPHER* cipher = NULL;
  52841. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  52842. int i, numCiphers = 0;
  52843. SSL_CTX* ctx = NULL;
  52844. SSL* ssl = NULL;
  52845. const char* testCertFile;
  52846. const char* testKeyFile;
  52847. char buf[256] = {0};
  52848. const char* cipher_id = NULL;
  52849. int expect_nid1 = NID_undef;
  52850. int expect_nid2 = NID_undef;
  52851. int expect_nid3 = NID_undef;
  52852. int expect_nid4 = NID_undef;
  52853. int expect_nid5 = 0;
  52854. const char* cipher_id2 = NULL;
  52855. int expect_nid21 = NID_undef;
  52856. int expect_nid22 = NID_undef;
  52857. int expect_nid23 = NID_undef;
  52858. int expect_nid24 = NID_undef;
  52859. int expect_nid25 = 0;
  52860. (void)cipher;
  52861. (void)supportedCiphers;
  52862. (void)i;
  52863. (void)numCiphers;
  52864. (void)ctx;
  52865. (void)ssl;
  52866. (void)testCertFile;
  52867. (void)testKeyFile;
  52868. #if defined(WOLFSSL_TLS13)
  52869. cipher_id = "TLS13-AES128-GCM-SHA256";
  52870. expect_nid1 = NID_auth_rsa;
  52871. expect_nid2 = NID_aes_128_gcm;
  52872. expect_nid3 = NID_sha256;
  52873. expect_nid4 = NID_kx_any;
  52874. expect_nid5 = 1;
  52875. #if !defined(WOLFSSL_NO_TLS12)
  52876. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  52877. expect_nid21 = NID_auth_rsa;
  52878. expect_nid22 = NID_aes_256_gcm;
  52879. expect_nid23 = NID_sha384;
  52880. expect_nid24 = NID_kx_ecdhe;
  52881. expect_nid25 = 1;
  52882. #endif
  52883. #endif
  52884. #ifdef NO_WOLFSSL_SERVER
  52885. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52886. #else
  52887. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52888. #endif
  52889. if (cipher_id) {
  52890. #ifndef NO_RSA
  52891. testCertFile = svrCertFile;
  52892. testKeyFile = svrKeyFile;
  52893. #elif defined(HAVE_ECC)
  52894. testCertFile = eccCertFile;
  52895. testKeyFile = eccKeyFile;
  52896. #else
  52897. testCertFile = NULL;
  52898. testKeyFile = NULL;
  52899. #endif
  52900. if (testCertFile != NULL && testKeyFile != NULL) {
  52901. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52902. SSL_FILETYPE_PEM));
  52903. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52904. SSL_FILETYPE_PEM));
  52905. }
  52906. ExpectNotNull(ssl = SSL_new(ctx));
  52907. ExpectIntEQ(SSL_in_init(ssl), 1);
  52908. supportedCiphers = SSL_get_ciphers(ssl);
  52909. numCiphers = sk_num(supportedCiphers);
  52910. for (i = 0; i < numCiphers; ++i) {
  52911. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52912. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52913. }
  52914. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  52915. break;
  52916. }
  52917. }
  52918. /* test case for */
  52919. if (i != numCiphers) {
  52920. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  52921. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  52922. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  52923. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  52924. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  52925. }
  52926. if (cipher_id2) {
  52927. for (i = 0; i < numCiphers; ++i) {
  52928. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52929. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52930. }
  52931. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  52932. break;
  52933. }
  52934. }
  52935. /* test case for */
  52936. if (i != numCiphers) {
  52937. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  52938. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  52939. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  52940. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  52941. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  52942. }
  52943. }
  52944. }
  52945. SSL_CTX_free(ctx);
  52946. SSL_free(ssl);
  52947. #endif
  52948. return EXPECT_RESULT();
  52949. }
  52950. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52951. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  52952. int* keyFormat)
  52953. {
  52954. int ret;
  52955. byte* key_buf = NULL;
  52956. size_t key_sz = 0;
  52957. EncryptedInfo encInfo;
  52958. XMEMSET(&encInfo, 0, sizeof(encInfo));
  52959. ret = load_file(privKeyFile, &key_buf, &key_sz);
  52960. if (ret == 0) {
  52961. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  52962. NULL, &encInfo, keyFormat);
  52963. }
  52964. if (key_buf != NULL) {
  52965. free(key_buf); key_buf = NULL;
  52966. }
  52967. (void)encInfo; /* not used in this test */
  52968. #ifdef DEBUG_WOLFSSL
  52969. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  52970. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  52971. (*pDer)->length);
  52972. #endif
  52973. return ret;
  52974. }
  52975. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  52976. {
  52977. int ret = CRYPTOCB_UNAVAILABLE;
  52978. const char* privKeyFile = (const char*)ctx;
  52979. DerBuffer* pDer = NULL;
  52980. int keyFormat = 0;
  52981. if (info->algo_type == WC_ALGO_TYPE_PK) {
  52982. #ifdef DEBUG_WOLFSSL
  52983. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  52984. #endif
  52985. #ifndef NO_RSA
  52986. if (info->pk.type == WC_PK_TYPE_RSA) {
  52987. switch (info->pk.rsa.type) {
  52988. case RSA_PUBLIC_ENCRYPT:
  52989. case RSA_PUBLIC_DECRYPT:
  52990. /* perform software based RSA public op */
  52991. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  52992. break;
  52993. case RSA_PRIVATE_ENCRYPT:
  52994. case RSA_PRIVATE_DECRYPT:
  52995. {
  52996. RsaKey key;
  52997. /* perform software based RSA private op */
  52998. #ifdef DEBUG_WOLFSSL
  52999. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  53000. #endif
  53001. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  53002. &keyFormat);
  53003. if (ret != 0) {
  53004. return ret;
  53005. }
  53006. ret = wc_InitRsaKey(&key, HEAP_HINT);
  53007. if (ret == 0) {
  53008. word32 keyIdx = 0;
  53009. /* load RSA private key and perform private transform */
  53010. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  53011. &key, pDer->length);
  53012. if (ret == 0) {
  53013. ret = wc_RsaFunction(
  53014. info->pk.rsa.in, info->pk.rsa.inLen,
  53015. info->pk.rsa.out, info->pk.rsa.outLen,
  53016. info->pk.rsa.type, &key, info->pk.rsa.rng);
  53017. }
  53018. else {
  53019. /* if decode fails, then fall-back to software based crypto */
  53020. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  53021. "key decode failed %d, falling back to "
  53022. "software\n", ret);
  53023. ret = CRYPTOCB_UNAVAILABLE;
  53024. }
  53025. wc_FreeRsaKey(&key);
  53026. }
  53027. wc_FreeDer(&pDer); pDer = NULL;
  53028. break;
  53029. }
  53030. }
  53031. #ifdef DEBUG_WOLFSSL
  53032. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  53033. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  53034. #endif
  53035. }
  53036. #endif /* !NO_RSA */
  53037. #ifdef HAVE_ECC
  53038. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  53039. /* mark this key as ephemeral */
  53040. if (info->pk.eckg.key != NULL) {
  53041. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  53042. sizeof(info->pk.eckg.key->label));
  53043. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  53044. }
  53045. }
  53046. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  53047. ecc_key key;
  53048. /* perform software based ECC sign */
  53049. #ifdef DEBUG_WOLFSSL
  53050. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  53051. #endif
  53052. if (info->pk.eccsign.key != NULL &&
  53053. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  53054. /* this is an empheral key */
  53055. #ifdef DEBUG_WOLFSSL
  53056. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  53057. "ephemeral key\n");
  53058. #endif
  53059. return CRYPTOCB_UNAVAILABLE;
  53060. }
  53061. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  53062. if (ret != 0) {
  53063. return ret;
  53064. }
  53065. ret = wc_ecc_init(&key);
  53066. if (ret == 0) {
  53067. word32 keyIdx = 0;
  53068. /* load ECC private key and perform private transform */
  53069. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  53070. &key, pDer->length);
  53071. if (ret == 0) {
  53072. ret = wc_ecc_sign_hash(
  53073. info->pk.eccsign.in, info->pk.eccsign.inlen,
  53074. info->pk.eccsign.out, info->pk.eccsign.outlen,
  53075. info->pk.eccsign.rng, &key);
  53076. }
  53077. else {
  53078. /* if decode fails, then fall-back to software based crypto */
  53079. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  53080. "decode failed %d, falling back to software\n", ret);
  53081. ret = CRYPTOCB_UNAVAILABLE;
  53082. }
  53083. wc_ecc_free(&key);
  53084. }
  53085. wc_FreeDer(&pDer); pDer = NULL;
  53086. #ifdef DEBUG_WOLFSSL
  53087. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  53088. ret, *info->pk.eccsign.outlen);
  53089. #endif
  53090. }
  53091. #endif /* HAVE_ECC */
  53092. #ifdef HAVE_ED25519
  53093. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  53094. ed25519_key key;
  53095. /* perform software based ED25519 sign */
  53096. #ifdef DEBUG_WOLFSSL
  53097. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  53098. #endif
  53099. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  53100. if (ret != 0) {
  53101. return ret;
  53102. }
  53103. ret = wc_ed25519_init(&key);
  53104. if (ret == 0) {
  53105. word32 keyIdx = 0;
  53106. /* load ED25519 private key and perform private transform */
  53107. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  53108. &key, pDer->length);
  53109. if (ret == 0) {
  53110. /* calculate public key */
  53111. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  53112. if (ret == 0) {
  53113. key.pubKeySet = 1;
  53114. ret = wc_ed25519_sign_msg_ex(
  53115. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  53116. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  53117. &key, info->pk.ed25519sign.type,
  53118. info->pk.ed25519sign.context,
  53119. info->pk.ed25519sign.contextLen);
  53120. }
  53121. }
  53122. else {
  53123. /* if decode fails, then fall-back to software based crypto */
  53124. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  53125. "decode failed %d, falling back to software\n", ret);
  53126. ret = CRYPTOCB_UNAVAILABLE;
  53127. }
  53128. wc_ed25519_free(&key);
  53129. }
  53130. wc_FreeDer(&pDer); pDer = NULL;
  53131. #ifdef DEBUG_WOLFSSL
  53132. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  53133. ret, *info->pk.ed25519sign.outLen);
  53134. #endif
  53135. }
  53136. #endif /* HAVE_ED25519 */
  53137. }
  53138. (void)thisDevId;
  53139. (void)keyFormat;
  53140. return ret;
  53141. }
  53142. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  53143. static int test_wc_CryptoCb_TLS(int tlsVer,
  53144. const char* cliCaPemFile, const char* cliCertPemFile,
  53145. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  53146. const char* svrCaPemFile, const char* svrCertPemFile,
  53147. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  53148. {
  53149. EXPECT_DECLS;
  53150. callback_functions client_cbf;
  53151. callback_functions server_cbf;
  53152. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  53153. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  53154. if (tlsVer == WOLFSSL_TLSV1_3) {
  53155. #ifdef WOLFSSL_TLS13
  53156. server_cbf.method = wolfTLSv1_3_server_method;
  53157. client_cbf.method = wolfTLSv1_3_client_method;
  53158. #endif
  53159. }
  53160. else if (tlsVer == WOLFSSL_TLSV1_2) {
  53161. #ifndef WOLFSSL_NO_TLS12
  53162. server_cbf.method = wolfTLSv1_2_server_method;
  53163. client_cbf.method = wolfTLSv1_2_client_method;
  53164. #endif
  53165. }
  53166. else if (tlsVer == WOLFSSL_TLSV1_1) {
  53167. #ifndef NO_OLD_TLS
  53168. server_cbf.method = wolfTLSv1_1_server_method;
  53169. client_cbf.method = wolfTLSv1_1_client_method;
  53170. #endif
  53171. }
  53172. else if (tlsVer == WOLFSSL_TLSV1) {
  53173. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  53174. server_cbf.method = wolfTLSv1_server_method;
  53175. client_cbf.method = wolfTLSv1_client_method;
  53176. #endif
  53177. }
  53178. else if (tlsVer == WOLFSSL_SSLV3) {
  53179. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  53180. defined(WOLFSSL_STATIC_RSA)
  53181. server_cbf.method = wolfSSLv3_server_method;
  53182. client_cbf.method = wolfSSLv3_client_method;
  53183. #endif
  53184. }
  53185. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  53186. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53187. server_cbf.method = wolfDTLSv1_2_server_method;
  53188. client_cbf.method = wolfDTLSv1_2_client_method;
  53189. #endif
  53190. }
  53191. else if (tlsVer == WOLFSSL_DTLSV1) {
  53192. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  53193. server_cbf.method = wolfDTLSv1_server_method;
  53194. client_cbf.method = wolfDTLSv1_client_method;
  53195. #endif
  53196. }
  53197. if (server_cbf.method == NULL) {
  53198. /* not enabled */
  53199. return TEST_SUCCESS;
  53200. }
  53201. /* Setup the keys for the TLS test */
  53202. client_cbf.certPemFile = cliCertPemFile;
  53203. client_cbf.keyPemFile = cliPubKeyPemFile;
  53204. client_cbf.caPemFile = cliCaPemFile;
  53205. server_cbf.certPemFile = svrCertPemFile;
  53206. server_cbf.keyPemFile = svrPubKeyPemFile;
  53207. server_cbf.caPemFile = svrCaPemFile;
  53208. /* Setup a crypto callback with pointer to private key file for testing */
  53209. client_cbf.devId = 1;
  53210. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  53211. (void*)cliPrivKeyPemFile);
  53212. server_cbf.devId = 2;
  53213. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  53214. (void*)svrPrivKeyPemFile);
  53215. /* Perform TLS server and client test */
  53216. /* First test is at WOLFSSL_CTX level */
  53217. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53218. /* Check for success */
  53219. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53220. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53221. if (EXPECT_SUCCESS()) {
  53222. /* Second test is a WOLFSSL object level */
  53223. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  53224. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53225. }
  53226. /* Check for success */
  53227. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53228. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53229. /* Un register the devId's */
  53230. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  53231. client_cbf.devId = INVALID_DEVID;
  53232. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  53233. server_cbf.devId = INVALID_DEVID;
  53234. return EXPECT_RESULT();
  53235. }
  53236. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  53237. static int test_wc_CryptoCb(void)
  53238. {
  53239. EXPECT_DECLS;
  53240. #ifdef WOLF_CRYPTO_CB
  53241. /* TODO: Add crypto callback API tests */
  53242. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53243. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  53244. int tlsVer;
  53245. #endif
  53246. #ifndef NO_RSA
  53247. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53248. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53249. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  53250. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  53251. TEST_SUCCESS);
  53252. }
  53253. #endif
  53254. #ifdef HAVE_ECC
  53255. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53256. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53257. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  53258. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  53259. TEST_SUCCESS);
  53260. }
  53261. #endif
  53262. #ifdef HAVE_ED25519
  53263. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  53264. if (tlsVer == WOLFSSL_DTLSV1) continue;
  53265. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53266. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  53267. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  53268. TEST_SUCCESS);
  53269. }
  53270. #endif
  53271. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  53272. #endif /* WOLF_CRYPTO_CB */
  53273. return EXPECT_RESULT();
  53274. }
  53275. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53276. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  53277. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  53278. const char* cliCaPemFile, const char* cliCertPemFile,
  53279. const char* cliPrivKeyPemFile,
  53280. const char* svrCaPemFile, const char* svrCertPemFile,
  53281. const char* svrPrivKeyPemFile,
  53282. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  53283. {
  53284. EXPECT_DECLS;
  53285. callback_functions client_cbf;
  53286. callback_functions server_cbf;
  53287. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  53288. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  53289. if (tlsVer == WOLFSSL_TLSV1_3) {
  53290. #ifdef WOLFSSL_TLS13
  53291. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  53292. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  53293. #endif
  53294. }
  53295. else if (tlsVer == WOLFSSL_TLSV1_2) {
  53296. #ifndef WOLFSSL_NO_TLS12
  53297. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  53298. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  53299. #endif
  53300. }
  53301. else if (tlsVer == WOLFSSL_TLSV1_1) {
  53302. #ifndef NO_OLD_TLS
  53303. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  53304. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  53305. #endif
  53306. }
  53307. else if (tlsVer == WOLFSSL_TLSV1) {
  53308. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  53309. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  53310. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  53311. #endif
  53312. }
  53313. else if (tlsVer == WOLFSSL_SSLV3) {
  53314. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  53315. defined(WOLFSSL_STATIC_RSA)
  53316. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  53317. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  53318. #endif
  53319. }
  53320. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  53321. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53322. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  53323. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  53324. #endif
  53325. }
  53326. else if (tlsVer == WOLFSSL_DTLSV1) {
  53327. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  53328. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  53329. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  53330. #endif
  53331. }
  53332. if (server_cbf.method_ex == NULL) {
  53333. /* not enabled */
  53334. return TEST_SUCCESS;
  53335. }
  53336. /* Setup the keys for the TLS test */
  53337. client_cbf.certPemFile = cliCertPemFile;
  53338. client_cbf.keyPemFile = cliPrivKeyPemFile;
  53339. client_cbf.caPemFile = cliCaPemFile;
  53340. server_cbf.certPemFile = svrCertPemFile;
  53341. server_cbf.keyPemFile = svrPrivKeyPemFile;
  53342. server_cbf.caPemFile = svrCaPemFile;
  53343. client_cbf.mem = cliMem;
  53344. client_cbf.memSz = cliMemSz;
  53345. server_cbf.mem = svrMem;
  53346. server_cbf.memSz = svrMemSz;
  53347. client_cbf.devId = INVALID_DEVID;
  53348. server_cbf.devId = INVALID_DEVID;
  53349. /* Perform TLS server and client test */
  53350. /* First test is at WOLFSSL_CTX level */
  53351. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53352. /* Check for success */
  53353. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53354. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53355. if (EXPECT_SUCCESS()) {
  53356. /* Second test is a WOLFSSL object level */
  53357. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  53358. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53359. }
  53360. /* Check for success */
  53361. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53362. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53363. return EXPECT_RESULT();
  53364. }
  53365. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  53366. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  53367. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  53368. {
  53369. EXPECT_DECLS;
  53370. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  53371. WOLFSSL_MEM_STATS mem_stats;
  53372. WOLFSSL_MEM_CONN_STATS ssl_stats;
  53373. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  53374. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53375. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53376. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53377. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53378. #endif
  53379. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  53380. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  53381. /* this should fail because kMaxCtxClients == 2 */
  53382. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  53383. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  53384. #ifdef DEBUG_WOLFSSL
  53385. wolfSSL_PrintStatsConn(&ssl_stats);
  53386. #endif
  53387. (void)ssl_stats;
  53388. }
  53389. /* display collected statistics */
  53390. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  53391. #ifdef DEBUG_WOLFSSL
  53392. wolfSSL_PrintStats(&mem_stats);
  53393. #endif
  53394. (void)mem_stats;
  53395. }
  53396. wolfSSL_free(ssl1);
  53397. wolfSSL_free(ssl2);
  53398. return EXPECT_RESULT();
  53399. }
  53400. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  53401. static int test_wolfSSL_CTX_StaticMemory(void)
  53402. {
  53403. EXPECT_DECLS;
  53404. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  53405. wolfSSL_method_func method_func;
  53406. WOLFSSL_CTX* ctx;
  53407. const int kMaxCtxClients = 2;
  53408. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53409. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  53410. int tlsVer;
  53411. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  53412. #endif
  53413. #endif
  53414. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  53415. #ifndef NO_WOLFSSL_SERVER
  53416. #ifndef WOLFSSL_NO_TLS12
  53417. method_func = wolfTLSv1_2_server_method_ex;
  53418. #else
  53419. method_func = wolfTLSv1_3_server_method_ex;
  53420. #endif
  53421. #else
  53422. #ifndef WOLFSSL_NO_TLS12
  53423. method_func = wolfTLSv1_2_client_method_ex;
  53424. #else
  53425. method_func = wolfTLSv1_3_client_method_ex;
  53426. #endif
  53427. #endif
  53428. /* Test creating CTX directly from static memory pool */
  53429. ctx = NULL;
  53430. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  53431. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  53432. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  53433. wolfSSL_CTX_free(ctx);
  53434. ctx = NULL;
  53435. /* Test for heap allocated CTX, then assigning static pool to it */
  53436. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  53437. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  53438. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  53439. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  53440. wolfSSL_CTX_free(ctx);
  53441. /* TLS Level Tests using static memory */
  53442. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53443. #ifndef NO_RSA
  53444. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53445. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53446. svrCertFile, cliCertFile, cliKeyFile,
  53447. cliCertFile, svrCertFile, svrKeyFile,
  53448. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53449. TEST_SUCCESS);
  53450. }
  53451. #endif
  53452. #ifdef HAVE_ECC
  53453. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53454. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53455. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  53456. cliEccCertFile, eccCertFile, eccKeyFile,
  53457. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53458. TEST_SUCCESS);
  53459. }
  53460. #endif
  53461. #ifdef HAVE_ED25519
  53462. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  53463. if (tlsVer == WOLFSSL_DTLSV1) continue;
  53464. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53465. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  53466. cliEdCertFile, edCertFile, edKeyFile,
  53467. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53468. TEST_SUCCESS);
  53469. }
  53470. #endif
  53471. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  53472. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  53473. return EXPECT_RESULT();
  53474. }
  53475. static int test_openssl_FIPS_drbg(void)
  53476. {
  53477. EXPECT_DECLS;
  53478. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  53479. DRBG_CTX* dctx = NULL;
  53480. byte data1[32], data2[32], zeroData[32];
  53481. byte testSeed[16];
  53482. size_t dlen = sizeof(data1);
  53483. int i;
  53484. XMEMSET(data1, 0, dlen);
  53485. XMEMSET(data2, 0, dlen);
  53486. XMEMSET(zeroData, 0, sizeof(zeroData));
  53487. for (i = 0; i < (int)sizeof(testSeed); i++) {
  53488. testSeed[i] = (byte)i;
  53489. }
  53490. ExpectNotNull(dctx = FIPS_get_default_drbg());
  53491. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  53492. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  53493. WOLFSSL_SUCCESS);
  53494. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  53495. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  53496. WOLFSSL_SUCCESS);
  53497. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  53498. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  53499. WOLFSSL_SUCCESS);
  53500. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  53501. WOLFSSL_SUCCESS);
  53502. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  53503. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  53504. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  53505. #ifndef HAVE_GLOBAL_RNG
  53506. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  53507. wolfSSL_FIPS_drbg_free(dctx);
  53508. #endif
  53509. #endif
  53510. return EXPECT_RESULT();
  53511. }
  53512. static int test_wolfSSL_FIPS_mode(void)
  53513. {
  53514. EXPECT_DECLS;
  53515. #if defined(OPENSSL_ALL)
  53516. #ifdef HAVE_FIPS
  53517. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  53518. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  53519. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  53520. #else
  53521. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  53522. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  53523. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  53524. #endif
  53525. #endif
  53526. return EXPECT_RESULT();
  53527. }
  53528. #ifdef WOLFSSL_DTLS
  53529. /* Prints out the current window */
  53530. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  53531. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  53532. int i;
  53533. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  53534. word32 b = w[i];
  53535. int j;
  53536. /* Prints out a 32 bit binary number in big endian order */
  53537. for (j = 0; j < 32; j++, b <<= 1) {
  53538. if (b & (((word32)1) << 31))
  53539. fprintf(stderr, "1");
  53540. else
  53541. fprintf(stderr, "0");
  53542. }
  53543. fprintf(stderr, " ");
  53544. }
  53545. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  53546. #else
  53547. (void)h;
  53548. (void)l;
  53549. (void)w;
  53550. #endif
  53551. }
  53552. /* a - cur_hi
  53553. * b - cur_lo
  53554. * c - next_hi
  53555. * d - next_lo
  53556. * e - window
  53557. * f - expected next_hi
  53558. * g - expected next_lo
  53559. * h - expected window[1]
  53560. * i - expected window[0]
  53561. */
  53562. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  53563. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  53564. DUW_TEST_print_window_binary((a), (b), (e)); \
  53565. ExpectIntEQ((c), (f)); \
  53566. ExpectIntEQ((d), (g)); \
  53567. ExpectIntEQ((e)[1], (h)); \
  53568. ExpectIntEQ((e)[0], (i)); \
  53569. } while (0)
  53570. static int test_wolfSSL_DtlsUpdateWindow(void)
  53571. {
  53572. EXPECT_DECLS;
  53573. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  53574. word32 next_lo = 0;
  53575. word16 next_hi = 0;
  53576. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  53577. fprintf(stderr, "\n");
  53578. #endif
  53579. XMEMSET(window, 0, sizeof window);
  53580. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  53581. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  53582. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  53583. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  53584. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  53585. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  53586. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  53587. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  53588. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  53589. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  53590. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  53591. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  53592. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  53593. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  53594. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  53595. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  53596. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  53597. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  53598. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  53599. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  53600. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  53601. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  53602. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  53603. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  53604. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  53605. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  53606. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  53607. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53608. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53609. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53610. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  53611. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  53612. return EXPECT_RESULT();
  53613. }
  53614. #endif /* WOLFSSL_DTLS */
  53615. #ifdef WOLFSSL_DTLS
  53616. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  53617. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  53618. {
  53619. DtlsMsg* cur;
  53620. static byte msg[100];
  53621. static byte msgInit = 0;
  53622. if (!msgInit) {
  53623. int i;
  53624. for (i = 0; i < 100; i++)
  53625. msg[i] = i + 1;
  53626. msgInit = 1;
  53627. }
  53628. /* Sanitize test parameters */
  53629. if (len > sizeof(msg))
  53630. return -1;
  53631. if (f_offset + f_len > sizeof(msg))
  53632. return -1;
  53633. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  53634. if (ssl->dtls_rx_msg_list == NULL)
  53635. return -100;
  53636. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  53637. return -200;
  53638. if (cur->fragBucketListCount != f_count)
  53639. return -300;
  53640. if (cur->ready != ready)
  53641. return -400;
  53642. if (cur->bytesReceived != bytesReceived)
  53643. return -500;
  53644. if (ready) {
  53645. if (cur->fragBucketList != NULL)
  53646. return -600;
  53647. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  53648. return -700;
  53649. }
  53650. else {
  53651. DtlsFragBucket* fb;
  53652. if (cur->fragBucketList == NULL)
  53653. return -800;
  53654. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  53655. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  53656. return -900;
  53657. }
  53658. }
  53659. return 0;
  53660. }
  53661. static int test_wolfSSL_DTLS_fragment_buckets(void)
  53662. {
  53663. EXPECT_DECLS;
  53664. WOLFSSL ssl[1];
  53665. XMEMSET(ssl, 0, sizeof(*ssl));
  53666. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  53667. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53668. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  53669. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  53670. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  53671. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  53672. /* Test all permutations of 3 regions */
  53673. /* 1 2 3 */
  53674. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53675. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  53676. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  53677. /* 1 3 2 */
  53678. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53679. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  53680. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  53681. /* 2 1 3 */
  53682. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  53683. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  53684. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  53685. /* 2 3 1 */
  53686. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  53687. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  53688. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  53689. /* 3 1 2 */
  53690. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  53691. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  53692. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  53693. /* 3 2 1 */
  53694. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  53695. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  53696. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  53697. /* Test overlapping regions */
  53698. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53699. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  53700. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  53701. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  53702. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  53703. /* Test overlapping multiple regions */
  53704. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53705. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  53706. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  53707. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  53708. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  53709. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  53710. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  53711. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  53712. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  53713. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  53714. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53715. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  53716. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  53717. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  53718. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  53719. ssl->dtls_rx_msg_list = NULL;
  53720. ssl->dtls_rx_msg_list_sz = 0;
  53721. return EXPECT_RESULT();
  53722. }
  53723. #endif
  53724. #if !defined(NO_FILESYSTEM) && \
  53725. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53726. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53727. static int test_wolfSSL_dtls_stateless2(void)
  53728. {
  53729. EXPECT_DECLS;
  53730. WOLFSSL *ssl_c = NULL;
  53731. WOLFSSL *ssl_c2 = NULL;
  53732. WOLFSSL *ssl_s = NULL;
  53733. struct test_memio_ctx test_ctx;
  53734. WOLFSSL_CTX *ctx_c = NULL;
  53735. WOLFSSL_CTX *ctx_s = NULL;
  53736. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53737. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53738. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53739. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  53740. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53741. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53742. /* send CH */
  53743. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53744. (ssl_c2->error == WANT_READ));
  53745. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53746. (ssl_s->error == WANT_READ));
  53747. ExpectIntNE(test_ctx.c_len, 0);
  53748. /* consume HRR */
  53749. test_ctx.c_len = 0;
  53750. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53751. wolfSSL_free(ssl_c2);
  53752. wolfSSL_free(ssl_c);
  53753. wolfSSL_free(ssl_s);
  53754. wolfSSL_CTX_free(ctx_c);
  53755. wolfSSL_CTX_free(ctx_s);
  53756. return EXPECT_RESULT();
  53757. }
  53758. #ifdef HAVE_MAX_FRAGMENT
  53759. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  53760. {
  53761. EXPECT_DECLS;
  53762. WOLFSSL *ssl_c = NULL;
  53763. WOLFSSL *ssl_c2 = NULL;
  53764. WOLFSSL *ssl_s = NULL;
  53765. struct test_memio_ctx test_ctx;
  53766. WOLFSSL_CTX *ctx_c = NULL;
  53767. WOLFSSL_CTX *ctx_s = NULL;
  53768. word16 max_fragment = 0;
  53769. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53770. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53771. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53772. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  53773. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  53774. WOLFSSL_SUCCESS);
  53775. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53776. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53777. if (ssl_s != NULL) {
  53778. max_fragment = ssl_s->max_fragment;
  53779. }
  53780. /* send CH */
  53781. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53782. (ssl_c2->error == WANT_READ));
  53783. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53784. (ssl_s->error == WANT_READ));
  53785. /* CH without cookie shouldn't change state */
  53786. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  53787. ExpectIntNE(test_ctx.c_len, 0);
  53788. /* consume HRR from buffer */
  53789. test_ctx.c_len = 0;
  53790. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53791. wolfSSL_free(ssl_c2);
  53792. wolfSSL_free(ssl_c);
  53793. wolfSSL_free(ssl_s);
  53794. wolfSSL_CTX_free(ctx_c);
  53795. wolfSSL_CTX_free(ctx_s);
  53796. return EXPECT_RESULT();
  53797. }
  53798. #endif /* HAVE_MAX_FRAGMENT */
  53799. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  53800. #define ROUNDS_WITH_HVR 4
  53801. #define ROUNDS_WITHOUT_HVR 2
  53802. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  53803. static int buf_is_hvr(const byte *data, int len)
  53804. {
  53805. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  53806. return 0;
  53807. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  53808. }
  53809. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  53810. {
  53811. EXPECT_DECLS;
  53812. struct test_memio_ctx test_ctx;
  53813. WOLFSSL_CTX *ctx_c = NULL;
  53814. WOLFSSL_CTX *ctx_s = NULL;
  53815. WOLFSSL *ssl_c = NULL;
  53816. WOLFSSL *ssl_s = NULL;
  53817. WOLFSSL_SESSION *sess = NULL;
  53818. int round_trips;
  53819. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53820. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  53821. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53822. #ifdef HAVE_SESSION_TICKET
  53823. if (useticket) {
  53824. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  53825. }
  53826. #endif
  53827. round_trips = ROUNDS_WITH_HVR;
  53828. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  53829. &round_trips), 0);
  53830. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  53831. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  53832. wolfSSL_shutdown(ssl_c);
  53833. wolfSSL_shutdown(ssl_s);
  53834. wolfSSL_free(ssl_c);
  53835. ssl_c = NULL;
  53836. wolfSSL_free(ssl_s);
  53837. ssl_s = NULL;
  53838. test_ctx.c_len = test_ctx.s_len = 0;
  53839. /* make resumption invalid */
  53840. if (bad && (sess != NULL)) {
  53841. if (useticket) {
  53842. #ifdef HAVE_SESSION_TICKET
  53843. if (sess->ticket != NULL) {
  53844. sess->ticket[0] = !sess->ticket[0];
  53845. }
  53846. #endif /* HAVE_SESSION_TICKET */
  53847. }
  53848. else {
  53849. sess->sessionID[0] = !sess->sessionID[0];
  53850. }
  53851. }
  53852. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  53853. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  53854. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  53855. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  53856. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  53857. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  53858. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  53859. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53860. (ssl_c->error == WANT_READ));
  53861. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53862. (ssl_s->error == WANT_READ));
  53863. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  53864. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  53865. if (!useticket) {
  53866. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  53867. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  53868. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  53869. }
  53870. wolfSSL_SESSION_free(sess);
  53871. wolfSSL_free(ssl_c);
  53872. wolfSSL_free(ssl_s);
  53873. wolfSSL_CTX_free(ctx_c);
  53874. wolfSSL_CTX_free(ctx_s);
  53875. return EXPECT_RESULT();
  53876. }
  53877. static int test_wolfSSL_dtls_stateless_resume(void)
  53878. {
  53879. EXPECT_DECLS;
  53880. #ifdef HAVE_SESSION_TICKET
  53881. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  53882. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  53883. #endif /* HAVE_SESION_TICKET */
  53884. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  53885. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  53886. return EXPECT_RESULT();
  53887. }
  53888. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  53889. #if !defined(NO_OLD_TLS)
  53890. static int test_wolfSSL_dtls_stateless_downgrade(void)
  53891. {
  53892. EXPECT_DECLS;
  53893. WOLFSSL_CTX *ctx_c = NULL;
  53894. WOLFSSL_CTX *ctx_c2 = NULL;
  53895. WOLFSSL_CTX *ctx_s = NULL;
  53896. WOLFSSL *ssl_c = NULL;
  53897. WOLFSSL *ssl_c2 = NULL;
  53898. WOLFSSL *ssl_s = NULL;
  53899. struct test_memio_ctx test_ctx;
  53900. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53901. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53902. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53903. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  53904. WOLFSSL_SUCCESS);
  53905. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  53906. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  53907. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  53908. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  53909. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53910. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53911. /* send CH */
  53912. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53913. (ssl_c2->error == WANT_READ));
  53914. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53915. (ssl_s->error == WANT_READ));
  53916. ExpectIntNE(test_ctx.c_len, 0);
  53917. /* consume HRR */
  53918. test_ctx.c_len = 0;
  53919. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53920. wolfSSL_free(ssl_c2);
  53921. wolfSSL_free(ssl_c);
  53922. wolfSSL_free(ssl_s);
  53923. wolfSSL_CTX_free(ctx_c);
  53924. wolfSSL_CTX_free(ctx_c2);
  53925. wolfSSL_CTX_free(ctx_s);
  53926. return EXPECT_RESULT();
  53927. }
  53928. #endif /* !defined(NO_OLD_TLS) */
  53929. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53930. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  53931. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53932. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53933. static int test_WOLFSSL_dtls_version_alert(void)
  53934. {
  53935. EXPECT_DECLS;
  53936. struct test_memio_ctx test_ctx;
  53937. WOLFSSL_CTX *ctx_c = NULL;
  53938. WOLFSSL_CTX *ctx_s = NULL;
  53939. WOLFSSL *ssl_c = NULL;
  53940. WOLFSSL *ssl_s = NULL;
  53941. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53942. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53943. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  53944. /* client hello */
  53945. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53946. (ssl_c->error == WANT_READ));
  53947. /* hrr */
  53948. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53949. (ssl_s->error == WANT_READ));
  53950. /* client hello 1 */
  53951. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53952. (ssl_c->error == WANT_READ));
  53953. /* server hello */
  53954. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53955. (ssl_s->error == WANT_READ));
  53956. /* should fail */
  53957. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53958. (ssl_c->error == VERSION_ERROR));
  53959. /* shuould fail */
  53960. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53961. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  53962. wolfSSL_free(ssl_c);
  53963. wolfSSL_free(ssl_s);
  53964. wolfSSL_CTX_free(ctx_c);
  53965. wolfSSL_CTX_free(ctx_s);
  53966. return EXPECT_RESULT();
  53967. }
  53968. #else
  53969. static int test_WOLFSSL_dtls_version_alert(void)
  53970. {
  53971. return TEST_SKIPPED;
  53972. }
  53973. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  53974. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  53975. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  53976. */
  53977. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  53978. && defined(WOLFSSL_TLS13) && \
  53979. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  53980. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53981. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  53982. {
  53983. struct test_memio_ctx *test_ctx;
  53984. byte buf[2048];
  53985. int idx, sz;
  53986. word32 tmp;
  53987. int ret;
  53988. idx = 5; /* space for record header */
  53989. buf[idx] = session_ticket; /* type */
  53990. idx++;
  53991. tmp = OPAQUE32_LEN +
  53992. OPAQUE32_LEN +
  53993. OPAQUE8_LEN + nonceLength +
  53994. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  53995. c32to24(tmp, buf + idx);
  53996. idx += OPAQUE24_LEN;
  53997. c32toa((word32)12345, buf+idx); /* lifetime */
  53998. idx += OPAQUE32_LEN;
  53999. c32toa((word32)12345, buf+idx); /* add */
  54000. idx += OPAQUE32_LEN;
  54001. buf[idx] = nonceLength; /* nonce length */
  54002. idx++;
  54003. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  54004. idx += nonceLength;
  54005. tmp = 1; /* ticket len */
  54006. c16toa((word16)tmp, buf+idx);
  54007. idx += 2;
  54008. buf[idx] = 0xFF; /* ticket */
  54009. idx++;
  54010. tmp = 0; /* ext len */
  54011. c16toa((word16)tmp, buf+idx);
  54012. idx += 2;
  54013. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  54014. handshake, 0, 0, 0);
  54015. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  54016. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  54017. return !(ret == sz);
  54018. }
  54019. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  54020. {
  54021. int ret = 0;
  54022. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  54023. ret = -1;
  54024. }
  54025. else {
  54026. int i;
  54027. for (i = 0; i < len; i++) {
  54028. if (sess->ticketNonce.data[i] != len) {
  54029. ret = -1;
  54030. break;
  54031. }
  54032. }
  54033. }
  54034. return ret;
  54035. }
  54036. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  54037. {
  54038. EXPECT_DECLS;
  54039. char *buf[1024];
  54040. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  54041. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  54042. (ssl_c->error == WANT_READ));
  54043. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  54044. return EXPECT_RESULT();
  54045. }
  54046. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  54047. {
  54048. EXPECT_DECLS;
  54049. WOLFSSL_SESSION *sess = NULL;
  54050. WOLFSSL_SESSION *cached = NULL;
  54051. WOLFSSL_CTX *ctx = ssl_c->ctx;
  54052. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  54053. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  54054. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  54055. NULL, ssl_c->options.side, 1,NULL), 0);
  54056. ExpectNotNull(cached = wolfSSL_SESSION_new());
  54057. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  54058. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  54059. wolfSSL_SESSION_free(cached);
  54060. wolfSSL_SESSION_free(sess);
  54061. return EXPECT_RESULT();
  54062. }
  54063. static int test_ticket_nonce_malloc(void)
  54064. {
  54065. EXPECT_DECLS;
  54066. struct test_memio_ctx test_ctx;
  54067. WOLFSSL_CTX *ctx_c = NULL;
  54068. WOLFSSL_CTX *ctx_s = NULL;
  54069. WOLFSSL *ssl_c = NULL;
  54070. WOLFSSL *ssl_s = NULL;
  54071. byte small;
  54072. byte medium;
  54073. byte big;
  54074. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54075. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54076. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  54077. /* will send ticket manually */
  54078. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  54079. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  54080. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  54081. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  54082. (ssl_s->options.handShakeDone == 0)) {
  54083. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  54084. (ssl_c->error == WANT_READ));
  54085. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  54086. (ssl_s->error == WANT_READ));
  54087. }
  54088. small = TLS13_TICKET_NONCE_STATIC_SZ;
  54089. medium = small + 20 <= 255 ? small + 20 : 255;
  54090. big = medium + 20 <= 255 ? small + 20 : 255;
  54091. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  54092. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  54093. ssl_c->session->ticketNonce.dataStatic);
  54094. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  54095. TEST_SUCCESS);
  54096. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  54097. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  54098. TEST_SUCCESS);
  54099. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  54100. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  54101. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  54102. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  54103. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  54104. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  54105. wolfSSL_free(ssl_c);
  54106. wolfSSL_free(ssl_s);
  54107. wolfSSL_CTX_free(ctx_c);
  54108. wolfSSL_CTX_free(ctx_s);
  54109. return EXPECT_RESULT();
  54110. }
  54111. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  54112. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  54113. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  54114. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54115. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  54116. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  54117. static int test_ticket_ret_create(void)
  54118. {
  54119. EXPECT_DECLS;
  54120. WOLFSSL_CTX *ctx_c = NULL;
  54121. WOLFSSL_CTX *ctx_s = NULL;
  54122. WOLFSSL *ssl_c = NULL;
  54123. WOLFSSL *ssl_s = NULL;
  54124. byte ticket[SESSION_TICKET_LEN];
  54125. struct test_memio_ctx test_ctx;
  54126. WOLFSSL_SESSION *sess = NULL;
  54127. word16 ticketLen = 0;
  54128. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54129. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54130. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  54131. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  54132. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  54133. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  54134. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  54135. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  54136. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  54137. if (sess != NULL) {
  54138. ticketLen = sess->ticketLen;
  54139. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  54140. }
  54141. wolfSSL_free(ssl_c);
  54142. ssl_c = NULL;
  54143. wolfSSL_free(ssl_s);
  54144. ssl_s = NULL;
  54145. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  54146. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  54147. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  54148. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  54149. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  54150. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  54151. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  54152. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  54153. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  54154. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  54155. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  54156. wolfSSL_SESSION_free(sess);
  54157. wolfSSL_free(ssl_c);
  54158. wolfSSL_free(ssl_s);
  54159. wolfSSL_CTX_free(ctx_c);
  54160. wolfSSL_CTX_free(ctx_s);
  54161. return EXPECT_RESULT();
  54162. }
  54163. #else
  54164. static int test_ticket_ret_create(void)
  54165. {
  54166. return TEST_SKIPPED;
  54167. }
  54168. #endif
  54169. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  54170. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  54171. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  54172. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54173. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  54174. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  54175. {
  54176. int ret;
  54177. WOLFSSL_SESSION* session = NULL;
  54178. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  54179. if (!wolfSSL_is_server(ssl)) {
  54180. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  54181. AssertNotNull(session);
  54182. }
  54183. do {
  54184. ret = wolfSSL_shutdown(ssl);
  54185. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  54186. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  54187. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  54188. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  54189. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  54190. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  54191. #endif
  54192. if (!wolfSSL_is_server(ssl)) {
  54193. /* client */
  54194. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54195. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54196. wolfSSL_set_session(ssl, session);
  54197. wolfSSL_SESSION_free(session);
  54198. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  54199. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  54200. }
  54201. else {
  54202. /* server */
  54203. /* Different ciphersuite so that the ticket will be invalidated based on
  54204. * the ciphersuite */
  54205. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  54206. WOLFSSL_SUCCESS);
  54207. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  54208. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  54209. }
  54210. }
  54211. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  54212. {
  54213. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  54214. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54215. WOLFSSL_SUCCESS);
  54216. }
  54217. static int test_ticket_and_psk_mixing(void)
  54218. {
  54219. EXPECT_DECLS;
  54220. /* Test mixing tickets and regular PSK */
  54221. callback_functions client_cbs, server_cbs;
  54222. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54223. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54224. client_cbs.method = wolfTLSv1_3_client_method;
  54225. server_cbs.method = wolfTLSv1_3_server_method;
  54226. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  54227. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  54228. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  54229. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54230. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  54231. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  54232. return EXPECT_RESULT();
  54233. }
  54234. #else
  54235. static int test_ticket_and_psk_mixing(void)
  54236. {
  54237. return TEST_SKIPPED;
  54238. }
  54239. #endif
  54240. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  54241. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  54242. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54243. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  54244. static int test_prioritize_psk_cb_called = FALSE;
  54245. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  54246. const char* identity, unsigned char* key, unsigned int key_max_len,
  54247. const char** ciphersuite)
  54248. {
  54249. test_prioritize_psk_cb_called = TRUE;
  54250. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  54251. }
  54252. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  54253. {
  54254. int ret;
  54255. WOLFSSL_SESSION* session = NULL;
  54256. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  54257. if (!wolfSSL_is_server(ssl)) {
  54258. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  54259. AssertNotNull(session);
  54260. }
  54261. do {
  54262. ret = wolfSSL_shutdown(ssl);
  54263. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  54264. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  54265. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  54266. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  54267. * important. */
  54268. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54269. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54270. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  54271. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  54272. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  54273. #endif
  54274. if (!wolfSSL_is_server(ssl)) {
  54275. /* client */
  54276. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  54277. wolfSSL_set_session(ssl, session);
  54278. wolfSSL_SESSION_free(session);
  54279. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  54280. }
  54281. else {
  54282. /* server */
  54283. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  54284. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  54285. #ifdef WOLFSSL_PRIORITIZE_PSK
  54286. /* The ticket should be first tried with all ciphersuites and chosen */
  54287. AssertFalse(test_prioritize_psk_cb_called);
  54288. #else
  54289. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  54290. * callback that sets this var. */
  54291. AssertTrue(test_prioritize_psk_cb_called);
  54292. #endif
  54293. }
  54294. }
  54295. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  54296. {
  54297. if (!wolfSSL_is_server(ssl))
  54298. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54299. WOLFSSL_SUCCESS);
  54300. else
  54301. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54302. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54303. }
  54304. static int test_prioritize_psk(void)
  54305. {
  54306. EXPECT_DECLS;
  54307. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  54308. * of the PSK's will be followed instead of the ciphersuite. */
  54309. callback_functions client_cbs, server_cbs;
  54310. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54311. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54312. client_cbs.method = wolfTLSv1_3_client_method;
  54313. server_cbs.method = wolfTLSv1_3_server_method;
  54314. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  54315. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  54316. client_cbs.on_result = test_prioritize_psk_on_result;
  54317. server_cbs.on_result = test_prioritize_psk_on_result;
  54318. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54319. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  54320. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  54321. return EXPECT_RESULT();
  54322. }
  54323. #else
  54324. static int test_prioritize_psk(void)
  54325. {
  54326. return TEST_SKIPPED;
  54327. }
  54328. #endif
  54329. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  54330. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  54331. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54332. !defined(WOLFSSL_NO_TLS12)
  54333. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  54334. {
  54335. EXPECT_DECLS;
  54336. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  54337. /* Set TLS 1.3 specific suite */
  54338. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  54339. return EXPECT_RESULT();
  54340. }
  54341. static int test_wolfSSL_CTX_set_ciphersuites(void)
  54342. {
  54343. EXPECT_DECLS;
  54344. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  54345. * do a 1.2 connection. */
  54346. test_ssl_cbf client_cbs, server_cbs;
  54347. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54348. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54349. client_cbs.method = wolfTLSv1_2_client_method;
  54350. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  54351. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  54352. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54353. &server_cbs, NULL), TEST_SUCCESS);
  54354. return EXPECT_RESULT();
  54355. }
  54356. #else
  54357. static int test_wolfSSL_CTX_set_ciphersuites(void)
  54358. {
  54359. return TEST_SKIPPED;
  54360. }
  54361. #endif
  54362. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  54363. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54364. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  54365. {
  54366. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54367. return TEST_SUCCESS;
  54368. }
  54369. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  54370. {
  54371. EXPECT_DECLS;
  54372. WOLFSSL_ALERT_HISTORY h;
  54373. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  54374. ExpectIntEQ(h.last_rx.level, alert_fatal);
  54375. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  54376. return EXPECT_RESULT();
  54377. }
  54378. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  54379. {
  54380. EXPECT_DECLS;
  54381. test_ssl_cbf client_cbs, server_cbs;
  54382. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54383. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54384. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  54385. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  54386. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  54387. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  54388. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  54389. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54390. &server_cbs, NULL), TEST_FAIL);
  54391. return EXPECT_RESULT();
  54392. }
  54393. #else
  54394. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  54395. {
  54396. return TEST_SKIPPED;
  54397. }
  54398. #endif
  54399. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  54400. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  54401. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54402. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  54403. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  54404. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  54405. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  54406. static int test_TLS_13_ticket_different_ciphers_run = 0;
  54407. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  54408. {
  54409. EXPECT_DECLS;
  54410. switch (test_TLS_13_ticket_different_ciphers_run) {
  54411. case 0:
  54412. /* First run */
  54413. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54414. WOLFSSL_SUCCESS);
  54415. if (wolfSSL_is_server(ssl)) {
  54416. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  54417. wolfSSL_get_SSL_CTX(ssl));
  54418. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  54419. test_TLS_13_ticket_different_ciphers_ctx));
  54420. }
  54421. break;
  54422. case 1:
  54423. /* Second run */
  54424. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54425. "TLS13-AES128-GCM-SHA256"),
  54426. WOLFSSL_SUCCESS);
  54427. if (!wolfSSL_is_server(ssl)) {
  54428. ExpectIntEQ(wolfSSL_set_session(ssl,
  54429. test_TLS_13_ticket_different_ciphers_session),
  54430. WOLFSSL_SUCCESS);
  54431. }
  54432. break;
  54433. default:
  54434. /* Bad state? */
  54435. Fail(("Should not enter here"), ("Should not enter here"));
  54436. }
  54437. return EXPECT_RESULT();
  54438. }
  54439. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  54440. {
  54441. EXPECT_DECLS;
  54442. switch (test_TLS_13_ticket_different_ciphers_run) {
  54443. case 0:
  54444. /* First run */
  54445. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  54446. wolfSSL_get1_session(ssl));
  54447. break;
  54448. case 1:
  54449. /* Second run */
  54450. ExpectTrue(wolfSSL_session_reused(ssl));
  54451. break;
  54452. default:
  54453. /* Bad state? */
  54454. Fail(("Should not enter here"), ("Should not enter here"));
  54455. }
  54456. return EXPECT_RESULT();
  54457. }
  54458. static int test_TLS_13_ticket_different_ciphers(void)
  54459. {
  54460. EXPECT_DECLS;
  54461. /* Check that we handle the connection when the ticket doesn't match
  54462. * the first ciphersuite. */
  54463. test_ssl_cbf client_cbs, server_cbs;
  54464. struct test_params {
  54465. method_provider client_meth;
  54466. method_provider server_meth;
  54467. int doUdp;
  54468. } params[] = {
  54469. #ifdef WOLFSSL_DTLS13
  54470. /* Test that the stateless code handles sessions correctly */
  54471. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  54472. #endif
  54473. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  54474. };
  54475. size_t i;
  54476. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54477. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54478. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54479. test_TLS_13_ticket_different_ciphers_run = 0;
  54480. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  54481. client_cbs.method = params[i].client_meth;
  54482. server_cbs.method = params[i].server_meth;
  54483. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  54484. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  54485. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  54486. server_cbs.ticNoInit = 1;
  54487. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54488. &server_cbs, NULL), TEST_SUCCESS);
  54489. test_TLS_13_ticket_different_ciphers_run++;
  54490. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  54491. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54492. &server_cbs, NULL), TEST_SUCCESS);
  54493. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  54494. test_TLS_13_ticket_different_ciphers_session = NULL;
  54495. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  54496. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  54497. }
  54498. return EXPECT_RESULT();
  54499. }
  54500. #else
  54501. static int test_TLS_13_ticket_different_ciphers(void)
  54502. {
  54503. return TEST_SKIPPED;
  54504. }
  54505. #endif
  54506. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  54507. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  54508. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  54509. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  54510. byte test_extra_alerts_wrong_cs_sh[] = {
  54511. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  54512. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  54513. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  54514. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  54515. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  54516. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  54517. 0xbb, 0x07, 0x54, 0x1c,
  54518. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  54519. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  54520. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  54521. };
  54522. static int test_extra_alerts_wrong_cs(void)
  54523. {
  54524. EXPECT_DECLS;
  54525. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  54526. struct test_memio_ctx test_ctx;
  54527. WOLFSSL_CTX *ctx_c = NULL;
  54528. WOLFSSL_ALERT_HISTORY h;
  54529. WOLFSSL *ssl_c = NULL;
  54530. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54531. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  54532. wolfTLSv1_2_client_method, NULL), 0);
  54533. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  54534. WOLFSSL_SUCCESS);
  54535. /* CH */
  54536. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54537. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54538. WOLFSSL_ERROR_WANT_READ);
  54539. /* consume CH */
  54540. test_ctx.s_len = 0;
  54541. /* inject SH */
  54542. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  54543. sizeof(test_extra_alerts_wrong_cs_sh));
  54544. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  54545. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54546. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54547. WOLFSSL_ERROR_WANT_READ);
  54548. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54549. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  54550. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54551. wolfSSL_free(ssl_c);
  54552. wolfSSL_CTX_free(ctx_c);
  54553. #endif
  54554. return EXPECT_RESULT();
  54555. }
  54556. #else
  54557. static int test_extra_alerts_wrong_cs(void)
  54558. {
  54559. return TEST_SKIPPED;
  54560. }
  54561. #endif
  54562. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  54563. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  54564. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  54565. {
  54566. tail_len -= msg_length;
  54567. XMEMMOVE(msg, msg + msg_length, tail_len);
  54568. *len = *len - msg_length;
  54569. }
  54570. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  54571. byte *found)
  54572. {
  54573. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  54574. const unsigned int _RECORD_HEADER_SZ = 5;
  54575. const int _change_cipher_hs = 55;
  54576. const int _change_cipher = 20;
  54577. const int _handshake = 22;
  54578. unsigned int tail_len;
  54579. byte *idx, *curr;
  54580. word8 currType;
  54581. word16 rLength;
  54582. word32 hLength;
  54583. idx = buf;
  54584. tail_len = *len;
  54585. *found = 0;
  54586. while (tail_len > _RECORD_HEADER_SZ) {
  54587. curr = idx;
  54588. currType = *idx;
  54589. ato16(idx + 3, &rLength);
  54590. idx += _RECORD_HEADER_SZ;
  54591. tail_len -= _RECORD_HEADER_SZ;
  54592. if (tail_len < rLength)
  54593. return -1;
  54594. if (type == _change_cipher_hs && currType == _change_cipher) {
  54595. if (rLength != 1)
  54596. return -1;
  54597. /* match */
  54598. test_remove_msg(curr, *len - (int)(curr - buf),
  54599. len, _RECORD_HEADER_SZ + 1);
  54600. *found = 1;
  54601. return 0;
  54602. }
  54603. if (currType != _handshake) {
  54604. idx += rLength;
  54605. tail_len -= rLength;
  54606. continue;
  54607. }
  54608. if (rLength < _HANDSHAKE_HEADER_SZ)
  54609. return -1;
  54610. currType = *idx;
  54611. ato24(idx+1, &hLength);
  54612. hLength += _HANDSHAKE_HEADER_SZ;
  54613. if (tail_len < hLength)
  54614. return -1;
  54615. if (currType != type) {
  54616. idx += hLength;
  54617. tail_len -= hLength;
  54618. continue;
  54619. }
  54620. /* match */
  54621. test_remove_msg(curr, *len - (int)(curr - buf), len,
  54622. hLength + _RECORD_HEADER_SZ);
  54623. *found = 1;
  54624. return 0;
  54625. }
  54626. /* not found */
  54627. return 0;
  54628. }
  54629. static int test_remove_hs_message(byte hs_message_type,
  54630. int extra_round, byte alert_type)
  54631. {
  54632. EXPECT_DECLS;
  54633. WOLFSSL_CTX *ctx_c = NULL;
  54634. WOLFSSL_CTX *ctx_s = NULL;
  54635. WOLFSSL *ssl_c = NULL;
  54636. WOLFSSL *ssl_s = NULL;
  54637. struct test_memio_ctx test_ctx;
  54638. WOLFSSL_ALERT_HISTORY h;
  54639. byte found = 0;
  54640. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54641. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54642. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  54643. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54644. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54645. WOLFSSL_ERROR_WANT_READ);
  54646. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54647. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54648. WOLFSSL_ERROR_WANT_READ);
  54649. if (extra_round) {
  54650. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54651. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54652. WOLFSSL_ERROR_WANT_READ);
  54653. /* this will complete handshake from server side */
  54654. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54655. }
  54656. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  54657. &test_ctx.c_len, hs_message_type, &found), 0);
  54658. if (!found) {
  54659. wolfSSL_free(ssl_c);
  54660. wolfSSL_CTX_free(ctx_c);
  54661. wolfSSL_free(ssl_s);
  54662. wolfSSL_CTX_free(ctx_s);
  54663. return TEST_SKIPPED;
  54664. }
  54665. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54666. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54667. WOLFSSL_ERROR_WANT_READ);
  54668. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54669. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  54670. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54671. wolfSSL_free(ssl_c);
  54672. wolfSSL_CTX_free(ctx_c);
  54673. wolfSSL_free(ssl_s);
  54674. wolfSSL_CTX_free(ctx_s);
  54675. return EXPECT_RESULT();
  54676. }
  54677. static int test_extra_alerts_skip_hs(void)
  54678. {
  54679. EXPECT_DECLS;
  54680. const byte _server_key_exchange = 12;
  54681. const byte _server_hello = 2;
  54682. const byte _certificate = 11;
  54683. /* server_hello */
  54684. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  54685. unexpected_message), TEST_FAIL);
  54686. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  54687. 0xff), TEST_FAIL);
  54688. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  54689. unexpected_message), TEST_FAIL);
  54690. return EXPECT_RESULT();
  54691. }
  54692. #else
  54693. static int test_extra_alerts_skip_hs(void)
  54694. {
  54695. return TEST_SKIPPED;
  54696. }
  54697. #endif
  54698. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  54699. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  54700. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  54701. unsigned char* key, unsigned int key_max_len)
  54702. {
  54703. (void)ssl;
  54704. (void)id;
  54705. (void)key_max_len;
  54706. /* zero means error */
  54707. key[0] = 0x10;
  54708. return 1;
  54709. }
  54710. static int test_extra_alerts_bad_psk(void)
  54711. {
  54712. EXPECT_DECLS;
  54713. WOLFSSL_CTX *ctx_c = NULL;
  54714. WOLFSSL_CTX *ctx_s = NULL;
  54715. WOLFSSL *ssl_c = NULL;
  54716. WOLFSSL *ssl_s = NULL;
  54717. struct test_memio_ctx test_ctx;
  54718. WOLFSSL_ALERT_HISTORY h;
  54719. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54720. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54721. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  54722. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  54723. WOLFSSL_SUCCESS);
  54724. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  54725. WOLFSSL_SUCCESS);
  54726. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  54727. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54728. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54729. WOLFSSL_ERROR_WANT_READ);
  54730. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54731. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  54732. WOLFSSL_ERROR_WANT_READ);
  54733. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54734. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54735. WOLFSSL_ERROR_WANT_READ);
  54736. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54737. ExpectIntEQ(h.last_tx.code, handshake_failure);
  54738. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54739. wolfSSL_free(ssl_c);
  54740. wolfSSL_CTX_free(ctx_c);
  54741. wolfSSL_free(ssl_s);
  54742. wolfSSL_CTX_free(ctx_s);
  54743. return EXPECT_RESULT();
  54744. }
  54745. #else
  54746. static int test_extra_alerts_bad_psk(void)
  54747. {
  54748. return TEST_SKIPPED;
  54749. }
  54750. #endif
  54751. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  54752. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54753. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  54754. int sz, void *ctx)
  54755. {
  54756. static int sentServerHello = FALSE;
  54757. if (!sentServerHello) {
  54758. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  54759. size_t i;
  54760. if (sz < (int)sizeof(renegExt))
  54761. return WOLFSSL_CBIO_ERR_GENERAL;
  54762. /* Remove SCR from ServerHello */
  54763. for (i = 0; i < sz - sizeof(renegExt); i++) {
  54764. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  54765. /* Found the extension. Change it to something unrecognized. */
  54766. buf[i+1] = 0x11;
  54767. break;
  54768. }
  54769. }
  54770. sentServerHello = TRUE;
  54771. }
  54772. return EmbedSend(ssl, buf, sz, ctx);
  54773. }
  54774. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  54775. {
  54776. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  54777. }
  54778. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  54779. {
  54780. WOLFSSL_ALERT_HISTORY h;
  54781. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  54782. AssertIntEQ(h.last_rx.code, handshake_failure);
  54783. AssertIntEQ(h.last_rx.level, alert_fatal);
  54784. }
  54785. static int test_harden_no_secure_renegotiation(void)
  54786. {
  54787. EXPECT_DECLS;
  54788. callback_functions client_cbs, server_cbs;
  54789. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54790. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54791. client_cbs.method = wolfTLSv1_2_client_method;
  54792. server_cbs.method = wolfTLSv1_2_server_method;
  54793. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  54794. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  54795. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54796. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  54797. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  54798. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  54799. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  54800. server_cbs.last_err == FATAL_ERROR);
  54801. return EXPECT_RESULT();
  54802. }
  54803. #else
  54804. static int test_harden_no_secure_renegotiation(void)
  54805. {
  54806. return TEST_SKIPPED;
  54807. }
  54808. #endif
  54809. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54810. static int test_override_alt_cert_chain_cert_cb(int preverify,
  54811. WOLFSSL_X509_STORE_CTX* store)
  54812. {
  54813. fprintf(stderr, "preverify: %d\n", preverify);
  54814. fprintf(stderr, "store->error: %d\n", store->error);
  54815. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  54816. if (store->error == OCSP_INVALID_STATUS) {
  54817. fprintf(stderr, "Overriding OCSP error\n");
  54818. return 1;
  54819. }
  54820. #ifndef WOLFSSL_ALT_CERT_CHAINS
  54821. else if ((store->error == ASN_NO_SIGNER_E ||
  54822. store->error == ASN_SELF_SIGNED_E
  54823. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  54824. defined(HAVE_WEBSERVER)
  54825. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  54826. #endif
  54827. ) && store->error_depth == store->totalCerts - 1) {
  54828. fprintf(stderr, "Overriding no signer error only for root cert\n");
  54829. return 1;
  54830. }
  54831. #endif
  54832. else
  54833. return preverify;
  54834. }
  54835. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  54836. int urlSz, unsigned char* request, int requestSz,
  54837. unsigned char** response)
  54838. {
  54839. (void)ioCtx;
  54840. (void)url;
  54841. (void)urlSz;
  54842. (void)request;
  54843. (void)requestSz;
  54844. (void)response;
  54845. return -1;
  54846. }
  54847. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  54848. {
  54849. EXPECT_DECLS;
  54850. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  54851. test_override_alt_cert_chain_cert_cb);
  54852. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  54853. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  54854. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  54855. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  54856. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  54857. WOLFSSL_SUCCESS);
  54858. return EXPECT_RESULT();
  54859. }
  54860. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  54861. {
  54862. EXPECT_DECLS;
  54863. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54864. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  54865. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  54866. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  54867. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  54868. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  54869. WOLFSSL_SUCCESS);
  54870. return EXPECT_RESULT();
  54871. }
  54872. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  54873. {
  54874. EXPECT_DECLS;
  54875. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  54876. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  54877. return EXPECT_RESULT();
  54878. }
  54879. static int test_override_alt_cert_chain(void)
  54880. {
  54881. EXPECT_DECLS;
  54882. size_t i;
  54883. struct test_params {
  54884. ctx_cb client_ctx_cb;
  54885. ctx_cb server_ctx_cb;
  54886. int result;
  54887. } params[] = {
  54888. {test_override_alt_cert_chain_client_ctx_ready,
  54889. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  54890. {test_override_alt_cert_chain_client_ctx_ready2,
  54891. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  54892. };
  54893. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54894. test_ssl_cbf client_cbs, server_cbs;
  54895. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54896. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54897. fprintf(stderr, "test config: %d\n", (int)i);
  54898. client_cbs.ctx_ready = params[i].client_ctx_cb;
  54899. server_cbs.ctx_ready = params[i].server_ctx_cb;
  54900. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54901. &server_cbs, NULL), params[i].result);
  54902. ExpectIntEQ(client_cbs.return_code, params[i].result);
  54903. ExpectIntEQ(server_cbs.return_code, params[i].result);
  54904. }
  54905. return EXPECT_RESULT();
  54906. }
  54907. #else
  54908. static int test_override_alt_cert_chain(void)
  54909. {
  54910. return TEST_SKIPPED;
  54911. }
  54912. #endif
  54913. #if defined(HAVE_RPK)
  54914. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  54915. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  54916. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  54917. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  54918. {
  54919. int ret = WOLFSSL_SUCCESS;
  54920. (void)mode;
  54921. (void)strctx;
  54922. WOLFSSL_ENTER("MyRpkVerifyCb");
  54923. return ret;
  54924. }
  54925. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  54926. static WC_INLINE int test_rpk_memio_setup(
  54927. struct test_memio_ctx *ctx,
  54928. WOLFSSL_CTX **ctx_c,
  54929. WOLFSSL_CTX **ctx_s,
  54930. WOLFSSL **ssl_c,
  54931. WOLFSSL **ssl_s,
  54932. method_provider method_c,
  54933. method_provider method_s,
  54934. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  54935. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  54936. const char* pkey_c, int fmt_kc, /* client private key and format */
  54937. const char* pkey_s, int fmt_ks /* server private key and format */
  54938. )
  54939. {
  54940. int ret;
  54941. if (ctx_c != NULL && *ctx_c == NULL) {
  54942. *ctx_c = wolfSSL_CTX_new(method_c());
  54943. if (*ctx_c == NULL) {
  54944. return -1;
  54945. }
  54946. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  54947. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  54948. if (ret != WOLFSSL_SUCCESS) {
  54949. return -1;
  54950. }
  54951. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  54952. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  54953. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  54954. if (ret != WOLFSSL_SUCCESS) {
  54955. return -1;
  54956. }
  54957. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  54958. if (ret != WOLFSSL_SUCCESS) {
  54959. return -1;
  54960. }
  54961. }
  54962. if (ctx_s != NULL && *ctx_s == NULL) {
  54963. *ctx_s = wolfSSL_CTX_new(method_s());
  54964. if (*ctx_s == NULL) {
  54965. return -1;
  54966. }
  54967. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  54968. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  54969. if (ret != WOLFSSL_SUCCESS) {
  54970. return -1;
  54971. }
  54972. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  54973. if (ret != WOLFSSL_SUCCESS) {
  54974. return -1;
  54975. }
  54976. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  54977. if (ret != WOLFSSL_SUCCESS) {
  54978. return -1;
  54979. }
  54980. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  54981. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  54982. if (ctx->s_ciphers != NULL) {
  54983. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  54984. if (ret != WOLFSSL_SUCCESS) {
  54985. return -1;
  54986. }
  54987. }
  54988. }
  54989. if (ctx_c != NULL && ssl_c != NULL) {
  54990. *ssl_c = wolfSSL_new(*ctx_c);
  54991. if (*ssl_c == NULL) {
  54992. return -1;
  54993. }
  54994. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  54995. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  54996. }
  54997. if (ctx_s != NULL && ssl_s != NULL) {
  54998. *ssl_s = wolfSSL_new(*ctx_s);
  54999. if (*ssl_s == NULL) {
  55000. return -1;
  55001. }
  55002. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  55003. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  55004. #if !defined(NO_DH)
  55005. SetDH(*ssl_s);
  55006. #endif
  55007. }
  55008. return 0;
  55009. }
  55010. #endif /* HAVE_RPK */
  55011. static int test_rpk_set_xxx_cert_type(void)
  55012. {
  55013. EXPECT_DECLS;
  55014. #if defined(HAVE_RPK)
  55015. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  55016. WOLFSSL_CTX* ctx = NULL;
  55017. WOLFSSL* ssl = NULL;
  55018. int tp;
  55019. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  55020. ExpectNotNull(ctx);
  55021. ssl = wolfSSL_new(ctx);
  55022. ExpectNotNull(ssl);
  55023. /*--------------------------------------------*/
  55024. /* tests for wolfSSL_CTX_set_client_cert_type */
  55025. /*--------------------------------------------*/
  55026. /* illegal parameter test caces */
  55027. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  55028. MAX_CLIENT_CERT_TYPE_CNT),
  55029. BAD_FUNC_ARG);
  55030. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55031. sizeof(ctype)),
  55032. BAD_FUNC_ARG);
  55033. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  55034. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  55035. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55036. MAX_CLIENT_CERT_TYPE_CNT),
  55037. BAD_FUNC_ARG);
  55038. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  55039. ctype[1] = 10; /* set unknown cert type */
  55040. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55041. MAX_CLIENT_CERT_TYPE_CNT),
  55042. BAD_FUNC_ARG);
  55043. /* pass larger type count */
  55044. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  55045. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  55046. ctype[2] = 1; /* pass unacceptable type count */
  55047. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55048. MAX_CLIENT_CERT_TYPE_CNT + 1),
  55049. BAD_FUNC_ARG);
  55050. /* should accept NULL for type buffer */
  55051. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  55052. MAX_CLIENT_CERT_TYPE_CNT),
  55053. WOLFSSL_SUCCESS);
  55054. /* should accept zero for type count */
  55055. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55056. 0),
  55057. WOLFSSL_SUCCESS);
  55058. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  55059. MAX_CLIENT_CERT_TYPE_CNT),
  55060. WOLFSSL_SUCCESS);
  55061. /*--------------------------------------------*/
  55062. /* tests for wolfSSL_CTX_set_server_cert_type */
  55063. /*--------------------------------------------*/
  55064. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  55065. MAX_SERVER_CERT_TYPE_CNT),
  55066. BAD_FUNC_ARG);
  55067. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55068. sizeof(ctype)),
  55069. BAD_FUNC_ARG);
  55070. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  55071. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  55072. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55073. MAX_SERVER_CERT_TYPE_CNT),
  55074. BAD_FUNC_ARG);
  55075. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  55076. ctype[1] = 10; /* set unknown cert type */
  55077. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55078. MAX_SERVER_CERT_TYPE_CNT),
  55079. BAD_FUNC_ARG);
  55080. /* pass larger type count */
  55081. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  55082. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  55083. ctype[2] = 1; /* pass unacceptable type count */
  55084. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55085. MAX_SERVER_CERT_TYPE_CNT + 1),
  55086. BAD_FUNC_ARG);
  55087. /* should accept NULL for type buffer */
  55088. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  55089. MAX_SERVER_CERT_TYPE_CNT),
  55090. WOLFSSL_SUCCESS);
  55091. /* should accept zero for type count */
  55092. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55093. 0),
  55094. WOLFSSL_SUCCESS);
  55095. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  55096. MAX_CLIENT_CERT_TYPE_CNT),
  55097. WOLFSSL_SUCCESS);
  55098. /*--------------------------------------------*/
  55099. /* tests for wolfSSL_set_client_cert_type */
  55100. /*--------------------------------------------*/
  55101. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  55102. MAX_CLIENT_CERT_TYPE_CNT),
  55103. BAD_FUNC_ARG);
  55104. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55105. sizeof(ctype)),
  55106. BAD_FUNC_ARG);
  55107. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  55108. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  55109. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55110. MAX_CLIENT_CERT_TYPE_CNT),
  55111. BAD_FUNC_ARG);
  55112. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  55113. ctype[1] = 10; /* set unknown cert type */
  55114. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55115. MAX_CLIENT_CERT_TYPE_CNT),
  55116. BAD_FUNC_ARG);
  55117. /* pass larger type count */
  55118. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  55119. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  55120. ctype[2] = 1; /* pass unacceptable type count */
  55121. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55122. MAX_CLIENT_CERT_TYPE_CNT + 1),
  55123. BAD_FUNC_ARG);
  55124. /* should accept NULL for type buffer */
  55125. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  55126. MAX_CLIENT_CERT_TYPE_CNT),
  55127. WOLFSSL_SUCCESS);
  55128. /* should accept zero for type count */
  55129. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55130. 0),
  55131. WOLFSSL_SUCCESS);
  55132. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  55133. MAX_CLIENT_CERT_TYPE_CNT),
  55134. WOLFSSL_SUCCESS);
  55135. /*--------------------------------------------*/
  55136. /* tests for wolfSSL_CTX_set_server_cert_type */
  55137. /*--------------------------------------------*/
  55138. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  55139. MAX_SERVER_CERT_TYPE_CNT),
  55140. BAD_FUNC_ARG);
  55141. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55142. sizeof(ctype)),
  55143. BAD_FUNC_ARG);
  55144. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  55145. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  55146. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55147. MAX_SERVER_CERT_TYPE_CNT),
  55148. BAD_FUNC_ARG);
  55149. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  55150. ctype[1] = 10; /* set unknown cert type */
  55151. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55152. MAX_SERVER_CERT_TYPE_CNT),
  55153. BAD_FUNC_ARG);
  55154. /* pass larger type count */
  55155. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  55156. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  55157. ctype[2] = 1; /* pass unacceptable type count */
  55158. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55159. MAX_SERVER_CERT_TYPE_CNT + 1),
  55160. BAD_FUNC_ARG);
  55161. /* should accept NULL for type buffer */
  55162. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  55163. MAX_SERVER_CERT_TYPE_CNT),
  55164. WOLFSSL_SUCCESS);
  55165. /* should accept zero for type count */
  55166. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55167. 0),
  55168. WOLFSSL_SUCCESS);
  55169. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  55170. MAX_SERVER_CERT_TYPE_CNT),
  55171. WOLFSSL_SUCCESS);
  55172. /*------------------------------------------------*/
  55173. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  55174. /*------------------------------------------------*/
  55175. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  55176. BAD_FUNC_ARG);
  55177. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  55178. BAD_FUNC_ARG);
  55179. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  55180. BAD_FUNC_ARG);
  55181. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  55182. BAD_FUNC_ARG);
  55183. /* clean up */
  55184. wolfSSL_free(ssl);
  55185. wolfSSL_CTX_free(ctx);
  55186. #endif
  55187. return EXPECT_RESULT();
  55188. }
  55189. static int test_tls13_rpk_handshake(void)
  55190. {
  55191. EXPECT_DECLS;
  55192. #if defined(HAVE_RPK)
  55193. int ret = 0;
  55194. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  55195. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  55196. struct test_memio_ctx test_ctx;
  55197. int err;
  55198. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  55199. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  55200. int typeCnt_c;
  55201. int typeCnt_s;
  55202. int tp;
  55203. (void)err;
  55204. (void)typeCnt_c;
  55205. (void)typeCnt_s;
  55206. (void)certType_c;
  55207. (void)certType_s;
  55208. /* TLS1.2
  55209. * Both client and server load x509 cert and start handshaking.
  55210. * Check no negotiation occurred.
  55211. */
  55212. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55213. ExpectIntEQ(
  55214. test_rpk_memio_setup(
  55215. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55216. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  55217. cliCertFile, WOLFSSL_FILETYPE_PEM,
  55218. svrCertFile, WOLFSSL_FILETYPE_PEM,
  55219. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55220. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  55221. , 0);
  55222. /* set client certificate type in client end */
  55223. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55224. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55225. typeCnt_c = 2;
  55226. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55227. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55228. typeCnt_s = 2;
  55229. /* both clien and server do not call client/server_cert_type APIs,
  55230. * expecting default settings works and no negotiation performed.
  55231. */
  55232. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55233. return TEST_FAIL;
  55234. /* confirm no negotiation occurred */
  55235. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55236. WOLFSSL_SUCCESS);
  55237. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55238. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55239. WOLFSSL_SUCCESS);
  55240. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55241. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55242. WOLFSSL_SUCCESS);
  55243. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55244. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55245. WOLFSSL_SUCCESS);
  55246. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55247. wolfSSL_free(ssl_c);
  55248. wolfSSL_CTX_free(ctx_c);
  55249. wolfSSL_free(ssl_s);
  55250. wolfSSL_CTX_free(ctx_s);
  55251. ssl_c = ssl_s = NULL;
  55252. ctx_c = ctx_s = NULL;
  55253. /* Both client and server load x509 cert and start handshaking.
  55254. * Check no negotiation occurred.
  55255. */
  55256. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55257. ExpectIntEQ(
  55258. test_rpk_memio_setup(
  55259. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55260. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55261. cliCertFile, WOLFSSL_FILETYPE_PEM,
  55262. svrCertFile, WOLFSSL_FILETYPE_PEM,
  55263. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55264. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55265. , 0);
  55266. /* set client certificate type in client end */
  55267. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55268. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55269. typeCnt_c = 2;
  55270. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55271. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55272. typeCnt_s = 2;
  55273. /* both clien and server do not call client/server_cert_type APIs,
  55274. * expecting default settings works and no negotiation performed.
  55275. */
  55276. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55277. return TEST_FAIL;
  55278. /* confirm no negotiation occurred */
  55279. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55280. WOLFSSL_SUCCESS);
  55281. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55282. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55283. WOLFSSL_SUCCESS);
  55284. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55285. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55286. WOLFSSL_SUCCESS);
  55287. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55288. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55289. WOLFSSL_SUCCESS);
  55290. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55291. wolfSSL_free(ssl_c);
  55292. wolfSSL_CTX_free(ctx_c);
  55293. wolfSSL_free(ssl_s);
  55294. wolfSSL_CTX_free(ctx_s);
  55295. ssl_c = ssl_s = NULL;
  55296. ctx_c = ctx_s = NULL;
  55297. /* Both client and server load RPK cert and start handshaking.
  55298. * Confirm negotiated cert types match as expected.
  55299. */
  55300. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55301. ExpectIntEQ(
  55302. test_rpk_memio_setup(
  55303. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55304. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55305. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55306. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55307. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55308. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55309. , 0);
  55310. /* set client certificate type in client end */
  55311. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55312. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55313. typeCnt_c = 2;
  55314. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55315. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55316. typeCnt_s = 2;
  55317. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55318. WOLFSSL_SUCCESS);
  55319. /* set server certificate type in client end */
  55320. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55321. WOLFSSL_SUCCESS);
  55322. /* set client certificate type in server end */
  55323. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55324. WOLFSSL_SUCCESS);
  55325. /* set server certificate type in server end */
  55326. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55327. WOLFSSL_SUCCESS);
  55328. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55329. return TEST_FAIL;
  55330. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55331. WOLFSSL_SUCCESS);
  55332. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55333. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55334. WOLFSSL_SUCCESS);
  55335. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55336. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55337. WOLFSSL_SUCCESS);
  55338. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55339. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55340. WOLFSSL_SUCCESS);
  55341. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55342. wolfSSL_free(ssl_c);
  55343. wolfSSL_CTX_free(ctx_c);
  55344. wolfSSL_free(ssl_s);
  55345. wolfSSL_CTX_free(ctx_s);
  55346. ssl_c = ssl_s = NULL;
  55347. ctx_c = ctx_s = NULL;
  55348. /* TLS1.2
  55349. * Both client and server load RPK cert and start handshaking.
  55350. * Confirm negotiated cert types match as expected.
  55351. */
  55352. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55353. ExpectIntEQ(
  55354. test_rpk_memio_setup(
  55355. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55356. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  55357. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55358. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55359. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55360. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55361. , 0);
  55362. /* set client certificate type in client end */
  55363. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55364. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55365. typeCnt_c = 2;
  55366. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55367. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55368. typeCnt_s = 2;
  55369. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55370. WOLFSSL_SUCCESS);
  55371. /* set server certificate type in client end */
  55372. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55373. WOLFSSL_SUCCESS);
  55374. /* set client certificate type in server end */
  55375. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55376. WOLFSSL_SUCCESS);
  55377. /* set server certificate type in server end */
  55378. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55379. WOLFSSL_SUCCESS);
  55380. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55381. return TEST_FAIL;
  55382. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55383. WOLFSSL_SUCCESS);
  55384. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55385. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55386. WOLFSSL_SUCCESS);
  55387. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55388. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55389. WOLFSSL_SUCCESS);
  55390. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55391. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55392. WOLFSSL_SUCCESS);
  55393. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55394. wolfSSL_free(ssl_c);
  55395. wolfSSL_CTX_free(ctx_c);
  55396. wolfSSL_free(ssl_s);
  55397. wolfSSL_CTX_free(ctx_s);
  55398. ssl_c = ssl_s = NULL;
  55399. ctx_c = ctx_s = NULL;
  55400. /* Both client and server load x509 cert.
  55401. * Have client call set_client_cert_type with both RPK and x509.
  55402. * This doesn't makes client add client cert type extension to ClientHello,
  55403. * since it does not load RPK cert actually.
  55404. */
  55405. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55406. ExpectIntEQ(
  55407. test_rpk_memio_setup(
  55408. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55409. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55410. cliCertFile, WOLFSSL_FILETYPE_PEM,
  55411. svrCertFile, WOLFSSL_FILETYPE_PEM,
  55412. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55413. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55414. , 0);
  55415. /* set client certificate type in client end */
  55416. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55417. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55418. typeCnt_c = 2;
  55419. /* client indicates both RPK and x509 certs are available but loaded RPK
  55420. * cert only. It does not have client add client-cert-type extension in CH.
  55421. */
  55422. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55423. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55424. typeCnt_c = 2;
  55425. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55426. WOLFSSL_SUCCESS);
  55427. /* client indicates both RPK and x509 certs are acceptable */
  55428. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55429. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55430. typeCnt_s = 2;
  55431. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55432. WOLFSSL_SUCCESS);
  55433. /* server indicates both RPK and x509 certs are acceptable */
  55434. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55435. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55436. typeCnt_c = 2;
  55437. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55438. WOLFSSL_SUCCESS);
  55439. /* server should indicate only RPK cert is available */
  55440. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55441. certType_s[1] = -1;
  55442. typeCnt_s = 1;
  55443. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55444. WOLFSSL_SUCCESS);
  55445. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55446. return TEST_FAIL;
  55447. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  55448. * be returned as cert type.
  55449. */
  55450. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55451. WOLFSSL_SUCCESS);
  55452. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55453. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55454. WOLFSSL_SUCCESS);
  55455. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55456. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55457. WOLFSSL_SUCCESS);
  55458. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55459. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55460. WOLFSSL_SUCCESS);
  55461. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55462. wolfSSL_free(ssl_c);
  55463. wolfSSL_CTX_free(ctx_c);
  55464. wolfSSL_free(ssl_s);
  55465. wolfSSL_CTX_free(ctx_s);
  55466. ssl_c = ssl_s = NULL;
  55467. ctx_c = ctx_s = NULL;
  55468. /* Have client load RPK cert and have server load x509 cert.
  55469. * Check the negotiation result from both ends.
  55470. */
  55471. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55472. ExpectIntEQ(
  55473. test_rpk_memio_setup(
  55474. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55475. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55476. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55477. svrCertFile, WOLFSSL_FILETYPE_PEM,
  55478. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55479. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55480. , 0);
  55481. /* have client tell to use RPK cert */
  55482. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55483. certType_c[1] = -1;
  55484. typeCnt_c = 1;
  55485. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55486. WOLFSSL_SUCCESS);
  55487. /* have client tell to accept both RPK and x509 cert */
  55488. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55489. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  55490. typeCnt_s = 2;
  55491. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55492. WOLFSSL_SUCCESS);
  55493. /* have server accept to both RPK and x509 cert */
  55494. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  55495. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  55496. typeCnt_c = 2;
  55497. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55498. WOLFSSL_SUCCESS);
  55499. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  55500. * end, expecting the default setting works.
  55501. */
  55502. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  55503. return TEST_FAIL;
  55504. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55505. WOLFSSL_SUCCESS);
  55506. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55507. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55508. WOLFSSL_SUCCESS);
  55509. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55510. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55511. WOLFSSL_SUCCESS);
  55512. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55513. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55514. WOLFSSL_SUCCESS);
  55515. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55516. wolfSSL_free(ssl_c);
  55517. wolfSSL_CTX_free(ctx_c);
  55518. wolfSSL_free(ssl_s);
  55519. wolfSSL_CTX_free(ctx_s);
  55520. ssl_c = ssl_s = NULL;
  55521. ctx_c = ctx_s = NULL;
  55522. /* Have both client and server load RPK cert, however, have server
  55523. * indicate its cert type x509.
  55524. * Client is expected to detect the cert type mismatch then to send alert
  55525. * with "unsupported_certificate".
  55526. */
  55527. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55528. ExpectIntEQ(
  55529. test_rpk_memio_setup(
  55530. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55531. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55532. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55533. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  55534. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55535. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55536. , 0);
  55537. /* have client tell to use RPK cert */
  55538. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55539. certType_c[1] = -1;
  55540. typeCnt_c = 1;
  55541. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55542. WOLFSSL_SUCCESS);
  55543. /* have client tell to accept both RPK and x509 cert */
  55544. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55545. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  55546. typeCnt_s = 2;
  55547. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55548. WOLFSSL_SUCCESS);
  55549. /* have server accept to both RPK and x509 cert */
  55550. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  55551. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  55552. typeCnt_c = 2;
  55553. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55554. WOLFSSL_SUCCESS);
  55555. /* have server tell to use x509 cert intentionally. This will bring
  55556. * certificate type mismatch in client side.
  55557. */
  55558. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55559. certType_s[1] = -1;
  55560. typeCnt_s = 1;
  55561. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55562. WOLFSSL_SUCCESS);
  55563. /* expect client detect cert type mismatch then send Alert */
  55564. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55565. if (ret != -1)
  55566. return TEST_FAIL;
  55567. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  55568. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55569. WOLFSSL_SUCCESS);
  55570. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55571. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55572. WOLFSSL_SUCCESS);
  55573. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55574. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55575. WOLFSSL_SUCCESS);
  55576. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55577. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55578. WOLFSSL_SUCCESS);
  55579. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55580. wolfSSL_free(ssl_c);
  55581. wolfSSL_CTX_free(ctx_c);
  55582. wolfSSL_free(ssl_s);
  55583. wolfSSL_CTX_free(ctx_s);
  55584. ssl_c = ssl_s = NULL;
  55585. ctx_c = ctx_s = NULL;
  55586. /* Have client load x509 cert and server load RPK cert,
  55587. * however, have client indicate its cert type RPK.
  55588. * Server is expected to detect the cert type mismatch then to send alert
  55589. * with "unsupported_certificate".
  55590. */
  55591. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55592. ExpectIntEQ(
  55593. test_rpk_memio_setup(
  55594. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55595. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55596. cliCertFile, WOLFSSL_FILETYPE_PEM,
  55597. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55598. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55599. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55600. , 0);
  55601. /* have client tell to use RPK cert intentionally */
  55602. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55603. certType_c[1] = -1;
  55604. typeCnt_c = 1;
  55605. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55606. WOLFSSL_SUCCESS);
  55607. /* have client tell to accept both RPK and x509 cert */
  55608. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55609. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  55610. typeCnt_s = 2;
  55611. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55612. WOLFSSL_SUCCESS);
  55613. /* have server accept to both RPK and x509 cert */
  55614. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  55615. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  55616. typeCnt_c = 2;
  55617. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55618. WOLFSSL_SUCCESS);
  55619. /* have server tell to use x509 cert intentionally. This will bring
  55620. * certificate type mismatch in client side.
  55621. */
  55622. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  55623. certType_s[1] = -1;
  55624. typeCnt_s = 1;
  55625. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55626. WOLFSSL_SUCCESS);
  55627. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55628. /* expect server detect cert type mismatch then send Alert */
  55629. ExpectIntNE(ret, 0);
  55630. err = wolfSSL_get_error(ssl_c, ret);
  55631. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  55632. /* client did not load RPK cert actually, so negotiation did not happen */
  55633. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55634. WOLFSSL_SUCCESS);
  55635. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55636. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55637. WOLFSSL_SUCCESS);
  55638. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55639. /* client did not load RPK cert actually, so negotiation did not happen */
  55640. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55641. WOLFSSL_SUCCESS);
  55642. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  55643. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55644. WOLFSSL_SUCCESS);
  55645. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  55646. wolfSSL_free(ssl_c);
  55647. wolfSSL_CTX_free(ctx_c);
  55648. wolfSSL_free(ssl_s);
  55649. wolfSSL_CTX_free(ctx_s);
  55650. ssl_c = ssl_s = NULL;
  55651. ctx_c = ctx_s = NULL;
  55652. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  55653. /* Both client and server load RPK cert and set certificate verify
  55654. * callbacks then start handshaking.
  55655. * Confirm both side can refer the peer's cert.
  55656. */
  55657. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55658. ExpectIntEQ(
  55659. test_rpk_memio_setup(
  55660. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55661. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  55662. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55663. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  55664. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  55665. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  55666. , 0);
  55667. /* set client certificate type in client end */
  55668. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  55669. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  55670. typeCnt_c = 2;
  55671. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  55672. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  55673. typeCnt_s = 2;
  55674. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  55675. WOLFSSL_SUCCESS);
  55676. /* set server certificate type in client end */
  55677. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  55678. WOLFSSL_SUCCESS);
  55679. /* set client certificate type in server end */
  55680. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  55681. WOLFSSL_SUCCESS);
  55682. /* set server certificate type in server end */
  55683. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  55684. WOLFSSL_SUCCESS);
  55685. /* set certificate verify callback to both client and server */
  55686. int isServer = 0;
  55687. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  55688. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  55689. isServer = 1;
  55690. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  55691. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  55692. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  55693. if (ret != 0)
  55694. return TEST_FAIL;
  55695. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  55696. WOLFSSL_SUCCESS);
  55697. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55698. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  55699. WOLFSSL_SUCCESS);
  55700. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55701. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  55702. WOLFSSL_SUCCESS);
  55703. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55704. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  55705. WOLFSSL_SUCCESS);
  55706. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  55707. wolfSSL_free(ssl_c);
  55708. wolfSSL_CTX_free(ctx_c);
  55709. wolfSSL_free(ssl_s);
  55710. wolfSSL_CTX_free(ctx_s);
  55711. ssl_c = ssl_s = NULL;
  55712. ctx_c = ctx_s = NULL;
  55713. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  55714. #endif /* HAVE_RPK */
  55715. return EXPECT_RESULT();
  55716. }
  55717. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  55718. static int test_dtls13_bad_epoch_ch(void)
  55719. {
  55720. EXPECT_DECLS;
  55721. WOLFSSL_CTX *ctx_c = NULL;
  55722. WOLFSSL_CTX *ctx_s = NULL;
  55723. WOLFSSL *ssl_c = NULL;
  55724. WOLFSSL *ssl_s = NULL;
  55725. struct test_memio_ctx test_ctx;
  55726. const int EPOCH_OFF = 3;
  55727. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55728. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55729. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  55730. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  55731. * with just one message */
  55732. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  55733. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  55734. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  55735. WOLFSSL_ERROR_WANT_READ);
  55736. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  55737. /* first CH should use epoch 0x0 */
  55738. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  55739. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  55740. /* change epoch to 2 */
  55741. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  55742. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  55743. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  55744. WOLFSSL_ERROR_WANT_READ);
  55745. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  55746. /* resend the CH */
  55747. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  55748. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55749. wolfSSL_free(ssl_c);
  55750. wolfSSL_CTX_free(ctx_c);
  55751. wolfSSL_free(ssl_s);
  55752. wolfSSL_CTX_free(ctx_s);
  55753. return EXPECT_RESULT();
  55754. }
  55755. #else
  55756. static int test_dtls13_bad_epoch_ch(void)
  55757. {
  55758. return TEST_SKIPPED;
  55759. }
  55760. #endif
  55761. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  55762. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  55763. {
  55764. EXPECT_DECLS;
  55765. WOLFSSL_SESSION *sess = NULL;
  55766. /* Setup the session to avoid errors */
  55767. ssl->session->timeout = -1;
  55768. ssl->session->side = WOLFSSL_CLIENT_END;
  55769. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  55770. defined(HAVE_SESSION_TICKET))
  55771. ssl->session->version = ssl->version;
  55772. #endif
  55773. /* Force a short session ID to be sent */
  55774. ssl->session->sessionIDSz = 4;
  55775. #ifndef NO_SESSION_CACHE_REF
  55776. /* Allow the client cache to be used */
  55777. ssl->session->idLen = 4;
  55778. #endif
  55779. ssl->session->isSetup = 1;
  55780. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  55781. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  55782. return EXPECT_RESULT();
  55783. }
  55784. static int test_short_session_id(void)
  55785. {
  55786. EXPECT_DECLS;
  55787. test_ssl_cbf client_cbf;
  55788. test_ssl_cbf server_cbf;
  55789. size_t i;
  55790. struct {
  55791. method_provider client_meth;
  55792. method_provider server_meth;
  55793. const char* tls_version;
  55794. } params[] = {
  55795. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  55796. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  55797. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  55798. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  55799. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  55800. #ifdef WOLFSSL_DTLS13
  55801. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  55802. #endif
  55803. #endif
  55804. #ifndef WOLFSSL_NO_TLS12
  55805. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  55806. #ifdef WOLFSSL_DTLS
  55807. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  55808. #endif
  55809. #endif
  55810. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  55811. !defined(NO_DES3))
  55812. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  55813. #ifdef WOLFSSL_DTLS
  55814. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  55815. #endif
  55816. #endif
  55817. };
  55818. fprintf(stderr, "\n");
  55819. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  55820. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  55821. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  55822. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  55823. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  55824. client_cbf.method = params[i].client_meth;
  55825. server_cbf.method = params[i].server_meth;
  55826. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  55827. &server_cbf, NULL), TEST_SUCCESS);
  55828. }
  55829. return EXPECT_RESULT();
  55830. }
  55831. #else
  55832. static int test_short_session_id(void)
  55833. {
  55834. return TEST_SKIPPED;
  55835. }
  55836. #endif
  55837. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  55838. && defined(WOLFSSL_DTLS13)
  55839. static byte* test_find_string(const char *string,
  55840. byte *buf, int buf_size)
  55841. {
  55842. int string_size, i;
  55843. string_size = (int)XSTRLEN(string);
  55844. for (i = 0; i < buf_size - string_size - 1; i++) {
  55845. if (XSTRCMP((char*)&buf[i], string) == 0)
  55846. return &buf[i];
  55847. }
  55848. return NULL;
  55849. }
  55850. static int test_wolfSSL_dtls13_null_cipher(void)
  55851. {
  55852. EXPECT_DECLS;
  55853. WOLFSSL_CTX *ctx_c = NULL;
  55854. WOLFSSL_CTX *ctx_s = NULL;
  55855. WOLFSSL *ssl_c = NULL;
  55856. WOLFSSL *ssl_s = NULL;
  55857. struct test_memio_ctx test_ctx;
  55858. const char *test_str = "test";
  55859. int test_str_size;
  55860. byte buf[255], *ptr = NULL;
  55861. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55862. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  55863. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  55864. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  55865. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  55866. test_str_size = XSTRLEN("test") + 1;
  55867. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  55868. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  55869. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  55870. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  55871. /* check that the packet was sent cleartext */
  55872. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  55873. test_ctx.s_len));
  55874. if (ptr != NULL) {
  55875. /* modify the message */
  55876. *ptr = 'H';
  55877. /* bad messages should be ignored in DTLS */
  55878. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  55879. ExpectIntEQ(ssl_s->error, WANT_READ);
  55880. }
  55881. wolfSSL_free(ssl_c);
  55882. wolfSSL_free(ssl_s);
  55883. wolfSSL_CTX_free(ctx_c);
  55884. wolfSSL_CTX_free(ctx_s);
  55885. return TEST_SUCCESS;
  55886. }
  55887. #else
  55888. static int test_wolfSSL_dtls13_null_cipher(void)
  55889. {
  55890. return TEST_SKIPPED;
  55891. }
  55892. #endif
  55893. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55894. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55895. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  55896. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  55897. {
  55898. SOCKADDR_S peer;
  55899. XSOCKLENT len;
  55900. int ret;
  55901. XMEMSET((byte*)&peer, 0, sizeof(peer));
  55902. len = sizeof(peer);
  55903. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  55904. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  55905. return -1;
  55906. switch (peer.ss_family) {
  55907. #ifdef WOLFSSL_IPV6
  55908. case WOLFSSL_IP6: {
  55909. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  55910. break;
  55911. }
  55912. #endif /* WOLFSSL_IPV6 */
  55913. case WOLFSSL_IP4:
  55914. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  55915. break;
  55916. default:
  55917. return -1;
  55918. }
  55919. return 0;
  55920. }
  55921. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  55922. {
  55923. char buf[1] = {'t'};
  55924. SOCKADDR_IN_T addr;
  55925. int sock_fd;
  55926. word16 port;
  55927. int err;
  55928. (void)ssl;
  55929. (void)ctx;
  55930. if (ssl == NULL)
  55931. return -1;
  55932. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  55933. if (err != 0)
  55934. return -1;
  55935. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  55936. if (sock_fd == -1)
  55937. return -1;
  55938. build_addr(&addr, wolfSSLIP, port, 1, 0);
  55939. /* send a packet to the server. Being another socket, the kernel will ensure
  55940. * the source port will be different. */
  55941. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  55942. sizeof(addr));
  55943. close(sock_fd);
  55944. if (err == -1)
  55945. return -1;
  55946. return 0;
  55947. }
  55948. /* setup a SSL session but just after the handshake send a packet to the server
  55949. * with a source address different than the one of the connected client. The I/O
  55950. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  55951. * is done in test_dtls_msg_from_other_peer_cb */
  55952. static int test_dtls_msg_from_other_peer(void)
  55953. {
  55954. EXPECT_DECLS;
  55955. callback_functions client_cbs;
  55956. callback_functions server_cbs;
  55957. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  55958. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  55959. client_cbs.method = wolfDTLSv1_2_client_method;
  55960. server_cbs.method = wolfDTLSv1_2_server_method;
  55961. client_cbs.doUdp = 1;
  55962. server_cbs.doUdp = 1;
  55963. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  55964. test_dtls_msg_from_other_peer_cb);
  55965. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  55966. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  55967. return EXPECT_RESULT();
  55968. }
  55969. #else
  55970. static int test_dtls_msg_from_other_peer(void)
  55971. {
  55972. return TEST_SKIPPED;
  55973. }
  55974. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55975. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55976. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  55977. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  55978. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55979. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55980. static int test_dtls_ipv6_check(void)
  55981. {
  55982. EXPECT_DECLS;
  55983. WOLFSSL_CTX *ctx_c = NULL;
  55984. WOLFSSL_CTX *ctx_s = NULL;
  55985. WOLFSSL *ssl_c = NULL;
  55986. WOLFSSL *ssl_s = NULL;
  55987. SOCKADDR_IN fake_addr6;
  55988. int sockfd = -1;
  55989. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  55990. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  55991. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55992. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  55993. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55994. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  55995. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55996. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  55997. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  55998. /* mimic a sockaddr_in6 struct, this way we can't test without
  55999. * WOLFSSL_IPV6 */
  56000. fake_addr6.sin_family = WOLFSSL_IP6;
  56001. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  56002. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  56003. /* can't return error here, as the peer is opaque for wolfssl library at
  56004. * this point */
  56005. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  56006. WOLFSSL_SUCCESS);
  56007. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  56008. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  56009. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  56010. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  56011. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  56012. WOLFSSL_SUCCESS);
  56013. /* reuse the socket */
  56014. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  56015. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  56016. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  56017. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  56018. if (sockfd != -1)
  56019. close(sockfd);
  56020. wolfSSL_free(ssl_c);
  56021. wolfSSL_CTX_free(ctx_c);
  56022. wolfSSL_free(ssl_s);
  56023. wolfSSL_CTX_free(ctx_s);
  56024. return EXPECT_RESULT();
  56025. }
  56026. #else
  56027. static int test_dtls_ipv6_check(void)
  56028. {
  56029. return TEST_SKIPPED;
  56030. }
  56031. #endif
  56032. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56033. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  56034. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  56035. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  56036. {
  56037. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  56038. }
  56039. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  56040. {
  56041. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  56042. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  56043. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  56044. }
  56045. else {
  56046. char testMsg[] = "Message after SCR";
  56047. char msgBuf[sizeof(testMsg)];
  56048. int ret;
  56049. if (!wolfSSL_is_server(ssl)) {
  56050. AssertIntEQ(WOLFSSL_SUCCESS,
  56051. wolfSSL_set_session(ssl,
  56052. test_wolfSSL_SCR_after_resumption_session));
  56053. }
  56054. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  56055. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  56056. sizeof(testMsg));
  56057. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56058. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  56059. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56060. AssertIntEQ(ret, sizeof(msgBuf));
  56061. }
  56062. }
  56063. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  56064. {
  56065. AssertIntEQ(WOLFSSL_SUCCESS,
  56066. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  56067. }
  56068. static int test_wolfSSL_SCR_after_resumption(void)
  56069. {
  56070. EXPECT_DECLS;
  56071. callback_functions func_cb_client;
  56072. callback_functions func_cb_server;
  56073. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  56074. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  56075. func_cb_client.method = wolfTLSv1_2_client_method;
  56076. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  56077. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  56078. func_cb_server.method = wolfTLSv1_2_server_method;
  56079. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  56080. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56081. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56082. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56083. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  56084. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  56085. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56086. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56087. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56088. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  56089. return EXPECT_RESULT();
  56090. }
  56091. #else
  56092. static int test_wolfSSL_SCR_after_resumption(void)
  56093. {
  56094. return TEST_SKIPPED;
  56095. }
  56096. #endif
  56097. static int test_wolfSSL_configure_args(void)
  56098. {
  56099. EXPECT_DECLS;
  56100. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  56101. ExpectNotNull(wolfSSL_configure_args());
  56102. #endif
  56103. return EXPECT_RESULT();
  56104. }
  56105. static int test_dtls_no_extensions(void)
  56106. {
  56107. EXPECT_DECLS;
  56108. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  56109. WOLFSSL *ssl_s = NULL;
  56110. WOLFSSL_CTX *ctx_s = NULL;
  56111. struct test_memio_ctx test_ctx;
  56112. const byte chNoExtensions[] = {
  56113. /* Handshake type */
  56114. 0x16,
  56115. /* Version */
  56116. 0xfe, 0xff,
  56117. /* Epoch */
  56118. 0x00, 0x00,
  56119. /* Seq number */
  56120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56121. /* Length */
  56122. 0x00, 0x40,
  56123. /* CH type */
  56124. 0x01,
  56125. /* Length */
  56126. 0x00, 0x00, 0x34,
  56127. /* Msg Seq */
  56128. 0x00, 0x00,
  56129. /* Frag offset */
  56130. 0x00, 0x00, 0x00,
  56131. /* Frag length */
  56132. 0x00, 0x00, 0x34,
  56133. /* Version */
  56134. 0xfe, 0xff,
  56135. /* Random */
  56136. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  56137. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  56138. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  56139. /* Session Length */
  56140. 0x00,
  56141. /* Cookie Length */
  56142. 0x00,
  56143. /* CS Length */
  56144. 0x00, 0x0c,
  56145. /* CS */
  56146. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  56147. /* Comp Meths Length */
  56148. 0x01,
  56149. /* Comp Meths */
  56150. 0x00
  56151. /* And finally... no extensions */
  56152. };
  56153. int i;
  56154. #ifdef OPENSSL_EXTRA
  56155. int repeats = 2;
  56156. #else
  56157. int repeats = 1;
  56158. #endif
  56159. for (i = 0; i < repeats; i++) {
  56160. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56161. ssl_s = NULL;
  56162. ctx_s = NULL;
  56163. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  56164. NULL, wolfDTLS_server_method), 0);
  56165. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  56166. test_ctx.s_len = sizeof(chNoExtensions);
  56167. #ifdef OPENSSL_EXTRA
  56168. if (i > 0) {
  56169. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  56170. WOLFSSL_SUCCESS);
  56171. }
  56172. #endif
  56173. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  56174. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56175. /* Expecting a handshake msg. Either HVR or SH. */
  56176. ExpectIntGT(test_ctx.c_len, 0);
  56177. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  56178. wolfSSL_free(ssl_s);
  56179. wolfSSL_CTX_free(ctx_s);
  56180. }
  56181. #endif
  56182. return EXPECT_RESULT();
  56183. }
  56184. static int test_TLSX_CA_NAMES_bad_extension(void)
  56185. {
  56186. EXPECT_DECLS;
  56187. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  56188. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  56189. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  56190. defined(HAVE_NULL_CIPHER)
  56191. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  56192. * parse the CA Names extension. Otherwise it will return other non-related
  56193. * errors. If CA Names will be parsed in more configurations, that should
  56194. * be reflected in the macro guard above. */
  56195. WOLFSSL *ssl_c = NULL;
  56196. WOLFSSL_CTX *ctx_c = NULL;
  56197. struct test_memio_ctx test_ctx;
  56198. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  56199. const byte shBadCaNamesExt[] = {
  56200. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  56201. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  56202. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  56203. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  56204. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  56205. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  56206. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  56207. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  56208. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  56209. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  56210. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  56211. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  56212. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  56213. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  56214. };
  56215. const byte shBadCaNamesExt2[] = {
  56216. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  56217. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  56218. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  56219. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  56220. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  56221. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  56222. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  56223. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  56224. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  56225. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  56226. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  56227. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  56228. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  56229. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  56230. };
  56231. int i = 0;
  56232. for (i = 0; i < 2; i++) {
  56233. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56234. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  56235. wolfTLSv1_3_client_method, NULL), 0);
  56236. switch (i) {
  56237. case 0:
  56238. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  56239. sizeof(shBadCaNamesExt));
  56240. test_ctx.c_len = sizeof(shBadCaNamesExt);
  56241. break;
  56242. case 1:
  56243. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  56244. sizeof(shBadCaNamesExt2));
  56245. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  56246. break;
  56247. }
  56248. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  56249. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  56250. wolfSSL_free(ssl_c);
  56251. ssl_c = NULL;
  56252. wolfSSL_CTX_free(ctx_c);
  56253. ctx_c = NULL;
  56254. }
  56255. #endif
  56256. return EXPECT_RESULT();
  56257. }
  56258. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56259. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56260. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  56261. {
  56262. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  56263. WOLFSSL_SUCCESS);
  56264. }
  56265. static int test_dtls_1_0_hvr_downgrade(void)
  56266. {
  56267. EXPECT_DECLS;
  56268. callback_functions func_cb_client;
  56269. callback_functions func_cb_server;
  56270. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56271. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56272. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56273. func_cb_client.method = wolfDTLS_client_method;
  56274. func_cb_server.method = wolfDTLSv1_2_server_method;
  56275. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  56276. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56277. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56278. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56279. return EXPECT_RESULT();
  56280. }
  56281. #else
  56282. static int test_dtls_1_0_hvr_downgrade(void)
  56283. {
  56284. EXPECT_DECLS;
  56285. return EXPECT_RESULT();
  56286. }
  56287. #endif
  56288. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  56289. defined(HAVE_SESSION_TICKET)
  56290. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  56291. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  56292. {
  56293. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  56294. AssertNotNull(test_session_ticket_no_id_session);
  56295. }
  56296. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  56297. {
  56298. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  56299. }
  56300. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  56301. {
  56302. test_session_ticket_no_id_session->sessionIDSz = 0;
  56303. AssertIntEQ(WOLFSSL_SUCCESS,
  56304. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  56305. }
  56306. static int test_session_ticket_no_id(void)
  56307. {
  56308. /* We are testing an expired (invalid crypto context in out case since the
  56309. * ctx changes) session ticket being sent with the session ID being 0
  56310. * length. */
  56311. EXPECT_DECLS;
  56312. callback_functions func_cb_client;
  56313. callback_functions func_cb_server;
  56314. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  56315. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  56316. func_cb_client.method = wolfTLSv1_2_client_method;
  56317. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  56318. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  56319. func_cb_server.method = wolfTLSv1_2_server_method;
  56320. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  56321. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56322. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56323. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56324. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  56325. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  56326. func_cb_client.method = wolfTLSv1_2_client_method;
  56327. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  56328. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  56329. func_cb_server.method = wolfTLSv1_2_server_method;
  56330. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  56331. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56332. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56333. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56334. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  56335. return EXPECT_RESULT();
  56336. }
  56337. #else
  56338. static int test_session_ticket_no_id(void)
  56339. {
  56340. EXPECT_DECLS;
  56341. return EXPECT_RESULT();
  56342. }
  56343. #endif
  56344. static int test_session_ticket_hs_update(void)
  56345. {
  56346. EXPECT_DECLS;
  56347. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  56348. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  56349. struct test_memio_ctx test_ctx;
  56350. struct test_memio_ctx test_ctx2;
  56351. struct test_memio_ctx test_ctx3;
  56352. WOLFSSL_CTX *ctx_c = NULL;
  56353. WOLFSSL_CTX *ctx_s = NULL;
  56354. WOLFSSL *ssl_c = NULL;
  56355. WOLFSSL *ssl_c2 = NULL;
  56356. WOLFSSL *ssl_c3 = NULL;
  56357. WOLFSSL *ssl_s = NULL;
  56358. WOLFSSL *ssl_s2 = NULL;
  56359. WOLFSSL *ssl_s3 = NULL;
  56360. WOLFSSL_SESSION *sess = NULL;
  56361. byte read_data[1];
  56362. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56363. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  56364. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  56365. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56366. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  56367. /* Generate tickets */
  56368. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56369. wolfSSL_SetLoggingPrefix("client");
  56370. /* Read the ticket msg */
  56371. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  56372. WOLFSSL_FATAL_ERROR);
  56373. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56374. WOLFSSL_ERROR_WANT_READ);
  56375. wolfSSL_SetLoggingPrefix(NULL);
  56376. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  56377. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  56378. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  56379. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  56380. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  56381. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  56382. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  56383. wolfSSL_SetLoggingPrefix("client");
  56384. /* Exchange initial flights for the second connection */
  56385. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  56386. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  56387. WOLFSSL_ERROR_WANT_READ);
  56388. wolfSSL_SetLoggingPrefix(NULL);
  56389. wolfSSL_SetLoggingPrefix("server");
  56390. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  56391. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  56392. WOLFSSL_ERROR_WANT_READ);
  56393. wolfSSL_SetLoggingPrefix(NULL);
  56394. /* Complete third connection so that new tickets are exchanged */
  56395. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  56396. /* Read the ticket msg */
  56397. wolfSSL_SetLoggingPrefix("client");
  56398. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  56399. WOLFSSL_FATAL_ERROR);
  56400. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  56401. WOLFSSL_ERROR_WANT_READ);
  56402. wolfSSL_SetLoggingPrefix(NULL);
  56403. /* Complete second connection */
  56404. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  56405. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  56406. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  56407. wolfSSL_free(ssl_c);
  56408. wolfSSL_free(ssl_c2);
  56409. wolfSSL_free(ssl_c3);
  56410. wolfSSL_free(ssl_s);
  56411. wolfSSL_free(ssl_s2);
  56412. wolfSSL_free(ssl_s3);
  56413. wolfSSL_CTX_free(ctx_c);
  56414. wolfSSL_CTX_free(ctx_s);
  56415. wolfSSL_SESSION_free(sess);
  56416. #endif
  56417. return EXPECT_RESULT();
  56418. }
  56419. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56420. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  56421. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  56422. {
  56423. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  56424. WOLFSSL_SUCCESS);
  56425. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  56426. }
  56427. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  56428. {
  56429. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  56430. }
  56431. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  56432. {
  56433. char testMsg[] = "Message after SCR";
  56434. char msgBuf[sizeof(testMsg)];
  56435. if (wolfSSL_is_server(ssl)) {
  56436. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  56437. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  56438. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  56439. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  56440. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  56441. sizeof(testMsg));
  56442. }
  56443. else {
  56444. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  56445. sizeof(testMsg));
  56446. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  56447. }
  56448. }
  56449. static int test_dtls_downgrade_scr_server(void)
  56450. {
  56451. EXPECT_DECLS;
  56452. callback_functions func_cb_client;
  56453. callback_functions func_cb_server;
  56454. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56455. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56456. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56457. func_cb_client.method = wolfDTLSv1_2_client_method;
  56458. func_cb_server.method = wolfDTLS_server_method;
  56459. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  56460. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  56461. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  56462. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  56463. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56464. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56465. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56466. return EXPECT_RESULT();
  56467. }
  56468. #else
  56469. static int test_dtls_downgrade_scr_server(void)
  56470. {
  56471. EXPECT_DECLS;
  56472. return EXPECT_RESULT();
  56473. }
  56474. #endif
  56475. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56476. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  56477. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  56478. {
  56479. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  56480. WOLFSSL_SUCCESS);
  56481. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  56482. }
  56483. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  56484. {
  56485. char testMsg[] = "Message after SCR";
  56486. char msgBuf[sizeof(testMsg)];
  56487. if (wolfSSL_is_server(ssl)) {
  56488. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  56489. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  56490. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  56491. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  56492. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  56493. sizeof(testMsg));
  56494. }
  56495. else {
  56496. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  56497. sizeof(testMsg));
  56498. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  56499. }
  56500. }
  56501. static int test_dtls_downgrade_scr(void)
  56502. {
  56503. EXPECT_DECLS;
  56504. callback_functions func_cb_client;
  56505. callback_functions func_cb_server;
  56506. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56507. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56508. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56509. func_cb_client.method = wolfDTLS_client_method;
  56510. func_cb_server.method = wolfDTLSv1_2_server_method;
  56511. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  56512. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  56513. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  56514. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56515. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  56516. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  56517. return EXPECT_RESULT();
  56518. }
  56519. #else
  56520. static int test_dtls_downgrade_scr(void)
  56521. {
  56522. EXPECT_DECLS;
  56523. return EXPECT_RESULT();
  56524. }
  56525. #endif
  56526. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  56527. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  56528. char *data, int sz, void *ctx)
  56529. {
  56530. static int call_counter = 0;
  56531. call_counter++;
  56532. (void)ssl;
  56533. (void)data;
  56534. (void)sz;
  56535. (void)ctx;
  56536. switch (call_counter) {
  56537. case 1:
  56538. case 2:
  56539. return WOLFSSL_CBIO_ERR_TIMEOUT;
  56540. case 3:
  56541. return WOLFSSL_CBIO_ERR_WANT_READ;
  56542. default:
  56543. AssertIntLE(call_counter, 3);
  56544. return -1;
  56545. }
  56546. }
  56547. #endif
  56548. /* Make sure we don't send acks before getting a server hello */
  56549. static int test_dtls_client_hello_timeout_downgrade(void)
  56550. {
  56551. EXPECT_DECLS;
  56552. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  56553. WOLFSSL_CTX *ctx_c = NULL;
  56554. WOLFSSL_CTX *ctx_s = NULL;
  56555. WOLFSSL *ssl_c = NULL;
  56556. WOLFSSL *ssl_s = NULL;
  56557. struct test_memio_ctx test_ctx;
  56558. DtlsRecordLayerHeader* dtlsRH;
  56559. size_t len;
  56560. byte sequence_number[8];
  56561. int i;
  56562. for (i = 0; i < 2; i++) {
  56563. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56564. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56565. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  56566. if (i == 0) {
  56567. /* First time simulate timeout in IO layer */
  56568. /* CH1 */
  56569. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56570. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56571. /* HVR */
  56572. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56573. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56574. /* CH2 */
  56575. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56576. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56577. /* SH flight */
  56578. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56579. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56580. /* Drop the SH */
  56581. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  56582. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56583. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  56584. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  56585. (sizeof(DtlsRecordLayerHeader) + len));
  56586. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  56587. /* Read the remainder of the flight */
  56588. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56589. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56590. wolfSSL_SSLSetIORecv(ssl_c,
  56591. test_dtls_client_hello_timeout_downgrade_read_cb);
  56592. /* CH3 */
  56593. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56594. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56595. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  56596. }
  56597. else {
  56598. /* Second time call wolfSSL_dtls_got_timeout */
  56599. /* CH1 */
  56600. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56601. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56602. /* HVR */
  56603. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56604. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56605. /* CH2 */
  56606. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56607. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56608. /* SH flight */
  56609. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56610. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56611. /* Drop the SH */
  56612. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  56613. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56614. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  56615. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  56616. (sizeof(DtlsRecordLayerHeader) + len));
  56617. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  56618. /* Read the remainder of the flight */
  56619. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56620. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56621. /* Quick timeout should be set as we received at least one msg */
  56622. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  56623. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  56624. /* Quick timeout should be cleared after a quick timeout */
  56625. /* CH3 */
  56626. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  56627. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  56628. }
  56629. /* Parse out to make sure we got exactly one ClientHello message */
  56630. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  56631. /* Second ClientHello after HVR */
  56632. sequence_number[7] = 2;
  56633. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  56634. ExpectIntEQ(dtlsRH->type, handshake);
  56635. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  56636. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  56637. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  56638. sizeof(sequence_number)), 0);
  56639. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56640. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  56641. /* Connection should be able to continue */
  56642. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56643. wolfSSL_free(ssl_c);
  56644. wolfSSL_free(ssl_s);
  56645. wolfSSL_CTX_free(ctx_c);
  56646. wolfSSL_CTX_free(ctx_s);
  56647. ssl_c = NULL;
  56648. ssl_s = NULL;
  56649. ctx_c = NULL;
  56650. ctx_s = NULL;
  56651. if (!EXPECT_SUCCESS())
  56652. break;
  56653. }
  56654. #endif
  56655. return EXPECT_RESULT();
  56656. }
  56657. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  56658. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  56659. int sz, void *ctx)
  56660. {
  56661. static int call_counter = 0;
  56662. call_counter++;
  56663. (void)ssl;
  56664. (void)data;
  56665. (void)sz;
  56666. (void)ctx;
  56667. switch (call_counter) {
  56668. case 1:
  56669. return WOLFSSL_CBIO_ERR_TIMEOUT;
  56670. case 2:
  56671. return WOLFSSL_CBIO_ERR_WANT_READ;
  56672. default:
  56673. AssertIntLE(call_counter, 2);
  56674. return -1;
  56675. }
  56676. }
  56677. #endif
  56678. /* Make sure we don't send acks before getting a server hello */
  56679. static int test_dtls_client_hello_timeout(void)
  56680. {
  56681. EXPECT_DECLS;
  56682. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  56683. WOLFSSL *ssl_c = NULL;
  56684. WOLFSSL_CTX *ctx_c = NULL;
  56685. struct test_memio_ctx test_ctx;
  56686. DtlsRecordLayerHeader* dtlsRH;
  56687. size_t idx;
  56688. size_t len;
  56689. byte sequence_number[8];
  56690. int i;
  56691. for (i = 0; i < 2; i++) {
  56692. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56693. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  56694. wolfDTLSv1_3_client_method, NULL), 0);
  56695. if (i == 0) {
  56696. /* First time simulate timeout in IO layer */
  56697. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  56698. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  56699. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56700. }
  56701. else {
  56702. /* Second time call wolfSSL_dtls_got_timeout */
  56703. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  56704. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56705. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  56706. }
  56707. /* Parse out to make sure we got exactly two ClientHello messages */
  56708. idx = 0;
  56709. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  56710. /* First ClientHello */
  56711. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  56712. ExpectIntEQ(dtlsRH->type, handshake);
  56713. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  56714. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  56715. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  56716. sizeof(sequence_number)), 0);
  56717. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56718. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  56719. idx += sizeof(DtlsRecordLayerHeader) + len;
  56720. /* Second ClientHello */
  56721. sequence_number[7] = 1;
  56722. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  56723. ExpectIntEQ(dtlsRH->type, handshake);
  56724. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  56725. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  56726. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  56727. sizeof(sequence_number)), 0);
  56728. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56729. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  56730. wolfSSL_free(ssl_c);
  56731. wolfSSL_CTX_free(ctx_c);
  56732. ssl_c = NULL;
  56733. ctx_c = NULL;
  56734. if (!EXPECT_SUCCESS())
  56735. break;
  56736. }
  56737. #endif
  56738. return EXPECT_RESULT();
  56739. }
  56740. /* DTLS test when dropping the changed cipher spec message */
  56741. static int test_dtls_dropped_ccs(void)
  56742. {
  56743. EXPECT_DECLS;
  56744. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  56745. WOLFSSL_CTX *ctx_c = NULL;
  56746. WOLFSSL_CTX *ctx_s = NULL;
  56747. WOLFSSL *ssl_c = NULL;
  56748. WOLFSSL *ssl_s = NULL;
  56749. struct test_memio_ctx test_ctx;
  56750. DtlsRecordLayerHeader* dtlsRH;
  56751. size_t len;
  56752. byte data[1];
  56753. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56754. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56755. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  56756. /* CH1 */
  56757. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56758. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56759. /* HVR */
  56760. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56761. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56762. /* CH2 */
  56763. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56764. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56765. /* Server first flight */
  56766. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  56767. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56768. /* Client flight */
  56769. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56770. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56771. /* Server ccs + finished */
  56772. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  56773. /* Drop the ccs */
  56774. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  56775. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  56776. ExpectIntEQ(len, 1);
  56777. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  56778. if (EXPECT_SUCCESS()) {
  56779. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  56780. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  56781. (sizeof(DtlsRecordLayerHeader) + len));
  56782. }
  56783. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  56784. /* Client rtx flight */
  56785. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  56786. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  56787. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  56788. /* Server ccs + finished rtx */
  56789. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  56790. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  56791. /* Client processes finished */
  56792. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  56793. wolfSSL_free(ssl_c);
  56794. wolfSSL_free(ssl_s);
  56795. wolfSSL_CTX_free(ctx_c);
  56796. wolfSSL_CTX_free(ctx_s);
  56797. #endif
  56798. return EXPECT_RESULT();
  56799. }
  56800. /**
  56801. * Make sure we don't send RSA Signature Hash Algorithms in the
  56802. * CertificateRequest when we don't have any such ciphers set.
  56803. * @return EXPECT_RESULT()
  56804. */
  56805. static int test_certreq_sighash_algos(void)
  56806. {
  56807. EXPECT_DECLS;
  56808. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  56809. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  56810. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  56811. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  56812. WOLFSSL_CTX *ctx_c = NULL;
  56813. WOLFSSL_CTX *ctx_s = NULL;
  56814. WOLFSSL *ssl_c = NULL;
  56815. WOLFSSL *ssl_s = NULL;
  56816. struct test_memio_ctx test_ctx;
  56817. int idx = 0;
  56818. int maxIdx = 0;
  56819. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  56820. test_ctx.c_ciphers = test_ctx.s_ciphers =
  56821. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  56822. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  56823. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  56824. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  56825. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  56826. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  56827. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  56828. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56829. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  56830. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56831. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  56832. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  56833. WOLFSSL_ERROR_WANT_READ);
  56834. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  56835. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  56836. WOLFSSL_ERROR_WANT_READ);
  56837. /* Find the CertificateRequest message */
  56838. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  56839. word16 len;
  56840. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  56841. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  56842. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  56843. ato16(test_ctx.c_buff + idx, &len);
  56844. idx += OPAQUE16_LEN;
  56845. if (test_ctx.c_buff[idx] == certificate_request) {
  56846. idx++;
  56847. /* length */
  56848. idx += OPAQUE24_LEN;
  56849. /* cert types */
  56850. idx += 1 + test_ctx.c_buff[idx];
  56851. /* Sig algos */
  56852. ato16(test_ctx.c_buff + idx, &len);
  56853. idx += OPAQUE16_LEN;
  56854. maxIdx = idx + (int)len;
  56855. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  56856. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  56857. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  56858. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  56859. else
  56860. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  56861. }
  56862. break;
  56863. }
  56864. else {
  56865. idx += (int)len;
  56866. }
  56867. }
  56868. ExpectIntLT(idx, test_ctx.c_len);
  56869. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  56870. wolfSSL_free(ssl_c);
  56871. wolfSSL_free(ssl_s);
  56872. wolfSSL_CTX_free(ctx_c);
  56873. wolfSSL_CTX_free(ctx_s);
  56874. #endif
  56875. return EXPECT_RESULT();
  56876. }
  56877. /*----------------------------------------------------------------------------*
  56878. | Main
  56879. *----------------------------------------------------------------------------*/
  56880. typedef int (*TEST_FUNC)(void);
  56881. typedef struct {
  56882. const char *name;
  56883. TEST_FUNC func;
  56884. byte run:1;
  56885. byte fail:1;
  56886. } TEST_CASE;
  56887. #define TEST_DECL(func) { #func, func, 0, 0 }
  56888. int testAll = 1;
  56889. TEST_CASE testCases[] = {
  56890. TEST_DECL(test_fileAccess),
  56891. /*********************************
  56892. * wolfcrypt
  56893. *********************************/
  56894. TEST_DECL(test_ForceZero),
  56895. TEST_DECL(test_wolfCrypt_Init),
  56896. /* Locking with Compat Mutex */
  56897. TEST_DECL(test_wc_SetMutexCb),
  56898. TEST_DECL(test_wc_LockMutex_ex),
  56899. /* Digests */
  56900. TEST_DECL(test_wc_InitMd5),
  56901. TEST_DECL(test_wc_Md5Update),
  56902. TEST_DECL(test_wc_Md5Final),
  56903. TEST_DECL(test_wc_InitSha),
  56904. TEST_DECL(test_wc_ShaUpdate),
  56905. TEST_DECL(test_wc_ShaFinal),
  56906. TEST_DECL(test_wc_InitSha256),
  56907. TEST_DECL(test_wc_Sha256Update),
  56908. TEST_DECL(test_wc_Sha256Final),
  56909. TEST_DECL(test_wc_Sha256FinalRaw),
  56910. TEST_DECL(test_wc_Sha256GetFlags),
  56911. TEST_DECL(test_wc_Sha256Free),
  56912. TEST_DECL(test_wc_Sha256GetHash),
  56913. TEST_DECL(test_wc_Sha256Copy),
  56914. TEST_DECL(test_wc_InitSha224),
  56915. TEST_DECL(test_wc_Sha224Update),
  56916. TEST_DECL(test_wc_Sha224Final),
  56917. TEST_DECL(test_wc_Sha224SetFlags),
  56918. TEST_DECL(test_wc_Sha224GetFlags),
  56919. TEST_DECL(test_wc_Sha224Free),
  56920. TEST_DECL(test_wc_Sha224GetHash),
  56921. TEST_DECL(test_wc_Sha224Copy),
  56922. TEST_DECL(test_wc_InitSha512),
  56923. TEST_DECL(test_wc_Sha512Update),
  56924. TEST_DECL(test_wc_Sha512Final),
  56925. TEST_DECL(test_wc_Sha512GetFlags),
  56926. TEST_DECL(test_wc_Sha512FinalRaw),
  56927. TEST_DECL(test_wc_Sha512Free),
  56928. TEST_DECL(test_wc_Sha512GetHash),
  56929. TEST_DECL(test_wc_Sha512Copy),
  56930. TEST_DECL(test_wc_InitSha512_224),
  56931. TEST_DECL(test_wc_Sha512_224Update),
  56932. TEST_DECL(test_wc_Sha512_224Final),
  56933. TEST_DECL(test_wc_Sha512_224GetFlags),
  56934. TEST_DECL(test_wc_Sha512_224FinalRaw),
  56935. TEST_DECL(test_wc_Sha512_224Free),
  56936. TEST_DECL(test_wc_Sha512_224GetHash),
  56937. TEST_DECL(test_wc_Sha512_224Copy),
  56938. TEST_DECL(test_wc_InitSha512_256),
  56939. TEST_DECL(test_wc_Sha512_256Update),
  56940. TEST_DECL(test_wc_Sha512_256Final),
  56941. TEST_DECL(test_wc_Sha512_256GetFlags),
  56942. TEST_DECL(test_wc_Sha512_256FinalRaw),
  56943. TEST_DECL(test_wc_Sha512_256Free),
  56944. TEST_DECL(test_wc_Sha512_256GetHash),
  56945. TEST_DECL(test_wc_Sha512_256Copy),
  56946. TEST_DECL(test_wc_InitSha384),
  56947. TEST_DECL(test_wc_Sha384Update),
  56948. TEST_DECL(test_wc_Sha384Final),
  56949. TEST_DECL(test_wc_Sha384GetFlags),
  56950. TEST_DECL(test_wc_Sha384FinalRaw),
  56951. TEST_DECL(test_wc_Sha384Free),
  56952. TEST_DECL(test_wc_Sha384GetHash),
  56953. TEST_DECL(test_wc_Sha384Copy),
  56954. TEST_DECL(test_wc_InitBlake2b),
  56955. TEST_DECL(test_wc_InitBlake2b_WithKey),
  56956. TEST_DECL(test_wc_InitBlake2s_WithKey),
  56957. TEST_DECL(test_wc_InitRipeMd),
  56958. TEST_DECL(test_wc_RipeMdUpdate),
  56959. TEST_DECL(test_wc_RipeMdFinal),
  56960. TEST_DECL(test_wc_InitSha3),
  56961. TEST_DECL(testing_wc_Sha3_Update),
  56962. TEST_DECL(test_wc_Sha3_224_Final),
  56963. TEST_DECL(test_wc_Sha3_256_Final),
  56964. TEST_DECL(test_wc_Sha3_384_Final),
  56965. TEST_DECL(test_wc_Sha3_512_Final),
  56966. TEST_DECL(test_wc_Sha3_224_Copy),
  56967. TEST_DECL(test_wc_Sha3_256_Copy),
  56968. TEST_DECL(test_wc_Sha3_384_Copy),
  56969. TEST_DECL(test_wc_Sha3_512_Copy),
  56970. TEST_DECL(test_wc_Sha3_GetFlags),
  56971. TEST_DECL(test_wc_InitShake256),
  56972. TEST_DECL(testing_wc_Shake256_Update),
  56973. TEST_DECL(test_wc_Shake256_Final),
  56974. TEST_DECL(test_wc_Shake256_Copy),
  56975. TEST_DECL(test_wc_Shake256Hash),
  56976. /* SM3 Digest */
  56977. TEST_DECL(test_wc_InitSm3Free),
  56978. TEST_DECL(test_wc_Sm3UpdateFinal),
  56979. TEST_DECL(test_wc_Sm3GetHash),
  56980. TEST_DECL(test_wc_Sm3Copy),
  56981. TEST_DECL(test_wc_Sm3FinalRaw),
  56982. TEST_DECL(test_wc_Sm3GetSetFlags),
  56983. TEST_DECL(test_wc_Sm3Hash),
  56984. TEST_DECL(test_wc_HashInit),
  56985. TEST_DECL(test_wc_HashSetFlags),
  56986. TEST_DECL(test_wc_HashGetFlags),
  56987. /* HMAC */
  56988. TEST_DECL(test_wc_Md5HmacSetKey),
  56989. TEST_DECL(test_wc_Md5HmacUpdate),
  56990. TEST_DECL(test_wc_Md5HmacFinal),
  56991. TEST_DECL(test_wc_ShaHmacSetKey),
  56992. TEST_DECL(test_wc_ShaHmacUpdate),
  56993. TEST_DECL(test_wc_ShaHmacFinal),
  56994. TEST_DECL(test_wc_Sha224HmacSetKey),
  56995. TEST_DECL(test_wc_Sha224HmacUpdate),
  56996. TEST_DECL(test_wc_Sha224HmacFinal),
  56997. TEST_DECL(test_wc_Sha256HmacSetKey),
  56998. TEST_DECL(test_wc_Sha256HmacUpdate),
  56999. TEST_DECL(test_wc_Sha256HmacFinal),
  57000. TEST_DECL(test_wc_Sha384HmacSetKey),
  57001. TEST_DECL(test_wc_Sha384HmacUpdate),
  57002. TEST_DECL(test_wc_Sha384HmacFinal),
  57003. /* CMAC */
  57004. TEST_DECL(test_wc_InitCmac),
  57005. TEST_DECL(test_wc_CmacUpdate),
  57006. TEST_DECL(test_wc_CmacFinal),
  57007. TEST_DECL(test_wc_AesCmacGenerate),
  57008. /* Cipher */
  57009. TEST_DECL(test_wc_AesGcmStream),
  57010. TEST_DECL(test_wc_Des3_SetIV),
  57011. TEST_DECL(test_wc_Des3_SetKey),
  57012. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  57013. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  57014. TEST_DECL(test_wc_Des3_EcbEncrypt),
  57015. TEST_DECL(test_wc_Chacha_SetKey),
  57016. TEST_DECL(test_wc_Chacha_Process),
  57017. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  57018. TEST_DECL(test_wc_Poly1305SetKey),
  57019. TEST_DECL(test_wc_CamelliaSetKey),
  57020. TEST_DECL(test_wc_CamelliaSetIV),
  57021. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  57022. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  57023. TEST_DECL(test_wc_Arc4SetKey),
  57024. TEST_DECL(test_wc_Arc4Process),
  57025. TEST_DECL(test_wc_Rc2SetKey),
  57026. TEST_DECL(test_wc_Rc2SetIV),
  57027. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  57028. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  57029. /* AES cipher and GMAC. */
  57030. TEST_DECL(test_wc_AesSetKey),
  57031. TEST_DECL(test_wc_AesSetIV),
  57032. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  57033. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  57034. TEST_DECL(test_wc_AesGcmSetKey),
  57035. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  57036. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  57037. TEST_DECL(test_wc_GmacSetKey),
  57038. TEST_DECL(test_wc_GmacUpdate),
  57039. TEST_DECL(test_wc_AesCcmSetKey),
  57040. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  57041. /* SM4 cipher */
  57042. TEST_DECL(test_wc_Sm4),
  57043. TEST_DECL(test_wc_Sm4Ecb),
  57044. TEST_DECL(test_wc_Sm4Cbc),
  57045. TEST_DECL(test_wc_Sm4Ctr),
  57046. TEST_DECL(test_wc_Sm4Gcm),
  57047. TEST_DECL(test_wc_Sm4Ccm),
  57048. /* RNG tests */
  57049. #ifdef HAVE_HASHDRBG
  57050. #ifdef TEST_RESEED_INTERVAL
  57051. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  57052. #endif
  57053. TEST_DECL(test_wc_RNG_GenerateBlock),
  57054. #endif
  57055. TEST_DECL(test_get_rand_digit),
  57056. TEST_DECL(test_wc_InitRngNonce),
  57057. TEST_DECL(test_wc_InitRngNonce_ex),
  57058. /* MP API tests */
  57059. TEST_DECL(test_get_digit_count),
  57060. TEST_DECL(test_mp_cond_copy),
  57061. TEST_DECL(test_mp_rand),
  57062. TEST_DECL(test_get_digit),
  57063. TEST_DECL(test_wc_export_int),
  57064. /* RSA */
  57065. TEST_DECL(test_wc_InitRsaKey),
  57066. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  57067. TEST_DECL(test_wc_RsaPublicKeyDecode),
  57068. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  57069. TEST_DECL(test_wc_MakeRsaKey),
  57070. TEST_DECL(test_wc_CheckProbablePrime),
  57071. TEST_DECL(test_wc_RsaPSS_Verify),
  57072. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  57073. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  57074. TEST_DECL(test_wc_RsaKeyToDer),
  57075. TEST_DECL(test_wc_RsaKeyToPublicDer),
  57076. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  57077. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  57078. TEST_DECL(test_wc_RsaEncryptSize),
  57079. TEST_DECL(test_wc_RsaSSL_SignVerify),
  57080. TEST_DECL(test_wc_RsaFlattenPublicKey),
  57081. TEST_DECL(test_RsaDecryptBoundsCheck),
  57082. /* DSA */
  57083. TEST_DECL(test_wc_InitDsaKey),
  57084. TEST_DECL(test_wc_DsaSignVerify),
  57085. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  57086. TEST_DECL(test_wc_MakeDsaKey),
  57087. TEST_DECL(test_wc_DsaKeyToDer),
  57088. TEST_DECL(test_wc_DsaKeyToPublicDer),
  57089. TEST_DECL(test_wc_DsaImportParamsRaw),
  57090. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  57091. TEST_DECL(test_wc_DsaExportParamsRaw),
  57092. TEST_DECL(test_wc_DsaExportKeyRaw),
  57093. /* DH */
  57094. TEST_DECL(test_wc_DhPublicKeyDecode),
  57095. /* wolfCrypt ECC tests */
  57096. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  57097. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  57098. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  57099. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  57100. !defined(HAVE_SELFTEST) && \
  57101. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  57102. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  57103. #endif
  57104. TEST_DECL(test_wc_ecc_make_key),
  57105. TEST_DECL(test_wc_ecc_init),
  57106. TEST_DECL(test_wc_ecc_check_key),
  57107. TEST_DECL(test_wc_ecc_get_generator),
  57108. TEST_DECL(test_wc_ecc_size),
  57109. TEST_DECL(test_wc_ecc_params),
  57110. TEST_DECL(test_wc_ecc_signVerify_hash),
  57111. TEST_DECL(test_wc_ecc_shared_secret),
  57112. TEST_DECL(test_wc_ecc_export_x963),
  57113. TEST_DECL(test_wc_ecc_export_x963_ex),
  57114. TEST_DECL(test_wc_ecc_import_x963),
  57115. TEST_DECL(test_wc_ecc_import_private_key),
  57116. TEST_DECL(test_wc_ecc_export_private_only),
  57117. TEST_DECL(test_wc_ecc_rs_to_sig),
  57118. TEST_DECL(test_wc_ecc_import_raw),
  57119. TEST_DECL(test_wc_ecc_import_unsigned),
  57120. TEST_DECL(test_wc_ecc_sig_size),
  57121. TEST_DECL(test_wc_ecc_ctx_new),
  57122. TEST_DECL(test_wc_ecc_ctx_reset),
  57123. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  57124. TEST_DECL(test_wc_ecc_ctx_set_info),
  57125. TEST_DECL(test_wc_ecc_encryptDecrypt),
  57126. TEST_DECL(test_wc_ecc_del_point),
  57127. TEST_DECL(test_wc_ecc_pointFns),
  57128. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  57129. TEST_DECL(test_wc_ecc_verify_hash_ex),
  57130. TEST_DECL(test_wc_ecc_mulmod),
  57131. TEST_DECL(test_wc_ecc_is_valid_idx),
  57132. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  57133. TEST_DECL(test_wc_ecc_sig_size_calc),
  57134. TEST_DECL(test_wc_EccPrivateKeyToDer),
  57135. /* SM2 elliptic curve */
  57136. TEST_DECL(test_wc_ecc_sm2_make_key),
  57137. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  57138. TEST_DECL(test_wc_ecc_sm2_create_digest),
  57139. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  57140. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  57141. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  57142. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  57143. /* Curve25519 */
  57144. TEST_DECL(test_wc_curve25519_init),
  57145. TEST_DECL(test_wc_curve25519_size),
  57146. TEST_DECL(test_wc_curve25519_export_key_raw),
  57147. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  57148. TEST_DECL(test_wc_curve25519_make_key),
  57149. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  57150. TEST_DECL(test_wc_curve25519_make_pub),
  57151. TEST_DECL(test_wc_curve25519_export_public_ex),
  57152. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  57153. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  57154. TEST_DECL(test_wc_curve25519_import_private),
  57155. /* ED25519 */
  57156. TEST_DECL(test_wc_ed25519_make_key),
  57157. TEST_DECL(test_wc_ed25519_init),
  57158. TEST_DECL(test_wc_ed25519_sign_msg),
  57159. TEST_DECL(test_wc_ed25519_import_public),
  57160. TEST_DECL(test_wc_ed25519_import_private_key),
  57161. TEST_DECL(test_wc_ed25519_export),
  57162. TEST_DECL(test_wc_ed25519_size),
  57163. TEST_DECL(test_wc_ed25519_exportKey),
  57164. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  57165. TEST_DECL(test_wc_Ed25519KeyToDer),
  57166. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  57167. /* Curve448 */
  57168. TEST_DECL(test_wc_curve448_make_key),
  57169. TEST_DECL(test_wc_curve448_shared_secret_ex),
  57170. TEST_DECL(test_wc_curve448_export_public_ex),
  57171. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  57172. TEST_DECL(test_wc_curve448_export_key_raw),
  57173. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  57174. TEST_DECL(test_wc_curve448_import_private),
  57175. TEST_DECL(test_wc_curve448_init),
  57176. TEST_DECL(test_wc_curve448_size),
  57177. /* Ed448 */
  57178. TEST_DECL(test_wc_ed448_make_key),
  57179. TEST_DECL(test_wc_ed448_init),
  57180. TEST_DECL(test_wc_ed448_sign_msg),
  57181. TEST_DECL(test_wc_ed448_import_public),
  57182. TEST_DECL(test_wc_ed448_import_private_key),
  57183. TEST_DECL(test_wc_ed448_export),
  57184. TEST_DECL(test_wc_ed448_size),
  57185. TEST_DECL(test_wc_ed448_exportKey),
  57186. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  57187. TEST_DECL(test_wc_Ed448KeyToDer),
  57188. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  57189. /* Signature API */
  57190. TEST_DECL(test_wc_SignatureGetSize_ecc),
  57191. TEST_DECL(test_wc_SignatureGetSize_rsa),
  57192. /* PEM and DER APIs. */
  57193. TEST_DECL(test_wc_PemToDer),
  57194. TEST_DECL(test_wc_AllocDer),
  57195. TEST_DECL(test_wc_CertPemToDer),
  57196. TEST_DECL(test_wc_KeyPemToDer),
  57197. TEST_DECL(test_wc_PubKeyPemToDer),
  57198. TEST_DECL(test_wc_PemPubKeyToDer),
  57199. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  57200. TEST_DECL(test_wc_CheckCertSigPubKey),
  57201. /* wolfCrypt ASN tests */
  57202. TEST_DECL(test_ToTraditional),
  57203. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  57204. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  57205. /* Certificate */
  57206. TEST_DECL(test_wc_SetSubjectRaw),
  57207. TEST_DECL(test_wc_GetSubjectRaw),
  57208. TEST_DECL(test_wc_SetIssuerRaw),
  57209. TEST_DECL(test_wc_SetIssueBuffer),
  57210. TEST_DECL(test_wc_SetSubjectKeyId),
  57211. TEST_DECL(test_wc_SetSubject),
  57212. TEST_DECL(test_CheckCertSignature),
  57213. TEST_DECL(test_wc_ParseCert),
  57214. TEST_DECL(test_wc_ParseCert_Error),
  57215. TEST_DECL(test_MakeCertWithPathLen),
  57216. TEST_DECL(test_wc_SetKeyUsage),
  57217. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  57218. TEST_DECL(test_wc_SetSubjectBuffer),
  57219. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  57220. /* wolfcrypt PKCS#7 */
  57221. TEST_DECL(test_wc_PKCS7_New),
  57222. TEST_DECL(test_wc_PKCS7_Init),
  57223. TEST_DECL(test_wc_PKCS7_InitWithCert),
  57224. TEST_DECL(test_wc_PKCS7_EncodeData),
  57225. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  57226. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  57227. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  57228. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  57229. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  57230. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  57231. TEST_DECL(test_wc_PKCS7_Degenerate),
  57232. TEST_DECL(test_wc_PKCS7_BER),
  57233. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  57234. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  57235. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  57236. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  57237. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  57238. /* wolfCrypt PKCS#12 */
  57239. TEST_DECL(test_wc_i2d_PKCS12),
  57240. /*
  57241. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  57242. * avoid memory leaks.
  57243. */
  57244. TEST_DECL(test_wolfCrypt_Cleanup),
  57245. TEST_DECL(test_wolfSSL_Init),
  57246. /*********************************
  57247. * OpenSSL compatibility API tests
  57248. *********************************/
  57249. /* If at some point a stub get implemented this test should fail indicating
  57250. * a need to implement a new test case
  57251. */
  57252. TEST_DECL(test_stubs_are_stubs),
  57253. /* ASN.1 compatibility API tests */
  57254. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  57255. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  57256. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  57257. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  57258. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  57259. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  57260. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  57261. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  57262. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  57263. TEST_DECL(test_wolfSSL_ASN1_get_object),
  57264. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  57265. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  57266. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  57267. TEST_DECL(test_wolfSSL_ASN1_STRING),
  57268. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  57269. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  57270. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  57271. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  57272. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  57273. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  57274. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  57275. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  57276. TEST_DECL(test_wolfSSL_ASN1_TIME),
  57277. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  57278. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  57279. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  57280. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  57281. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  57282. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  57283. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  57284. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  57285. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  57286. TEST_DECL(test_wolfSSL_lhash),
  57287. TEST_DECL(test_wolfSSL_certs),
  57288. TEST_DECL(test_wolfSSL_DES),
  57289. TEST_DECL(test_wolfSSL_private_keys),
  57290. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  57291. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  57292. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  57293. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  57294. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  57295. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  57296. #ifndef NO_BIO
  57297. TEST_DECL(test_wolfSSL_BIO),
  57298. TEST_DECL(test_wolfSSL_PEM_read_bio),
  57299. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  57300. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  57301. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  57302. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  57303. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  57304. #endif
  57305. /* EVP API testing */
  57306. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  57307. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  57308. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  57309. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  57310. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  57311. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  57312. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  57313. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  57314. TEST_DECL(test_wolfSSL_EVP_shake128),
  57315. TEST_DECL(test_wolfSSL_EVP_shake256),
  57316. TEST_DECL(test_wolfSSL_EVP_sm3),
  57317. TEST_DECL(test_EVP_blake2),
  57318. #ifdef OPENSSL_ALL
  57319. TEST_DECL(test_wolfSSL_EVP_md4),
  57320. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  57321. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  57322. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  57323. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  57324. #endif
  57325. TEST_DECL(test_EVP_MD_do_all),
  57326. TEST_DECL(test_wolfSSL_EVP_MD_size),
  57327. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  57328. TEST_DECL(test_wolfSSL_EVP_Digest),
  57329. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  57330. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  57331. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  57332. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  57333. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  57334. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  57335. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  57336. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  57337. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  57338. TEST_DECL(test_wolfssl_EVP_chacha20),
  57339. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  57340. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  57341. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  57342. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  57343. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  57344. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  57345. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  57346. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  57347. #ifdef OPENSSL_ALL
  57348. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  57349. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  57350. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  57351. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  57352. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  57353. TEST_DECL(test_wolfSSL_EVP_rc4),
  57354. TEST_DECL(test_wolfSSL_EVP_enc_null),
  57355. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  57356. TEST_DECL(test_wolfSSL_EVP_mdc2),
  57357. TEST_DECL(test_evp_cipher_aes_gcm),
  57358. #endif
  57359. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  57360. #ifdef OPENSSL_EXTRA
  57361. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  57362. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  57363. #endif
  57364. #ifdef OPENSSL_ALL
  57365. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  57366. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  57367. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  57368. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  57369. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  57370. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  57371. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  57372. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  57373. #endif
  57374. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  57375. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  57376. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  57377. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  57378. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  57379. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  57380. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  57381. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  57382. #ifndef NO_BIO
  57383. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  57384. #endif
  57385. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  57386. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  57387. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  57388. #ifndef NO_BIO
  57389. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  57390. #endif /* !NO_BIO */
  57391. #endif
  57392. #ifdef OPENSSL_ALL
  57393. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  57394. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  57395. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  57396. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  57397. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  57398. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  57399. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  57400. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  57401. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  57402. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  57403. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  57404. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  57405. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  57406. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  57407. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  57408. #endif
  57409. TEST_DECL(test_EVP_PKEY_rsa),
  57410. TEST_DECL(test_EVP_PKEY_ec),
  57411. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  57412. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  57413. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  57414. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  57415. TEST_DECL(test_EVP_PKEY_cmp),
  57416. #ifdef OPENSSL_ALL
  57417. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  57418. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  57419. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  57420. #endif
  57421. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  57422. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  57423. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57424. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  57425. #endif
  57426. #ifndef NO_BIO
  57427. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  57428. TEST_DECL(test_wolfSSL_GetLoggingCb),
  57429. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  57430. TEST_DECL(test_wc_ERR_remove_state),
  57431. TEST_DECL(test_wc_ERR_print_errors_fp),
  57432. #endif
  57433. TEST_DECL(test_wolfSSL_configure_args),
  57434. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  57435. TEST_DECL(test_wolfSSL_set1_curves_list),
  57436. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  57437. TEST_DECL(test_wolfSSL_OtherName),
  57438. TEST_DECL(test_wolfSSL_FPKI),
  57439. TEST_DECL(test_wolfSSL_URI),
  57440. TEST_DECL(test_wolfSSL_TBS),
  57441. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  57442. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  57443. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  57444. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  57445. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  57446. TEST_DECL(test_wolfSSL_X509_Name_canon),
  57447. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  57448. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  57449. TEST_DECL(test_wolfSSL_X509_NID),
  57450. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  57451. TEST_DECL(test_wolfSSL_get0_param),
  57452. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  57453. TEST_DECL(test_wolfSSL_set1_host),
  57454. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  57455. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  57456. TEST_DECL(test_wolfSSL_X509_STORE),
  57457. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  57458. TEST_DECL(test_X509_STORE_get0_objects),
  57459. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  57460. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  57461. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  57462. TEST_DECL(test_wolfSSL_X509_cmp_time),
  57463. TEST_DECL(test_wolfSSL_X509_time_adj),
  57464. /* X509 tests */
  57465. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  57466. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  57467. TEST_DECL(test_wolfSSL_X509_check_host),
  57468. TEST_DECL(test_wolfSSL_X509_check_email),
  57469. TEST_DECL(test_wolfSSL_X509_check_private_key),
  57470. TEST_DECL(test_wolfSSL_X509),
  57471. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  57472. TEST_DECL(test_wolfSSL_X509_sign),
  57473. TEST_DECL(test_wolfSSL_X509_sign2),
  57474. TEST_DECL(test_wolfSSL_X509_verify),
  57475. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  57476. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  57477. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  57478. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  57479. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  57480. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  57481. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  57482. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  57483. TEST_DECL(test_wolfSSL_X509_NAME),
  57484. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  57485. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  57486. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  57487. TEST_DECL(test_wolfSSL_X509_set_name),
  57488. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  57489. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  57490. TEST_DECL(test_wolfSSL_X509_set_version),
  57491. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  57492. TEST_DECL(test_wolfSSL_X509_CRL),
  57493. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  57494. TEST_DECL(test_wolfSSL_PEM_read_X509),
  57495. TEST_DECL(test_wolfSSL_X509_check_ca),
  57496. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  57497. TEST_DECL(test_wolfSSL_make_cert),
  57498. #ifndef NO_BIO
  57499. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  57500. TEST_DECL(test_wolfSSL_X509_INFO),
  57501. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  57502. #endif
  57503. #ifdef OPENSSL_ALL
  57504. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  57505. #endif
  57506. TEST_DECL(test_wolfSSL_X509_CA_num),
  57507. TEST_DECL(test_wolfSSL_X509_get_version),
  57508. #ifndef NO_BIO
  57509. TEST_DECL(test_wolfSSL_X509_print),
  57510. TEST_DECL(test_wolfSSL_X509_CRL_print),
  57511. #endif
  57512. TEST_DECL(test_X509_get_signature_nid),
  57513. /* X509 extension testing. */
  57514. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  57515. TEST_DECL(test_wolfSSL_X509_get_ext),
  57516. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  57517. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  57518. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  57519. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  57520. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  57521. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  57522. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  57523. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  57524. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  57525. TEST_DECL(test_wolfSSL_X509V3_EXT),
  57526. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  57527. TEST_DECL(test_wolfSSL_X509_cmp),
  57528. TEST_DECL(test_GENERAL_NAME_set0_othername),
  57529. TEST_DECL(test_othername_and_SID_ext),
  57530. TEST_DECL(test_wolfSSL_dup_CA_list),
  57531. /* OpenSSL sk_X509 API test */
  57532. TEST_DECL(test_sk_X509),
  57533. /* OpenSSL sk_X509_CRL API test */
  57534. TEST_DECL(test_sk_X509_CRL),
  57535. /* OpenSSL X509 REQ API test */
  57536. TEST_DECL(test_X509_REQ),
  57537. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  57538. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  57539. TEST_DECL(test_X509_LOOKUP_add_dir),
  57540. /* RAND compatibility API */
  57541. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  57542. TEST_DECL(test_wolfSSL_RAND_bytes),
  57543. TEST_DECL(test_wolfSSL_RAND),
  57544. /* BN compatibility API */
  57545. TEST_DECL(test_wolfSSL_BN_CTX),
  57546. TEST_DECL(test_wolfSSL_BN),
  57547. TEST_DECL(test_wolfSSL_BN_init),
  57548. TEST_DECL(test_wolfSSL_BN_enc_dec),
  57549. TEST_DECL(test_wolfSSL_BN_word),
  57550. TEST_DECL(test_wolfSSL_BN_bits),
  57551. TEST_DECL(test_wolfSSL_BN_shift),
  57552. TEST_DECL(test_wolfSSL_BN_math),
  57553. TEST_DECL(test_wolfSSL_BN_math_mod),
  57554. TEST_DECL(test_wolfSSL_BN_math_other),
  57555. TEST_DECL(test_wolfSSL_BN_rand),
  57556. TEST_DECL(test_wolfSSL_BN_prime),
  57557. /* OpenSSL PKCS5 API test */
  57558. TEST_DECL(test_wolfSSL_PKCS5),
  57559. /* OpenSSL PKCS8 API test */
  57560. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  57561. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  57562. /* OpenSSL PKCS7 API test */
  57563. TEST_DECL(test_wolfssl_PKCS7),
  57564. TEST_DECL(test_wolfSSL_PKCS7_certs),
  57565. TEST_DECL(test_wolfSSL_PKCS7_sign),
  57566. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  57567. #ifndef NO_BIO
  57568. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  57569. #ifdef HAVE_SMIME
  57570. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  57571. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  57572. #endif /* HAVE_SMIME */
  57573. #endif /* !NO_BIO */
  57574. /* OpenSSL PKCS12 API test */
  57575. TEST_DECL(test_wolfSSL_PKCS12),
  57576. /* Can't memory test as callbacks use Assert. */
  57577. TEST_DECL(test_error_queue_per_thread),
  57578. TEST_DECL(test_wolfSSL_ERR_put_error),
  57579. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  57580. #ifndef NO_BIO
  57581. TEST_DECL(test_wolfSSL_ERR_print_errors),
  57582. #endif
  57583. TEST_DECL(test_OBJ_NAME_do_all),
  57584. TEST_DECL(test_wolfSSL_OBJ),
  57585. TEST_DECL(test_wolfSSL_OBJ_cmp),
  57586. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  57587. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  57588. #ifdef OPENSSL_ALL
  57589. TEST_DECL(test_wolfSSL_OBJ_ln),
  57590. TEST_DECL(test_wolfSSL_OBJ_sn),
  57591. #endif
  57592. #ifndef NO_BIO
  57593. TEST_DECL(test_wolfSSL_BIO_gets),
  57594. TEST_DECL(test_wolfSSL_BIO_puts),
  57595. TEST_DECL(test_wolfSSL_BIO_dump),
  57596. /* Can't memory test as server hangs. */
  57597. TEST_DECL(test_wolfSSL_BIO_should_retry),
  57598. TEST_DECL(test_wolfSSL_BIO_write),
  57599. TEST_DECL(test_wolfSSL_BIO_printf),
  57600. TEST_DECL(test_wolfSSL_BIO_f_md),
  57601. TEST_DECL(test_wolfSSL_BIO_up_ref),
  57602. TEST_DECL(test_wolfSSL_BIO_reset),
  57603. TEST_DECL(test_wolfSSL_BIO_get_len),
  57604. #endif
  57605. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57606. TEST_DECL(test_wolfSSL_check_domain),
  57607. #endif
  57608. TEST_DECL(test_wolfSSL_cert_cb),
  57609. /* Can't memory test as tcp_connect aborts. */
  57610. TEST_DECL(test_wolfSSL_SESSION),
  57611. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  57612. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  57613. TEST_DECL(test_wolfSSL_ticket_keys),
  57614. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  57615. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  57616. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  57617. TEST_DECL(test_wolfSSL_verify_mode),
  57618. TEST_DECL(test_wolfSSL_verify_depth),
  57619. TEST_DECL(test_wolfSSL_msg_callback),
  57620. TEST_DECL(test_wolfSSL_MD4),
  57621. TEST_DECL(test_wolfSSL_MD5),
  57622. TEST_DECL(test_wolfSSL_MD5_Transform),
  57623. TEST_DECL(test_wolfSSL_SHA),
  57624. TEST_DECL(test_wolfSSL_SHA_Transform),
  57625. TEST_DECL(test_wolfSSL_SHA224),
  57626. TEST_DECL(test_wolfSSL_SHA256),
  57627. TEST_DECL(test_wolfSSL_SHA256_Transform),
  57628. TEST_DECL(test_wolfSSL_SHA512_Transform),
  57629. TEST_DECL(test_wolfSSL_HMAC_CTX),
  57630. TEST_DECL(test_wolfSSL_HMAC),
  57631. TEST_DECL(test_wolfSSL_CMAC),
  57632. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  57633. TEST_DECL(test_wolfSSL_DES_ncbc),
  57634. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  57635. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  57636. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  57637. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  57638. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  57639. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  57640. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  57641. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  57642. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  57643. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  57644. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  57645. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  57646. TEST_DECL(test_wolfSSL_PEM_read),
  57647. TEST_DECL(test_wolfSSL_OpenSSL_version),
  57648. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  57649. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  57650. TEST_DECL(test_CONF_modules_xxx),
  57651. #ifdef OPENSSL_ALL
  57652. TEST_DECL(test_wolfSSL_TXT_DB),
  57653. TEST_DECL(test_wolfSSL_NCONF),
  57654. #endif
  57655. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  57656. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  57657. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  57658. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  57659. TEST_DECL(test_CRYPTO_THREADID_xxx),
  57660. TEST_DECL(test_ENGINE_cleanup),
  57661. /* test the no op functions for compatibility */
  57662. TEST_DECL(test_no_op_functions),
  57663. /* OpenSSL error API tests */
  57664. TEST_DECL(test_ERR_load_crypto_strings),
  57665. #ifdef OPENSSL_ALL
  57666. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  57667. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  57668. TEST_DECL(test_wolfSSL_CTX_ctrl),
  57669. #endif /* OPENSSL_ALL */
  57670. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  57671. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  57672. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  57673. TEST_DECL(test_wolfSSL_RSA),
  57674. TEST_DECL(test_wolfSSL_RSA_DER),
  57675. TEST_DECL(test_wolfSSL_RSA_print),
  57676. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  57677. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  57678. TEST_DECL(test_wolfSSL_RSA_get0_key),
  57679. TEST_DECL(test_wolfSSL_RSA_meth),
  57680. TEST_DECL(test_wolfSSL_RSA_verify),
  57681. TEST_DECL(test_wolfSSL_RSA_sign),
  57682. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  57683. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  57684. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  57685. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  57686. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  57687. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  57688. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  57689. TEST_DECL(test_wolfSSL_RSA_ex_data),
  57690. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  57691. TEST_DECL(test_wolfSSL_RSA_To_Der),
  57692. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  57693. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  57694. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  57695. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  57696. TEST_DECL(test_wolfSSL_DH),
  57697. TEST_DECL(test_wolfSSL_DH_dup),
  57698. TEST_DECL(test_wolfSSL_DH_check),
  57699. TEST_DECL(test_wolfSSL_DH_prime),
  57700. TEST_DECL(test_wolfSSL_DH_1536_prime),
  57701. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  57702. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  57703. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  57704. TEST_DECL(test_wolfSSL_d2i_DHparams),
  57705. TEST_DECL(test_wolfSSL_DH_LoadDer),
  57706. TEST_DECL(test_wolfSSL_i2d_DHparams),
  57707. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  57708. TEST_DECL(test_wolfSSL_EC_GROUP),
  57709. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  57710. TEST_DECL(test_wolfSSL_EC_POINT),
  57711. TEST_DECL(test_wolfSSL_SPAKE),
  57712. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  57713. TEST_DECL(test_EC_i2d),
  57714. TEST_DECL(test_wolfSSL_EC_curve),
  57715. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  57716. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  57717. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  57718. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  57719. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  57720. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  57721. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  57722. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  57723. TEST_DECL(test_ECDSA_size_sign),
  57724. TEST_DECL(test_ECDH_compute_key),
  57725. #endif
  57726. #ifdef OPENSSL_EXTRA
  57727. TEST_DECL(test_ED25519),
  57728. TEST_DECL(test_ED448),
  57729. #endif
  57730. TEST_DECL(test_DSA_do_sign_verify),
  57731. #ifdef OPENSSL_ALL
  57732. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  57733. TEST_DECL(test_wolfSSL_DSA_SIG),
  57734. #endif
  57735. TEST_DECL(test_openssl_generate_key_and_cert),
  57736. TEST_DECL(test_wolfSSL_FIPS_mode),
  57737. TEST_DECL(test_openssl_FIPS_drbg),
  57738. /*********************************
  57739. * CertManager API tests
  57740. *********************************/
  57741. TEST_DECL(test_wolfSSL_CertManagerAPI),
  57742. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  57743. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  57744. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  57745. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  57746. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  57747. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  57748. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  57749. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  57750. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  57751. TEST_DECL(test_wolfSSL_CertManagerCRL),
  57752. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  57753. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  57754. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  57755. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57756. !defined(WOLFSSL_NO_CLIENT_AUTH))
  57757. TEST_DECL(test_various_pathlen_chains),
  57758. #endif
  57759. /*********************************
  57760. * SSL/TLS API tests
  57761. *********************************/
  57762. TEST_DECL(test_wolfSSL_Method_Allocators),
  57763. #ifndef NO_WOLFSSL_SERVER
  57764. TEST_DECL(test_wolfSSL_CTX_new),
  57765. #endif
  57766. TEST_DECL(test_server_wolfSSL_new),
  57767. TEST_DECL(test_client_wolfSSL_new),
  57768. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  57769. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  57770. TEST_DECL(test_for_double_Free),
  57771. #endif
  57772. TEST_DECL(test_wolfSSL_set_options),
  57773. #ifdef WOLFSSL_TLS13
  57774. /* TLS v1.3 API tests */
  57775. TEST_DECL(test_tls13_apis),
  57776. TEST_DECL(test_tls13_cipher_suites),
  57777. #endif
  57778. TEST_DECL(test_wolfSSL_tmp_dh),
  57779. TEST_DECL(test_wolfSSL_ctrl),
  57780. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57781. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57782. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57783. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57784. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  57785. #endif
  57786. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  57787. TEST_DECL(test_wolfSSL_security_level),
  57788. TEST_DECL(test_wolfSSL_SSL_in_init),
  57789. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  57790. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  57791. TEST_DECL(test_CONF_CTX_FILE),
  57792. TEST_DECL(test_CONF_CTX_CMDLINE),
  57793. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  57794. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  57795. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  57796. /* Bad certificate signature tests */
  57797. TEST_DECL(test_EccSigFailure_cm),
  57798. TEST_DECL(test_RsaSigFailure_cm),
  57799. #endif /* NO_CERTS */
  57800. /* PKCS8 testing */
  57801. TEST_DECL(test_wolfSSL_no_password_cb),
  57802. TEST_DECL(test_wolfSSL_PKCS8),
  57803. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  57804. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  57805. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57806. TEST_DECL(test_wolfSSL_get_finished),
  57807. /* Uses Assert in handshake callback. */
  57808. TEST_DECL(test_wolfSSL_CTX_add_session),
  57809. /* Large number of memory allocations. */
  57810. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  57811. /* Large number of memory allocations. */
  57812. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  57813. /* Large number of memory allocations. */
  57814. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  57815. /* Large number of memory allocations. */
  57816. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  57817. /* Large number of memory allocations. */
  57818. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  57819. /* Large number of memory allocations. */
  57820. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  57821. #endif
  57822. TEST_DECL(test_SSL_CIPHER_get_xxx),
  57823. TEST_DECL(test_wolfSSL_ERR_strings),
  57824. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  57825. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  57826. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  57827. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  57828. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  57829. /* Large number of memory allocations. */
  57830. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  57831. TEST_DECL(test_wolfSSL_CertRsaPss),
  57832. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  57833. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  57834. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  57835. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  57836. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  57837. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  57838. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  57839. TEST_DECL(test_multiple_crls_same_issuer),
  57840. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  57841. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  57842. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  57843. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  57844. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  57845. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  57846. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  57847. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  57848. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  57849. TEST_DECL(test_SetTmpEC_DHE_Sz),
  57850. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  57851. #ifdef WOLFSSL_DTLS
  57852. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  57853. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  57854. #endif
  57855. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  57856. /* Uses Assert in handshake callback. */
  57857. TEST_DECL(test_wolfSSL_dtls_plaintext),
  57858. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  57859. defined(HAVE_IO_TESTS_DEPENDENCIES)
  57860. TEST_DECL(test_wolfSSL_read_write),
  57861. /* Can't memory test as server hangs if client fails before second connect.
  57862. */
  57863. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  57864. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  57865. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  57866. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  57867. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  57868. /* Can't memory test as server hangs. */
  57869. TEST_DECL(test_wolfSSL_dtls_export),
  57870. /* Uses Assert in handshake callback. */
  57871. TEST_DECL(test_wolfSSL_tls_export),
  57872. #endif
  57873. TEST_DECL(test_wolfSSL_SetMinVersion),
  57874. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  57875. /* wolfSSL handshake APIs. */
  57876. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  57877. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  57878. TEST_DECL(test_wolfSSL_BUF),
  57879. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  57880. /* Can't memory test as server hangs. */
  57881. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  57882. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  57883. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  57884. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  57885. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  57886. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  57887. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  57888. /* Can't memory test as server hangs. */
  57889. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  57890. TEST_DECL(test_wolfSSL_Tls13_postauth),
  57891. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  57892. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  57893. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  57894. TEST_DECL(test_wolfSSL_THREADID_hash),
  57895. /* TLS extensions tests */
  57896. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57897. #ifdef HAVE_SNI
  57898. TEST_DECL(test_wolfSSL_UseSNI_params),
  57899. /* Uses Assert in handshake callback. */
  57900. TEST_DECL(test_wolfSSL_UseSNI_connection),
  57901. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  57902. #endif /* HAVE_SNI */
  57903. #endif
  57904. TEST_DECL(test_wolfSSL_UseTrustedCA),
  57905. TEST_DECL(test_wolfSSL_UseMaxFragment),
  57906. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  57907. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  57908. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57909. /* Uses Assert in handshake callback. */
  57910. TEST_DECL(test_wolfSSL_UseALPN_connection),
  57911. TEST_DECL(test_wolfSSL_UseALPN_params),
  57912. #endif
  57913. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  57914. /* Uses Assert in handshake callback. */
  57915. TEST_DECL(test_wolfSSL_set_alpn_protos),
  57916. #endif
  57917. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  57918. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  57919. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  57920. TEST_DECL(test_tls_ext_duplicate),
  57921. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  57922. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  57923. /* Uses Assert in handshake callback. */
  57924. TEST_DECL(test_wolfSSL_Tls13_ECH),
  57925. #endif
  57926. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  57927. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  57928. /* OCSP Stapling */
  57929. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  57930. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  57931. /* Multicast */
  57932. TEST_DECL(test_wolfSSL_mcast),
  57933. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  57934. TEST_DECL(test_wolfSSL_msgCb),
  57935. TEST_DECL(test_wolfSSL_either_side),
  57936. TEST_DECL(test_wolfSSL_DTLS_either_side),
  57937. /* Uses Assert in handshake callback. */
  57938. TEST_DECL(test_wolfSSL_dtls_fragments),
  57939. /* Uses Assert in handshake callback. */
  57940. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  57941. /* Uses Assert in handshake callback. */
  57942. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  57943. /* Uses Assert in handshake callback. */
  57944. TEST_DECL(test_wolfSSL_dtls_bad_record),
  57945. /* Uses Assert in handshake callback. */
  57946. TEST_DECL(test_wolfSSL_dtls_stateless),
  57947. TEST_DECL(test_generate_cookie),
  57948. /* Can't memory test as server hangs. */
  57949. TEST_DECL(test_wolfSSL_BIO_connect),
  57950. /* Can't memory test as server Asserts in thread. */
  57951. TEST_DECL(test_wolfSSL_BIO_accept),
  57952. TEST_DECL(test_wolfSSL_BIO_tls),
  57953. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  57954. TEST_DECL(test_DhCallbacks),
  57955. #endif
  57956. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  57957. TEST_DECL(test_export_keying_material),
  57958. #endif
  57959. /* Can't memory test as client/server Asserts in thread. */
  57960. TEST_DECL(test_ticket_and_psk_mixing),
  57961. /* Can't memory test as client/server Asserts in thread. */
  57962. TEST_DECL(test_prioritize_psk),
  57963. /* Can't memory test as client/server hangs. */
  57964. TEST_DECL(test_wc_CryptoCb),
  57965. /* Can't memory test as client/server hangs. */
  57966. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  57967. #if !defined(NO_FILESYSTEM) && \
  57968. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57969. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  57970. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  57971. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  57972. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  57973. #ifdef HAVE_MAX_FRAGMENT
  57974. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  57975. #endif /* HAVE_MAX_FRAGMENT */
  57976. #ifndef NO_RSA
  57977. TEST_DECL(test_wolfSSL_dtls_stateless2),
  57978. #if !defined(NO_OLD_TLS)
  57979. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  57980. #endif /* !defined(NO_OLD_TLS) */
  57981. #endif /* ! NO_RSA */
  57982. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  57983. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  57984. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  57985. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  57986. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  57987. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  57988. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  57989. && defined(WOLFSSL_TLS13) && \
  57990. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  57991. TEST_DECL(test_ticket_nonce_malloc),
  57992. #endif
  57993. TEST_DECL(test_ticket_ret_create),
  57994. TEST_DECL(test_extra_alerts_wrong_cs),
  57995. TEST_DECL(test_extra_alerts_skip_hs),
  57996. TEST_DECL(test_extra_alerts_bad_psk),
  57997. /* Can't memory test as client/server Asserts. */
  57998. TEST_DECL(test_harden_no_secure_renegotiation),
  57999. TEST_DECL(test_override_alt_cert_chain),
  58000. TEST_DECL(test_rpk_set_xxx_cert_type),
  58001. TEST_DECL(test_tls13_rpk_handshake),
  58002. TEST_DECL(test_dtls13_bad_epoch_ch),
  58003. TEST_DECL(test_short_session_id),
  58004. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  58005. /* Can't memory test as client/server hangs. */
  58006. TEST_DECL(test_dtls_msg_from_other_peer),
  58007. TEST_DECL(test_dtls_ipv6_check),
  58008. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  58009. TEST_DECL(test_dtls_no_extensions),
  58010. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  58011. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  58012. TEST_DECL(test_session_ticket_no_id),
  58013. TEST_DECL(test_session_ticket_hs_update),
  58014. TEST_DECL(test_dtls_downgrade_scr_server),
  58015. TEST_DECL(test_dtls_downgrade_scr),
  58016. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  58017. TEST_DECL(test_dtls_client_hello_timeout),
  58018. TEST_DECL(test_dtls_dropped_ccs),
  58019. TEST_DECL(test_certreq_sighash_algos),
  58020. /* This test needs to stay at the end to clean up any caches allocated. */
  58021. TEST_DECL(test_wolfSSL_Cleanup)
  58022. };
  58023. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  58024. static void TestSetup(void)
  58025. {
  58026. /* Stub, for now. Add common test setup code here. */
  58027. }
  58028. static void TestCleanup(void)
  58029. {
  58030. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  58031. /* Clear any errors added to the error queue during the test run. */
  58032. wolfSSL_ERR_clear_error();
  58033. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  58034. }
  58035. /* Print out all API test cases with numeric identifier.
  58036. */
  58037. void ApiTest_PrintTestCases(void)
  58038. {
  58039. int i;
  58040. printf("All Test Cases:\n");
  58041. for (i = 0; i < TEST_CASE_CNT; i++) {
  58042. printf("%3d: %s\n", i + 1, testCases[i].name);
  58043. }
  58044. }
  58045. /* Add test case with index to the list to run.
  58046. *
  58047. * @param [in] idx Index of test case to run starting at 1.
  58048. * @return 0 on success.
  58049. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  58050. */
  58051. int ApiTest_RunIdx(int idx)
  58052. {
  58053. if (idx < 1 || idx > TEST_CASE_CNT) {
  58054. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  58055. return BAD_FUNC_ARG;
  58056. }
  58057. testAll = 0;
  58058. testCases[idx-1].run = 1;
  58059. return 0;
  58060. }
  58061. /* Add test case with name to the list to run.
  58062. *
  58063. * @param [in] name Name of test case to run.
  58064. * @return 0 on success.
  58065. * @return BAD_FUNC_ARG when name is not a known test case name.
  58066. */
  58067. int ApiTest_RunName(char* name)
  58068. {
  58069. int i;
  58070. for (i = 0; i < TEST_CASE_CNT; i++) {
  58071. if (XSTRCMP(testCases[i].name, name) == 0) {
  58072. testAll = 0;
  58073. testCases[i].run = 1;
  58074. return 0;
  58075. }
  58076. }
  58077. printf("Test case name not found: %s\n", name);
  58078. printf("Use --list to see all test case names.\n");
  58079. return BAD_FUNC_ARG;
  58080. }
  58081. /* Converts the result code to a string.
  58082. *
  58083. * @param [in] res Test result code.
  58084. * @return String describing test result.
  58085. */
  58086. static const char* apitest_res_string(int res)
  58087. {
  58088. const char* str = "invalid result";
  58089. switch (res) {
  58090. case TEST_SUCCESS:
  58091. str = "passed";
  58092. break;
  58093. case TEST_FAIL:
  58094. str = "failed";
  58095. break;
  58096. case TEST_SKIPPED:
  58097. str = "skipped";
  58098. break;
  58099. }
  58100. return str;
  58101. }
  58102. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58103. static double gettime_secs(void)
  58104. {
  58105. struct timeval tv;
  58106. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  58107. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  58108. }
  58109. #endif
  58110. int ApiTest(void)
  58111. {
  58112. int i;
  58113. int ret;
  58114. int res = 0;
  58115. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58116. double timeDiff;
  58117. #endif
  58118. printf(" Begin API Tests\n");
  58119. fflush(stdout);
  58120. /* we must perform init and cleanup if not all tests are running */
  58121. if (!testAll) {
  58122. #ifdef WOLFCRYPT_ONLY
  58123. if (wolfCrypt_Init() != 0) {
  58124. printf("wolfCrypt Initialization failed\n");
  58125. res = 1;
  58126. }
  58127. #else
  58128. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  58129. printf("wolfSSL Initialization failed\n");
  58130. res = 1;
  58131. }
  58132. #endif
  58133. }
  58134. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  58135. if (res == 0) {
  58136. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  58137. printf("failed to create tmp dir\n");
  58138. res = 1;
  58139. }
  58140. else {
  58141. tmpDirNameSet = 1;
  58142. }
  58143. }
  58144. #endif
  58145. if (res == 0) {
  58146. for (i = 0; i < TEST_CASE_CNT; ++i) {
  58147. EXPECT_DECLS;
  58148. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  58149. currentTestName = testCases[i].name;
  58150. #endif
  58151. /* When not testing all cases then skip if not marked for running.
  58152. */
  58153. if (!testAll && !testCases[i].run) {
  58154. continue;
  58155. }
  58156. TestSetup();
  58157. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  58158. fflush(stdout);
  58159. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58160. timeDiff = gettime_secs();
  58161. #endif
  58162. ret = testCases[i].func();
  58163. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58164. timeDiff = gettime_secs() - timeDiff;
  58165. #endif
  58166. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  58167. if (ret != TEST_SKIPPED) {
  58168. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  58169. }
  58170. else
  58171. #endif
  58172. {
  58173. printf(" %s\n", apitest_res_string(ret));
  58174. }
  58175. fflush(stdout);
  58176. /* if return code is < 0 and not skipped then assert error */
  58177. Expect((ret > 0 || ret == TEST_SKIPPED),
  58178. ("Test failed\n"),
  58179. ("ret %d", ret));
  58180. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  58181. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  58182. TestCleanup();
  58183. }
  58184. }
  58185. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  58186. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  58187. wc_ecc_fp_free(); /* free per thread cache */
  58188. #endif
  58189. if (!testAll) {
  58190. #ifdef WOLFCRYPT_ONLY
  58191. wolfCrypt_Cleanup();
  58192. #else
  58193. wolfSSL_Cleanup();
  58194. #endif
  58195. }
  58196. (void)testDevId;
  58197. if (res != 0) {
  58198. printf("\nFAILURES:\n");
  58199. for (i = 0; i < TEST_CASE_CNT; ++i) {
  58200. if (testCases[i].fail) {
  58201. printf(" %3d: %s\n", i + 1, testCases[i].name);
  58202. }
  58203. }
  58204. printf("\n");
  58205. fflush(stdout);
  58206. }
  58207. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  58208. if (tmpDirNameSet) {
  58209. printf("\nBinary dumps of the memio streams can be found in the\n"
  58210. "%s directory. This can be imported into\n"
  58211. "Wireshark by transforming the file with\n"
  58212. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  58213. "And then loading test_output.dump.hex into Wireshark using\n"
  58214. "the \"Import from Hex Dump...\" option and selecting the\n"
  58215. "TCP encapsulation option.\n", tmpDirName);
  58216. }
  58217. #endif
  58218. printf(" End API Tests\n");
  58219. fflush(stdout);
  58220. return res;
  58221. }