api.c 2.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867
  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. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  334. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  335. * number tracking */
  336. #include "wolfssl/internal.h"
  337. #endif
  338. /* force enable test buffers */
  339. #ifndef USE_CERT_BUFFERS_2048
  340. #define USE_CERT_BUFFERS_2048
  341. #endif
  342. #ifndef USE_CERT_BUFFERS_256
  343. #define USE_CERT_BUFFERS_256
  344. #endif
  345. #include <wolfssl/certs_test.h>
  346. #define WOLFSSL_TEST_UTILS_INCLUDED
  347. #include "tests/utils.c"
  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) && \
  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;
  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_READ;
  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. ret = wolfSSL_connect(ctx->c_ssl);
  5011. if (ret == WOLFSSL_SUCCESS) {
  5012. hs_c = 1;
  5013. }
  5014. else {
  5015. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5016. if (err != WOLFSSL_ERROR_WANT_READ &&
  5017. err != WOLFSSL_ERROR_WANT_WRITE) {
  5018. failing_c = 1;
  5019. hs_c = 1;
  5020. if (failing_c && failing_s) {
  5021. break;
  5022. }
  5023. }
  5024. }
  5025. }
  5026. if (!hs_s) {
  5027. ret = wolfSSL_accept(ctx->s_ssl);
  5028. if (ret == WOLFSSL_SUCCESS) {
  5029. hs_s = 1;
  5030. }
  5031. else {
  5032. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5033. if (err != WOLFSSL_ERROR_WANT_READ &&
  5034. err != WOLFSSL_ERROR_WANT_WRITE) {
  5035. failing_s = 1;
  5036. hs_s = 1;
  5037. if (failing_c && failing_s) {
  5038. break;
  5039. }
  5040. }
  5041. }
  5042. }
  5043. handshake_complete = hs_c && hs_s;
  5044. max_rounds--;
  5045. if (rounds != NULL) {
  5046. *rounds += 1;
  5047. }
  5048. }
  5049. if (!handshake_complete) {
  5050. return TEST_FAIL;
  5051. }
  5052. return TEST_SUCCESS;
  5053. }
  5054. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5055. {
  5056. EXPECT_DECLS;
  5057. char input[1024];
  5058. int idx = 0;
  5059. const char* msg_c = "hello wolfssl!";
  5060. int msglen_c = (int)XSTRLEN(msg_c);
  5061. const char* msg_s = "I hear you fa shizzle!";
  5062. int msglen_s = (int)XSTRLEN(msg_s);
  5063. if (ctx->c_msg != NULL) {
  5064. msg_c = ctx->c_msg;
  5065. msglen_c = ctx->c_msglen;
  5066. }
  5067. if (ctx->s_msg != NULL) {
  5068. msg_s = ctx->s_msg;
  5069. msglen_s = ctx->s_msglen;
  5070. }
  5071. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5072. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5073. if (idx >= 0) {
  5074. input[idx] = '\0';
  5075. }
  5076. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5077. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5078. ctx->s_cb.return_code = EXPECT_RESULT();
  5079. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5080. if (idx >= 0) {
  5081. input[idx] = '\0';
  5082. }
  5083. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5084. ctx->c_cb.return_code = EXPECT_RESULT();
  5085. if (ctx->c_cb.on_result != NULL) {
  5086. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5087. }
  5088. if (ctx->s_cb.on_result != NULL) {
  5089. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5090. }
  5091. return EXPECT_RESULT();
  5092. }
  5093. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5094. {
  5095. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5096. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5097. if (ctx->c_cb.on_cleanup != NULL) {
  5098. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5099. }
  5100. if (ctx->s_cb.on_cleanup != NULL) {
  5101. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5102. }
  5103. wolfSSL_shutdown(ctx->s_ssl);
  5104. wolfSSL_shutdown(ctx->c_ssl);
  5105. wolfSSL_free(ctx->s_ssl);
  5106. wolfSSL_free(ctx->c_ssl);
  5107. if (!ctx->s_cb.isSharedCtx) {
  5108. wolfSSL_CTX_free(ctx->s_ctx);
  5109. ctx->s_ctx = NULL;
  5110. }
  5111. if (!ctx->c_cb.isSharedCtx) {
  5112. wolfSSL_CTX_free(ctx->c_ctx);
  5113. ctx->c_ctx = NULL;
  5114. }
  5115. if (!ctx->s_cb.ticNoInit) {
  5116. #if defined(HAVE_SESSION_TICKET) && \
  5117. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5118. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5119. OpenSSLTicketCleanup();
  5120. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5121. TicketCleanup();
  5122. #endif
  5123. #endif
  5124. }
  5125. }
  5126. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5127. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5128. {
  5129. EXPECT_DECLS;
  5130. struct test_ssl_memio_ctx test_ctx;
  5131. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5132. size_t msg_len;
  5133. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5134. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5135. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5136. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5137. test_ctx.c_ctx = client_cb->ctx;
  5138. test_ctx.s_ctx = server_cb->ctx;
  5139. test_ctx.c_cb.return_code = TEST_FAIL;
  5140. test_ctx.s_cb.return_code = TEST_FAIL;
  5141. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5142. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5143. if (client_on_handshake != NULL) {
  5144. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5145. TEST_SUCCESS);
  5146. }
  5147. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5148. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5149. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  5150. MD_MAX_SIZE);
  5151. ExpectIntGE(msg_len, 0);
  5152. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5153. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  5154. MD_MAX_SIZE);
  5155. ExpectIntGE(msg_len, 0);
  5156. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5157. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  5158. test_ssl_memio_cleanup(&test_ctx);
  5159. client_cb->return_code = test_ctx.c_cb.return_code;
  5160. server_cb->return_code = test_ctx.s_cb.return_code;
  5161. return EXPECT_RESULT();
  5162. }
  5163. #endif
  5164. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  5165. #ifdef WOLFSSL_SESSION_EXPORT
  5166. #ifdef WOLFSSL_DTLS
  5167. /* set up function for sending session information */
  5168. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  5169. {
  5170. WOLFSSL_CTX* ctx = NULL;
  5171. WOLFSSL* ssl = NULL;
  5172. AssertNotNull(inSsl);
  5173. AssertNotNull(buf);
  5174. AssertIntNE(0, sz);
  5175. /* Set ctx to DTLS 1.2 */
  5176. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  5177. AssertNotNull(ctx);
  5178. ssl = wolfSSL_new(ctx);
  5179. AssertNotNull(ssl);
  5180. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  5181. wolfSSL_free(ssl);
  5182. wolfSSL_CTX_free(ctx);
  5183. (void)userCtx;
  5184. return 0;
  5185. }
  5186. #endif
  5187. /* returns negative value on fail and positive (including 0) on success */
  5188. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  5189. {
  5190. int ret, err, loop_count, count, timeout = 10;
  5191. char msg[] = "I hear you fa shizzle!";
  5192. char input[1024];
  5193. loop_count = ((func_args*)args)->argc;
  5194. #ifdef WOLFSSL_ASYNC_CRYPT
  5195. err = 0; /* Reset error */
  5196. #endif
  5197. do {
  5198. #ifdef WOLFSSL_ASYNC_CRYPT
  5199. if (err == WC_PENDING_E) {
  5200. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5201. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5202. }
  5203. #endif
  5204. ret = wolfSSL_accept(ssl);
  5205. err = wolfSSL_get_error(ssl, 0);
  5206. if (err == WOLFSSL_ERROR_WANT_READ ||
  5207. err == WOLFSSL_ERROR_WANT_WRITE) {
  5208. int select_ret;
  5209. err = WC_PENDING_E;
  5210. select_ret = tcp_select(*sockfd, timeout);
  5211. if (select_ret == TEST_TIMEOUT) {
  5212. return WOLFSSL_FATAL_ERROR;
  5213. }
  5214. }
  5215. } while (err == WC_PENDING_E);
  5216. if (ret != WOLFSSL_SUCCESS) {
  5217. char buff[WOLFSSL_MAX_ERROR_SZ];
  5218. fprintf(stderr, "error = %d, %s\n", err,
  5219. wolfSSL_ERR_error_string(err, buff));
  5220. return ret;
  5221. }
  5222. for (count = 0; count < loop_count; count++) {
  5223. int select_ret;
  5224. select_ret = tcp_select(*sockfd, timeout);
  5225. if (select_ret == TEST_TIMEOUT) {
  5226. ret = WOLFSSL_FATAL_ERROR;
  5227. break;
  5228. }
  5229. do {
  5230. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5231. if (ret > 0) {
  5232. input[ret] = '\0';
  5233. fprintf(stderr, "Client message: %s\n", input);
  5234. }
  5235. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5236. do {
  5237. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  5238. return WOLFSSL_FATAL_ERROR;
  5239. }
  5240. err = wolfSSL_get_error(ssl, ret);
  5241. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  5242. }
  5243. return ret;
  5244. }
  5245. #endif /* WOLFSSL_SESSION_EXPORT */
  5246. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  5247. {
  5248. SOCKET_T sockfd = 0;
  5249. SOCKET_T clientfd = 0;
  5250. word16 port;
  5251. callback_functions* cbf;
  5252. WOLFSSL_CTX* ctx = NULL;
  5253. WOLFSSL* ssl = NULL;
  5254. func_args* opts = (func_args*)args;
  5255. char msg[] = "I hear you fa shizzle!";
  5256. char input[1024];
  5257. int idx;
  5258. int ret, err = 0;
  5259. int sharedCtx = 0;
  5260. int doUdp = 0;
  5261. SOCKADDR_IN_T cliAddr;
  5262. socklen_t cliLen;
  5263. const char* certFile = svrCertFile;
  5264. const char* keyFile = svrKeyFile;
  5265. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5266. size_t msg_len = 0;
  5267. #endif
  5268. wolfSSL_SetLoggingPrefix("server");
  5269. #ifdef WOLFSSL_TIRTOS
  5270. fdOpenSession(Task_self());
  5271. #endif
  5272. opts->return_code = TEST_FAIL;
  5273. cbf = opts->callbacks;
  5274. if (cbf != NULL && cbf->ctx) {
  5275. ctx = cbf->ctx;
  5276. sharedCtx = 1;
  5277. }
  5278. else
  5279. {
  5280. WOLFSSL_METHOD* method = NULL;
  5281. if (cbf != NULL && cbf->method != NULL) {
  5282. method = cbf->method();
  5283. }
  5284. else {
  5285. method = wolfSSLv23_server_method();
  5286. }
  5287. ctx = wolfSSL_CTX_new(method);
  5288. }
  5289. if (ctx == NULL) {
  5290. /* Release the wait for TCP ready. */
  5291. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5292. opts->signal->ready = 1;
  5293. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5294. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5295. goto done;
  5296. }
  5297. if (cbf == NULL || !cbf->ticNoInit) {
  5298. #if defined(HAVE_SESSION_TICKET) && \
  5299. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5300. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5301. OpenSSLTicketInit();
  5302. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  5303. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5304. TicketInit();
  5305. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  5306. #endif
  5307. #endif
  5308. }
  5309. #if defined(USE_WINDOWS_API)
  5310. port = opts->signal->port;
  5311. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5312. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5313. /* Let tcp_listen assign port */
  5314. port = 0;
  5315. #else
  5316. /* Use default port */
  5317. port = wolfSSLPort;
  5318. #endif
  5319. if (cbf != NULL)
  5320. doUdp = cbf->doUdp;
  5321. /* do it here to detect failure */
  5322. tcp_accept(
  5323. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  5324. if (doUdp) {
  5325. cliLen = sizeof(cliAddr);
  5326. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  5327. (struct sockaddr*)&cliAddr, &cliLen);
  5328. AssertIntGT(idx, 0);
  5329. }
  5330. else {
  5331. CloseSocket(sockfd);
  5332. }
  5333. wolfSSL_CTX_set_verify(ctx,
  5334. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5335. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5336. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5337. #endif
  5338. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5339. != WOLFSSL_SUCCESS) {
  5340. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5341. goto done;
  5342. }
  5343. if (cbf != NULL && cbf->certPemFile != NULL)
  5344. certFile = cbf->certPemFile;
  5345. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5346. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5347. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5348. #else
  5349. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  5350. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5351. #endif
  5352. /*err_sys("can't load server cert chain file, "
  5353. "Please run from wolfSSL home dir");*/
  5354. goto done;
  5355. }
  5356. if (cbf != NULL && cbf->keyPemFile != NULL)
  5357. keyFile = cbf->keyPemFile;
  5358. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5359. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5360. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5361. #else
  5362. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  5363. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5364. #endif
  5365. /*err_sys("can't load server key file, "
  5366. "Please run from wolfSSL home dir");*/
  5367. goto done;
  5368. }
  5369. #ifdef HAVE_CRL
  5370. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5371. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5372. goto done;
  5373. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5374. != WOLFSSL_SUCCESS)
  5375. goto done;
  5376. }
  5377. #endif
  5378. /* call ctx setup callback */
  5379. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5380. cbf->ctx_ready(ctx);
  5381. }
  5382. ssl = wolfSSL_new(ctx);
  5383. if (ssl == NULL) {
  5384. goto done;
  5385. }
  5386. if (doUdp) {
  5387. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5388. if (err != WOLFSSL_SUCCESS)
  5389. goto done;
  5390. }
  5391. #ifdef WOLFSSL_SESSION_EXPORT
  5392. /* only add in more complex nonblocking case with session export tests */
  5393. if (args && opts->argc > 0) {
  5394. /* set as nonblock and time out for waiting on read/write */
  5395. tcp_set_nonblocking(&clientfd);
  5396. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  5397. }
  5398. #endif
  5399. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5400. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  5401. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5402. #else
  5403. if (wolfSSL_use_certificate_file(ssl, certFile,
  5404. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5405. #endif
  5406. /*err_sys("can't load server cert chain file, "
  5407. "Please run from wolfSSL home dir");*/
  5408. goto done;
  5409. }
  5410. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5411. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5412. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5413. #else
  5414. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  5415. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5416. #endif
  5417. /*err_sys("can't load server key file, "
  5418. "Please run from wolfSSL home dir");*/
  5419. goto done;
  5420. }
  5421. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5422. /*err_sys("SSL_set_fd failed");*/
  5423. goto done;
  5424. }
  5425. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5426. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5427. #elif !defined(NO_DH)
  5428. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5429. #endif
  5430. /* call ssl setup callback */
  5431. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5432. cbf->ssl_ready(ssl);
  5433. }
  5434. #ifdef WOLFSSL_SESSION_EXPORT
  5435. /* only add in more complex nonblocking case with session export tests */
  5436. if (opts->argc > 0) {
  5437. ret = nonblocking_accept_read(args, ssl, &clientfd);
  5438. if (ret >= 0) {
  5439. opts->return_code = TEST_SUCCESS;
  5440. }
  5441. #ifdef WOLFSSL_TIRTOS
  5442. Task_yield();
  5443. #endif
  5444. goto done;
  5445. }
  5446. #endif
  5447. #ifdef WOLFSSL_ASYNC_CRYPT
  5448. err = 0; /* Reset error */
  5449. #endif
  5450. do {
  5451. #ifdef WOLFSSL_ASYNC_CRYPT
  5452. if (err == WC_PENDING_E) {
  5453. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5454. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5455. }
  5456. #endif
  5457. ret = wolfSSL_negotiate(ssl);
  5458. err = wolfSSL_get_error(ssl, 0);
  5459. } while (err == WC_PENDING_E);
  5460. if (ret != WOLFSSL_SUCCESS) {
  5461. char buff[WOLFSSL_MAX_ERROR_SZ];
  5462. fprintf(stderr, "error = %d, %s\n", err,
  5463. wolfSSL_ERR_error_string(err, buff));
  5464. /*err_sys("SSL_accept failed");*/
  5465. goto done;
  5466. }
  5467. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5468. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  5469. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  5470. AssertIntGE(msg_len, 0);
  5471. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  5472. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  5473. AssertIntGE(msg_len, 0);
  5474. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5475. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5476. if (idx > 0) {
  5477. input[idx] = '\0';
  5478. fprintf(stderr, "Client message: %s\n", input);
  5479. }
  5480. else if (idx < 0) {
  5481. goto done;
  5482. }
  5483. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5484. /*err_sys("SSL_write failed");*/
  5485. goto done;
  5486. }
  5487. if (cbf != NULL && cbf->on_result != NULL)
  5488. cbf->on_result(ssl);
  5489. #ifdef WOLFSSL_TIRTOS
  5490. Task_yield();
  5491. #endif
  5492. opts->return_code = TEST_SUCCESS;
  5493. done:
  5494. if (cbf != NULL)
  5495. cbf->last_err = err;
  5496. if (cbf != NULL && cbf->on_cleanup != NULL)
  5497. cbf->on_cleanup(ssl);
  5498. wolfSSL_shutdown(ssl);
  5499. wolfSSL_free(ssl);
  5500. if (!sharedCtx)
  5501. wolfSSL_CTX_free(ctx);
  5502. CloseSocket(clientfd);
  5503. #ifdef WOLFSSL_TIRTOS
  5504. fdCloseSession(Task_self());
  5505. #endif
  5506. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5507. && defined(HAVE_THREAD_LS)
  5508. wc_ecc_fp_free(); /* free per thread cache */
  5509. #endif
  5510. if (cbf == NULL || !cbf->ticNoInit) {
  5511. #if defined(HAVE_SESSION_TICKET) && \
  5512. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5513. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5514. OpenSSLTicketCleanup();
  5515. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5516. TicketCleanup();
  5517. #endif
  5518. #endif
  5519. }
  5520. wolfSSL_SetLoggingPrefix(NULL);
  5521. #ifndef WOLFSSL_TIRTOS
  5522. return 0;
  5523. #endif
  5524. }
  5525. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5526. !defined(WOLFSSL_NO_TLS12)
  5527. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  5528. {
  5529. SOCKET_T sockfd = 0;
  5530. SOCKET_T clientfd = 0;
  5531. word16 port;
  5532. callback_functions* cbf;
  5533. WOLFSSL_CTX* ctx = 0;
  5534. WOLFSSL* ssl = 0;
  5535. char msg[] = "I hear you fa shizzle!";
  5536. char input[1024];
  5537. int idx;
  5538. int ret, err = 0;
  5539. int sharedCtx = 0;
  5540. func_args* opts = (func_args*)args;
  5541. int loop_count = opts->argc;
  5542. int count = 0;
  5543. #ifdef WOLFSSL_TIRTOS
  5544. fdOpenSession(Task_self());
  5545. #endif
  5546. opts->return_code = TEST_FAIL;
  5547. cbf = opts->callbacks;
  5548. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5549. if (cbf != NULL && cbf->ctx) {
  5550. ctx = cbf->ctx;
  5551. sharedCtx = 1;
  5552. }
  5553. else
  5554. #endif
  5555. {
  5556. WOLFSSL_METHOD* method = NULL;
  5557. if (cbf != NULL && cbf->method != NULL) {
  5558. method = cbf->method();
  5559. }
  5560. else {
  5561. method = wolfSSLv23_server_method();
  5562. }
  5563. ctx = wolfSSL_CTX_new(method);
  5564. }
  5565. #if defined(USE_WINDOWS_API)
  5566. port = opts->signal->port;
  5567. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5568. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5569. /* Let tcp_listen assign port */
  5570. port = 0;
  5571. #else
  5572. /* Use default port */
  5573. port = wolfSSLPort;
  5574. #endif
  5575. wolfSSL_CTX_set_verify(ctx,
  5576. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5577. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5578. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5579. #endif
  5580. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  5581. != WOLFSSL_SUCCESS) {
  5582. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5583. /* Release the wait for TCP ready. */
  5584. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5585. opts->signal->ready = 1;
  5586. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5587. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  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. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5596. opts->signal->ready = 1;
  5597. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5598. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5599. goto done;
  5600. }
  5601. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  5602. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5603. /*err_sys("can't load server key file, "
  5604. "Please run from wolfSSL home dir");*/
  5605. /* Release the wait for TCP ready. */
  5606. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5607. opts->signal->ready = 1;
  5608. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5609. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5610. goto done;
  5611. }
  5612. /* call ctx setup callback */
  5613. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5614. cbf->ctx_ready(ctx);
  5615. }
  5616. while (count != loop_count) {
  5617. ssl = wolfSSL_new(ctx);
  5618. if (ssl == NULL) {
  5619. /* Release the wait for TCP ready. */
  5620. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5621. opts->signal->ready = 1;
  5622. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5623. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5624. goto done;
  5625. }
  5626. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  5627. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5628. /*err_sys("can't load server cert chain file, "
  5629. "Please run from wolfSSL home dir");*/
  5630. /* Release the wait for TCP ready. */
  5631. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5632. opts->signal->ready = 1;
  5633. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5634. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5635. goto done;
  5636. }
  5637. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  5638. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5639. /*err_sys("can't load server key file, "
  5640. "Please run from wolfSSL home dir");*/
  5641. /* Release the wait for TCP ready. */
  5642. PTHREAD_CHECK_RET(pthread_mutex_lock(&opts->signal->mutex));
  5643. opts->signal->ready = 1;
  5644. PTHREAD_CHECK_RET(pthread_cond_signal(&opts->signal->cond));
  5645. PTHREAD_CHECK_RET(pthread_mutex_unlock(&opts->signal->mutex));
  5646. goto done;
  5647. }
  5648. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5649. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5650. #elif !defined(NO_DH)
  5651. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5652. #endif
  5653. /* call ssl setup callback */
  5654. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5655. cbf->ssl_ready(ssl);
  5656. }
  5657. /* do it here to detect failure */
  5658. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  5659. 0);
  5660. CloseSocket(sockfd);
  5661. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  5662. /*err_sys("SSL_set_fd failed");*/
  5663. goto done;
  5664. }
  5665. #ifdef WOLFSSL_ASYNC_CRYPT
  5666. err = 0; /* Reset error */
  5667. #endif
  5668. do {
  5669. #ifdef WOLFSSL_ASYNC_CRYPT
  5670. if (err == WC_PENDING_E) {
  5671. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5672. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5673. }
  5674. #endif
  5675. ret = wolfSSL_accept(ssl);
  5676. err = wolfSSL_get_error(ssl, 0);
  5677. } while (err == WC_PENDING_E);
  5678. if (ret != WOLFSSL_SUCCESS) {
  5679. char buff[WOLFSSL_MAX_ERROR_SZ];
  5680. fprintf(stderr, "error = %d, %s\n", err,
  5681. wolfSSL_ERR_error_string(err, buff));
  5682. /*err_sys("SSL_accept failed");*/
  5683. goto done;
  5684. }
  5685. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5686. if (idx > 0) {
  5687. input[idx] = '\0';
  5688. fprintf(stderr, "Client message: %s\n", input);
  5689. }
  5690. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  5691. /*err_sys("SSL_write failed");*/
  5692. goto done;
  5693. }
  5694. /* free ssl for this connection */
  5695. wolfSSL_shutdown(ssl);
  5696. wolfSSL_free(ssl); ssl = NULL;
  5697. CloseSocket(clientfd);
  5698. count++;
  5699. }
  5700. #ifdef WOLFSSL_TIRTOS
  5701. Task_yield();
  5702. #endif
  5703. opts->return_code = TEST_SUCCESS;
  5704. done:
  5705. if (ssl != NULL) {
  5706. wolfSSL_shutdown(ssl);
  5707. wolfSSL_free(ssl);
  5708. }
  5709. if (!sharedCtx)
  5710. wolfSSL_CTX_free(ctx);
  5711. CloseSocket(clientfd);
  5712. #ifdef WOLFSSL_TIRTOS
  5713. fdCloseSession(Task_self());
  5714. #endif
  5715. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5716. && defined(HAVE_THREAD_LS)
  5717. wc_ecc_fp_free(); /* free per thread cache */
  5718. #endif
  5719. #ifndef WOLFSSL_TIRTOS
  5720. return 0;
  5721. #endif
  5722. }
  5723. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5724. static int test_client_nofail(void* args, cbType cb)
  5725. {
  5726. #if !defined(NO_WOLFSSL_CLIENT)
  5727. SOCKET_T sockfd = 0;
  5728. callback_functions* cbf;
  5729. WOLFSSL_CTX* ctx = 0;
  5730. WOLFSSL* ssl = 0;
  5731. WOLFSSL_CIPHER* cipher;
  5732. char msg[64] = "hello wolfssl!";
  5733. char reply[1024];
  5734. int input;
  5735. int msgSz = (int)XSTRLEN(msg);
  5736. int ret, err = 0;
  5737. int cipherSuite;
  5738. int sharedCtx = 0;
  5739. int doUdp = 0;
  5740. const char* cipherName1, *cipherName2;
  5741. wolfSSL_SetLoggingPrefix("client");
  5742. #ifdef WOLFSSL_TIRTOS
  5743. fdOpenSession(Task_self());
  5744. #endif
  5745. ((func_args*)args)->return_code = TEST_FAIL;
  5746. cbf = ((func_args*)args)->callbacks;
  5747. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5748. if (cbf != NULL && cbf->ctx) {
  5749. ctx = cbf->ctx;
  5750. sharedCtx = cbf->isSharedCtx;
  5751. }
  5752. else
  5753. #endif
  5754. {
  5755. WOLFSSL_METHOD* method = NULL;
  5756. if (cbf != NULL && cbf->method != NULL) {
  5757. method = cbf->method();
  5758. }
  5759. else {
  5760. method = wolfSSLv23_client_method();
  5761. }
  5762. ctx = wolfSSL_CTX_new(method);
  5763. }
  5764. if (cbf != NULL)
  5765. doUdp = cbf->doUdp;
  5766. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5767. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5768. #endif
  5769. /* Do connect here so server detects failures */
  5770. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5771. doUdp, 0, NULL);
  5772. /* Connect the socket so that we don't have to set the peer later on */
  5773. if (doUdp)
  5774. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  5775. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5776. {
  5777. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5778. goto done;
  5779. }
  5780. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5781. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5782. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5783. #else
  5784. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5785. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5786. #endif
  5787. /*err_sys("can't load client cert file, "
  5788. "Please run from wolfSSL home dir");*/
  5789. goto done;
  5790. }
  5791. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5792. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5793. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5794. #else
  5795. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5796. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5797. #endif
  5798. /*err_sys("can't load client key file, "
  5799. "Please run from wolfSSL home dir");*/
  5800. goto done;
  5801. }
  5802. #ifdef HAVE_CRL
  5803. if (cbf != NULL && cbf->crlPemFile != NULL) {
  5804. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  5805. goto done;
  5806. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  5807. != WOLFSSL_SUCCESS)
  5808. goto done;
  5809. }
  5810. #endif
  5811. /* call ctx setup callback */
  5812. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5813. cbf->ctx_ready(ctx);
  5814. }
  5815. ssl = wolfSSL_new(ctx);
  5816. if (ssl == NULL) {
  5817. goto done;
  5818. }
  5819. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5820. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5821. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5822. #else
  5823. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  5824. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5825. #endif
  5826. /*err_sys("can't load client cert file, "
  5827. "Please run from wolfSSL home dir");*/
  5828. goto done;
  5829. }
  5830. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5831. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5832. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5833. #else
  5834. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5835. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5836. #endif
  5837. /*err_sys("can't load client key file, "
  5838. "Please run from wolfSSL home dir");*/
  5839. goto done;
  5840. }
  5841. if (!doUdp) {
  5842. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5843. /*err_sys("SSL_set_fd failed");*/
  5844. goto done;
  5845. }
  5846. }
  5847. else {
  5848. #ifdef WOLFSSL_DTLS
  5849. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5850. /*err_sys("SSL_set_fd failed");*/
  5851. goto done;
  5852. }
  5853. #else
  5854. goto done;
  5855. #endif
  5856. }
  5857. /* call ssl setup callback */
  5858. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5859. cbf->ssl_ready(ssl);
  5860. }
  5861. #ifdef WOLFSSL_ASYNC_CRYPT
  5862. err = 0; /* Reset error */
  5863. #endif
  5864. do {
  5865. #ifdef WOLFSSL_ASYNC_CRYPT
  5866. if (err == WC_PENDING_E) {
  5867. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5868. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5869. }
  5870. #endif
  5871. ret = wolfSSL_negotiate(ssl);
  5872. err = wolfSSL_get_error(ssl, 0);
  5873. } while (err == WC_PENDING_E);
  5874. if (ret != WOLFSSL_SUCCESS) {
  5875. char buff[WOLFSSL_MAX_ERROR_SZ];
  5876. fprintf(stderr, "error = %d, %s\n", err,
  5877. wolfSSL_ERR_error_string(err, buff));
  5878. /*err_sys("SSL_connect failed");*/
  5879. goto done;
  5880. }
  5881. /* test the various get cipher methods */
  5882. /* Internal cipher suite names */
  5883. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  5884. cipherName1 = wolfSSL_get_cipher_name(ssl);
  5885. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  5886. (cipherSuite >> 8), cipherSuite & 0xFF);
  5887. AssertStrEQ(cipherName1, cipherName2);
  5888. /* IANA Cipher Suites Names */
  5889. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  5890. then it's the internal cipher suite name */
  5891. cipher = wolfSSL_get_current_cipher(ssl);
  5892. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  5893. cipherName2 = wolfSSL_get_cipher(ssl);
  5894. AssertStrEQ(cipherName1, cipherName2);
  5895. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  5896. !defined(WOLFSSL_QT)
  5897. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  5898. (cipherSuite >> 8), cipherSuite & 0xFF);
  5899. AssertStrEQ(cipherName1, cipherName2);
  5900. #endif
  5901. if (cb != NULL)
  5902. (cb)(ctx, ssl);
  5903. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5904. /*err_sys("SSL_write failed");*/
  5905. goto done;
  5906. }
  5907. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5908. if (input > 0) {
  5909. reply[input] = '\0';
  5910. fprintf(stderr, "Server response: %s\n", reply);
  5911. }
  5912. if (cbf != NULL && cbf->on_result != NULL)
  5913. cbf->on_result(ssl);
  5914. ((func_args*)args)->return_code = TEST_SUCCESS;
  5915. done:
  5916. if (cbf != NULL)
  5917. cbf->last_err = err;
  5918. if (cbf != NULL && cbf->on_cleanup != NULL)
  5919. cbf->on_cleanup(ssl);
  5920. wolfSSL_free(ssl);
  5921. if (!sharedCtx)
  5922. wolfSSL_CTX_free(ctx);
  5923. CloseSocket(sockfd);
  5924. #ifdef WOLFSSL_TIRTOS
  5925. fdCloseSession(Task_self());
  5926. #endif
  5927. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5928. && defined(HAVE_THREAD_LS)
  5929. wc_ecc_fp_free(); /* free per thread cache */
  5930. #endif
  5931. #else
  5932. (void)args;
  5933. (void)cb;
  5934. #endif /* !NO_WOLFSSL_CLIENT */
  5935. wolfSSL_SetLoggingPrefix(NULL);
  5936. return 0;
  5937. }
  5938. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  5939. callback_functions* server_cb, cbType client_on_handshake)
  5940. {
  5941. func_args client_args;
  5942. func_args server_args;
  5943. tcp_ready ready;
  5944. THREAD_TYPE serverThread;
  5945. XMEMSET(&client_args, 0, sizeof(func_args));
  5946. XMEMSET(&server_args, 0, sizeof(func_args));
  5947. #ifdef WOLFSSL_TIRTOS
  5948. fdOpenSession(Task_self());
  5949. #endif
  5950. StartTCP();
  5951. InitTcpReady(&ready);
  5952. #if defined(USE_WINDOWS_API)
  5953. /* use RNG to get random port if using windows */
  5954. ready.port = GetRandomPort();
  5955. #endif
  5956. server_args.signal = &ready;
  5957. server_args.callbacks = server_cb;
  5958. client_args.signal = &ready;
  5959. client_args.callbacks = client_cb;
  5960. start_thread(test_server_nofail, &server_args, &serverThread);
  5961. wait_tcp_ready(&server_args);
  5962. test_client_nofail(&client_args, client_on_handshake);
  5963. join_thread(serverThread);
  5964. client_cb->return_code = client_args.return_code;
  5965. server_cb->return_code = server_args.return_code;
  5966. FreeTcpReady(&ready);
  5967. #ifdef WOLFSSL_TIRTOS
  5968. fdOpenSession(Task_self());
  5969. #endif
  5970. }
  5971. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  5972. callback_functions* server_cb)
  5973. {
  5974. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  5975. }
  5976. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5977. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  5978. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  5979. void* server_args)
  5980. {
  5981. SOCKET_T sockfd = 0;
  5982. callback_functions* cbf;
  5983. WOLFSSL_CTX* ctx = 0;
  5984. WOLFSSL* ssl = 0;
  5985. WOLFSSL_SESSION* session = NULL;
  5986. char msg[64] = "hello wolfssl!";
  5987. char reply[1024];
  5988. int input;
  5989. int msgSz = (int)XSTRLEN(msg);
  5990. int ret, err = 0;
  5991. int sharedCtx = 0;
  5992. #ifdef WOLFSSL_TIRTOS
  5993. fdOpenSession(Task_self());
  5994. #endif
  5995. ((func_args*)args)->return_code = TEST_FAIL;
  5996. cbf = ((func_args*)args)->callbacks;
  5997. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5998. if (cbf != NULL && cbf->ctx) {
  5999. ctx = cbf->ctx;
  6000. sharedCtx = 1;
  6001. }
  6002. else
  6003. #endif
  6004. {
  6005. WOLFSSL_METHOD* method = NULL;
  6006. if (cbf != NULL && cbf->method != NULL) {
  6007. method = cbf->method();
  6008. }
  6009. else {
  6010. method = wolfSSLv23_client_method();
  6011. }
  6012. ctx = wolfSSL_CTX_new(method);
  6013. }
  6014. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6015. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6016. #endif
  6017. /* Do connect here so server detects failures */
  6018. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6019. 0, 0, NULL);
  6020. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6021. WOLFSSL_SUCCESS) {
  6022. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6023. goto done;
  6024. }
  6025. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6026. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6027. /*err_sys("can't load client cert file, "
  6028. "Please run from wolfSSL home dir");*/
  6029. goto done;
  6030. }
  6031. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6032. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6033. /*err_sys("can't load client key file, "
  6034. "Please run from wolfSSL home dir");*/
  6035. goto done;
  6036. }
  6037. /* call ctx setup callback */
  6038. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6039. cbf->ctx_ready(ctx);
  6040. }
  6041. ssl = wolfSSL_new(ctx);
  6042. if (ssl == NULL) {
  6043. goto done;
  6044. }
  6045. /* keep handshake resources for re-using WOLFSSL obj */
  6046. wolfSSL_KeepArrays(ssl);
  6047. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6048. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6049. goto done;
  6050. }
  6051. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6052. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6053. /*err_sys("can't load client cert file, "
  6054. "Please run from wolfSSL home dir");*/
  6055. goto done;
  6056. }
  6057. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6058. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6059. /*err_sys("can't load client key file, "
  6060. "Please run from wolfSSL home dir");*/
  6061. goto done;
  6062. }
  6063. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6064. /*err_sys("SSL_set_fd failed");*/
  6065. goto done;
  6066. }
  6067. /* call ssl setup callback */
  6068. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6069. cbf->ssl_ready(ssl);
  6070. }
  6071. #ifdef WOLFSSL_ASYNC_CRYPT
  6072. err = 0; /* Reset error */
  6073. #endif
  6074. do {
  6075. #ifdef WOLFSSL_ASYNC_CRYPT
  6076. if (err == WC_PENDING_E) {
  6077. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6078. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6079. }
  6080. #endif
  6081. ret = wolfSSL_connect(ssl);
  6082. err = wolfSSL_get_error(ssl, 0);
  6083. } while (err == WC_PENDING_E);
  6084. if (ret != WOLFSSL_SUCCESS) {
  6085. char buff[WOLFSSL_MAX_ERROR_SZ];
  6086. fprintf(stderr, "error = %d, %s\n", err,
  6087. wolfSSL_ERR_error_string(err, buff));
  6088. /*err_sys("SSL_connect failed");*/
  6089. goto done;
  6090. }
  6091. /* Build first session */
  6092. if (cb != NULL)
  6093. cb(ctx, ssl);
  6094. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6095. /*err_sys("SSL_write failed");*/
  6096. goto done;
  6097. }
  6098. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6099. if (input > 0) {
  6100. reply[input] = '\0';
  6101. fprintf(stderr, "Server response: %s\n", reply);
  6102. }
  6103. /* Session Resumption by re-using WOLFSSL object */
  6104. wolfSSL_set_quiet_shutdown(ssl, 1);
  6105. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6106. /* err_sys ("SSL shutdown failed"); */
  6107. goto done;
  6108. }
  6109. session = wolfSSL_get1_session(ssl);
  6110. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6111. wolfSSL_SESSION_free(session);
  6112. /* err_sys ("SSL_clear failed"); */
  6113. goto done;
  6114. }
  6115. wolfSSL_set_session(ssl, session);
  6116. wolfSSL_SESSION_free(session);
  6117. session = NULL;
  6118. /* close socket once */
  6119. CloseSocket(sockfd);
  6120. sockfd = 0;
  6121. /* wait until server ready */
  6122. wait_tcp_ready((func_args*)server_args);
  6123. fprintf(stderr, "session resumption\n");
  6124. /* Do re-connect */
  6125. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6126. 0, 0, NULL);
  6127. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6128. /*err_sys("SSL_set_fd failed");*/
  6129. goto done;
  6130. }
  6131. #ifdef WOLFSSL_ASYNC_CRYPT
  6132. err = 0; /* Reset error */
  6133. #endif
  6134. do {
  6135. #ifdef WOLFSSL_ASYNC_CRYPT
  6136. if (err == WC_PENDING_E) {
  6137. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6138. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6139. }
  6140. #endif
  6141. ret = wolfSSL_connect(ssl);
  6142. err = wolfSSL_get_error(ssl, 0);
  6143. } while (err == WC_PENDING_E);
  6144. if (ret != WOLFSSL_SUCCESS) {
  6145. char buff[WOLFSSL_MAX_ERROR_SZ];
  6146. fprintf(stderr, "error = %d, %s\n", err,
  6147. wolfSSL_ERR_error_string(err, buff));
  6148. /*err_sys("SSL_connect failed");*/
  6149. goto done;
  6150. }
  6151. /* Build first session */
  6152. if (cb != NULL)
  6153. cb(ctx, ssl);
  6154. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6155. /*err_sys("SSL_write failed");*/
  6156. goto done;
  6157. }
  6158. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6159. if (input > 0) {
  6160. reply[input] = '\0';
  6161. fprintf(stderr, "Server response: %s\n", reply);
  6162. }
  6163. ((func_args*)args)->return_code = TEST_SUCCESS;
  6164. done:
  6165. wolfSSL_free(ssl);
  6166. if (!sharedCtx)
  6167. wolfSSL_CTX_free(ctx);
  6168. CloseSocket(sockfd);
  6169. #ifdef WOLFSSL_TIRTOS
  6170. fdCloseSession(Task_self());
  6171. #endif
  6172. return;
  6173. }
  6174. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6175. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  6176. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  6177. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  6178. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  6179. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  6180. #define HAVE_ALPN_PROTOS_SUPPORT
  6181. #endif
  6182. /* Generic TLS client / server with callbacks for API unit tests
  6183. * Used by SNI / ALPN / crypto callback helper functions */
  6184. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  6185. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  6186. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  6187. #define ENABLE_TLS_CALLBACK_TEST
  6188. #endif
  6189. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  6190. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  6191. /* TLS server for API unit testing - generic */
  6192. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  6193. {
  6194. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6195. WOLFSSL_CTX* ctx = NULL;
  6196. WOLFSSL* ssl = NULL;
  6197. SOCKET_T sfd = 0;
  6198. SOCKET_T cfd = 0;
  6199. word16 port;
  6200. char msg[] = "I hear you fa shizzle!";
  6201. int len = (int) XSTRLEN(msg);
  6202. char input[1024];
  6203. int idx;
  6204. int ret, err = 0;
  6205. ((func_args*)args)->return_code = TEST_FAIL;
  6206. #if defined(USE_WINDOWS_API)
  6207. port = ((func_args*)args)->signal->port;
  6208. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6209. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6210. /* Let tcp_listen assign port */
  6211. port = 0;
  6212. #else
  6213. /* Use default port */
  6214. port = wolfSSLPort;
  6215. #endif
  6216. #ifdef WOLFSSL_DTLS
  6217. if (callbacks->method == wolfDTLS_server_method
  6218. #ifdef WOLFSSL_STATIC_MEMORY
  6219. || callbacks->method_ex == wolfDTLS_server_method_ex
  6220. #endif
  6221. #ifndef NO_OLD_TLS
  6222. || callbacks->method == wolfDTLSv1_server_method
  6223. #ifdef WOLFSSL_STATIC_MEMORY
  6224. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  6225. #endif
  6226. #endif
  6227. #ifndef WOLFSSL_NO_TLS12
  6228. || callbacks->method == wolfDTLSv1_2_server_method
  6229. #ifdef WOLFSSL_STATIC_MEMORY
  6230. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  6231. #endif
  6232. #endif
  6233. #ifdef WOLFSSL_DTLS13
  6234. || callbacks->method == wolfDTLSv1_3_server_method
  6235. #ifdef WOLFSSL_STATIC_MEMORY
  6236. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  6237. #endif
  6238. #endif
  6239. ) {
  6240. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  6241. }
  6242. else
  6243. #endif
  6244. {
  6245. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6246. }
  6247. #ifdef WOLFSSL_STATIC_MEMORY
  6248. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6249. callbacks->memSz > 0) {
  6250. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6251. callbacks->mem, callbacks->memSz, 0, 1);
  6252. if (ret != WOLFSSL_SUCCESS) {
  6253. fprintf(stderr, "CTX static new failed %d\n", ret);
  6254. goto cleanup;
  6255. }
  6256. }
  6257. #else
  6258. ctx = wolfSSL_CTX_new(callbacks->method());
  6259. #endif
  6260. if (ctx == NULL) {
  6261. fprintf(stderr, "CTX new failed\n");
  6262. goto cleanup;
  6263. }
  6264. /* set defaults */
  6265. if (callbacks->caPemFile == NULL)
  6266. callbacks->caPemFile = cliCertFile;
  6267. if (callbacks->certPemFile == NULL)
  6268. callbacks->certPemFile = svrCertFile;
  6269. if (callbacks->keyPemFile == NULL)
  6270. callbacks->keyPemFile = svrKeyFile;
  6271. #ifdef WOLFSSL_TIRTOS
  6272. fdOpenSession(Task_self());
  6273. #endif
  6274. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6275. wolfSSL_CTX_set_verify(ctx,
  6276. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6277. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6278. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6279. #endif
  6280. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  6281. if (callbacks->method == wolfDTLSv1_2_server_method) {
  6282. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  6283. goto cleanup;
  6284. }
  6285. #endif
  6286. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6287. WOLFSSL_SUCCESS) {
  6288. goto cleanup;
  6289. }
  6290. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6291. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6292. goto cleanup;
  6293. }
  6294. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6295. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6296. goto cleanup;
  6297. }
  6298. #ifdef HAVE_CRL
  6299. if (callbacks->crlPemFile != NULL) {
  6300. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6301. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6302. goto cleanup;
  6303. }
  6304. }
  6305. #endif
  6306. if (callbacks->ctx_ready)
  6307. callbacks->ctx_ready(ctx);
  6308. ssl = wolfSSL_new(ctx);
  6309. if (ssl == NULL) {
  6310. fprintf(stderr, "SSL new failed\n");
  6311. goto cleanup;
  6312. }
  6313. if (wolfSSL_dtls(ssl)) {
  6314. SOCKADDR_IN_T cliAddr;
  6315. socklen_t cliLen;
  6316. cliLen = sizeof(cliAddr);
  6317. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  6318. (struct sockaddr*)&cliAddr, &cliLen);
  6319. if (idx <= 0) {
  6320. goto cleanup;
  6321. }
  6322. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6323. }
  6324. else {
  6325. CloseSocket(sfd);
  6326. }
  6327. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  6328. goto cleanup;
  6329. }
  6330. if (callbacks->loadToSSL) {
  6331. wolfSSL_SetDevId(ssl, callbacks->devId);
  6332. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6333. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6334. goto cleanup;
  6335. }
  6336. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6337. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6338. goto cleanup;
  6339. }
  6340. }
  6341. #ifdef NO_PSK
  6342. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6343. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6344. #elif !defined(NO_DH)
  6345. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6346. #endif
  6347. #endif
  6348. if (callbacks->ssl_ready)
  6349. callbacks->ssl_ready(ssl);
  6350. #ifdef WOLFSSL_ASYNC_CRYPT
  6351. err = 0; /* Reset error */
  6352. #endif
  6353. do {
  6354. #ifdef WOLFSSL_ASYNC_CRYPT
  6355. if (err == WC_PENDING_E) {
  6356. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6357. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6358. }
  6359. #endif
  6360. ret = wolfSSL_accept(ssl);
  6361. err = wolfSSL_get_error(ssl, ret);
  6362. } while (err == WC_PENDING_E);
  6363. if (ret != WOLFSSL_SUCCESS) {
  6364. char buff[WOLFSSL_MAX_ERROR_SZ];
  6365. fprintf(stderr, "accept error = %d, %s\n", err,
  6366. wolfSSL_ERR_error_string(err, buff));
  6367. /*err_sys("SSL_accept failed");*/
  6368. }
  6369. else {
  6370. #ifdef WOLFSSL_ASYNC_CRYPT
  6371. err = 0; /* Reset error */
  6372. #endif
  6373. do {
  6374. #ifdef WOLFSSL_ASYNC_CRYPT
  6375. if (err == WC_PENDING_E) {
  6376. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6377. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6378. }
  6379. #endif
  6380. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6381. err = wolfSSL_get_error(ssl, idx);
  6382. } while (err == WC_PENDING_E);
  6383. if (idx > 0) {
  6384. input[idx] = 0;
  6385. fprintf(stderr, "Client message: %s\n", input);
  6386. }
  6387. #ifdef WOLFSSL_ASYNC_CRYPT
  6388. err = 0; /* Reset error */
  6389. #endif
  6390. do {
  6391. #ifdef WOLFSSL_ASYNC_CRYPT
  6392. if (err == WC_PENDING_E) {
  6393. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6394. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6395. }
  6396. #endif
  6397. ret = wolfSSL_write(ssl, msg, len);
  6398. err = wolfSSL_get_error(ssl, ret);
  6399. } while (err == WC_PENDING_E);
  6400. if (len != ret) {
  6401. goto cleanup;
  6402. }
  6403. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  6404. defined(WOLFSSL_DTLS)
  6405. if (wolfSSL_dtls(ssl)) {
  6406. byte* import;
  6407. word32 sz;
  6408. wolfSSL_dtls_export(ssl, NULL, &sz);
  6409. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6410. if (import == NULL) {
  6411. goto cleanup;
  6412. }
  6413. idx = wolfSSL_dtls_export(ssl, import, &sz);
  6414. if (idx < 0) {
  6415. goto cleanup;
  6416. }
  6417. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  6418. goto cleanup;
  6419. }
  6420. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6421. }
  6422. #endif
  6423. #ifdef WOLFSSL_TIRTOS
  6424. Task_yield();
  6425. #endif
  6426. ((func_args*)args)->return_code = TEST_SUCCESS;
  6427. }
  6428. if (callbacks->on_result)
  6429. callbacks->on_result(ssl);
  6430. wolfSSL_shutdown(ssl);
  6431. cleanup:
  6432. wolfSSL_free(ssl);
  6433. wolfSSL_CTX_free(ctx);
  6434. CloseSocket(cfd);
  6435. #ifdef WOLFSSL_TIRTOS
  6436. fdCloseSession(Task_self());
  6437. #endif
  6438. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6439. && defined(HAVE_THREAD_LS)
  6440. wc_ecc_fp_free(); /* free per thread cache */
  6441. #endif
  6442. #ifndef WOLFSSL_TIRTOS
  6443. return 0;
  6444. #endif
  6445. }
  6446. /* TLS Client for API unit testing - generic */
  6447. static void run_wolfssl_client(void* args)
  6448. {
  6449. callback_functions* callbacks = ((func_args*)args)->callbacks;
  6450. WOLFSSL_CTX* ctx = NULL;
  6451. WOLFSSL* ssl = NULL;
  6452. SOCKET_T sfd = 0;
  6453. char msg[] = "hello wolfssl server!";
  6454. int len = (int) XSTRLEN(msg);
  6455. char input[1024];
  6456. int ret, err = 0;
  6457. ((func_args*)args)->return_code = TEST_FAIL;
  6458. /* set defaults */
  6459. if (callbacks->caPemFile == NULL)
  6460. callbacks->caPemFile = caCertFile;
  6461. if (callbacks->certPemFile == NULL)
  6462. callbacks->certPemFile = cliCertFile;
  6463. if (callbacks->keyPemFile == NULL)
  6464. callbacks->keyPemFile = cliKeyFile;
  6465. #ifdef WOLFSSL_STATIC_MEMORY
  6466. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  6467. callbacks->memSz > 0) {
  6468. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  6469. callbacks->mem, callbacks->memSz, 0, 1);
  6470. if (ret != WOLFSSL_SUCCESS) {
  6471. fprintf(stderr, "CTX static new failed %d\n", ret);
  6472. goto cleanup;
  6473. }
  6474. }
  6475. #else
  6476. ctx = wolfSSL_CTX_new(callbacks->method());
  6477. #endif
  6478. if (ctx == NULL) {
  6479. fprintf(stderr, "CTX new failed\n");
  6480. goto cleanup;
  6481. }
  6482. #ifdef WOLFSSL_TIRTOS
  6483. fdOpenSession(Task_self());
  6484. #endif
  6485. if (!callbacks->loadToSSL) {
  6486. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  6487. }
  6488. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6489. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6490. #endif
  6491. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  6492. WOLFSSL_SUCCESS) {
  6493. goto cleanup;
  6494. }
  6495. if (!callbacks->loadToSSL) {
  6496. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  6497. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6498. goto cleanup;
  6499. }
  6500. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  6501. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6502. goto cleanup;
  6503. }
  6504. }
  6505. #ifdef HAVE_CRL
  6506. if (callbacks->crlPemFile != NULL) {
  6507. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  6508. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6509. goto cleanup;
  6510. }
  6511. }
  6512. #endif
  6513. if (callbacks->ctx_ready)
  6514. callbacks->ctx_ready(ctx);
  6515. ssl = wolfSSL_new(ctx);
  6516. if (wolfSSL_dtls(ssl)) {
  6517. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6518. 1, 0, ssl);
  6519. }
  6520. else {
  6521. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6522. 0, 0, ssl);
  6523. }
  6524. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  6525. goto cleanup;
  6526. }
  6527. if (callbacks->loadToSSL) {
  6528. wolfSSL_SetDevId(ssl, callbacks->devId);
  6529. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  6530. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6531. goto cleanup;
  6532. }
  6533. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  6534. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6535. goto cleanup;
  6536. }
  6537. }
  6538. if (callbacks->ssl_ready)
  6539. callbacks->ssl_ready(ssl);
  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_connect(ssl);
  6551. err = wolfSSL_get_error(ssl, ret);
  6552. } while (err == WC_PENDING_E);
  6553. if (ret != WOLFSSL_SUCCESS) {
  6554. char buff[WOLFSSL_MAX_ERROR_SZ];
  6555. fprintf(stderr, "error = %d, %s\n", err,
  6556. wolfSSL_ERR_error_string(err, buff));
  6557. /*err_sys("SSL_connect failed");*/
  6558. }
  6559. else {
  6560. #ifdef WOLFSSL_ASYNC_CRYPT
  6561. err = 0; /* Reset error */
  6562. #endif
  6563. do {
  6564. #ifdef WOLFSSL_ASYNC_CRYPT
  6565. if (err == WC_PENDING_E) {
  6566. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6567. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6568. }
  6569. #endif
  6570. ret = wolfSSL_write(ssl, msg, len);
  6571. err = wolfSSL_get_error(ssl, ret);
  6572. } while (err == WC_PENDING_E);
  6573. if (len != ret)
  6574. goto cleanup;
  6575. #ifdef WOLFSSL_ASYNC_CRYPT
  6576. err = 0; /* Reset error */
  6577. #endif
  6578. do {
  6579. #ifdef WOLFSSL_ASYNC_CRYPT
  6580. if (err == WC_PENDING_E) {
  6581. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6582. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6583. }
  6584. #endif
  6585. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6586. err = wolfSSL_get_error(ssl, ret);
  6587. } while (err == WC_PENDING_E);
  6588. if (ret > 0) {
  6589. input[ret] = '\0'; /* null term */
  6590. fprintf(stderr, "Server response: %s\n", input);
  6591. }
  6592. ((func_args*)args)->return_code = TEST_SUCCESS;
  6593. }
  6594. if (callbacks->on_result)
  6595. callbacks->on_result(ssl);
  6596. cleanup:
  6597. wolfSSL_free(ssl);
  6598. wolfSSL_CTX_free(ctx);
  6599. CloseSocket(sfd);
  6600. #ifdef WOLFSSL_TIRTOS
  6601. fdCloseSession(Task_self());
  6602. #endif
  6603. }
  6604. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6605. static int test_wolfSSL_read_write(void)
  6606. {
  6607. /* The unit testing for read and write shall happen simultaneously, since
  6608. * one can't do anything with one without the other. (Except for a failure
  6609. * test case.) This function will call all the others that will set up,
  6610. * execute, and report their test findings.
  6611. *
  6612. * Set up the success case first. This function will become the template
  6613. * for the other tests. This should eventually be renamed
  6614. *
  6615. * The success case isn't interesting, how can this fail?
  6616. * - Do not give the client context a CA certificate. The connect should
  6617. * fail. Do not need server for this?
  6618. * - Using NULL for the ssl object on server. Do not need client for this.
  6619. * - Using NULL for the ssl object on client. Do not need server for this.
  6620. * - Good ssl objects for client and server. Client write() without server
  6621. * read().
  6622. * - Good ssl objects for client and server. Server write() without client
  6623. * read().
  6624. * - Forgetting the password callback?
  6625. */
  6626. tcp_ready ready;
  6627. func_args client_args;
  6628. func_args server_args;
  6629. THREAD_TYPE serverThread;
  6630. EXPECT_DECLS;
  6631. XMEMSET(&client_args, 0, sizeof(func_args));
  6632. XMEMSET(&server_args, 0, sizeof(func_args));
  6633. #ifdef WOLFSSL_TIRTOS
  6634. fdOpenSession(Task_self());
  6635. #endif
  6636. StartTCP();
  6637. InitTcpReady(&ready);
  6638. #if defined(USE_WINDOWS_API)
  6639. /* use RNG to get random port if using windows */
  6640. ready.port = GetRandomPort();
  6641. #endif
  6642. server_args.signal = &ready;
  6643. client_args.signal = &ready;
  6644. start_thread(test_server_nofail, &server_args, &serverThread);
  6645. wait_tcp_ready(&server_args);
  6646. test_client_nofail(&client_args, NULL);
  6647. join_thread(serverThread);
  6648. ExpectTrue(client_args.return_code);
  6649. ExpectTrue(server_args.return_code);
  6650. FreeTcpReady(&ready);
  6651. #ifdef WOLFSSL_TIRTOS
  6652. fdOpenSession(Task_self());
  6653. #endif
  6654. return EXPECT_RESULT();
  6655. }
  6656. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  6657. {
  6658. EXPECT_DECLS;
  6659. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6660. !defined(WOLFSSL_NO_TLS12)
  6661. /* The unit test for session resumption by re-using WOLFSSL object.
  6662. * WOLFSSL object is not cleared after first session. It reuse the object
  6663. * for second connection.
  6664. */
  6665. tcp_ready ready;
  6666. func_args client_args;
  6667. func_args server_args;
  6668. THREAD_TYPE serverThread;
  6669. callback_functions client_cbf;
  6670. callback_functions server_cbf;
  6671. XMEMSET(&client_args, 0, sizeof(func_args));
  6672. XMEMSET(&server_args, 0, sizeof(func_args));
  6673. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6674. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6675. #ifdef WOLFSSL_TIRTOS
  6676. fdOpenSession(Task_self());
  6677. #endif
  6678. StartTCP();
  6679. InitTcpReady(&ready);
  6680. #if defined(USE_WINDOWS_API)
  6681. /* use RNG to get random port if using windows */
  6682. ready.port = GetRandomPort();
  6683. #endif
  6684. client_cbf.method = wolfTLSv1_2_client_method;
  6685. server_cbf.method = wolfTLSv1_2_server_method;
  6686. client_args.callbacks = &client_cbf;
  6687. server_args.callbacks = &server_cbf;
  6688. server_args.signal = &ready;
  6689. client_args.signal = &ready;
  6690. /* the var is used for loop number */
  6691. server_args.argc = 2;
  6692. start_thread(test_server_loop, &server_args, &serverThread);
  6693. wait_tcp_ready(&server_args);
  6694. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  6695. join_thread(serverThread);
  6696. ExpectTrue(client_args.return_code);
  6697. ExpectTrue(server_args.return_code);
  6698. FreeTcpReady(&ready);
  6699. #ifdef WOLFSSL_TIRTOS
  6700. fdOpenSession(Task_self());
  6701. #endif
  6702. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  6703. * !defined(WOLFSSL_TLS13) */
  6704. return EXPECT_RESULT();
  6705. }
  6706. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6707. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6708. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  6709. WOLFSSL_CTX* ctx)
  6710. {
  6711. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6712. myVerifyAction = VERIFY_USE_PREVERFIY;
  6713. wolfSSL_CTX_set_verify_depth(ctx, 2);
  6714. return TEST_SUCCESS;
  6715. }
  6716. #endif
  6717. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  6718. {
  6719. EXPECT_DECLS;
  6720. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6721. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6722. test_ssl_cbf client_cbf;
  6723. test_ssl_cbf server_cbf;
  6724. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6725. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6726. #ifdef WOLFSSL_TLS13
  6727. client_cbf.method = wolfTLSv1_3_client_method;
  6728. #endif /* WOLFSSL_TLS13 */
  6729. client_cbf.ctx_ready =
  6730. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  6731. /* test case 1 verify depth is equal to peer chain */
  6732. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6733. &server_cbf, NULL), TEST_SUCCESS);
  6734. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6735. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6736. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6737. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6738. return EXPECT_RESULT();
  6739. }
  6740. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6741. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6742. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  6743. WOLFSSL_CTX* ctx)
  6744. {
  6745. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6746. myVerifyAction = VERIFY_USE_PREVERFIY;
  6747. wolfSSL_CTX_set_verify_depth(ctx, 1);
  6748. return TEST_SUCCESS;
  6749. }
  6750. #endif
  6751. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  6752. {
  6753. EXPECT_DECLS;
  6754. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6755. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6756. test_ssl_cbf client_cbf;
  6757. test_ssl_cbf server_cbf;
  6758. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6759. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6760. #ifdef WOLFSSL_TLS13
  6761. client_cbf.method = wolfTLSv1_3_client_method;
  6762. #endif /* WOLFSSL_TLS13 */
  6763. client_cbf.ctx_ready =
  6764. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  6765. /* test case 2
  6766. * verify depth is zero, number of peer's chain is 2.
  6767. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  6768. * callback.
  6769. */
  6770. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6771. &server_cbf, NULL), TEST_SUCCESS);
  6772. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6773. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6774. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6775. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6776. return EXPECT_RESULT();
  6777. }
  6778. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6779. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6780. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  6781. WOLFSSL_CTX* ctx)
  6782. {
  6783. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  6784. myVerifyAction = VERIFY_USE_PREVERFIY;
  6785. wolfSSL_CTX_set_verify_depth(ctx, 0);
  6786. return TEST_SUCCESS;
  6787. }
  6788. #endif
  6789. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  6790. {
  6791. EXPECT_DECLS;
  6792. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  6793. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  6794. test_ssl_cbf client_cbf;
  6795. test_ssl_cbf server_cbf;
  6796. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6797. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6798. #ifdef WOLFSSL_TLS13
  6799. client_cbf.method = wolfTLSv1_3_client_method;
  6800. #endif /* WOLFSSL_TLS13 */
  6801. client_cbf.ctx_ready =
  6802. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  6803. /* test case 3
  6804. * verify depth is zero, number of peer's chain is 2
  6805. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  6806. * therefore, handshake becomes failure.
  6807. */
  6808. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6809. &server_cbf, NULL), TEST_SUCCESS);
  6810. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6811. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6812. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  6813. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  6814. return EXPECT_RESULT();
  6815. }
  6816. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6817. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6818. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6819. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  6820. {
  6821. EXPECT_DECLS;
  6822. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  6823. return EXPECT_RESULT();
  6824. }
  6825. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  6826. {
  6827. EXPECT_DECLS;
  6828. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  6829. return EXPECT_RESULT();
  6830. }
  6831. #endif
  6832. static int test_wolfSSL_CTX_set_cipher_list(void)
  6833. {
  6834. EXPECT_DECLS;
  6835. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6836. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  6837. && !defined(NO_SHA256) && defined(HAVE_ECC)
  6838. WOLFSSL_CTX* ctxClient = NULL;
  6839. WOLFSSL* sslClient = NULL;
  6840. test_ssl_cbf client_cbf;
  6841. test_ssl_cbf server_cbf;
  6842. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6843. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6844. server_cbf.method = wolfTLSv1_2_server_method;
  6845. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  6846. client_cbf.method = wolfTLSv1_2_client_method;
  6847. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  6848. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6849. &server_cbf, NULL), TEST_SUCCESS);
  6850. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  6851. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  6852. /* check with cipher string that has '+' */
  6853. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  6854. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  6855. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  6856. /* check for the existence of an ECDHE ECDSA cipher suite */
  6857. if (EXPECT_SUCCESS()) {
  6858. int i = 0;
  6859. int found = 0;
  6860. const char* suite;
  6861. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  6862. WOLFSSL_CIPHER* current;
  6863. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  6864. do {
  6865. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  6866. if (current) {
  6867. suite = wolfSSL_CIPHER_get_name(current);
  6868. if (suite && XSTRSTR(suite, "ECDSA")) {
  6869. found = 1;
  6870. break;
  6871. }
  6872. }
  6873. } while (current);
  6874. ExpectIntEQ(found, 1);
  6875. }
  6876. wolfSSL_free(sslClient);
  6877. wolfSSL_CTX_free(ctxClient);
  6878. #endif
  6879. return EXPECT_RESULT();
  6880. }
  6881. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6882. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6883. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  6884. WOLFSSL* ssl)
  6885. {
  6886. EXPECT_DECLS;
  6887. size_t msg_len;
  6888. (void)ctx;
  6889. /* get_finished test */
  6890. /* 1. get own sent message */
  6891. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  6892. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  6893. ExpectIntGE(msg_len, 0);
  6894. /* 2. get peer message */
  6895. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  6896. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  6897. ExpectIntGE(msg_len, 0);
  6898. return EXPECT_RESULT();
  6899. }
  6900. #endif
  6901. static int test_wolfSSL_get_finished(void)
  6902. {
  6903. EXPECT_DECLS;
  6904. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  6905. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  6906. test_ssl_cbf client_cbf;
  6907. test_ssl_cbf server_cbf;
  6908. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  6909. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  6910. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  6911. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  6912. TEST_SUCCESS);
  6913. /* test received msg vs sent msg */
  6914. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6915. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6916. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  6917. return EXPECT_RESULT();
  6918. }
  6919. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6920. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6921. !defined(NO_SESSION_CACHE)
  6922. /* Sessions to restore/store */
  6923. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6924. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6925. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6926. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6927. {
  6928. /* Don't store sessions. Lookup is still enabled. */
  6929. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6930. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6931. #ifdef OPENSSL_EXTRA
  6932. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  6933. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  6934. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  6935. #endif
  6936. /* Require both peers to provide certs */
  6937. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6938. }
  6939. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6940. {
  6941. WOLFSSL_SESSION** sess;
  6942. if (wolfSSL_is_server(ssl))
  6943. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6944. else
  6945. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6946. if (*sess == NULL) {
  6947. #ifdef NO_SESSION_CACHE_REF
  6948. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6949. #else
  6950. /* Test for backwards compatibility */
  6951. if (wolfSSL_is_server(ssl)) {
  6952. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6953. }
  6954. else {
  6955. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6956. }
  6957. #endif
  6958. /* Now save the session in the internal store to make it available
  6959. * for lookup. For TLS 1.3, we can't save the session without
  6960. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6961. * session from cache. */
  6962. if (wolfSSL_is_server(ssl)
  6963. #ifndef WOLFSSL_TICKET_HAVE_ID
  6964. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6965. #endif
  6966. )
  6967. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6968. *sess), WOLFSSL_SUCCESS);
  6969. }
  6970. else {
  6971. /* If we have a session retrieved then remaining connections should be
  6972. * resuming on that session */
  6973. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6974. }
  6975. /* Save CTX to be able to decrypt tickets */
  6976. if (wolfSSL_is_server(ssl) &&
  6977. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6978. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6979. = wolfSSL_get_SSL_CTX(ssl));
  6980. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6981. WOLFSSL_SUCCESS);
  6982. }
  6983. #ifdef SESSION_CERTS
  6984. #ifndef WOLFSSL_TICKET_HAVE_ID
  6985. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6986. wolfSSL_session_reused(ssl))
  6987. #endif
  6988. {
  6989. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6990. * for all connections. TLS 1.3 only has tickets so if we don't
  6991. * include the session id in the ticket then the certificates
  6992. * will not be available on resumption. */
  6993. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6994. AssertNotNull(peer);
  6995. wolfSSL_X509_free(peer);
  6996. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6997. #ifdef OPENSSL_EXTRA
  6998. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  6999. #endif
  7000. }
  7001. #endif /* SESSION_CERTS */
  7002. }
  7003. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7004. {
  7005. /* Set the session to reuse for the client */
  7006. AssertIntEQ(wolfSSL_set_session(ssl,
  7007. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7008. }
  7009. #endif
  7010. static int test_wolfSSL_CTX_add_session(void)
  7011. {
  7012. EXPECT_DECLS;
  7013. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7014. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7015. !defined(NO_SESSION_CACHE)
  7016. tcp_ready ready;
  7017. func_args client_args;
  7018. func_args server_args;
  7019. THREAD_TYPE serverThread;
  7020. callback_functions client_cb;
  7021. callback_functions server_cb;
  7022. method_provider methods[][2] = {
  7023. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7024. !defined(NO_DES3))
  7025. /* Without AES there are almost no ciphersuites available. This leads
  7026. * to no ciphersuites being available and an error. */
  7027. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7028. #endif
  7029. #ifndef WOLFSSL_NO_TLS12
  7030. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7031. #endif
  7032. /* Needs the default ticket callback since it is tied to the
  7033. * connection context and this makes it easy to carry over the ticket
  7034. * crypto context between connections */
  7035. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7036. defined(HAVE_SESSION_TICKET)
  7037. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7038. #endif
  7039. };
  7040. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7041. size_t i, j;
  7042. for (i = 0; i < methodsLen; i++) {
  7043. /* First run creates a connection while the second+ run will attempt
  7044. * to resume the connection. The trick is that the internal cache
  7045. * is turned off. wolfSSL_CTX_add_session should put the session in
  7046. * the cache anyway. */
  7047. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7048. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7049. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7050. #ifdef NO_SESSION_CACHE_REF
  7051. for (j = 0; j < 4; j++) {
  7052. #else
  7053. /* The session may be overwritten in this case. Do only one resumption
  7054. * to stop this test from failing intermittently. */
  7055. for (j = 0; j < 2; j++) {
  7056. #endif
  7057. #ifdef WOLFSSL_TIRTOS
  7058. fdOpenSession(Task_self());
  7059. #endif
  7060. StartTCP();
  7061. InitTcpReady(&ready);
  7062. XMEMSET(&client_args, 0, sizeof(func_args));
  7063. XMEMSET(&server_args, 0, sizeof(func_args));
  7064. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7065. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7066. client_cb.method = methods[i][0];
  7067. server_cb.method = methods[i][1];
  7068. server_args.signal = &ready;
  7069. server_args.callbacks = &server_cb;
  7070. client_args.signal = &ready;
  7071. client_args.callbacks = &client_cb;
  7072. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7073. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7074. server_cb.isSharedCtx = 1;
  7075. }
  7076. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7077. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7078. if (j != 0)
  7079. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7080. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7081. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7082. server_cb.ticNoInit = 1; /* Use default builtin */
  7083. start_thread(test_server_nofail, &server_args, &serverThread);
  7084. wait_tcp_ready(&server_args);
  7085. test_client_nofail(&client_args, NULL);
  7086. join_thread(serverThread);
  7087. ExpectTrue(client_args.return_code);
  7088. ExpectTrue(server_args.return_code);
  7089. FreeTcpReady(&ready);
  7090. if (EXPECT_FAIL())
  7091. break;
  7092. }
  7093. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7094. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7095. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7096. if (EXPECT_FAIL())
  7097. break;
  7098. }
  7099. #endif
  7100. return EXPECT_RESULT();
  7101. }
  7102. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7103. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7104. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7105. defined(HAVE_SESSION_TICKET) && \
  7106. !defined(TITAN_SESSION_CACHE) && \
  7107. !defined(HUGE_SESSION_CACHE) && \
  7108. !defined(BIG_SESSION_CACHE) && \
  7109. !defined(MEDIUM_SESSION_CACHE)
  7110. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7111. /* Sessions to restore/store */
  7112. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7113. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7114. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7115. static int twcase_new_session_called = 0;
  7116. static int twcase_remove_session_called = 0;
  7117. static int twcase_get_session_called = 0;
  7118. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7119. #define SESSION_CACHE_SIZE 33
  7120. typedef struct {
  7121. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7122. WOLFSSL_SESSION* value;
  7123. } hashTable_entry;
  7124. typedef struct {
  7125. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7126. size_t capacity; /* size of entries */
  7127. size_t length; /* number of items in the hash table */
  7128. wolfSSL_Mutex htLock; /* lock */
  7129. }hashTable;
  7130. static hashTable server_sessionCache;
  7131. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7132. {
  7133. int i;
  7134. unsigned int len;
  7135. (void)ssl;
  7136. /*
  7137. * This example uses a hash table.
  7138. * Steps you should take for a non-demo code:
  7139. * - acquire a lock for the file named according to the session id
  7140. * - open the file
  7141. * - encrypt and write the SSL_SESSION object to the file
  7142. * - release the lock
  7143. *
  7144. * Return:
  7145. * 0: The callback does not wish to hold a reference of the sess
  7146. * 1: The callback wants to hold a reference of the sess. The callback is
  7147. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7148. */
  7149. if (sess == NULL)
  7150. return 0;
  7151. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7152. return 0;
  7153. }
  7154. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7155. if (server_sessionCache.entries[i].value == NULL) {
  7156. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7157. server_sessionCache.entries[i].value = sess;
  7158. server_sessionCache.length++;
  7159. break;
  7160. }
  7161. }
  7162. ++twcase_new_session_called;
  7163. wc_UnLockMutex(&server_sessionCache.htLock);
  7164. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  7165. twcase_new_session_called);
  7166. return 1;
  7167. }
  7168. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  7169. {
  7170. int i;
  7171. (void)ctx;
  7172. (void)sess;
  7173. if (sess == NULL)
  7174. return;
  7175. /*
  7176. * This example uses a hash table.
  7177. * Steps you should take for a non-demo code:
  7178. * - acquire a lock for the file named according to the session id
  7179. * - remove the file
  7180. * - release the lock
  7181. */
  7182. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7183. return;
  7184. }
  7185. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7186. if (server_sessionCache.entries[i].key != NULL &&
  7187. XMEMCMP(server_sessionCache.entries[i].key,
  7188. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7189. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7190. server_sessionCache.entries[i].value = NULL;
  7191. server_sessionCache.entries[i].key = NULL;
  7192. server_sessionCache.length--;
  7193. break;
  7194. }
  7195. }
  7196. ++twcase_remove_session_called;
  7197. wc_UnLockMutex(&server_sessionCache.htLock);
  7198. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  7199. twcase_remove_session_called);
  7200. }
  7201. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  7202. const unsigned char *id, int len, int *ref)
  7203. {
  7204. int i;
  7205. (void)ssl;
  7206. (void)id;
  7207. (void)len;
  7208. /*
  7209. * This example uses a hash table.
  7210. * Steps you should take for a non-demo code:
  7211. * - acquire a lock for the file named according to the session id in the
  7212. * 2nd arg
  7213. * - read and decrypt contents of file and create a new SSL_SESSION
  7214. * - object release the lock
  7215. * - return the new session object
  7216. */
  7217. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  7218. ++twcase_get_session_called);
  7219. /* This callback want to retain a copy of the object. If we want wolfSSL to
  7220. * be responsible for the pointer then set to 0. */
  7221. *ref = 1;
  7222. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7223. if (server_sessionCache.entries[i].key != NULL &&
  7224. XMEMCMP(server_sessionCache.entries[i].key, id,
  7225. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  7226. return server_sessionCache.entries[i].value;
  7227. }
  7228. }
  7229. return NULL;
  7230. }
  7231. static int twcase_get_sessionCb_cleanup(void)
  7232. {
  7233. int i;
  7234. int cnt = 0;
  7235. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  7236. * for freeing sessions */
  7237. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7238. if (server_sessionCache.entries[i].value != NULL) {
  7239. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  7240. cnt++;
  7241. }
  7242. }
  7243. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  7244. cnt);
  7245. return TEST_SUCCESS;
  7246. }
  7247. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  7248. {
  7249. EXPECT_DECLS;
  7250. /* off - Disable internal cache */
  7251. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7252. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7253. #ifdef OPENSSL_EXTRA
  7254. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7255. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7256. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7257. #endif
  7258. /* off - Do not setup external cache */
  7259. /* Require both peers to provide certs */
  7260. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7261. return EXPECT_RESULT();
  7262. }
  7263. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  7264. {
  7265. /* on - internal cache is on by default */
  7266. /* off - Do not setup external cache */
  7267. /* Require both peers to provide certs */
  7268. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7269. return TEST_SUCCESS;
  7270. }
  7271. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  7272. {
  7273. EXPECT_DECLS;
  7274. /* off - Disable internal cache */
  7275. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7276. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7277. #ifdef OPENSSL_EXTRA
  7278. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7279. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7280. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7281. #endif
  7282. /* on - Enable external cache */
  7283. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7284. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7285. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7286. /* Require both peers to provide certs */
  7287. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7288. return EXPECT_RESULT();
  7289. }
  7290. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  7291. {
  7292. /* on - internal cache is on by default */
  7293. /* on - Enable external cache */
  7294. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7295. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7296. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7297. /* Require both peers to provide certs */
  7298. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7299. return TEST_SUCCESS;
  7300. }
  7301. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  7302. {
  7303. /* on - internal cache is on by default */
  7304. /* on - Enable external cache */
  7305. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  7306. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  7307. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  7308. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  7309. /* Require both peers to provide certs */
  7310. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7311. return TEST_SUCCESS;
  7312. }
  7313. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7314. {
  7315. EXPECT_DECLS;
  7316. WOLFSSL_SESSION** sess;
  7317. if (wolfSSL_is_server(ssl))
  7318. sess = &twcase_server_first_session_ptr;
  7319. else
  7320. return TEST_SUCCESS;
  7321. if (*sess == NULL) {
  7322. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7323. /* Now save the session in the internal store to make it available
  7324. * for lookup. For TLS 1.3, we can't save the session without
  7325. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7326. * session from cache. */
  7327. if (wolfSSL_is_server(ssl)
  7328. #ifndef WOLFSSL_TICKET_HAVE_ID
  7329. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7330. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  7331. #endif
  7332. ) {
  7333. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7334. *sess), WOLFSSL_SUCCESS);
  7335. }
  7336. }
  7337. /* Save CTX to be able to decrypt tickets */
  7338. if (twcase_server_current_ctx_ptr == NULL) {
  7339. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  7340. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7341. WOLFSSL_SUCCESS);
  7342. }
  7343. #ifdef SESSION_CERTS
  7344. #ifndef WOLFSSL_TICKET_HAVE_ID
  7345. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7346. wolfSSL_session_reused(ssl))
  7347. #endif
  7348. {
  7349. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7350. * for all connections. TLS 1.3 only has tickets so if we don't
  7351. * include the session id in the ticket then the certificates
  7352. * will not be available on resumption. */
  7353. WOLFSSL_X509* peer = NULL;
  7354. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  7355. wolfSSL_X509_free(peer);
  7356. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7357. }
  7358. #endif
  7359. return EXPECT_RESULT();
  7360. }
  7361. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  7362. {
  7363. EXPECT_DECLS;
  7364. WOLFSSL_SESSION** sess;
  7365. sess = &twcase_client_first_session_ptr;
  7366. if (*sess == NULL) {
  7367. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  7368. }
  7369. else {
  7370. /* If we have a session retrieved then remaining connections should be
  7371. * resuming on that session */
  7372. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  7373. }
  7374. #ifdef SESSION_CERTS
  7375. #ifndef WOLFSSL_TICKET_HAVE_ID
  7376. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7377. wolfSSL_session_reused(ssl))
  7378. #endif
  7379. {
  7380. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7381. ExpectNotNull(peer);
  7382. wolfSSL_X509_free(peer);
  7383. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7384. #ifdef OPENSSL_EXTRA
  7385. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  7386. #endif
  7387. }
  7388. #endif
  7389. return EXPECT_RESULT();
  7390. }
  7391. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  7392. {
  7393. EXPECT_DECLS;
  7394. /* Set the session to reuse for the client */
  7395. ExpectNotNull(ssl);
  7396. ExpectNotNull(twcase_client_first_session_ptr);
  7397. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  7398. WOLFSSL_SUCCESS);
  7399. return EXPECT_RESULT();
  7400. }
  7401. struct test_add_session_ext_params {
  7402. method_provider client_meth;
  7403. method_provider server_meth;
  7404. const char* tls_version;
  7405. };
  7406. static int test_wolfSSL_CTX_add_session_ext(
  7407. struct test_add_session_ext_params* param)
  7408. {
  7409. EXPECT_DECLS;
  7410. /* Test the default 33 sessions */
  7411. int j;
  7412. /* Clear cache before starting */
  7413. wolfSSL_CTX_flush_sessions(NULL, -1);
  7414. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  7415. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  7416. return BAD_MUTEX_E;
  7417. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  7418. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  7419. for (j = 0; j < 5; j++) {
  7420. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  7421. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  7422. test_ssl_cbf client_cb;
  7423. test_ssl_cbf server_cb;
  7424. (void)dtls;
  7425. /* Test five cache configurations */
  7426. twcase_client_first_session_ptr = NULL;
  7427. twcase_server_first_session_ptr = NULL;
  7428. twcase_server_current_ctx_ptr = NULL;
  7429. twcase_new_session_called = 0;
  7430. twcase_remove_session_called = 0;
  7431. twcase_get_session_called = 0;
  7432. /* connection 1 - first connection */
  7433. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  7434. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7435. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7436. client_cb.method = param->client_meth;
  7437. server_cb.method = param->server_meth;
  7438. if (dtls)
  7439. client_cb.doUdp = server_cb.doUdp = 1;
  7440. /* Setup internal and external cache */
  7441. switch (j) {
  7442. case 0:
  7443. /* SSL_OP_NO_TICKET stateful ticket case */
  7444. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  7445. break;
  7446. case 1:
  7447. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  7448. break;
  7449. case 2:
  7450. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  7451. break;
  7452. case 3:
  7453. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  7454. break;
  7455. case 4:
  7456. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  7457. break;
  7458. }
  7459. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  7460. /* Add session to internal cache and save SSL session for testing */
  7461. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  7462. /* Save client SSL session for testing */
  7463. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  7464. server_cb.ticNoInit = 1; /* Use default builtin */
  7465. /* Don't free/release ctx */
  7466. server_cb.ctx = twcase_server_current_ctx_ptr;
  7467. server_cb.isSharedCtx = 1;
  7468. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7469. &server_cb, NULL), TEST_SUCCESS);
  7470. ExpectIntEQ(twcase_get_session_called, 0);
  7471. if (EXPECT_FAIL()) {
  7472. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7473. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7474. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7475. break;
  7476. }
  7477. switch (j) {
  7478. case 0:
  7479. case 1:
  7480. case 2:
  7481. /* cache cannot be searched with out a connection */
  7482. /* Add a new session */
  7483. ExpectIntEQ(twcase_new_session_called, 1);
  7484. /* In twcase_server_sess_ctx_pre_shutdown
  7485. * wolfSSL_CTX_add_session which evicts the existing session
  7486. * in cache and adds it back in */
  7487. ExpectIntLE(twcase_remove_session_called, 1);
  7488. break;
  7489. case 3:
  7490. case 4:
  7491. /* no external cache */
  7492. ExpectIntEQ(twcase_new_session_called, 0);
  7493. ExpectIntEQ(twcase_remove_session_called, 0);
  7494. break;
  7495. }
  7496. /* connection 2 - session resume */
  7497. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  7498. twcase_new_session_called = 0;
  7499. twcase_remove_session_called = 0;
  7500. twcase_get_session_called = 0;
  7501. server_cb.on_result = 0;
  7502. client_cb.on_result = 0;
  7503. server_cb.ticNoInit = 1; /* Use default builtin */
  7504. server_cb.ctx = twcase_server_current_ctx_ptr;
  7505. /* try session resumption */
  7506. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  7507. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  7508. &server_cb, NULL), TEST_SUCCESS);
  7509. /* Clear cache before checking */
  7510. wolfSSL_CTX_flush_sessions(NULL, -1);
  7511. switch (j) {
  7512. case 0:
  7513. if (tls13) {
  7514. /* (D)TLSv1.3 stateful case */
  7515. /* cache hit */
  7516. /* DTLS accesses cache once for stateless parsing and
  7517. * once for stateful parsing */
  7518. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7519. /* (D)TLSv1.3 creates a new ticket,
  7520. * updates both internal and external cache */
  7521. ExpectIntEQ(twcase_new_session_called, 1);
  7522. /* A new session ID is created for a new ticket */
  7523. ExpectIntEQ(twcase_remove_session_called, 2);
  7524. }
  7525. else {
  7526. /* non (D)TLSv1.3 case, no update */
  7527. /* DTLS accesses cache once for stateless parsing and
  7528. * once for stateful parsing */
  7529. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7530. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7531. #else
  7532. ExpectIntEQ(twcase_get_session_called, 1);
  7533. #endif
  7534. ExpectIntEQ(twcase_new_session_called, 0);
  7535. /* Called on session added in
  7536. * twcase_server_sess_ctx_pre_shutdown */
  7537. ExpectIntEQ(twcase_remove_session_called, 1);
  7538. }
  7539. break;
  7540. case 1:
  7541. if (tls13) {
  7542. /* (D)TLSv1.3 case */
  7543. /* cache hit */
  7544. ExpectIntEQ(twcase_get_session_called, 1);
  7545. /* (D)TLSv1.3 creates a new ticket,
  7546. * updates both internal and external cache */
  7547. ExpectIntEQ(twcase_new_session_called, 1);
  7548. /* Called on session added in
  7549. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7550. ExpectIntEQ(twcase_remove_session_called, 1);
  7551. }
  7552. else {
  7553. /* non (D)TLSv1.3 case */
  7554. /* cache hit */
  7555. /* DTLS accesses cache once for stateless parsing and
  7556. * once for stateful parsing */
  7557. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7558. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7559. #else
  7560. ExpectIntEQ(twcase_get_session_called, 1);
  7561. #endif
  7562. ExpectIntEQ(twcase_new_session_called, 0);
  7563. /* Called on session added in
  7564. * twcase_server_sess_ctx_pre_shutdown */
  7565. ExpectIntEQ(twcase_remove_session_called, 1);
  7566. }
  7567. break;
  7568. case 2:
  7569. if (tls13) {
  7570. /* (D)TLSv1.3 case */
  7571. /* cache hit */
  7572. ExpectIntEQ(twcase_get_session_called, 1);
  7573. /* (D)TLSv1.3 creates a new ticket,
  7574. * updates both internal and external cache */
  7575. ExpectIntEQ(twcase_new_session_called, 1);
  7576. /* Called on session added in
  7577. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  7578. ExpectIntEQ(twcase_remove_session_called, 1);
  7579. }
  7580. else {
  7581. /* non (D)TLSv1.3 case */
  7582. /* cache hit */
  7583. /* DTLS accesses cache once for stateless parsing and
  7584. * once for stateful parsing */
  7585. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  7586. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  7587. #else
  7588. ExpectIntEQ(twcase_get_session_called, 1);
  7589. #endif
  7590. ExpectIntEQ(twcase_new_session_called, 0);
  7591. /* Called on session added in
  7592. * twcase_server_sess_ctx_pre_shutdown */
  7593. ExpectIntEQ(twcase_remove_session_called, 1);
  7594. }
  7595. break;
  7596. case 3:
  7597. case 4:
  7598. /* no external cache */
  7599. ExpectIntEQ(twcase_get_session_called, 0);
  7600. ExpectIntEQ(twcase_new_session_called, 0);
  7601. ExpectIntEQ(twcase_remove_session_called, 0);
  7602. break;
  7603. }
  7604. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  7605. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  7606. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  7607. if (EXPECT_FAIL())
  7608. break;
  7609. }
  7610. twcase_get_sessionCb_cleanup();
  7611. XMEMSET(&server_sessionCache.entries, 0,
  7612. sizeof(server_sessionCache.entries));
  7613. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  7614. wc_FreeMutex(&server_sessionCache.htLock);
  7615. return EXPECT_RESULT();
  7616. }
  7617. #endif
  7618. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  7619. {
  7620. EXPECT_DECLS;
  7621. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7622. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7623. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7624. defined(HAVE_SESSION_TICKET) && \
  7625. !defined(TITAN_SESSION_CACHE) && \
  7626. !defined(HUGE_SESSION_CACHE) && \
  7627. !defined(BIG_SESSION_CACHE) && \
  7628. !defined(MEDIUM_SESSION_CACHE)
  7629. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7630. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7631. struct test_add_session_ext_params param[1] = {
  7632. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  7633. };
  7634. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7635. #endif
  7636. #endif
  7637. return EXPECT_RESULT();
  7638. }
  7639. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  7640. {
  7641. EXPECT_DECLS;
  7642. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7643. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7644. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7645. defined(HAVE_SESSION_TICKET) && \
  7646. !defined(TITAN_SESSION_CACHE) && \
  7647. !defined(HUGE_SESSION_CACHE) && \
  7648. !defined(BIG_SESSION_CACHE) && \
  7649. !defined(MEDIUM_SESSION_CACHE)
  7650. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7651. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  7652. #ifdef WOLFSSL_DTLS13
  7653. struct test_add_session_ext_params param[1] = {
  7654. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  7655. };
  7656. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7657. #endif
  7658. #endif
  7659. #endif
  7660. return EXPECT_RESULT();
  7661. }
  7662. static int test_wolfSSL_CTX_add_session_ext_tls12(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. struct test_add_session_ext_params param[1] = {
  7675. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  7676. };
  7677. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7678. #endif
  7679. #endif
  7680. return EXPECT_RESULT();
  7681. }
  7682. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  7683. {
  7684. EXPECT_DECLS;
  7685. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7686. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7687. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7688. defined(HAVE_SESSION_TICKET) && \
  7689. !defined(TITAN_SESSION_CACHE) && \
  7690. !defined(HUGE_SESSION_CACHE) && \
  7691. !defined(BIG_SESSION_CACHE) && \
  7692. !defined(MEDIUM_SESSION_CACHE)
  7693. #ifndef WOLFSSL_NO_TLS12
  7694. #ifdef WOLFSSL_DTLS
  7695. struct test_add_session_ext_params param[1] = {
  7696. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  7697. };
  7698. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7699. #endif
  7700. #endif
  7701. #endif
  7702. return EXPECT_RESULT();
  7703. }
  7704. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  7705. {
  7706. EXPECT_DECLS;
  7707. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7708. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7709. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7710. defined(HAVE_SESSION_TICKET) && \
  7711. !defined(TITAN_SESSION_CACHE) && \
  7712. !defined(HUGE_SESSION_CACHE) && \
  7713. !defined(BIG_SESSION_CACHE) && \
  7714. !defined(MEDIUM_SESSION_CACHE)
  7715. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7716. !defined(NO_DES3))
  7717. struct test_add_session_ext_params param[1] = {
  7718. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  7719. };
  7720. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7721. #endif
  7722. #endif
  7723. return EXPECT_RESULT();
  7724. }
  7725. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  7726. {
  7727. EXPECT_DECLS;
  7728. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7729. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7730. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7731. defined(HAVE_SESSION_TICKET) && \
  7732. !defined(TITAN_SESSION_CACHE) && \
  7733. !defined(HUGE_SESSION_CACHE) && \
  7734. !defined(BIG_SESSION_CACHE) && \
  7735. !defined(MEDIUM_SESSION_CACHE)
  7736. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7737. !defined(NO_DES3))
  7738. #ifdef WOLFSSL_DTLS
  7739. struct test_add_session_ext_params param[1] = {
  7740. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  7741. };
  7742. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  7743. #endif
  7744. #endif
  7745. #endif
  7746. return EXPECT_RESULT();
  7747. }
  7748. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7749. /* canned export of a session using older version 3 */
  7750. static unsigned char version_3[] = {
  7751. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  7752. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  7753. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  7754. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  7755. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  7756. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7757. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  7758. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  7759. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  7760. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7761. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7762. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  7763. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7764. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  7765. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  7766. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  7767. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  7768. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  7769. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  7770. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  7771. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7772. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7773. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7774. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7775. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7776. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7777. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7778. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7779. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7780. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7782. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  7783. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  7784. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  7785. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  7786. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  7787. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  7788. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  7789. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  7790. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  7791. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  7792. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7793. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  7794. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  7795. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7796. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  7797. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  7798. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  7799. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  7800. 0x2E, 0x31, 0xED, 0x4F
  7801. };
  7802. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  7803. static int test_wolfSSL_dtls_export(void)
  7804. {
  7805. EXPECT_DECLS;
  7806. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  7807. tcp_ready ready;
  7808. func_args client_args;
  7809. func_args server_args;
  7810. THREAD_TYPE serverThread;
  7811. callback_functions server_cbf;
  7812. callback_functions client_cbf;
  7813. #ifdef WOLFSSL_TIRTOS
  7814. fdOpenSession(Task_self());
  7815. #endif
  7816. InitTcpReady(&ready);
  7817. #if defined(USE_WINDOWS_API)
  7818. /* use RNG to get random port if using windows */
  7819. ready.port = GetRandomPort();
  7820. #endif
  7821. /* set using dtls */
  7822. XMEMSET(&client_args, 0, sizeof(func_args));
  7823. XMEMSET(&server_args, 0, sizeof(func_args));
  7824. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7825. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7826. server_cbf.method = wolfDTLSv1_2_server_method;
  7827. client_cbf.method = wolfDTLSv1_2_client_method;
  7828. server_args.callbacks = &server_cbf;
  7829. client_args.callbacks = &client_cbf;
  7830. server_args.signal = &ready;
  7831. client_args.signal = &ready;
  7832. start_thread(run_wolfssl_server, &server_args, &serverThread);
  7833. wait_tcp_ready(&server_args);
  7834. run_wolfssl_client(&client_args);
  7835. join_thread(serverThread);
  7836. ExpectTrue(client_args.return_code);
  7837. ExpectTrue(server_args.return_code);
  7838. FreeTcpReady(&ready);
  7839. #ifdef WOLFSSL_TIRTOS
  7840. fdOpenSession(Task_self());
  7841. #endif
  7842. if (EXPECT_SUCCESS()) {
  7843. SOCKET_T sockfd = 0;
  7844. WOLFSSL_CTX* ctx = NULL;
  7845. WOLFSSL* ssl = NULL;
  7846. char msg[64] = "hello wolfssl!";
  7847. char reply[1024];
  7848. int msgSz = (int)XSTRLEN(msg);
  7849. byte *session, *window;
  7850. unsigned int sessionSz = 0;
  7851. unsigned int windowSz = 0;
  7852. #ifndef TEST_IPV6
  7853. struct sockaddr_in peerAddr;
  7854. #else
  7855. struct sockaddr_in6 peerAddr;
  7856. #endif /* TEST_IPV6 */
  7857. int i;
  7858. /* Set ctx to DTLS 1.2 */
  7859. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  7860. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7861. /* test importing version 3 */
  7862. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7863. /* test importing bad length and bad version */
  7864. version_3[2] += 1;
  7865. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7866. version_3[2] -= 1; version_3[1] = 0XA0;
  7867. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  7868. wolfSSL_free(ssl);
  7869. wolfSSL_CTX_free(ctx);
  7870. /* check storing client state after connection and storing window only */
  7871. #ifdef WOLFSSL_TIRTOS
  7872. fdOpenSession(Task_self());
  7873. #endif
  7874. InitTcpReady(&ready);
  7875. #if defined(USE_WINDOWS_API)
  7876. /* use RNG to get random port if using windows */
  7877. ready.port = GetRandomPort();
  7878. #endif
  7879. /* set using dtls */
  7880. XMEMSET(&server_args, 0, sizeof(func_args));
  7881. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7882. server_cbf.method = wolfDTLSv1_2_server_method;
  7883. server_cbf.doUdp = 1;
  7884. server_args.callbacks = &server_cbf;
  7885. server_args.argc = 3; /* set loop_count to 3 */
  7886. server_args.signal = &ready;
  7887. start_thread(test_server_nofail, &server_args, &serverThread);
  7888. wait_tcp_ready(&server_args);
  7889. /* create and connect with client */
  7890. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  7891. ExpectIntEQ(WOLFSSL_SUCCESS,
  7892. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  7893. ExpectIntEQ(WOLFSSL_SUCCESS,
  7894. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  7895. ExpectIntEQ(WOLFSSL_SUCCESS,
  7896. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  7897. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  7898. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7899. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7900. /* store server information connected too */
  7901. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  7902. #ifndef TEST_IPV6
  7903. peerAddr.sin_family = AF_INET;
  7904. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  7905. peerAddr.sin_port = XHTONS(server_args.signal->port);
  7906. #else
  7907. peerAddr.sin6_family = AF_INET6;
  7908. ExpectIntEQ(
  7909. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  7910. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  7911. #endif
  7912. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7913. WOLFSSL_SUCCESS);
  7914. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  7915. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  7916. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7917. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  7918. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7919. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7920. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  7921. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7922. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7923. wolfSSL_free(ssl);
  7924. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  7925. /* restore state */
  7926. ExpectNotNull(ssl = wolfSSL_new(ctx));
  7927. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  7928. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  7929. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  7930. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  7931. WOLFSSL_SUCCESS);
  7932. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  7933. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  7934. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  7935. wolfSSL_free(ssl);
  7936. }
  7937. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7938. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7939. wolfSSL_CTX_free(ctx);
  7940. fprintf(stderr, "done and waiting for server\n");
  7941. join_thread(serverThread);
  7942. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  7943. FreeTcpReady(&ready);
  7944. #ifdef WOLFSSL_TIRTOS
  7945. fdOpenSession(Task_self());
  7946. #endif
  7947. }
  7948. #endif
  7949. return EXPECT_RESULT();
  7950. }
  7951. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  7952. #ifdef WOLFSSL_TLS13
  7953. static const byte canned_client_tls13_session[] = {
  7954. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  7955. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7956. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  7957. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7958. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  7959. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7960. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7961. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  7962. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  7963. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7964. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7965. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  7966. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  7967. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  7968. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  7969. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  7970. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  7971. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  7972. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  7973. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  7974. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  7975. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  7976. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  7977. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  7978. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  7979. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  7980. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  7981. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  7982. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  7983. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  7984. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  7985. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  7986. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  7987. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  7988. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  7989. 0x00, 0x03
  7990. };
  7991. static const byte canned_server_tls13_session[] = {
  7992. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  7993. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  7994. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  7995. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  7996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  7997. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  7998. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  7999. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8000. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8001. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8002. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8003. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8004. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8005. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8006. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8007. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8008. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8009. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8010. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8011. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8012. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8013. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8014. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8015. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8016. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8017. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8018. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8019. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8020. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8021. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8022. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8023. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8024. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8025. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8026. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8027. 0x00, 0x04
  8028. };
  8029. #endif /* WOLFSSL_TLS13 */
  8030. static const byte canned_client_session[] = {
  8031. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8032. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8033. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8034. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8035. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8036. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8038. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8039. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8040. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8041. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8042. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8043. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8044. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8045. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8046. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8047. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8048. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8049. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8050. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8051. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8052. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8053. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8054. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8055. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8056. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8057. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8058. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8059. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8060. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8062. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8063. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8064. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8065. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8066. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8067. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8068. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8069. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8070. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8071. 0x00, 0x03
  8072. };
  8073. static const byte canned_server_session[] = {
  8074. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8075. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8076. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8077. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8078. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8079. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8080. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8081. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8082. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8083. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8084. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8085. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8086. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8087. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8088. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8089. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8090. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8091. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8092. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8093. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8094. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8095. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8096. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8097. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8098. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8099. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8100. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8101. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8102. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8107. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8108. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8109. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8110. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8111. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8112. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8113. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8114. 0x00, 0x04
  8115. };
  8116. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8117. {
  8118. SOCKET_T sockfd = 0;
  8119. SOCKET_T clientfd = 0;
  8120. word16 port;
  8121. callback_functions* cbf;
  8122. WOLFSSL_CTX* ctx = 0;
  8123. WOLFSSL* ssl = 0;
  8124. char msg[] = "I hear you fa shizzle!";
  8125. char input[1024];
  8126. int idx;
  8127. #ifdef WOLFSSL_TIRTOS
  8128. fdOpenSession(Task_self());
  8129. #endif
  8130. ((func_args*)args)->return_code = TEST_FAIL;
  8131. cbf = ((func_args*)args)->callbacks;
  8132. #if defined(USE_WINDOWS_API)
  8133. port = ((func_args*)args)->signal->port;
  8134. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8135. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8136. /* Let tcp_listen assign port */
  8137. port = 0;
  8138. #else
  8139. /* Use default port */
  8140. port = wolfSSLPort;
  8141. #endif
  8142. /* do it here to detect failure */
  8143. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8144. CloseSocket(sockfd);
  8145. {
  8146. WOLFSSL_METHOD* method = NULL;
  8147. if (cbf != NULL && cbf->method != NULL) {
  8148. method = cbf->method();
  8149. }
  8150. else {
  8151. method = wolfTLSv1_2_server_method();
  8152. }
  8153. ctx = wolfSSL_CTX_new(method);
  8154. }
  8155. if (ctx == NULL) {
  8156. goto done;
  8157. }
  8158. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8159. /* call ctx setup callback */
  8160. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8161. cbf->ctx_ready(ctx);
  8162. }
  8163. ssl = wolfSSL_new(ctx);
  8164. if (ssl == NULL) {
  8165. goto done;
  8166. }
  8167. wolfSSL_set_fd(ssl, clientfd);
  8168. /* call ssl setup callback */
  8169. if (cbf != NULL && cbf->ssl_ready != NULL) {
  8170. cbf->ssl_ready(ssl);
  8171. }
  8172. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  8173. if (idx > 0) {
  8174. input[idx] = '\0';
  8175. fprintf(stderr, "Client message export/import: %s\n", input);
  8176. }
  8177. else {
  8178. fprintf(stderr, "ret = %d error = %d\n", idx,
  8179. wolfSSL_get_error(ssl, idx));
  8180. goto done;
  8181. }
  8182. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  8183. /*err_sys("SSL_write failed");*/
  8184. #ifdef WOLFSSL_TIRTOS
  8185. return;
  8186. #else
  8187. return 0;
  8188. #endif
  8189. }
  8190. #ifdef WOLFSSL_TIRTOS
  8191. Task_yield();
  8192. #endif
  8193. ((func_args*)args)->return_code = TEST_SUCCESS;
  8194. done:
  8195. wolfSSL_shutdown(ssl);
  8196. wolfSSL_free(ssl);
  8197. wolfSSL_CTX_free(ctx);
  8198. CloseSocket(clientfd);
  8199. #ifdef WOLFSSL_TIRTOS
  8200. fdCloseSession(Task_self());
  8201. #endif
  8202. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8203. && defined(HAVE_THREAD_LS)
  8204. wc_ecc_fp_free(); /* free per thread cache */
  8205. #endif
  8206. #if defined(HAVE_SESSION_TICKET) && \
  8207. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  8208. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  8209. OpenSSLTicketCleanup();
  8210. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  8211. TicketCleanup();
  8212. #endif
  8213. #endif
  8214. #ifndef WOLFSSL_TIRTOS
  8215. return 0;
  8216. #endif
  8217. }
  8218. static void load_tls12_canned_server(WOLFSSL* ssl)
  8219. {
  8220. int clientfd = wolfSSL_get_fd(ssl);
  8221. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  8222. sizeof(canned_server_session)), sizeof(canned_server_session));
  8223. wolfSSL_set_fd(ssl, clientfd);
  8224. }
  8225. #ifdef WOLFSSL_TLS13
  8226. static void load_tls13_canned_server(WOLFSSL* ssl)
  8227. {
  8228. int clientfd = wolfSSL_get_fd(ssl);
  8229. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  8230. sizeof(canned_server_tls13_session)),
  8231. sizeof(canned_server_tls13_session));
  8232. wolfSSL_set_fd(ssl, clientfd);
  8233. }
  8234. #endif
  8235. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  8236. static int test_wolfSSL_tls_export_run(int v)
  8237. {
  8238. EXPECT_DECLS;
  8239. SOCKET_T sockfd = 0;
  8240. WOLFSSL_CTX* ctx = 0;
  8241. WOLFSSL* ssl = 0;
  8242. char msg[64] = "hello wolfssl!";
  8243. char reply[1024];
  8244. word32 replySz;
  8245. int msgSz = (int)XSTRLEN(msg);
  8246. const byte* clientSession = NULL;
  8247. int clientSessionSz = 0;
  8248. tcp_ready ready;
  8249. func_args server_args;
  8250. THREAD_TYPE serverThread;
  8251. callback_functions server_cbf;
  8252. #ifdef WOLFSSL_TIRTOS
  8253. fdOpenSession(Task_self());
  8254. #endif
  8255. InitTcpReady(&ready);
  8256. #if defined(USE_WINDOWS_API)
  8257. /* use RNG to get random port if using windows */
  8258. ready.port = GetRandomPort();
  8259. #endif
  8260. XMEMSET(&server_args, 0, sizeof(func_args));
  8261. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8262. switch (v) {
  8263. case WOLFSSL_TLSV1_2:
  8264. server_cbf.method = wolfTLSv1_2_server_method;
  8265. server_cbf.ssl_ready = load_tls12_canned_server;
  8266. /* setup the client side */
  8267. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8268. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8269. clientSession = canned_client_session;
  8270. clientSessionSz = sizeof(canned_client_session);
  8271. break;
  8272. #ifdef WOLFSSL_TLS13
  8273. case WOLFSSL_TLSV1_3:
  8274. server_cbf.method = wolfTLSv1_3_server_method;
  8275. server_cbf.ssl_ready = load_tls13_canned_server;
  8276. /* setup the client side */
  8277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8278. clientSession = canned_client_tls13_session;
  8279. clientSessionSz = sizeof(canned_client_tls13_session);
  8280. break;
  8281. #endif
  8282. }
  8283. server_args.callbacks = &server_cbf;
  8284. server_args.signal = &ready;
  8285. start_thread(tls_export_server, &server_args, &serverThread);
  8286. wait_tcp_ready(&server_args);
  8287. #ifdef WOLFSSL_TIRTOS
  8288. fdOpenSession(Task_self());
  8289. #endif
  8290. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8291. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  8292. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  8293. clientSessionSz);
  8294. replySz = sizeof(reply);
  8295. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  8296. #if !defined(NO_PSK) && defined(HAVE_ANON)
  8297. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  8298. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  8299. #endif
  8300. wolfSSL_set_fd(ssl, sockfd);
  8301. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8302. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  8303. wolfSSL_free(ssl);
  8304. wolfSSL_CTX_free(ctx);
  8305. CloseSocket(sockfd);
  8306. #ifdef WOLFSSL_TIRTOS
  8307. fdCloseSession(Task_self());
  8308. #endif
  8309. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  8310. && defined(HAVE_THREAD_LS)
  8311. wc_ecc_fp_free(); /* free per thread cache */
  8312. #endif
  8313. join_thread(serverThread);
  8314. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8315. FreeTcpReady(&ready);
  8316. #ifdef WOLFSSL_TIRTOS
  8317. fdOpenSession(Task_self());
  8318. #endif
  8319. return EXPECT_RESULT();
  8320. }
  8321. #endif
  8322. static int test_wolfSSL_tls_export(void)
  8323. {
  8324. int res = TEST_SKIPPED;
  8325. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8326. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  8327. #ifdef WOLFSSL_TLS13
  8328. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  8329. #endif
  8330. res = TEST_RES_CHECK(1);
  8331. #endif
  8332. return res;
  8333. }
  8334. /*----------------------------------------------------------------------------*
  8335. | TLS extensions tests
  8336. *----------------------------------------------------------------------------*/
  8337. #ifdef ENABLE_TLS_CALLBACK_TEST
  8338. /* Connection test runner - generic */
  8339. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  8340. callback_functions* server_callbacks)
  8341. {
  8342. tcp_ready ready;
  8343. func_args client_args;
  8344. func_args server_args;
  8345. THREAD_TYPE serverThread;
  8346. XMEMSET(&client_args, 0, sizeof(func_args));
  8347. XMEMSET(&server_args, 0, sizeof(func_args));
  8348. StartTCP();
  8349. client_args.callbacks = client_callbacks;
  8350. server_args.callbacks = server_callbacks;
  8351. #ifdef WOLFSSL_TIRTOS
  8352. fdOpenSession(Task_self());
  8353. #endif
  8354. /* RUN Server side */
  8355. InitTcpReady(&ready);
  8356. #if defined(USE_WINDOWS_API)
  8357. /* use RNG to get random port if using windows */
  8358. ready.port = GetRandomPort();
  8359. #endif
  8360. server_args.signal = &ready;
  8361. client_args.signal = &ready;
  8362. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8363. wait_tcp_ready(&server_args);
  8364. /* RUN Client side */
  8365. run_wolfssl_client(&client_args);
  8366. join_thread(serverThread);
  8367. FreeTcpReady(&ready);
  8368. #ifdef WOLFSSL_TIRTOS
  8369. fdCloseSession(Task_self());
  8370. #endif
  8371. client_callbacks->return_code = client_args.return_code;
  8372. server_callbacks->return_code = server_args.return_code;
  8373. }
  8374. #endif /* ENABLE_TLS_CALLBACK_TEST */
  8375. #ifdef HAVE_SNI
  8376. static int test_wolfSSL_UseSNI_params(void)
  8377. {
  8378. EXPECT_DECLS;
  8379. #if !defined(NO_WOLFSSL_CLIENT)
  8380. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8381. WOLFSSL *ssl = wolfSSL_new(ctx);
  8382. ExpectNotNull(ctx);
  8383. ExpectNotNull(ssl);
  8384. /* invalid [ctx|ssl] */
  8385. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  8386. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  8387. /* invalid type */
  8388. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  8389. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  8390. /* invalid data */
  8391. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  8392. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  8393. /* success case */
  8394. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  8395. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  8396. wolfSSL_free(ssl);
  8397. wolfSSL_CTX_free(ctx);
  8398. #endif /* !NO_WOLFSSL_CLIENT */
  8399. return EXPECT_RESULT();
  8400. }
  8401. /* BEGIN of connection tests callbacks */
  8402. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8403. {
  8404. AssertIntEQ(WOLFSSL_SUCCESS,
  8405. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8406. }
  8407. static void use_SNI_at_ssl(WOLFSSL* ssl)
  8408. {
  8409. AssertIntEQ(WOLFSSL_SUCCESS,
  8410. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  8411. }
  8412. static void different_SNI_at_ssl(WOLFSSL* ssl)
  8413. {
  8414. AssertIntEQ(WOLFSSL_SUCCESS,
  8415. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  8416. }
  8417. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  8418. {
  8419. use_SNI_at_ssl(ssl);
  8420. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8421. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  8422. }
  8423. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  8424. {
  8425. use_SNI_at_ssl(ssl);
  8426. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8427. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  8428. }
  8429. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8430. {
  8431. use_SNI_at_ctx(ctx);
  8432. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8433. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8434. }
  8435. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  8436. {
  8437. use_SNI_at_ssl(ssl);
  8438. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  8439. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8440. }
  8441. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  8442. {
  8443. use_SNI_at_ctx(ctx);
  8444. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  8445. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  8446. }
  8447. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  8448. {
  8449. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  8450. }
  8451. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  8452. {
  8453. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  8454. }
  8455. static void verify_SNI_no_matching(WOLFSSL* ssl)
  8456. {
  8457. byte type = WOLFSSL_SNI_HOST_NAME;
  8458. void* request = (void*) &type; /* to be overwritten */
  8459. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  8460. AssertNotNull(request);
  8461. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8462. AssertNull(request);
  8463. }
  8464. static void verify_SNI_real_matching(WOLFSSL* ssl)
  8465. {
  8466. byte type = WOLFSSL_SNI_HOST_NAME;
  8467. void* request = NULL;
  8468. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  8469. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8470. AssertNotNull(request);
  8471. AssertStrEQ("www.wolfssl.com", (char*)request);
  8472. }
  8473. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  8474. {
  8475. byte type = WOLFSSL_SNI_HOST_NAME;
  8476. void* request = NULL;
  8477. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  8478. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  8479. AssertNotNull(request);
  8480. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  8481. }
  8482. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8483. {
  8484. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  8485. }
  8486. /* END of connection tests callbacks */
  8487. static int test_wolfSSL_UseSNI_connection(void)
  8488. {
  8489. int res = TEST_SKIPPED;
  8490. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8491. callback_functions client_cb;
  8492. callback_functions server_cb;
  8493. size_t i;
  8494. #ifdef WOLFSSL_STATIC_MEMORY
  8495. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  8496. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  8497. #endif
  8498. struct {
  8499. method_provider client_meth;
  8500. method_provider server_meth;
  8501. #ifdef WOLFSSL_STATIC_MEMORY
  8502. wolfSSL_method_func client_meth_ex;
  8503. wolfSSL_method_func server_meth_ex;
  8504. #endif
  8505. } methods[] = {
  8506. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  8507. {wolfSSLv23_client_method, wolfSSLv23_server_method
  8508. #ifdef WOLFSSL_STATIC_MEMORY
  8509. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  8510. #endif
  8511. },
  8512. #endif
  8513. #ifndef WOLFSSL_NO_TLS12
  8514. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  8515. #ifdef WOLFSSL_STATIC_MEMORY
  8516. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  8517. #endif
  8518. },
  8519. #endif
  8520. #ifdef WOLFSSL_TLS13
  8521. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  8522. #ifdef WOLFSSL_STATIC_MEMORY
  8523. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  8524. #endif
  8525. },
  8526. #endif
  8527. };
  8528. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  8529. for (i = 0; i < methodsSz; i++) {
  8530. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  8531. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8532. client_cb.method = methods[i].client_meth;
  8533. server_cb.method = methods[i].server_meth;
  8534. client_cb.devId = testDevId;
  8535. server_cb.devId = testDevId;
  8536. #ifdef WOLFSSL_STATIC_MEMORY
  8537. client_cb.method_ex = methods[i].client_meth_ex;
  8538. server_cb.method_ex = methods[i].server_meth_ex;
  8539. client_cb.mem = cliMem;
  8540. client_cb.memSz = (word32)sizeof(cliMem);
  8541. server_cb.mem = svrMem;
  8542. server_cb.memSz = (word32)sizeof(svrMem);;
  8543. #endif
  8544. /* success case at ctx */
  8545. fprintf(stderr, "\n\tsuccess case at ctx\n");
  8546. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8547. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8548. test_wolfSSL_client_server(&client_cb, &server_cb);
  8549. /* success case at ssl */
  8550. fprintf(stderr, "\tsuccess case at ssl\n");
  8551. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  8552. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  8553. test_wolfSSL_client_server(&client_cb, &server_cb);
  8554. /* default mismatch behavior */
  8555. fprintf(stderr, "\tdefault mismatch behavior\n");
  8556. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8557. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  8558. test_wolfSSL_client_server(&client_cb, &server_cb);
  8559. /* continue on mismatch */
  8560. fprintf(stderr, "\tcontinue on mismatch\n");
  8561. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8562. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  8563. test_wolfSSL_client_server(&client_cb, &server_cb);
  8564. /* fake answer on mismatch */
  8565. fprintf(stderr, "\tfake answer on mismatch\n");
  8566. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8567. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  8568. test_wolfSSL_client_server(&client_cb, &server_cb);
  8569. /* sni abort - success */
  8570. fprintf(stderr, "\tsni abort - success\n");
  8571. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8572. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  8573. test_wolfSSL_client_server(&client_cb, &server_cb);
  8574. /* sni abort - abort when absent (ctx) */
  8575. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  8576. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8577. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8578. test_wolfSSL_client_server(&client_cb, &server_cb);
  8579. /* sni abort - abort when absent (ssl) */
  8580. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  8581. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  8582. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  8583. test_wolfSSL_client_server(&client_cb, &server_cb);
  8584. /* sni abort - success when overwritten */
  8585. fprintf(stderr, "\tsni abort - success when overwritten\n");
  8586. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  8587. 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;
  8588. test_wolfSSL_client_server(&client_cb, &server_cb);
  8589. /* sni abort - success when allowing mismatches */
  8590. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  8591. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  8592. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  8593. test_wolfSSL_client_server(&client_cb, &server_cb);
  8594. }
  8595. res = TEST_RES_CHECK(1);
  8596. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  8597. return res;
  8598. }
  8599. static int test_wolfSSL_SNI_GetFromBuffer(void)
  8600. {
  8601. EXPECT_DECLS;
  8602. byte buff[] = { /* www.paypal.com */
  8603. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  8604. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  8605. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  8606. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8607. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8608. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  8609. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  8610. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  8611. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8612. };
  8613. byte buff2[] = { /* api.textmate.org */
  8614. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  8615. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  8616. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  8617. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  8618. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  8619. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  8620. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  8621. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  8622. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  8623. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  8624. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  8625. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  8626. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  8627. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  8628. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  8629. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  8630. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  8631. };
  8632. byte buff3[] = { /* no sni extension */
  8633. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  8634. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  8635. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  8636. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  8637. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  8638. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  8639. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  8640. };
  8641. byte buff4[] = { /* last extension has zero size */
  8642. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  8643. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8644. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8645. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8646. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  8647. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  8648. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  8649. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  8650. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  8651. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  8652. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  8653. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  8654. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  8655. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  8656. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  8657. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  8658. 0x12, 0x00, 0x00
  8659. };
  8660. byte buff5[] = { /* SSL v2.0 client hello */
  8661. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  8662. /* dummy bytes below, just to pass size check */
  8663. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  8664. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  8665. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  8666. };
  8667. byte result[32] = {0};
  8668. word32 length = 32;
  8669. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  8670. 0, result, &length));
  8671. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  8672. 0, result, &length));
  8673. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8674. 1, result, &length));
  8675. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8676. 0, result, &length));
  8677. buff[0] = 0x16;
  8678. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8679. 0, result, &length));
  8680. buff[1] = 0x03;
  8681. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  8682. sizeof(buff), 0, result, &length));
  8683. buff[2] = 0x03;
  8684. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  8685. sizeof(buff), 0, result, &length));
  8686. buff[4] = 0x64;
  8687. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  8688. 0, result, &length));
  8689. if (EXPECT_SUCCESS())
  8690. result[length] = 0;
  8691. ExpectStrEQ("www.paypal.com", (const char*) result);
  8692. length = 32;
  8693. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  8694. 0, result, &length));
  8695. if (EXPECT_SUCCESS())
  8696. result[length] = 0;
  8697. ExpectStrEQ("api.textmate.org", (const char*) result);
  8698. /* SSL v2.0 tests */
  8699. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  8700. sizeof(buff5), 0, result, &length));
  8701. buff5[2] = 0x02;
  8702. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8703. sizeof(buff5), 0, result, &length));
  8704. buff5[2] = 0x01; buff5[6] = 0x08;
  8705. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8706. sizeof(buff5), 0, result, &length));
  8707. buff5[6] = 0x09; buff5[8] = 0x01;
  8708. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  8709. sizeof(buff5), 0, result, &length));
  8710. return EXPECT_RESULT();
  8711. }
  8712. #endif /* HAVE_SNI */
  8713. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  8714. static int test_wolfSSL_UseTrustedCA(void)
  8715. {
  8716. EXPECT_DECLS;
  8717. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  8718. && !defined(NO_RSA)
  8719. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8720. WOLFSSL_CTX *ctx = NULL;
  8721. WOLFSSL *ssl = NULL;
  8722. byte id[20];
  8723. #ifndef NO_WOLFSSL_SERVER
  8724. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  8725. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8726. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8727. #else
  8728. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  8729. #endif
  8730. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  8731. XMEMSET(id, 0, sizeof(id));
  8732. /* error cases */
  8733. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  8734. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8735. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  8736. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8737. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  8738. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8739. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  8740. #ifdef NO_SHA
  8741. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8742. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8743. #endif
  8744. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8745. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  8746. /* success cases */
  8747. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8748. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  8749. #ifndef NO_SHA
  8750. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8751. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  8752. #endif
  8753. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  8754. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  8755. wolfSSL_free(ssl);
  8756. wolfSSL_CTX_free(ctx);
  8757. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8758. #endif /* HAVE_TRUSTED_CA */
  8759. return EXPECT_RESULT();
  8760. }
  8761. static int test_wolfSSL_UseMaxFragment(void)
  8762. {
  8763. EXPECT_DECLS;
  8764. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  8765. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8766. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8767. #ifndef NO_WOLFSSL_SERVER
  8768. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8769. #else
  8770. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8771. #endif
  8772. WOLFSSL *ssl = NULL;
  8773. #ifdef OPENSSL_EXTRA
  8774. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  8775. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  8776. #else
  8777. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  8778. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  8779. #endif
  8780. #ifndef NO_WOLFSSL_SERVER
  8781. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8782. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8783. #endif
  8784. ExpectNotNull(ctx);
  8785. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8786. #ifdef OPENSSL_EXTRA
  8787. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  8788. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  8789. #else
  8790. UseMaxFragment = wolfSSL_UseMaxFragment;
  8791. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  8792. #endif
  8793. /* error cases */
  8794. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  8795. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  8796. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  8797. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  8798. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  8799. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  8800. /* success case */
  8801. #ifdef OPENSSL_EXTRA
  8802. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8803. #else
  8804. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  8805. #endif
  8806. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  8807. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  8808. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  8809. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  8810. #ifdef OPENSSL_EXTRA
  8811. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8812. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8813. #else
  8814. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  8815. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  8816. #endif
  8817. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  8818. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  8819. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  8820. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  8821. #ifdef OPENSSL_EXTRA
  8822. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8823. #else
  8824. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  8825. #endif
  8826. wolfSSL_free(ssl);
  8827. wolfSSL_CTX_free(ctx);
  8828. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8829. #endif
  8830. return EXPECT_RESULT();
  8831. }
  8832. static int test_wolfSSL_UseTruncatedHMAC(void)
  8833. {
  8834. EXPECT_DECLS;
  8835. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  8836. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  8837. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8838. #ifndef NO_WOLFSSL_SERVER
  8839. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  8840. #else
  8841. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8842. #endif
  8843. WOLFSSL *ssl = NULL;
  8844. ExpectNotNull(ctx);
  8845. #ifndef NO_WOLFSSL_SERVER
  8846. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  8847. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  8848. #endif
  8849. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8850. /* error cases */
  8851. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  8852. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  8853. /* success case */
  8854. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  8855. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  8856. wolfSSL_free(ssl);
  8857. wolfSSL_CTX_free(ctx);
  8858. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8859. #endif
  8860. return EXPECT_RESULT();
  8861. }
  8862. static int test_wolfSSL_UseSupportedCurve(void)
  8863. {
  8864. EXPECT_DECLS;
  8865. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  8866. !defined(NO_TLS)
  8867. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8868. WOLFSSL *ssl = wolfSSL_new(ctx);
  8869. ExpectNotNull(ctx);
  8870. ExpectNotNull(ssl);
  8871. /* error cases */
  8872. ExpectIntNE(WOLFSSL_SUCCESS,
  8873. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8874. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  8875. ExpectIntNE(WOLFSSL_SUCCESS,
  8876. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  8877. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  8878. /* success case */
  8879. ExpectIntEQ(WOLFSSL_SUCCESS,
  8880. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  8881. ExpectIntEQ(WOLFSSL_SUCCESS,
  8882. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  8883. wolfSSL_free(ssl);
  8884. wolfSSL_CTX_free(ctx);
  8885. #endif
  8886. return EXPECT_RESULT();
  8887. }
  8888. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  8889. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  8890. {
  8891. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  8892. }
  8893. static void use_ALPN_all(WOLFSSL* ssl)
  8894. {
  8895. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8896. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8897. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8898. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8899. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8900. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8901. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8902. }
  8903. static void use_ALPN_all_continue(WOLFSSL* ssl)
  8904. {
  8905. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8906. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8907. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8908. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8909. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8910. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  8911. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8912. }
  8913. static void use_ALPN_one(WOLFSSL* ssl)
  8914. {
  8915. /* spdy/2 */
  8916. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8917. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8918. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8919. }
  8920. static void use_ALPN_unknown(WOLFSSL* ssl)
  8921. {
  8922. /* http/2.0 */
  8923. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8924. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8925. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  8926. }
  8927. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  8928. {
  8929. /* http/2.0 */
  8930. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  8931. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  8932. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  8933. }
  8934. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  8935. {
  8936. /* spdy/3 */
  8937. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8938. char *proto = NULL;
  8939. word16 protoSz = 0;
  8940. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8941. /* check value */
  8942. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  8943. if (proto) {
  8944. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  8945. }
  8946. }
  8947. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  8948. {
  8949. char *proto = NULL;
  8950. word16 protoSz = 0;
  8951. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  8952. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8953. /* check value */
  8954. AssertIntEQ(1, (0 == protoSz));
  8955. AssertIntEQ(1, (NULL == proto));
  8956. }
  8957. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  8958. {
  8959. /* http/1.1 */
  8960. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  8961. char *proto;
  8962. word16 protoSz = 0;
  8963. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8964. /* check value */
  8965. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8966. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8967. }
  8968. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  8969. {
  8970. /* spdy/2 */
  8971. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  8972. char *proto;
  8973. word16 protoSz = 0;
  8974. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  8975. /* check value */
  8976. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  8977. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  8978. }
  8979. static void verify_ALPN_client_list(WOLFSSL* ssl)
  8980. {
  8981. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  8982. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  8983. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  8984. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  8985. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  8986. char *clist = NULL;
  8987. word16 clistSz = 0;
  8988. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  8989. &clistSz));
  8990. /* check value */
  8991. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  8992. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  8993. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  8994. }
  8995. static int test_wolfSSL_UseALPN_connection(void)
  8996. {
  8997. int res = TEST_SKIPPED;
  8998. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  8999. callback_functions client_cb;
  9000. callback_functions server_cb;
  9001. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9002. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9003. client_cb.method = wolfSSLv23_client_method;
  9004. server_cb.method = wolfSSLv23_server_method;
  9005. client_cb.devId = testDevId;
  9006. server_cb.devId = testDevId;
  9007. /* success case same list */
  9008. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9009. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9010. test_wolfSSL_client_server(&client_cb, &server_cb);
  9011. /* success case only one for server */
  9012. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9013. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9014. test_wolfSSL_client_server(&client_cb, &server_cb);
  9015. /* success case only one for client */
  9016. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9017. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9018. test_wolfSSL_client_server(&client_cb, &server_cb);
  9019. /* success case none for client */
  9020. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9021. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9022. test_wolfSSL_client_server(&client_cb, &server_cb);
  9023. /* success case mismatch behavior but option 'continue' set */
  9024. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9025. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9026. test_wolfSSL_client_server(&client_cb, &server_cb);
  9027. /* success case read protocol send by client */
  9028. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9029. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9030. test_wolfSSL_client_server(&client_cb, &server_cb);
  9031. /* mismatch behavior with same list
  9032. * the first and only this one must be taken */
  9033. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9034. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  9035. test_wolfSSL_client_server(&client_cb, &server_cb);
  9036. /* default mismatch behavior */
  9037. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9038. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  9039. test_wolfSSL_client_server(&client_cb, &server_cb);
  9040. res = TEST_RES_CHECK(1);
  9041. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9042. return res;
  9043. }
  9044. static int test_wolfSSL_UseALPN_params(void)
  9045. {
  9046. EXPECT_DECLS;
  9047. #ifndef NO_WOLFSSL_CLIENT
  9048. /* "http/1.1" */
  9049. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9050. /* "spdy/1" */
  9051. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  9052. /* "spdy/2" */
  9053. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9054. /* "spdy/3" */
  9055. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9056. char buff[256];
  9057. word32 idx;
  9058. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9059. WOLFSSL *ssl = wolfSSL_new(ctx);
  9060. ExpectNotNull(ctx);
  9061. ExpectNotNull(ssl);
  9062. /* error cases */
  9063. ExpectIntNE(WOLFSSL_SUCCESS,
  9064. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  9065. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9066. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  9067. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9068. /* success case */
  9069. /* http1 only */
  9070. ExpectIntEQ(WOLFSSL_SUCCESS,
  9071. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  9072. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9073. /* http1, spdy1 */
  9074. XMEMCPY(buff, http1, sizeof(http1));
  9075. idx = sizeof(http1);
  9076. buff[idx++] = ',';
  9077. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9078. idx += sizeof(spdy1);
  9079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9080. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9081. /* http1, spdy2, spdy1 */
  9082. XMEMCPY(buff, http1, sizeof(http1));
  9083. idx = sizeof(http1);
  9084. buff[idx++] = ',';
  9085. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9086. idx += sizeof(spdy2);
  9087. buff[idx++] = ',';
  9088. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9089. idx += sizeof(spdy1);
  9090. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9091. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9092. /* spdy3, http1, spdy2, spdy1 */
  9093. XMEMCPY(buff, spdy3, sizeof(spdy3));
  9094. idx = sizeof(spdy3);
  9095. buff[idx++] = ',';
  9096. XMEMCPY(buff+idx, http1, sizeof(http1));
  9097. idx += sizeof(http1);
  9098. buff[idx++] = ',';
  9099. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  9100. idx += sizeof(spdy2);
  9101. buff[idx++] = ',';
  9102. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  9103. idx += sizeof(spdy1);
  9104. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  9105. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9106. wolfSSL_free(ssl);
  9107. wolfSSL_CTX_free(ctx);
  9108. #endif
  9109. return EXPECT_RESULT();
  9110. }
  9111. #endif /* HAVE_ALPN */
  9112. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  9113. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  9114. {
  9115. unsigned char p[] = {
  9116. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9117. 6, 's', 'p', 'd', 'y', '/', '2',
  9118. 6, 's', 'p', 'd', 'y', '/', '1',
  9119. };
  9120. unsigned char p_len = sizeof(p);
  9121. int ret;
  9122. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  9123. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9124. AssertIntEQ(ret, 0);
  9125. #else
  9126. AssertIntEQ(ret, SSL_SUCCESS);
  9127. #endif
  9128. }
  9129. static void set_alpn_protos(SSL* ssl)
  9130. {
  9131. unsigned char p[] = {
  9132. 6, 's', 'p', 'd', 'y', '/', '3',
  9133. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  9134. 6, 's', 'p', 'd', 'y', '/', '2',
  9135. 6, 's', 'p', 'd', 'y', '/', '1',
  9136. };
  9137. unsigned char p_len = sizeof(p);
  9138. int ret;
  9139. ret = SSL_set_alpn_protos(ssl, p, p_len);
  9140. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  9141. AssertIntEQ(ret, 0);
  9142. #else
  9143. AssertIntEQ(ret, SSL_SUCCESS);
  9144. #endif
  9145. }
  9146. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  9147. {
  9148. /* "spdy/3" */
  9149. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9150. const unsigned char *proto;
  9151. unsigned int protoSz = 0;
  9152. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9153. /* check value */
  9154. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9155. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9156. }
  9157. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  9158. {
  9159. /* "http/1.1" */
  9160. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9161. const unsigned char *proto;
  9162. unsigned int protoSz = 0;
  9163. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  9164. /* check value */
  9165. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9166. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9167. }
  9168. static int test_wolfSSL_set_alpn_protos(void)
  9169. {
  9170. int res = TEST_SKIPPED;
  9171. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9172. callback_functions client_cb;
  9173. callback_functions server_cb;
  9174. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9175. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9176. client_cb.method = wolfSSLv23_client_method;
  9177. server_cb.method = wolfSSLv23_server_method;
  9178. client_cb.devId = testDevId;
  9179. server_cb.devId = testDevId;
  9180. /* use CTX_alpn_protos */
  9181. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9182. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  9183. test_wolfSSL_client_server(&client_cb, &server_cb);
  9184. /* use set_alpn_protos */
  9185. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  9186. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  9187. test_wolfSSL_client_server(&client_cb, &server_cb);
  9188. res = TEST_SUCCESS;
  9189. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9190. return res;
  9191. }
  9192. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  9193. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  9194. {
  9195. EXPECT_DECLS;
  9196. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  9197. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9198. WOLFSSL *ssl = wolfSSL_new(ctx);
  9199. ExpectNotNull(ctx);
  9200. ExpectNotNull(ssl);
  9201. /* error cases */
  9202. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  9203. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  9204. /* success cases */
  9205. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  9206. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  9207. wolfSSL_free(ssl);
  9208. wolfSSL_CTX_free(ctx);
  9209. #endif
  9210. return EXPECT_RESULT();
  9211. }
  9212. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  9213. {
  9214. EXPECT_DECLS;
  9215. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  9216. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9217. WOLFSSL *ssl = wolfSSL_new(ctx);
  9218. ExpectNotNull(ctx);
  9219. ExpectNotNull(ssl);
  9220. /* error cases */
  9221. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  9222. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  9223. /* success cases */
  9224. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  9225. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  9226. wolfSSL_free(ssl);
  9227. wolfSSL_CTX_free(ctx);
  9228. #endif
  9229. return EXPECT_RESULT();
  9230. }
  9231. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  9232. static int test_wolfSSL_SCR_Reconnect(void)
  9233. {
  9234. EXPECT_DECLS;
  9235. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  9236. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  9237. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  9238. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9239. struct test_memio_ctx test_ctx;
  9240. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  9241. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  9242. byte data;
  9243. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9244. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  9245. test_ctx.s_ciphers =
  9246. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  9247. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9248. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9249. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  9250. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  9251. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  9252. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  9253. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9254. /* WOLFSSL_FATAL_ERROR since it will block */
  9255. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  9256. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9257. WOLFSSL_ERROR_WANT_READ);
  9258. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  9259. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  9260. WOLFSSL_ERROR_WANT_READ);
  9261. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9262. wolfSSL_free(ssl_c);
  9263. ssl_c = NULL;
  9264. wolfSSL_free(ssl_s);
  9265. ssl_s = NULL;
  9266. wolfSSL_CTX_free(ctx_c);
  9267. ctx_c = NULL;
  9268. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  9269. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9270. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9271. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9272. wolfSSL_free(ssl_s);
  9273. wolfSSL_free(ssl_c);
  9274. wolfSSL_CTX_free(ctx_s);
  9275. wolfSSL_CTX_free(ctx_c);
  9276. #endif
  9277. return EXPECT_RESULT();
  9278. }
  9279. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  9280. (!defined(NO_RSA) || defined(HAVE_ECC))
  9281. /* Called when writing. */
  9282. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9283. {
  9284. (void)ssl;
  9285. (void)buf;
  9286. (void)sz;
  9287. (void)ctx;
  9288. /* Force error return from wolfSSL_accept_TLSv13(). */
  9289. return WANT_WRITE;
  9290. }
  9291. /* Called when reading. */
  9292. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  9293. {
  9294. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  9295. int len = (int)msg->length;
  9296. (void)ssl;
  9297. (void)sz;
  9298. /* Pass back as much of message as will fit in buffer. */
  9299. if (len > sz)
  9300. len = sz;
  9301. XMEMCPY(buf, msg->buffer, len);
  9302. /* Move over returned data. */
  9303. msg->buffer += len;
  9304. msg->length -= len;
  9305. /* Amount actually copied. */
  9306. return len;
  9307. }
  9308. #endif
  9309. /* Test the detection of duplicate known TLS extensions.
  9310. * Specifically in a ClientHello.
  9311. */
  9312. static int test_tls_ext_duplicate(void)
  9313. {
  9314. EXPECT_DECLS;
  9315. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  9316. !defined(NO_FILESYSTEM)
  9317. const unsigned char clientHelloDupTlsExt[] = {
  9318. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  9319. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  9320. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  9321. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  9322. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  9323. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  9324. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  9325. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  9326. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9327. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  9328. 0x00, 0x9e, 0x01, 0x00,
  9329. /* Extensions - duplicate signature algorithms. */
  9330. 0x00, 0x19, 0x00, 0x0d,
  9331. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  9332. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  9333. /* Supported Versions extension for TLS 1.3. */
  9334. 0x00, 0x2b,
  9335. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  9336. };
  9337. WOLFSSL_BUFFER_INFO msg;
  9338. const char* testCertFile;
  9339. const char* testKeyFile;
  9340. WOLFSSL_CTX *ctx = NULL;
  9341. WOLFSSL *ssl = NULL;
  9342. #ifndef NO_RSA
  9343. testCertFile = svrCertFile;
  9344. testKeyFile = svrKeyFile;
  9345. #elif defined(HAVE_ECC)
  9346. testCertFile = eccCertFile;
  9347. testKeyFile = eccKeyFile;
  9348. #endif
  9349. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9350. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  9351. WOLFSSL_FILETYPE_PEM));
  9352. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  9353. WOLFSSL_FILETYPE_PEM));
  9354. /* Read from 'msg'. */
  9355. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  9356. /* No where to send to - dummy sender. */
  9357. wolfSSL_SetIOSend(ctx, DummySend);
  9358. ssl = wolfSSL_new(ctx);
  9359. ExpectNotNull(ssl);
  9360. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  9361. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  9362. wolfSSL_SetIOReadCtx(ssl, &msg);
  9363. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  9364. /* can return duplicate ext error or socket error if the peer closed down
  9365. * while sending alert */
  9366. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  9367. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  9368. }
  9369. wolfSSL_free(ssl);
  9370. wolfSSL_CTX_free(ctx);
  9371. #endif
  9372. return EXPECT_RESULT();
  9373. }
  9374. /*----------------------------------------------------------------------------*
  9375. | X509 Tests
  9376. *----------------------------------------------------------------------------*/
  9377. static int test_wolfSSL_X509_NAME_get_entry(void)
  9378. {
  9379. EXPECT_DECLS;
  9380. #if !defined(NO_CERTS) && !defined(NO_RSA)
  9381. #if defined(OPENSSL_ALL) || \
  9382. (defined(OPENSSL_EXTRA) && \
  9383. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  9384. /* use openssl like name to test mapping */
  9385. X509_NAME_ENTRY* ne;
  9386. X509_NAME* name;
  9387. X509* x509 = NULL;
  9388. #ifndef NO_FILESYSTEM
  9389. ASN1_STRING* asn;
  9390. char* subCN = NULL;
  9391. #endif
  9392. int idx;
  9393. ASN1_OBJECT *object = NULL;
  9394. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9395. defined(WOLFSSL_NGINX)
  9396. #ifndef NO_BIO
  9397. BIO* bio = NULL;
  9398. #endif
  9399. #endif
  9400. #ifndef NO_FILESYSTEM
  9401. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9402. WOLFSSL_FILETYPE_PEM));
  9403. ExpectNotNull(name = X509_get_subject_name(x509));
  9404. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9405. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9406. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  9407. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  9408. wolfSSL_FreeX509(x509);
  9409. x509 = NULL;
  9410. #endif
  9411. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  9412. WOLFSSL_FILETYPE_PEM));
  9413. ExpectNotNull(name = X509_get_subject_name(x509));
  9414. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  9415. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  9416. defined(WOLFSSL_NGINX)
  9417. #ifndef NO_BIO
  9418. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9419. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  9420. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9421. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  9422. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  9423. BIO_free(bio);
  9424. #endif
  9425. #endif
  9426. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  9427. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  9428. wolfSSL_FreeX509(x509);
  9429. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  9430. #endif /* !NO_CERTS && !NO_RSA */
  9431. return EXPECT_RESULT();
  9432. }
  9433. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  9434. static int test_wolfSSL_PKCS12(void)
  9435. {
  9436. EXPECT_DECLS;
  9437. /* .p12 file is encrypted with DES3 */
  9438. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  9439. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  9440. * Password Key
  9441. */
  9442. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  9443. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  9444. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  9445. byte buf[6000];
  9446. char file[] = "./certs/test-servercert.p12";
  9447. char order[] = "./certs/ecc-rsa-server.p12";
  9448. #ifdef WC_RC2
  9449. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  9450. #endif
  9451. char pass[] = "a password";
  9452. const char goodPsw[] = "wolfSSL test";
  9453. const char badPsw[] = "bad";
  9454. #ifdef HAVE_ECC
  9455. WOLFSSL_X509_NAME *subject = NULL;
  9456. WOLFSSL_X509 *x509 = NULL;
  9457. #endif
  9458. XFILE f = XBADFILE;
  9459. int bytes, ret, goodPswLen, badPswLen;
  9460. WOLFSSL_BIO *bio = NULL;
  9461. WOLFSSL_EVP_PKEY *pkey = NULL;
  9462. WC_PKCS12 *pkcs12 = NULL;
  9463. WC_PKCS12 *pkcs12_2 = NULL;
  9464. WOLFSSL_X509 *cert = NULL;
  9465. WOLFSSL_X509 *tmp = NULL;
  9466. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  9467. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9468. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9469. WOLFSSL_CTX *ctx = NULL;
  9470. WOLFSSL *ssl = NULL;
  9471. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  9472. #endif
  9473. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9474. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9475. if (f != XBADFILE) {
  9476. XFCLOSE(f);
  9477. f = XBADFILE;
  9478. }
  9479. goodPswLen = (int)XSTRLEN(goodPsw);
  9480. badPswLen = (int)XSTRLEN(badPsw);
  9481. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  9482. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  9483. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  9484. ExpectNotNull(pkcs12);
  9485. BIO_free(bio);
  9486. bio = NULL;
  9487. /* check verify MAC directly */
  9488. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  9489. /* check verify MAC fail case directly */
  9490. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  9491. /* check verify MAC fail case */
  9492. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9493. ExpectNull(pkey);
  9494. ExpectNull(cert);
  9495. /* check parse with no extra certs kept */
  9496. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9497. 1);
  9498. ExpectNotNull(pkey);
  9499. ExpectNotNull(cert);
  9500. wolfSSL_EVP_PKEY_free(pkey);
  9501. pkey = NULL;
  9502. wolfSSL_X509_free(cert);
  9503. cert = NULL;
  9504. /* check parse with extra certs kept */
  9505. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9506. 1);
  9507. ExpectNotNull(pkey);
  9508. ExpectNotNull(cert);
  9509. ExpectNotNull(ca);
  9510. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9511. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9512. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  9513. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9514. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9515. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9516. #else
  9517. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9518. #endif
  9519. /* Copy stack structure */
  9520. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  9521. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  9522. /* CTX now owns the tmp_ca stack structure */
  9523. tmp_ca = NULL;
  9524. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  9525. ExpectNotNull(tmp_ca);
  9526. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  9527. /* Check that the main cert is also set */
  9528. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  9529. ExpectNotNull(ssl = SSL_new(ctx));
  9530. ExpectNotNull(SSL_get_certificate(ssl));
  9531. SSL_free(ssl);
  9532. SSL_CTX_free(ctx);
  9533. ctx = NULL;
  9534. #endif
  9535. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9536. /* should be 2 other certs on stack */
  9537. ExpectNotNull(tmp = sk_X509_pop(ca));
  9538. X509_free(tmp);
  9539. ExpectNotNull(tmp = sk_X509_pop(ca));
  9540. X509_free(tmp);
  9541. ExpectNull(sk_X509_pop(ca));
  9542. EVP_PKEY_free(pkey);
  9543. pkey = NULL;
  9544. X509_free(cert);
  9545. cert = NULL;
  9546. sk_X509_pop_free(ca, X509_free);
  9547. ca = NULL;
  9548. /* check PKCS12_create */
  9549. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  9550. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9551. SSL_SUCCESS);
  9552. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9553. -1, -1, 100, -1, 0)));
  9554. EVP_PKEY_free(pkey);
  9555. pkey = NULL;
  9556. X509_free(cert);
  9557. cert = NULL;
  9558. sk_X509_pop_free(ca, NULL);
  9559. ca = NULL;
  9560. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9561. SSL_SUCCESS);
  9562. PKCS12_free(pkcs12_2);
  9563. pkcs12_2 = NULL;
  9564. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  9565. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9566. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  9567. 2000, 1, 0)));
  9568. EVP_PKEY_free(pkey);
  9569. pkey = NULL;
  9570. X509_free(cert);
  9571. cert = NULL;
  9572. sk_X509_pop_free(ca, NULL);
  9573. ca = NULL;
  9574. /* convert to DER then back and parse */
  9575. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9576. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  9577. PKCS12_free(pkcs12_2);
  9578. pkcs12_2 = NULL;
  9579. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  9580. BIO_free(bio);
  9581. bio = NULL;
  9582. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9583. SSL_SUCCESS);
  9584. /* should be 2 other certs on stack */
  9585. ExpectNotNull(tmp = sk_X509_pop(ca));
  9586. X509_free(tmp);
  9587. ExpectNotNull(tmp = sk_X509_pop(ca));
  9588. X509_free(tmp);
  9589. ExpectNull(sk_X509_pop(ca));
  9590. #ifndef NO_RC4
  9591. PKCS12_free(pkcs12_2);
  9592. pkcs12_2 = NULL;
  9593. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  9594. NID_pbe_WithSHA1And128BitRC4,
  9595. NID_pbe_WithSHA1And128BitRC4,
  9596. 2000, 1, 0)));
  9597. EVP_PKEY_free(pkey);
  9598. pkey = NULL;
  9599. X509_free(cert);
  9600. cert = NULL;
  9601. sk_X509_pop_free(ca, NULL);
  9602. ca = NULL;
  9603. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  9604. SSL_SUCCESS);
  9605. #endif /* NO_RC4 */
  9606. EVP_PKEY_free(pkey);
  9607. pkey = NULL;
  9608. X509_free(cert);
  9609. cert = NULL;
  9610. PKCS12_free(pkcs12);
  9611. pkcs12 = NULL;
  9612. PKCS12_free(pkcs12_2);
  9613. pkcs12_2 = NULL;
  9614. sk_X509_pop_free(ca, NULL);
  9615. ca = NULL;
  9616. #ifdef HAVE_ECC
  9617. /* test order of parsing */
  9618. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  9619. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9620. if (f != XBADFILE) {
  9621. XFCLOSE(f);
  9622. f = XBADFILE;
  9623. }
  9624. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9625. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9626. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  9627. WOLFSSL_SUCCESS);
  9628. /* check use of pkey after parse */
  9629. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  9630. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  9631. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9632. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  9633. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  9634. #else
  9635. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  9636. #endif
  9637. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  9638. SSL_CTX_free(ctx);
  9639. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9640. #endif
  9641. ExpectNotNull(pkey);
  9642. ExpectNotNull(cert);
  9643. ExpectNotNull(ca);
  9644. /* compare subject lines of certificates */
  9645. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9646. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  9647. SSL_FILETYPE_PEM));
  9648. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9649. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9650. X509_free(x509);
  9651. x509 = NULL;
  9652. /* test expected fail case */
  9653. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9654. SSL_FILETYPE_PEM));
  9655. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9656. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9657. X509_free(x509);
  9658. x509 = NULL;
  9659. X509_free(cert);
  9660. cert = NULL;
  9661. /* get subject line from ca stack */
  9662. ExpectNotNull(cert = sk_X509_pop(ca));
  9663. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  9664. /* compare subject from certificate in ca to expected */
  9665. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  9666. SSL_FILETYPE_PEM));
  9667. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  9668. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  9669. EVP_PKEY_free(pkey);
  9670. pkey = NULL;
  9671. X509_free(x509);
  9672. x509 = NULL;
  9673. X509_free(cert);
  9674. cert = NULL;
  9675. BIO_free(bio);
  9676. bio = NULL;
  9677. PKCS12_free(pkcs12);
  9678. pkcs12 = NULL;
  9679. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  9680. ca = NULL;
  9681. /* test order of parsing */
  9682. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9683. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9684. if (f != XBADFILE) {
  9685. XFCLOSE(f);
  9686. f = XBADFILE;
  9687. }
  9688. /* check verify MAC fail case */
  9689. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9690. ExpectNull(pkey);
  9691. ExpectNull(cert);
  9692. /* check parse with no extra certs kept */
  9693. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9694. 1);
  9695. ExpectNotNull(pkey);
  9696. ExpectNotNull(cert);
  9697. wolfSSL_EVP_PKEY_free(pkey);
  9698. pkey = NULL;
  9699. wolfSSL_X509_free(cert);
  9700. cert = NULL;
  9701. /* check parse with extra certs kept */
  9702. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9703. 1);
  9704. ExpectNotNull(pkey);
  9705. ExpectNotNull(cert);
  9706. ExpectNotNull(ca);
  9707. wolfSSL_EVP_PKEY_free(pkey);
  9708. pkey = NULL;
  9709. wolfSSL_X509_free(cert);
  9710. cert = NULL;
  9711. sk_X509_pop_free(ca, NULL);
  9712. ca = NULL;
  9713. PKCS12_free(pkcs12);
  9714. pkcs12 = NULL;
  9715. #endif /* HAVE_ECC */
  9716. #ifdef WC_RC2
  9717. /* test PKCS#12 with RC2 encryption */
  9718. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  9719. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  9720. if (f != XBADFILE) {
  9721. XFCLOSE(f);
  9722. f = XBADFILE;
  9723. }
  9724. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  9725. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  9726. /* check verify MAC fail case */
  9727. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  9728. ExpectNull(pkey);
  9729. ExpectNull(cert);
  9730. /* check parse with not extra certs kept */
  9731. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  9732. WOLFSSL_SUCCESS);
  9733. ExpectNotNull(pkey);
  9734. ExpectNotNull(cert);
  9735. wolfSSL_EVP_PKEY_free(pkey);
  9736. pkey = NULL;
  9737. wolfSSL_X509_free(cert);
  9738. cert = NULL;
  9739. /* check parse with extra certs kept */
  9740. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  9741. WOLFSSL_SUCCESS);
  9742. ExpectNotNull(pkey);
  9743. ExpectNotNull(cert);
  9744. ExpectNotNull(ca);
  9745. wolfSSL_EVP_PKEY_free(pkey);
  9746. wolfSSL_X509_free(cert);
  9747. sk_X509_pop_free(ca, NULL);
  9748. BIO_free(bio);
  9749. bio = NULL;
  9750. PKCS12_free(pkcs12);
  9751. pkcs12 = NULL;
  9752. #endif /* WC_RC2 */
  9753. /* Test i2d_PKCS12_bio */
  9754. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  9755. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  9756. if (f != XBADFILE)
  9757. XFCLOSE(f);
  9758. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  9759. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  9760. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  9761. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  9762. PKCS12_free(pkcs12);
  9763. BIO_free(bio);
  9764. (void)order;
  9765. #endif /* OPENSSL_EXTRA */
  9766. #endif /* HAVE_FIPS */
  9767. return EXPECT_RESULT();
  9768. }
  9769. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9770. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  9771. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  9772. #define TEST_PKCS8_ENC
  9773. #endif
  9774. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9775. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9776. /* used to keep track if FailTestCallback was called */
  9777. static int failTestCallbackCalled = 0;
  9778. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  9779. {
  9780. (void)passwd;
  9781. (void)sz;
  9782. (void)rw;
  9783. (void)userdata;
  9784. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  9785. failTestCallbackCalled = 1;
  9786. return -1;
  9787. }
  9788. #endif
  9789. static int test_wolfSSL_no_password_cb(void)
  9790. {
  9791. EXPECT_DECLS;
  9792. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  9793. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  9794. WOLFSSL_CTX* ctx = NULL;
  9795. byte buff[FOURK_BUF];
  9796. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9797. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9798. XFILE f = XBADFILE;
  9799. int bytes;
  9800. #ifndef NO_WOLFSSL_CLIENT
  9801. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  9802. #else
  9803. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  9804. #endif
  9805. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  9806. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  9807. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9808. if (f != XBADFILE) {
  9809. XFCLOSE(f);
  9810. f = XBADFILE;
  9811. }
  9812. ExpectIntLE(bytes, sizeof(buff));
  9813. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9814. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9815. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9816. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9817. if (f != XBADFILE)
  9818. XFCLOSE(f);
  9819. ExpectIntLE(bytes, sizeof(buff));
  9820. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9821. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9822. wolfSSL_CTX_free(ctx);
  9823. /* Password callback should not be called by default */
  9824. ExpectIntEQ(failTestCallbackCalled, 0);
  9825. #endif
  9826. return EXPECT_RESULT();
  9827. }
  9828. #ifdef TEST_PKCS8_ENC
  9829. /* for PKCS8 test case */
  9830. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  9831. {
  9832. int flag = 0;
  9833. (void)rw;
  9834. if (userdata != NULL) {
  9835. flag = *((int*)userdata); /* user set data */
  9836. }
  9837. switch (flag) {
  9838. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  9839. * can be anything the user wishes to be passed to the callback
  9840. * associated with the WOLFSSL_CTX */
  9841. XSTRNCPY(passwd, "yassl123", sz);
  9842. return 8;
  9843. default:
  9844. return BAD_FUNC_ARG;
  9845. }
  9846. }
  9847. #endif /* TEST_PKCS8_ENC */
  9848. /* Testing functions dealing with PKCS8 */
  9849. static int test_wolfSSL_PKCS8(void)
  9850. {
  9851. EXPECT_DECLS;
  9852. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  9853. !defined(WOLFCRYPT_ONLY)
  9854. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9855. byte buff[FOURK_BUF];
  9856. byte der[FOURK_BUF];
  9857. #ifndef NO_RSA
  9858. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  9859. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  9860. #endif
  9861. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  9862. #ifdef HAVE_ECC
  9863. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  9864. #endif
  9865. XFILE f = XBADFILE;
  9866. int bytes = 0;
  9867. WOLFSSL_CTX* ctx = NULL;
  9868. #if defined(HAVE_ECC) && !defined(NO_CODING)
  9869. int ret;
  9870. ecc_key key;
  9871. word32 x = 0;
  9872. #endif
  9873. #ifdef TEST_PKCS8_ENC
  9874. #if !defined(NO_RSA) && !defined(NO_SHA)
  9875. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  9876. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  9877. #endif
  9878. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9879. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  9880. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  9881. #endif
  9882. int flag;
  9883. #endif
  9884. (void)der;
  9885. #ifndef NO_WOLFSSL_CLIENT
  9886. #ifndef WOLFSSL_NO_TLS12
  9887. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9888. #else
  9889. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9890. #endif
  9891. #else
  9892. #ifndef WOLFSSL_NO_TLS12
  9893. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  9894. #else
  9895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  9896. #endif
  9897. #endif
  9898. #ifdef TEST_PKCS8_ENC
  9899. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  9900. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  9901. flag = 1; /* used by password callback as return code */
  9902. #if !defined(NO_RSA) && !defined(NO_SHA)
  9903. /* test loading PEM PKCS8 encrypted file */
  9904. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  9905. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9906. if (f != XBADFILE) {
  9907. XFCLOSE(f);
  9908. f = XBADFILE;
  9909. }
  9910. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9911. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9912. /* this next case should fail because of password callback return code */
  9913. flag = 0; /* used by password callback as return code */
  9914. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9915. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9916. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9917. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9918. "yassl123"), 0);
  9919. /* test that error value is returned with a bad password */
  9920. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9921. "bad"), 0);
  9922. /* test loading PEM PKCS8 encrypted file */
  9923. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  9924. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9925. if (f != XBADFILE) {
  9926. XFCLOSE(f);
  9927. f = XBADFILE;
  9928. }
  9929. flag = 1; /* used by password callback as return code */
  9930. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9931. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9932. /* this next case should fail because of password callback return code */
  9933. flag = 0; /* used by password callback as return code */
  9934. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9935. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9936. #endif /* !NO_RSA && !NO_SHA */
  9937. #if defined(HAVE_ECC) && !defined(NO_SHA)
  9938. /* test loading PEM PKCS8 encrypted ECC Key file */
  9939. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  9940. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9941. if (f != XBADFILE) {
  9942. XFCLOSE(f);
  9943. f = XBADFILE;
  9944. }
  9945. flag = 1; /* used by password callback as return code */
  9946. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9947. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9948. /* this next case should fail because of password callback return code */
  9949. flag = 0; /* used by password callback as return code */
  9950. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9951. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9952. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  9953. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9954. "yassl123"), 0);
  9955. /* test that error value is returned with a bad password */
  9956. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  9957. "bad"), 0);
  9958. /* test loading DER PKCS8 encrypted ECC Key file */
  9959. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  9960. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9961. if (f != XBADFILE) {
  9962. XFCLOSE(f);
  9963. f = XBADFILE;
  9964. }
  9965. flag = 1; /* used by password callback as return code */
  9966. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9967. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9968. /* this next case should fail because of password callback return code */
  9969. flag = 0; /* used by password callback as return code */
  9970. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9971. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9972. /* leave flag as "okay" */
  9973. flag = 1;
  9974. #endif /* HAVE_ECC && !NO_SHA */
  9975. #endif /* TEST_PKCS8_ENC */
  9976. #ifndef NO_RSA
  9977. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  9978. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  9979. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9980. if (f != XBADFILE) {
  9981. XFCLOSE(f);
  9982. f = XBADFILE;
  9983. }
  9984. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9985. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  9986. /* test loading PEM PKCS8 private key file (not encrypted) */
  9987. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  9988. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9989. if (f != XBADFILE) {
  9990. XFCLOSE(f);
  9991. f = XBADFILE;
  9992. }
  9993. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  9994. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  9995. #endif /* !NO_RSA */
  9996. /* Test PKCS8 PEM ECC key no crypt */
  9997. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  9998. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  9999. if (f != XBADFILE) {
  10000. XFCLOSE(f);
  10001. f = XBADFILE;
  10002. }
  10003. #ifdef HAVE_ECC
  10004. /* Test PKCS8 PEM ECC key no crypt */
  10005. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10006. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10007. #ifndef NO_CODING
  10008. /* decrypt PKCS8 PEM to key in DER format */
  10009. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  10010. (word32)sizeof(der), NULL)), 0);
  10011. ret = wc_ecc_init(&key);
  10012. if (ret == 0) {
  10013. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  10014. wc_ecc_free(&key);
  10015. }
  10016. ExpectIntEQ(ret, 0);
  10017. #endif
  10018. /* Test PKCS8 DER ECC key no crypt */
  10019. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10020. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10021. if (f != XBADFILE)
  10022. XFCLOSE(f);
  10023. /* Test using a PKCS8 ECC PEM */
  10024. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10025. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10026. #else
  10027. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  10028. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  10029. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  10030. #endif /* HAVE_ECC */
  10031. wolfSSL_CTX_free(ctx);
  10032. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10033. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  10034. return EXPECT_RESULT();
  10035. }
  10036. static int test_wolfSSL_PKCS8_ED25519(void)
  10037. {
  10038. EXPECT_DECLS;
  10039. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10040. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  10041. defined(HAVE_ED25519_KEY_IMPORT)
  10042. const byte encPrivKey[] = \
  10043. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10044. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  10045. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  10046. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  10047. "2L9W4v7swXkV+X+a1ww=\n"
  10048. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10049. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10050. byte der[FOURK_BUF];
  10051. WOLFSSL_CTX* ctx = NULL;
  10052. int bytes;
  10053. XMEMSET(der, 0, sizeof(der));
  10054. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10055. (word32)sizeof(der), password)), 0);
  10056. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10057. #ifndef NO_WOLFSSL_SERVER
  10058. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10059. #else
  10060. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10061. #endif
  10062. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10063. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10064. wolfSSL_CTX_free(ctx);
  10065. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10066. #endif
  10067. return EXPECT_RESULT();
  10068. }
  10069. static int test_wolfSSL_PKCS8_ED448(void)
  10070. {
  10071. EXPECT_DECLS;
  10072. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  10073. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  10074. defined(HAVE_ED448_KEY_IMPORT)
  10075. const byte encPrivKey[] = \
  10076. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  10077. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  10078. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  10079. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  10080. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  10081. "-----END ENCRYPTED PRIVATE KEY-----\n";
  10082. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  10083. byte der[FOURK_BUF];
  10084. WOLFSSL_CTX* ctx = NULL;
  10085. int bytes;
  10086. XMEMSET(der, 0, sizeof(der));
  10087. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  10088. (word32)sizeof(der), password)), 0);
  10089. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10090. #ifndef NO_WOLFSSL_SERVER
  10091. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10092. #else
  10093. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10094. #endif
  10095. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  10096. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10097. wolfSSL_CTX_free(ctx);
  10098. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10099. #endif
  10100. return EXPECT_RESULT();
  10101. }
  10102. /* Testing functions dealing with PKCS5 */
  10103. static int test_wolfSSL_PKCS5(void)
  10104. {
  10105. EXPECT_DECLS;
  10106. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  10107. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  10108. const char* passwd = "myfipsPa$$W0rd";
  10109. #else
  10110. const char *passwd = "pass1234";
  10111. #endif
  10112. const unsigned char *salt = (unsigned char *)"salt1234";
  10113. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  10114. DYNAMIC_TYPE_TMP_BUFFER);
  10115. int ret = 0;
  10116. ExpectNotNull(out);
  10117. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  10118. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  10119. WOLFSSL_SUCCESS);
  10120. #ifdef WOLFSSL_SHA512
  10121. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  10122. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  10123. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  10124. #endif
  10125. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10126. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  10127. return EXPECT_RESULT();
  10128. }
  10129. /* test parsing URI from certificate */
  10130. static int test_wolfSSL_URI(void)
  10131. {
  10132. EXPECT_DECLS;
  10133. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10134. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  10135. defined(OPENSSL_EXTRA))
  10136. WOLFSSL_X509* x509 = NULL;
  10137. const char uri[] = "./certs/client-uri-cert.pem";
  10138. const char urn[] = "./certs/client-absolute-urn.pem";
  10139. const char badUri[] = "./certs/client-relative-uri.pem";
  10140. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  10141. WOLFSSL_FILETYPE_PEM));
  10142. wolfSSL_FreeX509(x509);
  10143. x509 = NULL;
  10144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  10145. WOLFSSL_FILETYPE_PEM));
  10146. wolfSSL_FreeX509(x509);
  10147. x509 = NULL;
  10148. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  10149. && !defined(WOLFSSL_FPKI)
  10150. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10151. WOLFSSL_FILETYPE_PEM));
  10152. #else
  10153. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  10154. WOLFSSL_FILETYPE_PEM));
  10155. #endif
  10156. wolfSSL_FreeX509(x509);
  10157. #endif
  10158. return EXPECT_RESULT();
  10159. }
  10160. static int test_wolfSSL_TBS(void)
  10161. {
  10162. EXPECT_DECLS;
  10163. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  10164. && defined(OPENSSL_EXTRA)
  10165. WOLFSSL_X509* x509 = NULL;
  10166. const unsigned char* tbs;
  10167. int tbsSz;
  10168. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  10169. WOLFSSL_FILETYPE_PEM));
  10170. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  10171. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  10172. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  10173. ExpectIntEQ(tbsSz, 1003);
  10174. wolfSSL_FreeX509(x509);
  10175. #endif
  10176. return EXPECT_RESULT();
  10177. }
  10178. static int test_wolfSSL_X509_verify(void)
  10179. {
  10180. EXPECT_DECLS;
  10181. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  10182. defined(OPENSSL_EXTRA)
  10183. WOLFSSL_X509* ca = NULL;
  10184. WOLFSSL_X509* serv = NULL;
  10185. WOLFSSL_EVP_PKEY* pkey = NULL;
  10186. unsigned char buf[2048];
  10187. const unsigned char* pt = NULL;
  10188. int bufSz;
  10189. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  10190. WOLFSSL_FILETYPE_PEM));
  10191. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  10192. WOLFSSL_SUCCESS);
  10193. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  10194. WOLFSSL_SUCCESS);
  10195. ExpectIntEQ(bufSz, 294);
  10196. bufSz = 2048;
  10197. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  10198. WOLFSSL_SUCCESS);
  10199. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  10200. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  10201. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  10202. WOLFSSL_FILETYPE_PEM));
  10203. /* success case */
  10204. pt = buf;
  10205. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10206. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  10207. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  10208. wolfSSL_EVP_PKEY_free(pkey);
  10209. pkey = NULL;
  10210. /* fail case */
  10211. bufSz = 2048;
  10212. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  10213. WOLFSSL_SUCCESS);
  10214. pt = buf;
  10215. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  10216. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  10217. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  10218. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  10219. wolfSSL_EVP_PKEY_free(pkey);
  10220. wolfSSL_FreeX509(ca);
  10221. wolfSSL_FreeX509(serv);
  10222. #endif
  10223. return EXPECT_RESULT();
  10224. }
  10225. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10226. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10227. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10228. /* create certificate with version 2 */
  10229. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  10230. {
  10231. EXPECT_DECLS;
  10232. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  10233. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  10234. unsigned char *der = NULL, *key = NULL, *pt;
  10235. char *header = NULL, *name = NULL;
  10236. int derSz;
  10237. long keySz;
  10238. XFILE fp = XBADFILE;
  10239. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  10240. time_t t;
  10241. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10242. WOLFSSL_FILETYPE_PEM));
  10243. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  10244. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  10245. WOLFSSL_SUCCESS);
  10246. if (fp != XBADFILE)
  10247. XFCLOSE(fp);
  10248. pt = key;
  10249. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  10250. (const unsigned char**)&pt, keySz));
  10251. /* create the version 2 certificate */
  10252. ExpectNotNull(x509v2 = X509_new());
  10253. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  10254. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  10255. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  10256. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  10257. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  10258. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  10259. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  10260. t = time(NULL);
  10261. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  10262. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  10263. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  10264. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  10265. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  10266. derSz = wolfSSL_i2d_X509(x509v2, &der);
  10267. ExpectIntGT(derSz, 0);
  10268. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  10269. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10270. /* TODO: Replace with API call */
  10271. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  10272. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10273. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10274. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10275. wolfSSL_X509_free(x509);
  10276. wolfSSL_X509_free(x509v2);
  10277. wolfSSL_EVP_PKEY_free(priv);
  10278. wolfSSL_EVP_PKEY_free(pub);
  10279. wolfSSL_ASN1_TIME_free(notBefore);
  10280. wolfSSL_ASN1_TIME_free(notAfter);
  10281. return EXPECT_RESULT();
  10282. }
  10283. /* override certificate version error */
  10284. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  10285. {
  10286. EXPECT_DECLS;
  10287. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10288. ExpectIntEQ(store->error, ASN_VERSION_E);
  10289. #else
  10290. ExpectIntEQ(store->error, 0);
  10291. #endif
  10292. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  10293. (void)preverify;
  10294. return EXPECT_RESULT() == TEST_SUCCESS;
  10295. }
  10296. /* set verify callback that will override bad certificate version */
  10297. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  10298. {
  10299. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  10300. return TEST_SUCCESS;
  10301. }
  10302. #endif
  10303. static int test_wolfSSL_X509_TLS_version_test_1(void)
  10304. {
  10305. EXPECT_DECLS;
  10306. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10307. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10308. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10309. test_ssl_cbf func_cb_client;
  10310. test_ssl_cbf func_cb_server;
  10311. /* test server rejects a client certificate that is not version 3 */
  10312. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10313. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10314. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10315. #ifndef WOLFSSL_NO_TLS12
  10316. func_cb_client.method = wolfTLSv1_2_client_method;
  10317. #else
  10318. func_cb_client.method = wolfTLSv1_3_client_method;
  10319. #endif
  10320. #ifndef WOLFSSL_NO_TLS12
  10321. func_cb_server.method = wolfTLSv1_2_server_method;
  10322. #else
  10323. func_cb_server.method = wolfTLSv1_3_server_method;
  10324. #endif
  10325. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  10326. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10327. &func_cb_server, NULL), TEST_FAIL);
  10328. #else
  10329. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10330. &func_cb_server, NULL), TEST_SUCCESS);
  10331. #endif
  10332. #endif
  10333. return EXPECT_RESULT();
  10334. }
  10335. static int test_wolfSSL_X509_TLS_version_test_2(void)
  10336. {
  10337. EXPECT_DECLS;
  10338. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  10339. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  10340. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  10341. test_ssl_cbf func_cb_client;
  10342. test_ssl_cbf func_cb_server;
  10343. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  10344. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  10345. func_cb_client.ctx_ready = &test_set_x509_badversion;
  10346. func_cb_server.ctx_ready = &test_set_override_x509;
  10347. #ifndef WOLFSSL_NO_TLS12
  10348. func_cb_client.method = wolfTLSv1_2_client_method;
  10349. #else
  10350. func_cb_client.method = wolfTLSv1_3_client_method;
  10351. #endif
  10352. #ifndef WOLFSSL_NO_TLS12
  10353. func_cb_server.method = wolfTLSv1_2_server_method;
  10354. #else
  10355. func_cb_server.method = wolfTLSv1_3_server_method;
  10356. #endif
  10357. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  10358. &func_cb_server, NULL), TEST_SUCCESS);
  10359. #endif
  10360. return EXPECT_RESULT();
  10361. }
  10362. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  10363. * version allowed.
  10364. * POST: 1 on success.
  10365. */
  10366. static int test_wolfSSL_CTX_SetMinVersion(void)
  10367. {
  10368. int res = TEST_SKIPPED;
  10369. #ifndef NO_WOLFSSL_CLIENT
  10370. int failFlag = WOLFSSL_SUCCESS;
  10371. WOLFSSL_CTX* ctx;
  10372. int itr;
  10373. #ifndef NO_OLD_TLS
  10374. const int versions[] = {
  10375. #ifdef WOLFSSL_ALLOW_TLSV10
  10376. WOLFSSL_TLSV1,
  10377. #endif
  10378. WOLFSSL_TLSV1_1,
  10379. WOLFSSL_TLSV1_2 };
  10380. #elif !defined(WOLFSSL_NO_TLS12)
  10381. const int versions[] = { WOLFSSL_TLSV1_2 };
  10382. #elif defined(WOLFSSL_TLS13)
  10383. const int versions[] = { WOLFSSL_TLSV1_3 };
  10384. #else
  10385. const int versions[0];
  10386. #endif
  10387. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10388. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  10389. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  10390. != WOLFSSL_SUCCESS) {
  10391. failFlag = WOLFSSL_FAILURE;
  10392. }
  10393. }
  10394. wolfSSL_CTX_free(ctx);
  10395. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  10396. #endif
  10397. return res;
  10398. } /* END test_wolfSSL_CTX_SetMinVersion */
  10399. /*----------------------------------------------------------------------------*
  10400. | OCSP Stapling
  10401. *----------------------------------------------------------------------------*/
  10402. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  10403. * need to contact the CA, lowering the cost of cert revocation checking.
  10404. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  10405. * POST: 1 returned for success.
  10406. */
  10407. static int test_wolfSSL_UseOCSPStapling(void)
  10408. {
  10409. EXPECT_DECLS;
  10410. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  10411. !defined(NO_WOLFSSL_CLIENT)
  10412. WOLFSSL_CTX* ctx = NULL;
  10413. WOLFSSL* ssl = NULL;
  10414. #ifndef NO_WOLFSSL_CLIENT
  10415. #ifndef WOLFSSL_NO_TLS12
  10416. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10417. #else
  10418. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10419. #endif
  10420. #else
  10421. #ifndef WOLFSSL_NO_TLS12
  10422. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10423. #else
  10424. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10425. #endif
  10426. #endif
  10427. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10428. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  10429. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10430. #ifndef NO_WOLFSSL_CLIENT
  10431. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10432. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10433. #else
  10434. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  10435. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10436. #endif
  10437. wolfSSL_free(ssl);
  10438. wolfSSL_CTX_free(ctx);
  10439. #endif
  10440. return EXPECT_RESULT();
  10441. } /* END test_wolfSSL_UseOCSPStapling */
  10442. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  10443. * stapling eliminates the need to contact the CA and lowers cert revocation
  10444. * check.
  10445. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  10446. */
  10447. static int test_wolfSSL_UseOCSPStaplingV2(void)
  10448. {
  10449. EXPECT_DECLS;
  10450. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  10451. !defined(NO_WOLFSSL_CLIENT)
  10452. WOLFSSL_CTX* ctx = NULL;
  10453. WOLFSSL* ssl = NULL;
  10454. #ifndef NO_WOLFSSL_CLIENT
  10455. #ifndef WOLFSSL_NO_TLS12
  10456. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10457. #else
  10458. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10459. #endif
  10460. #else
  10461. #ifndef WOLFSSL_NO_TLS12
  10462. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10463. #else
  10464. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10465. #endif
  10466. #endif
  10467. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10468. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  10469. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10470. #ifndef NO_WOLFSSL_CLIENT
  10471. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10472. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  10473. #else
  10474. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  10475. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  10476. #endif
  10477. wolfSSL_free(ssl);
  10478. wolfSSL_CTX_free(ctx);
  10479. #endif
  10480. return EXPECT_RESULT();
  10481. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  10482. /*----------------------------------------------------------------------------*
  10483. | Multicast Tests
  10484. *----------------------------------------------------------------------------*/
  10485. static int test_wolfSSL_mcast(void)
  10486. {
  10487. EXPECT_DECLS;
  10488. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  10489. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  10490. WOLFSSL_CTX* ctx = NULL;
  10491. WOLFSSL* ssl = NULL;
  10492. byte preMasterSecret[512];
  10493. byte clientRandom[32];
  10494. byte serverRandom[32];
  10495. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  10496. byte buf[256];
  10497. word16 newId;
  10498. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  10499. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  10500. ExpectNotNull(ssl = wolfSSL_new(ctx));
  10501. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  10502. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  10503. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  10504. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  10505. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  10506. WOLFSSL_SUCCESS);
  10507. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  10508. ExpectIntLE(newId, 100);
  10509. wolfSSL_free(ssl);
  10510. wolfSSL_CTX_free(ctx);
  10511. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  10512. * WOLFSSL_SNIFFER) */
  10513. return EXPECT_RESULT();
  10514. }
  10515. /*----------------------------------------------------------------------------*
  10516. | Wolfcrypt
  10517. *----------------------------------------------------------------------------*/
  10518. /*
  10519. * Unit test for the wc_InitBlake2b()
  10520. */
  10521. static int test_wc_InitBlake2b(void)
  10522. {
  10523. EXPECT_DECLS;
  10524. #ifdef HAVE_BLAKE2
  10525. Blake2b blake;
  10526. /* Test good arg. */
  10527. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  10528. /* Test bad arg. */
  10529. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  10530. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  10531. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  10532. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  10533. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  10534. #endif
  10535. return EXPECT_RESULT();
  10536. } /* END test_wc_InitBlake2b*/
  10537. /*
  10538. * Unit test for the wc_InitBlake2b_WithKey()
  10539. */
  10540. static int test_wc_InitBlake2b_WithKey(void)
  10541. {
  10542. EXPECT_DECLS;
  10543. #ifdef HAVE_BLAKE2
  10544. Blake2b blake;
  10545. word32 digestSz = BLAKE2B_KEYBYTES;
  10546. byte key[BLAKE2B_KEYBYTES];
  10547. word32 keylen = BLAKE2B_KEYBYTES;
  10548. XMEMSET(key, 0, sizeof(key));
  10549. /* Test good arg. */
  10550. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  10551. /* Test bad args. */
  10552. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  10553. BAD_FUNC_ARG);
  10554. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  10555. BAD_FUNC_ARG);
  10556. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  10557. #endif
  10558. return EXPECT_RESULT();
  10559. } /* END wc_InitBlake2b_WithKey*/
  10560. /*
  10561. * Unit test for the wc_InitBlake2s_WithKey()
  10562. */
  10563. static int test_wc_InitBlake2s_WithKey(void)
  10564. {
  10565. EXPECT_DECLS;
  10566. #ifdef HAVE_BLAKE2S
  10567. Blake2s blake;
  10568. word32 digestSz = BLAKE2S_KEYBYTES;
  10569. byte *key = (byte*)"01234567890123456789012345678901";
  10570. word32 keylen = BLAKE2S_KEYBYTES;
  10571. /* Test good arg. */
  10572. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  10573. /* Test bad args. */
  10574. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  10575. BAD_FUNC_ARG);
  10576. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  10577. BAD_FUNC_ARG);
  10578. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  10579. #endif
  10580. return EXPECT_RESULT();
  10581. } /* END wc_InitBlake2s_WithKey*/
  10582. /*
  10583. * Unit test for the wc_InitMd5()
  10584. */
  10585. static int test_wc_InitMd5(void)
  10586. {
  10587. EXPECT_DECLS;
  10588. #ifndef NO_MD5
  10589. wc_Md5 md5;
  10590. /* Test good arg. */
  10591. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10592. /* Test bad arg. */
  10593. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  10594. wc_Md5Free(&md5);
  10595. #endif
  10596. return EXPECT_RESULT();
  10597. } /* END test_wc_InitMd5 */
  10598. /*
  10599. * Testing wc_UpdateMd5()
  10600. */
  10601. static int test_wc_Md5Update(void)
  10602. {
  10603. EXPECT_DECLS;
  10604. #ifndef NO_MD5
  10605. wc_Md5 md5;
  10606. byte hash[WC_MD5_DIGEST_SIZE];
  10607. testVector a, b, c;
  10608. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10609. /* Input */
  10610. a.input = "a";
  10611. a.inLen = XSTRLEN(a.input);
  10612. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  10613. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10614. /* Update input. */
  10615. a.input = "abc";
  10616. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  10617. "\x72";
  10618. a.inLen = XSTRLEN(a.input);
  10619. a.outLen = XSTRLEN(a.output);
  10620. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  10621. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  10622. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  10623. /* Pass in bad values. */
  10624. b.input = NULL;
  10625. b.inLen = 0;
  10626. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  10627. c.input = NULL;
  10628. c.inLen = WC_MD5_DIGEST_SIZE;
  10629. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  10630. BAD_FUNC_ARG);
  10631. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  10632. BAD_FUNC_ARG);
  10633. wc_Md5Free(&md5);
  10634. #endif
  10635. return EXPECT_RESULT();
  10636. } /* END test_wc_Md5Update() */
  10637. /*
  10638. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  10639. */
  10640. static int test_wc_Md5Final(void)
  10641. {
  10642. EXPECT_DECLS;
  10643. #ifndef NO_MD5
  10644. /* Instantiate */
  10645. wc_Md5 md5;
  10646. byte* hash_test[3];
  10647. byte hash1[WC_MD5_DIGEST_SIZE];
  10648. byte hash2[2*WC_MD5_DIGEST_SIZE];
  10649. byte hash3[5*WC_MD5_DIGEST_SIZE];
  10650. int times, i;
  10651. /* Initialize */
  10652. ExpectIntEQ(wc_InitMd5(&md5), 0);
  10653. hash_test[0] = hash1;
  10654. hash_test[1] = hash2;
  10655. hash_test[2] = hash3;
  10656. times = sizeof(hash_test)/sizeof(byte*);
  10657. for (i = 0; i < times; i++) {
  10658. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  10659. }
  10660. /* Test bad args. */
  10661. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  10662. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  10663. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  10664. wc_Md5Free(&md5);
  10665. #endif
  10666. return EXPECT_RESULT();
  10667. }
  10668. /*
  10669. * Unit test for the wc_InitSha()
  10670. */
  10671. static int test_wc_InitSha(void)
  10672. {
  10673. EXPECT_DECLS;
  10674. #ifndef NO_SHA
  10675. wc_Sha sha;
  10676. /* Test good arg. */
  10677. ExpectIntEQ(wc_InitSha(&sha), 0);
  10678. /* Test bad arg. */
  10679. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  10680. wc_ShaFree(&sha);
  10681. #endif
  10682. return EXPECT_RESULT();
  10683. } /* END test_wc_InitSha */
  10684. /*
  10685. * Tesing wc_ShaUpdate()
  10686. */
  10687. static int test_wc_ShaUpdate(void)
  10688. {
  10689. EXPECT_DECLS;
  10690. #ifndef NO_SHA
  10691. wc_Sha sha;
  10692. byte hash[WC_SHA_DIGEST_SIZE];
  10693. testVector a, b, c;
  10694. ExpectIntEQ(wc_InitSha(&sha), 0);
  10695. /* Input. */
  10696. a.input = "a";
  10697. a.inLen = XSTRLEN(a.input);
  10698. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  10699. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  10700. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10701. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10702. /* Update input. */
  10703. a.input = "abc";
  10704. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  10705. "\x6C\x9C\xD0\xD8\x9D";
  10706. a.inLen = XSTRLEN(a.input);
  10707. a.outLen = XSTRLEN(a.output);
  10708. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  10709. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  10710. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  10711. /* Try passing in bad values. */
  10712. b.input = NULL;
  10713. b.inLen = 0;
  10714. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  10715. c.input = NULL;
  10716. c.inLen = WC_SHA_DIGEST_SIZE;
  10717. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  10718. BAD_FUNC_ARG);
  10719. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  10720. BAD_FUNC_ARG);
  10721. wc_ShaFree(&sha);
  10722. #endif
  10723. return EXPECT_RESULT();
  10724. } /* END test_wc_ShaUpdate() */
  10725. /*
  10726. * Unit test on wc_ShaFinal
  10727. */
  10728. static int test_wc_ShaFinal(void)
  10729. {
  10730. EXPECT_DECLS;
  10731. #ifndef NO_SHA
  10732. wc_Sha sha;
  10733. byte* hash_test[3];
  10734. byte hash1[WC_SHA_DIGEST_SIZE];
  10735. byte hash2[2*WC_SHA_DIGEST_SIZE];
  10736. byte hash3[5*WC_SHA_DIGEST_SIZE];
  10737. int times, i;
  10738. /* Initialize*/
  10739. ExpectIntEQ(wc_InitSha(&sha), 0);
  10740. hash_test[0] = hash1;
  10741. hash_test[1] = hash2;
  10742. hash_test[2] = hash3;
  10743. times = sizeof(hash_test)/sizeof(byte*);
  10744. for (i = 0; i < times; i++) {
  10745. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  10746. }
  10747. /* Test bad args. */
  10748. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  10749. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  10750. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  10751. wc_ShaFree(&sha);
  10752. #endif
  10753. return EXPECT_RESULT();
  10754. } /* END test_wc_ShaFinal */
  10755. /*
  10756. * Unit test for wc_InitSha256()
  10757. */
  10758. static int test_wc_InitSha256(void)
  10759. {
  10760. EXPECT_DECLS;
  10761. #ifndef NO_SHA256
  10762. wc_Sha256 sha256;
  10763. /* Test good arg. */
  10764. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10765. /* Test bad arg. */
  10766. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  10767. wc_Sha256Free(&sha256);
  10768. #endif
  10769. return EXPECT_RESULT();
  10770. } /* END test_wc_InitSha256 */
  10771. /*
  10772. * Unit test for wc_Sha256Update()
  10773. */
  10774. static int test_wc_Sha256Update(void)
  10775. {
  10776. EXPECT_DECLS;
  10777. #ifndef NO_SHA256
  10778. wc_Sha256 sha256;
  10779. byte hash[WC_SHA256_DIGEST_SIZE];
  10780. testVector a, b, c;
  10781. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10782. /* Input. */
  10783. a.input = "a";
  10784. a.inLen = XSTRLEN(a.input);
  10785. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  10786. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  10787. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10788. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10789. /* Update input. */
  10790. a.input = "abc";
  10791. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  10792. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  10793. "\x15\xAD";
  10794. a.inLen = XSTRLEN(a.input);
  10795. a.outLen = XSTRLEN(a.output);
  10796. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  10797. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  10798. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  10799. /* Try passing in bad values */
  10800. b.input = NULL;
  10801. b.inLen = 0;
  10802. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  10803. c.input = NULL;
  10804. c.inLen = WC_SHA256_DIGEST_SIZE;
  10805. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  10806. BAD_FUNC_ARG);
  10807. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  10808. BAD_FUNC_ARG);
  10809. wc_Sha256Free(&sha256);
  10810. #endif
  10811. return EXPECT_RESULT();
  10812. } /* END test_wc_Sha256Update */
  10813. /*
  10814. * Unit test function for wc_Sha256Final()
  10815. */
  10816. static int test_wc_Sha256Final(void)
  10817. {
  10818. EXPECT_DECLS;
  10819. #ifndef NO_SHA256
  10820. wc_Sha256 sha256;
  10821. byte* hash_test[3];
  10822. byte hash1[WC_SHA256_DIGEST_SIZE];
  10823. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10824. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10825. int times, i;
  10826. /* Initialize */
  10827. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10828. hash_test[0] = hash1;
  10829. hash_test[1] = hash2;
  10830. hash_test[2] = hash3;
  10831. times = sizeof(hash_test) / sizeof(byte*);
  10832. for (i = 0; i < times; i++) {
  10833. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  10834. }
  10835. /* Test bad args. */
  10836. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  10837. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  10838. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  10839. wc_Sha256Free(&sha256);
  10840. #endif
  10841. return EXPECT_RESULT();
  10842. } /* END test_wc_Sha256Final */
  10843. /*
  10844. * Unit test function for wc_Sha256FinalRaw()
  10845. */
  10846. static int test_wc_Sha256FinalRaw(void)
  10847. {
  10848. EXPECT_DECLS;
  10849. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  10850. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  10851. !defined(WOLFSSL_NO_HASH_RAW)
  10852. wc_Sha256 sha256;
  10853. byte* hash_test[3];
  10854. byte hash1[WC_SHA256_DIGEST_SIZE];
  10855. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  10856. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  10857. int times, i;
  10858. /* Initialize */
  10859. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10860. hash_test[0] = hash1;
  10861. hash_test[1] = hash2;
  10862. hash_test[2] = hash3;
  10863. times = sizeof(hash_test) / sizeof(byte*);
  10864. for (i = 0; i < times; i++) {
  10865. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  10866. }
  10867. /* Test bad args. */
  10868. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  10869. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  10870. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  10871. wc_Sha256Free(&sha256);
  10872. #endif
  10873. return EXPECT_RESULT();
  10874. } /* END test_wc_Sha256FinalRaw */
  10875. /*
  10876. * Unit test function for wc_Sha256GetFlags()
  10877. */
  10878. static int test_wc_Sha256GetFlags(void)
  10879. {
  10880. EXPECT_DECLS;
  10881. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  10882. wc_Sha256 sha256;
  10883. word32 flags = 0;
  10884. /* Initialize */
  10885. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10886. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  10887. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  10888. wc_Sha256Free(&sha256);
  10889. #endif
  10890. return EXPECT_RESULT();
  10891. } /* END test_wc_Sha256GetFlags */
  10892. /*
  10893. * Unit test function for wc_Sha256Free()
  10894. */
  10895. static int test_wc_Sha256Free(void)
  10896. {
  10897. EXPECT_DECLS;
  10898. #ifndef NO_SHA256
  10899. wc_Sha256Free(NULL);
  10900. /* Set result to SUCCESS. */
  10901. ExpectTrue(1);
  10902. #endif
  10903. return EXPECT_RESULT();
  10904. } /* END test_wc_Sha256Free */
  10905. /*
  10906. * Unit test function for wc_Sha256GetHash()
  10907. */
  10908. static int test_wc_Sha256GetHash(void)
  10909. {
  10910. EXPECT_DECLS;
  10911. #ifndef NO_SHA256
  10912. wc_Sha256 sha256;
  10913. byte hash1[WC_SHA256_DIGEST_SIZE];
  10914. /* Initialize */
  10915. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10916. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  10917. /* test bad arguments*/
  10918. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  10919. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  10920. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  10921. wc_Sha256Free(&sha256);
  10922. #endif
  10923. return EXPECT_RESULT();
  10924. } /* END test_wc_Sha256GetHash */
  10925. /*
  10926. * Unit test function for wc_Sha256Copy()
  10927. */
  10928. static int test_wc_Sha256Copy(void)
  10929. {
  10930. EXPECT_DECLS;
  10931. #ifndef NO_SHA256
  10932. wc_Sha256 sha256;
  10933. wc_Sha256 temp;
  10934. XMEMSET(&sha256, 0, sizeof(sha256));
  10935. XMEMSET(&temp, 0, sizeof(temp));
  10936. /* Initialize */
  10937. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  10938. ExpectIntEQ(wc_InitSha256(&temp), 0);
  10939. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  10940. /* test bad arguments*/
  10941. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  10942. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  10943. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  10944. wc_Sha256Free(&sha256);
  10945. wc_Sha256Free(&temp);
  10946. #endif
  10947. return EXPECT_RESULT();
  10948. } /* END test_wc_Sha256Copy */
  10949. /*
  10950. * Testing wc_InitSha512()
  10951. */
  10952. static int test_wc_InitSha512(void)
  10953. {
  10954. EXPECT_DECLS;
  10955. #ifdef WOLFSSL_SHA512
  10956. wc_Sha512 sha512;
  10957. /* Test good arg. */
  10958. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  10959. /* Test bad arg. */
  10960. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  10961. wc_Sha512Free(&sha512);
  10962. #endif
  10963. return EXPECT_RESULT();
  10964. } /* END test_wc_InitSha512 */
  10965. /*
  10966. * wc_Sha512Update() test.
  10967. */
  10968. static int test_wc_Sha512Update(void)
  10969. {
  10970. EXPECT_DECLS;
  10971. #ifdef WOLFSSL_SHA512
  10972. wc_Sha512 sha512;
  10973. byte hash[WC_SHA512_DIGEST_SIZE];
  10974. testVector a, b, c;
  10975. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  10976. /* Input. */
  10977. a.input = "a";
  10978. a.inLen = XSTRLEN(a.input);
  10979. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  10980. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  10981. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  10982. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  10983. /* Update input. */
  10984. a.input = "abc";
  10985. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  10986. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  10987. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  10988. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  10989. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  10990. a.inLen = XSTRLEN(a.input);
  10991. a.outLen = XSTRLEN(a.output);
  10992. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  10993. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  10994. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  10995. /* Try passing in bad values */
  10996. b.input = NULL;
  10997. b.inLen = 0;
  10998. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  10999. c.input = NULL;
  11000. c.inLen = WC_SHA512_DIGEST_SIZE;
  11001. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11002. BAD_FUNC_ARG);
  11003. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  11004. BAD_FUNC_ARG);
  11005. wc_Sha512Free(&sha512);
  11006. #endif
  11007. return EXPECT_RESULT();
  11008. } /* END test_wc_Sha512Update */
  11009. #ifdef WOLFSSL_SHA512
  11010. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11011. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11012. /* Performs test for
  11013. * - wc_Sha512Final/wc_Sha512FinalRaw
  11014. * - wc_Sha512_224Final/wc_Sha512_224Final
  11015. * - wc_Sha512_256Final/wc_Sha512_256Final
  11016. * parameter:
  11017. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  11018. * WC_HASH_TYPE_SHA512_256
  11019. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  11020. *return 0 on success
  11021. */
  11022. static int test_Sha512_Family_Final(int type, int isRaw)
  11023. {
  11024. EXPECT_DECLS;
  11025. wc_Sha512 sha512;
  11026. byte* hash_test[3];
  11027. byte hash1[WC_SHA512_DIGEST_SIZE];
  11028. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11029. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11030. int times, i;
  11031. int(*initFp)(wc_Sha512*);
  11032. int(*finalFp)(wc_Sha512*, byte*);
  11033. void(*freeFp)(wc_Sha512*);
  11034. if (type == WC_HASH_TYPE_SHA512) {
  11035. initFp = wc_InitSha512;
  11036. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11037. !defined(WOLFSSL_NO_HASH_RAW)
  11038. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  11039. #else
  11040. finalFp = (isRaw)? NULL : wc_Sha512Final;
  11041. #endif
  11042. freeFp = wc_Sha512Free;
  11043. }
  11044. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11045. #if !defined(WOLFSSL_NOSHA512_224)
  11046. else if (type == WC_HASH_TYPE_SHA512_224) {
  11047. initFp = wc_InitSha512_224;
  11048. #if !defined(WOLFSSL_NO_HASH_RAW)
  11049. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  11050. #else
  11051. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  11052. #endif
  11053. freeFp = wc_Sha512_224Free;
  11054. }
  11055. #endif
  11056. #if !defined(WOLFSSL_NOSHA512_256)
  11057. else if (type == WC_HASH_TYPE_SHA512_256) {
  11058. initFp = wc_InitSha512_256;
  11059. #if !defined(WOLFSSL_NO_HASH_RAW)
  11060. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  11061. #else
  11062. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  11063. #endif
  11064. freeFp = wc_Sha512_256Free;
  11065. }
  11066. #endif
  11067. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11068. else
  11069. return TEST_FAIL;
  11070. /* Initialize */
  11071. ExpectIntEQ(initFp(&sha512), 0);
  11072. hash_test[0] = hash1;
  11073. hash_test[1] = hash2;
  11074. hash_test[2] = hash3;
  11075. times = sizeof(hash_test) / sizeof(byte *);
  11076. /* Good test args. */
  11077. for (i = 0; i < times; i++) {
  11078. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  11079. }
  11080. /* Test bad args. */
  11081. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  11082. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  11083. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  11084. freeFp(&sha512);
  11085. return EXPECT_RESULT();
  11086. }
  11087. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11088. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11089. #endif /* WOLFSSL_SHA512 */
  11090. /*
  11091. * Unit test function for wc_Sha512Final()
  11092. */
  11093. static int test_wc_Sha512Final(void)
  11094. {
  11095. EXPECT_DECLS;
  11096. #ifdef WOLFSSL_SHA512
  11097. wc_Sha512 sha512;
  11098. byte* hash_test[3];
  11099. byte hash1[WC_SHA512_DIGEST_SIZE];
  11100. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11101. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11102. int times, i;
  11103. /* Initialize */
  11104. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11105. hash_test[0] = hash1;
  11106. hash_test[1] = hash2;
  11107. hash_test[2] = hash3;
  11108. times = sizeof(hash_test) / sizeof(byte *);
  11109. for (i = 0; i < times; i++) {
  11110. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  11111. }
  11112. /* Test bad args. */
  11113. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  11114. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  11115. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  11116. wc_Sha512Free(&sha512);
  11117. #endif
  11118. return EXPECT_RESULT();
  11119. } /* END test_wc_Sha512Final */
  11120. /*
  11121. * Unit test function for wc_Sha512GetFlags()
  11122. */
  11123. static int test_wc_Sha512GetFlags(void)
  11124. {
  11125. EXPECT_DECLS;
  11126. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  11127. wc_Sha512 sha512;
  11128. word32 flags = 0;
  11129. /* Initialize */
  11130. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11131. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  11132. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  11133. wc_Sha512Free(&sha512);
  11134. #endif
  11135. return EXPECT_RESULT();
  11136. } /* END test_wc_Sha512GetFlags */
  11137. /*
  11138. * Unit test function for wc_Sha512FinalRaw()
  11139. */
  11140. static int test_wc_Sha512FinalRaw(void)
  11141. {
  11142. EXPECT_DECLS;
  11143. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11144. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11145. !defined(WOLFSSL_NO_HASH_RAW)
  11146. wc_Sha512 sha512;
  11147. byte* hash_test[3];
  11148. byte hash1[WC_SHA512_DIGEST_SIZE];
  11149. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  11150. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  11151. int times, i;
  11152. /* Initialize */
  11153. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11154. hash_test[0] = hash1;
  11155. hash_test[1] = hash2;
  11156. hash_test[2] = hash3;
  11157. times = sizeof(hash_test) / sizeof(byte*);
  11158. /* Good test args. */
  11159. for (i = 0; i < times; i++) {
  11160. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  11161. }
  11162. /* Test bad args. */
  11163. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11164. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11165. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  11166. wc_Sha512Free(&sha512);
  11167. #endif
  11168. return EXPECT_RESULT();
  11169. } /* END test_wc_Sha512FinalRaw */
  11170. /*
  11171. * Unit test function for wc_Sha512Free()
  11172. */
  11173. static int test_wc_Sha512Free(void)
  11174. {
  11175. EXPECT_DECLS;
  11176. #ifdef WOLFSSL_SHA512
  11177. wc_Sha512Free(NULL);
  11178. /* Set result to SUCCESS. */
  11179. ExpectTrue(1);
  11180. #endif
  11181. return EXPECT_RESULT();
  11182. } /* END test_wc_Sha512Free */
  11183. #ifdef WOLFSSL_SHA512
  11184. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11185. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  11186. static int test_Sha512_Family_GetHash(int type )
  11187. {
  11188. EXPECT_DECLS;
  11189. int(*initFp)(wc_Sha512*);
  11190. int(*ghashFp)(wc_Sha512*, byte*);
  11191. wc_Sha512 sha512;
  11192. byte hash1[WC_SHA512_DIGEST_SIZE];
  11193. if (type == WC_HASH_TYPE_SHA512) {
  11194. initFp = wc_InitSha512;
  11195. ghashFp = wc_Sha512GetHash;
  11196. }
  11197. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11198. #if !defined(WOLFSSL_NOSHA512_224)
  11199. else if (type == WC_HASH_TYPE_SHA512_224) {
  11200. initFp = wc_InitSha512_224;
  11201. ghashFp = wc_Sha512_224GetHash;
  11202. }
  11203. #endif
  11204. #if !defined(WOLFSSL_NOSHA512_256)
  11205. else if (type == WC_HASH_TYPE_SHA512_256) {
  11206. initFp = wc_InitSha512_256;
  11207. ghashFp = wc_Sha512_256GetHash;
  11208. }
  11209. #endif
  11210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11211. else {
  11212. initFp = NULL;
  11213. ghashFp = NULL;
  11214. }
  11215. if (initFp == NULL || ghashFp == NULL)
  11216. return TEST_FAIL;
  11217. ExpectIntEQ(initFp(&sha512), 0);
  11218. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  11219. /* test bad arguments*/
  11220. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  11221. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  11222. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  11223. wc_Sha512Free(&sha512);
  11224. return EXPECT_RESULT();
  11225. }
  11226. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  11227. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  11228. #endif /* WOLFSSL_SHA512 */
  11229. /*
  11230. * Unit test function for wc_Sha512GetHash()
  11231. */
  11232. static int test_wc_Sha512GetHash(void)
  11233. {
  11234. EXPECT_DECLS;
  11235. #ifdef WOLFSSL_SHA512
  11236. wc_Sha512 sha512;
  11237. byte hash1[WC_SHA512_DIGEST_SIZE];
  11238. /* Initialize */
  11239. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11240. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  11241. /* test bad arguments*/
  11242. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  11243. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  11244. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  11245. wc_Sha512Free(&sha512);
  11246. #endif
  11247. return EXPECT_RESULT();
  11248. } /* END test_wc_Sha512GetHash */
  11249. /*
  11250. * Unit test function for wc_Sha512Copy()
  11251. */
  11252. static int test_wc_Sha512Copy(void)
  11253. {
  11254. EXPECT_DECLS;
  11255. #ifdef WOLFSSL_SHA512
  11256. wc_Sha512 sha512;
  11257. wc_Sha512 temp;
  11258. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11259. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11260. /* Initialize */
  11261. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11262. ExpectIntEQ(wc_InitSha512(&temp), 0);
  11263. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  11264. /* test bad arguments*/
  11265. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  11266. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  11267. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  11268. wc_Sha512Free(&sha512);
  11269. wc_Sha512Free(&temp);
  11270. #endif
  11271. return EXPECT_RESULT();
  11272. } /* END test_wc_Sha512Copy */
  11273. static int test_wc_InitSha512_224(void)
  11274. {
  11275. EXPECT_DECLS;
  11276. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11277. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11278. wc_Sha512 sha512;
  11279. /* Test good arg. */
  11280. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11281. /* Test bad arg. */
  11282. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  11283. wc_Sha512_224Free(&sha512);
  11284. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11285. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11286. return EXPECT_RESULT();
  11287. }
  11288. static int test_wc_Sha512_224Update(void)
  11289. {
  11290. EXPECT_DECLS;
  11291. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11292. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11293. wc_Sha512 sha512;
  11294. byte hash[WC_SHA512_DIGEST_SIZE];
  11295. testVector a, c;
  11296. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11297. /* Input. */
  11298. a.input = "a";
  11299. a.inLen = XSTRLEN(a.input);
  11300. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  11301. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  11302. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11303. 0);
  11304. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11305. /* Update input. */
  11306. a.input = "abc";
  11307. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  11308. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  11309. a.inLen = XSTRLEN(a.input);
  11310. a.outLen = XSTRLEN(a.output);
  11311. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11312. 0);
  11313. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  11314. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  11315. c.input = NULL;
  11316. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  11317. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11318. BAD_FUNC_ARG);
  11319. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11320. BAD_FUNC_ARG);
  11321. wc_Sha512_224Free(&sha512);
  11322. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11323. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11324. return EXPECT_RESULT();
  11325. }
  11326. static int test_wc_Sha512_224Final(void)
  11327. {
  11328. EXPECT_DECLS;
  11329. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11330. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11331. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  11332. TEST_SUCCESS);
  11333. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  11334. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11335. return EXPECT_RESULT();
  11336. }
  11337. static int test_wc_Sha512_224GetFlags(void)
  11338. {
  11339. EXPECT_DECLS;
  11340. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11341. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  11342. wc_Sha512 sha512;
  11343. wc_Sha512 copy;
  11344. word32 flags = 0;
  11345. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11346. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11347. /* Initialize */
  11348. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11349. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  11350. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  11351. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11352. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  11353. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  11354. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11355. wc_Sha512_224Free(&copy);
  11356. wc_Sha512_224Free(&sha512);
  11357. #endif
  11358. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11359. return EXPECT_RESULT();
  11360. }
  11361. static int test_wc_Sha512_224FinalRaw(void)
  11362. {
  11363. EXPECT_DECLS;
  11364. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11365. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  11366. !defined(WOLFSSL_NO_HASH_RAW)
  11367. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  11368. TEST_SUCCESS);
  11369. #endif
  11370. return EXPECT_RESULT();
  11371. }
  11372. static int test_wc_Sha512_224Free(void)
  11373. {
  11374. EXPECT_DECLS;
  11375. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11376. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11377. wc_Sha512_224Free(NULL);
  11378. /* Set result to SUCCESS. */
  11379. ExpectTrue(1);
  11380. #endif
  11381. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11382. return EXPECT_RESULT();
  11383. }
  11384. static int test_wc_Sha512_224GetHash(void)
  11385. {
  11386. EXPECT_DECLS;
  11387. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11388. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11389. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  11390. TEST_SUCCESS);
  11391. #endif
  11392. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11393. return EXPECT_RESULT();
  11394. }
  11395. static int test_wc_Sha512_224Copy(void)
  11396. {
  11397. EXPECT_DECLS;
  11398. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11399. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  11400. wc_Sha512 sha512;
  11401. wc_Sha512 temp;
  11402. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11403. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11404. /* Initialize */
  11405. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  11406. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  11407. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  11408. /* test bad arguments*/
  11409. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  11410. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  11411. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  11412. wc_Sha512_224Free(&sha512);
  11413. wc_Sha512_224Free(&temp);
  11414. #endif
  11415. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11416. return EXPECT_RESULT();
  11417. }
  11418. static int test_wc_InitSha512_256(void)
  11419. {
  11420. EXPECT_DECLS;
  11421. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11422. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11423. wc_Sha512 sha512;
  11424. /* Test good arg. */
  11425. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11426. /* Test bad arg. */
  11427. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  11428. wc_Sha512_256Free(&sha512);
  11429. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11430. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11431. return EXPECT_RESULT();
  11432. }
  11433. static int test_wc_Sha512_256Update(void)
  11434. {
  11435. EXPECT_DECLS;
  11436. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11437. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11438. wc_Sha512 sha512;
  11439. byte hash[WC_SHA512_DIGEST_SIZE];
  11440. testVector a, c;
  11441. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11442. /* Input. */
  11443. a.input = "a";
  11444. a.inLen = XSTRLEN(a.input);
  11445. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  11446. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  11447. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  11448. 0);
  11449. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11450. /* Update input. */
  11451. a.input = "abc";
  11452. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  11453. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  11454. "\x07\xe7\xaf\x23";
  11455. a.inLen = XSTRLEN(a.input);
  11456. a.outLen = XSTRLEN(a.output);
  11457. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  11458. 0);
  11459. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  11460. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  11461. c.input = NULL;
  11462. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  11463. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  11464. BAD_FUNC_ARG);
  11465. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11466. BAD_FUNC_ARG);
  11467. wc_Sha512_256Free(&sha512);
  11468. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11469. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11470. return EXPECT_RESULT();
  11471. }
  11472. static int test_wc_Sha512_256Final(void)
  11473. {
  11474. EXPECT_DECLS;
  11475. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11476. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11477. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  11478. TEST_SUCCESS);
  11479. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  11480. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11481. return EXPECT_RESULT();
  11482. }
  11483. static int test_wc_Sha512_256GetFlags(void)
  11484. {
  11485. EXPECT_DECLS;
  11486. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11487. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  11488. wc_Sha512 sha512, copy;
  11489. word32 flags = 0;
  11490. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11491. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  11492. /* Initialize */
  11493. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11494. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  11495. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  11496. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11497. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  11498. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  11499. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  11500. wc_Sha512_256Free(&sha512);
  11501. #endif
  11502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11503. return EXPECT_RESULT();
  11504. }
  11505. static int test_wc_Sha512_256FinalRaw(void)
  11506. {
  11507. EXPECT_DECLS;
  11508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  11509. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  11510. !defined(WOLFSSL_NO_HASH_RAW)
  11511. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  11512. TEST_SUCCESS);
  11513. #endif
  11514. return EXPECT_RESULT();
  11515. }
  11516. static int test_wc_Sha512_256Free(void)
  11517. {
  11518. EXPECT_DECLS;
  11519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11521. wc_Sha512_256Free(NULL);
  11522. /* Set result to SUCCESS. */
  11523. ExpectTrue(1);
  11524. #endif
  11525. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11526. return EXPECT_RESULT();
  11527. }
  11528. static int test_wc_Sha512_256GetHash(void)
  11529. {
  11530. EXPECT_DECLS;
  11531. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11532. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11533. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  11534. TEST_SUCCESS);
  11535. #endif
  11536. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11537. return EXPECT_RESULT();
  11538. }
  11539. static int test_wc_Sha512_256Copy(void)
  11540. {
  11541. EXPECT_DECLS;
  11542. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11543. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  11544. wc_Sha512 sha512;
  11545. wc_Sha512 temp;
  11546. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  11547. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  11548. /* Initialize */
  11549. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  11550. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  11551. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  11552. /* test bad arguments*/
  11553. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  11554. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  11555. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  11556. wc_Sha512_256Free(&sha512);
  11557. wc_Sha512_256Free(&temp);
  11558. #endif
  11559. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  11560. return EXPECT_RESULT();
  11561. }
  11562. /*
  11563. * Testing wc_InitSha384()
  11564. */
  11565. static int test_wc_InitSha384(void)
  11566. {
  11567. EXPECT_DECLS;
  11568. #ifdef WOLFSSL_SHA384
  11569. wc_Sha384 sha384;
  11570. /* Test good arg. */
  11571. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11572. /* Test bad arg. */
  11573. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  11574. wc_Sha384Free(&sha384);
  11575. #endif
  11576. return EXPECT_RESULT();
  11577. } /* END test_wc_InitSha384 */
  11578. /*
  11579. * test wc_Sha384Update()
  11580. */
  11581. static int test_wc_Sha384Update(void)
  11582. {
  11583. EXPECT_DECLS;
  11584. #ifdef WOLFSSL_SHA384
  11585. wc_Sha384 sha384;
  11586. byte hash[WC_SHA384_DIGEST_SIZE];
  11587. testVector a, b, c;
  11588. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11589. /* Input */
  11590. a.input = "a";
  11591. a.inLen = XSTRLEN(a.input);
  11592. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  11593. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  11594. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11595. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11596. /* Update input. */
  11597. a.input = "abc";
  11598. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  11599. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  11600. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  11601. "\xc8\x25\xa7";
  11602. a.inLen = XSTRLEN(a.input);
  11603. a.outLen = XSTRLEN(a.output);
  11604. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  11605. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  11606. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  11607. /* Pass in bad values. */
  11608. b.input = NULL;
  11609. b.inLen = 0;
  11610. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  11611. c.input = NULL;
  11612. c.inLen = WC_SHA384_DIGEST_SIZE;
  11613. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  11614. BAD_FUNC_ARG);
  11615. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  11616. BAD_FUNC_ARG);
  11617. wc_Sha384Free(&sha384);
  11618. #endif
  11619. return EXPECT_RESULT();
  11620. } /* END test_wc_Sha384Update */
  11621. /*
  11622. * Unit test function for wc_Sha384Final();
  11623. */
  11624. static int test_wc_Sha384Final(void)
  11625. {
  11626. EXPECT_DECLS;
  11627. #ifdef WOLFSSL_SHA384
  11628. wc_Sha384 sha384;
  11629. byte* hash_test[3];
  11630. byte hash1[WC_SHA384_DIGEST_SIZE];
  11631. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11632. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11633. int times, i;
  11634. /* Initialize */
  11635. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11636. hash_test[0] = hash1;
  11637. hash_test[1] = hash2;
  11638. hash_test[2] = hash3;
  11639. times = sizeof(hash_test) / sizeof(byte*);
  11640. /* Good test args. */
  11641. for (i = 0; i < times; i++) {
  11642. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  11643. }
  11644. /* Test bad args. */
  11645. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  11646. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  11647. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  11648. wc_Sha384Free(&sha384);
  11649. #endif
  11650. return EXPECT_RESULT();
  11651. } /* END test_wc_Sha384Final */
  11652. /*
  11653. * Unit test function for wc_Sha384GetFlags()
  11654. */
  11655. static int test_wc_Sha384GetFlags(void)
  11656. {
  11657. EXPECT_DECLS;
  11658. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  11659. wc_Sha384 sha384;
  11660. word32 flags = 0;
  11661. /* Initialize */
  11662. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11663. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  11664. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11665. wc_Sha384Free(&sha384);
  11666. #endif
  11667. return EXPECT_RESULT();
  11668. } /* END test_wc_Sha384GetFlags */
  11669. /*
  11670. * Unit test function for wc_Sha384FinalRaw()
  11671. */
  11672. static int test_wc_Sha384FinalRaw(void)
  11673. {
  11674. EXPECT_DECLS;
  11675. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  11676. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  11677. !defined(WOLFSSL_NO_HASH_RAW)
  11678. wc_Sha384 sha384;
  11679. byte* hash_test[3];
  11680. byte hash1[WC_SHA384_DIGEST_SIZE];
  11681. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  11682. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  11683. int times, i;
  11684. /* Initialize */
  11685. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11686. hash_test[0] = hash1;
  11687. hash_test[1] = hash2;
  11688. hash_test[2] = hash3;
  11689. times = sizeof(hash_test) / sizeof(byte*);
  11690. /* Good test args. */
  11691. for (i = 0; i < times; i++) {
  11692. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  11693. }
  11694. /* Test bad args. */
  11695. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11696. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11697. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  11698. wc_Sha384Free(&sha384);
  11699. #endif
  11700. return EXPECT_RESULT();
  11701. } /* END test_wc_Sha384FinalRaw */
  11702. /*
  11703. * Unit test function for wc_Sha384Free()
  11704. */
  11705. static int test_wc_Sha384Free(void)
  11706. {
  11707. EXPECT_DECLS;
  11708. #ifdef WOLFSSL_SHA384
  11709. wc_Sha384Free(NULL);
  11710. /* Set result to SUCCESS. */
  11711. ExpectTrue(1);
  11712. #endif
  11713. return EXPECT_RESULT();
  11714. } /* END test_wc_Sha384Free */
  11715. /*
  11716. * Unit test function for wc_Sha384GetHash()
  11717. */
  11718. static int test_wc_Sha384GetHash(void)
  11719. {
  11720. EXPECT_DECLS;
  11721. #ifdef WOLFSSL_SHA384
  11722. wc_Sha384 sha384;
  11723. byte hash1[WC_SHA384_DIGEST_SIZE];
  11724. /* Initialize */
  11725. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11726. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  11727. /* test bad arguments*/
  11728. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  11729. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  11730. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  11731. wc_Sha384Free(&sha384);
  11732. #endif
  11733. return EXPECT_RESULT();
  11734. } /* END test_wc_Sha384GetHash */
  11735. /*
  11736. * Unit test function for wc_Sha384Copy()
  11737. */
  11738. static int test_wc_Sha384Copy(void)
  11739. {
  11740. EXPECT_DECLS;
  11741. #ifdef WOLFSSL_SHA384
  11742. wc_Sha384 sha384;
  11743. wc_Sha384 temp;
  11744. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  11745. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  11746. /* Initialize */
  11747. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  11748. ExpectIntEQ(wc_InitSha384(&temp), 0);
  11749. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  11750. /* test bad arguments*/
  11751. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  11752. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  11753. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  11754. wc_Sha384Free(&sha384);
  11755. wc_Sha384Free(&temp);
  11756. #endif
  11757. return EXPECT_RESULT();
  11758. } /* END test_wc_Sha384Copy */
  11759. /*
  11760. * Testing wc_InitSha224();
  11761. */
  11762. static int test_wc_InitSha224(void)
  11763. {
  11764. EXPECT_DECLS;
  11765. #ifdef WOLFSSL_SHA224
  11766. wc_Sha224 sha224;
  11767. /* Test good arg. */
  11768. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11769. /* Test bad arg. */
  11770. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  11771. wc_Sha224Free(&sha224);
  11772. #endif
  11773. return EXPECT_RESULT();
  11774. } /* END test_wc_InitSha224 */
  11775. /*
  11776. * Unit test on wc_Sha224Update
  11777. */
  11778. static int test_wc_Sha224Update(void)
  11779. {
  11780. EXPECT_DECLS;
  11781. #ifdef WOLFSSL_SHA224
  11782. wc_Sha224 sha224;
  11783. byte hash[WC_SHA224_DIGEST_SIZE];
  11784. testVector a, b, c;
  11785. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11786. /* Input. */
  11787. a.input = "a";
  11788. a.inLen = XSTRLEN(a.input);
  11789. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  11790. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  11791. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11792. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11793. /* Update input. */
  11794. a.input = "abc";
  11795. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  11796. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  11797. a.inLen = XSTRLEN(a.input);
  11798. a.outLen = XSTRLEN(a.output);
  11799. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  11800. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  11801. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  11802. /* Pass in bad values. */
  11803. b.input = NULL;
  11804. b.inLen = 0;
  11805. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  11806. c.input = NULL;
  11807. c.inLen = WC_SHA224_DIGEST_SIZE;
  11808. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  11809. BAD_FUNC_ARG);
  11810. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  11811. BAD_FUNC_ARG);
  11812. wc_Sha224Free(&sha224);
  11813. #endif
  11814. return EXPECT_RESULT();
  11815. } /* END test_wc_Sha224Update */
  11816. /*
  11817. * Unit test for wc_Sha224Final();
  11818. */
  11819. static int test_wc_Sha224Final(void)
  11820. {
  11821. EXPECT_DECLS;
  11822. #ifdef WOLFSSL_SHA224
  11823. wc_Sha224 sha224;
  11824. byte* hash_test[3];
  11825. byte hash1[WC_SHA224_DIGEST_SIZE];
  11826. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11827. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11828. int times, i;
  11829. /* Initialize */
  11830. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11831. hash_test[0] = hash1;
  11832. hash_test[1] = hash2;
  11833. hash_test[2] = hash3;
  11834. times = sizeof(hash_test) / sizeof(byte*);
  11835. /* Good test args. */
  11836. /* Testing oversized buffers. */
  11837. for (i = 0; i < times; i++) {
  11838. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  11839. }
  11840. /* Test bad args. */
  11841. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  11842. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  11843. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  11844. wc_Sha224Free(&sha224);
  11845. #endif
  11846. return EXPECT_RESULT();
  11847. } /* END test_wc_Sha224Final */
  11848. /*
  11849. * Unit test function for wc_Sha224SetFlags()
  11850. */
  11851. static int test_wc_Sha224SetFlags(void)
  11852. {
  11853. EXPECT_DECLS;
  11854. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11855. wc_Sha224 sha224;
  11856. word32 flags = WC_HASH_FLAG_WILLCOPY;
  11857. /* Initialize */
  11858. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11859. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  11860. flags = 0;
  11861. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11862. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  11863. wc_Sha224Free(&sha224);
  11864. #endif
  11865. return EXPECT_RESULT();
  11866. } /* END test_wc_Sha224SetFlags */
  11867. /*
  11868. * Unit test function for wc_Sha224GetFlags()
  11869. */
  11870. static int test_wc_Sha224GetFlags(void)
  11871. {
  11872. EXPECT_DECLS;
  11873. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11874. wc_Sha224 sha224;
  11875. word32 flags = 0;
  11876. /* Initialize */
  11877. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11878. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  11879. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11880. wc_Sha224Free(&sha224);
  11881. #endif
  11882. return EXPECT_RESULT();
  11883. } /* END test_wc_Sha224GetFlags */
  11884. /*
  11885. * Unit test function for wc_Sha224Free()
  11886. */
  11887. static int test_wc_Sha224Free(void)
  11888. {
  11889. EXPECT_DECLS;
  11890. #ifdef WOLFSSL_SHA224
  11891. wc_Sha224Free(NULL);
  11892. /* Set result to SUCCESS. */
  11893. ExpectTrue(1);
  11894. #endif
  11895. return EXPECT_RESULT();
  11896. } /* END test_wc_Sha224Free */
  11897. /*
  11898. * Unit test function for wc_Sha224GetHash()
  11899. */
  11900. static int test_wc_Sha224GetHash(void)
  11901. {
  11902. EXPECT_DECLS;
  11903. #ifdef WOLFSSL_SHA224
  11904. wc_Sha224 sha224;
  11905. byte hash1[WC_SHA224_DIGEST_SIZE];
  11906. /* Initialize */
  11907. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11908. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  11909. /* test bad arguments*/
  11910. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  11911. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  11912. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  11913. wc_Sha224Free(&sha224);
  11914. #endif
  11915. return EXPECT_RESULT();
  11916. } /* END test_wc_Sha224GetHash */
  11917. /*
  11918. * Unit test function for wc_Sha224Copy()
  11919. */
  11920. static int test_wc_Sha224Copy(void)
  11921. {
  11922. EXPECT_DECLS;
  11923. #ifdef WOLFSSL_SHA224
  11924. wc_Sha224 sha224;
  11925. wc_Sha224 temp;
  11926. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  11927. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  11928. /* Initialize */
  11929. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  11930. ExpectIntEQ(wc_InitSha224(&temp), 0);
  11931. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  11932. /* test bad arguments*/
  11933. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  11934. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  11935. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  11936. wc_Sha224Free(&sha224);
  11937. wc_Sha224Free(&temp);
  11938. #endif
  11939. return EXPECT_RESULT();
  11940. } /* END test_wc_Sha224Copy */
  11941. /*
  11942. * Testing wc_InitRipeMd()
  11943. */
  11944. static int test_wc_InitRipeMd(void)
  11945. {
  11946. EXPECT_DECLS;
  11947. #ifdef WOLFSSL_RIPEMD
  11948. RipeMd ripemd;
  11949. /* Test good arg. */
  11950. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  11951. /* Test bad arg. */
  11952. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  11953. #endif
  11954. return EXPECT_RESULT();
  11955. } /* END test_wc_InitRipeMd */
  11956. /*
  11957. * Testing wc_RipeMdUpdate()
  11958. */
  11959. static int test_wc_RipeMdUpdate(void)
  11960. {
  11961. EXPECT_DECLS;
  11962. #ifdef WOLFSSL_RIPEMD
  11963. RipeMd ripemd;
  11964. byte hash[RIPEMD_DIGEST_SIZE];
  11965. testVector a, b, c;
  11966. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  11967. /* Input */
  11968. a.input = "a";
  11969. a.inLen = XSTRLEN(a.input);
  11970. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  11971. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  11972. /* Update input. */
  11973. a.input = "abc";
  11974. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11975. "\xb0\x87\xf1\x5a\x0b\xfc";
  11976. a.inLen = XSTRLEN(a.input);
  11977. a.outLen = XSTRLEN(a.output);
  11978. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  11979. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  11980. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  11981. /* Pass in bad values. */
  11982. b.input = NULL;
  11983. b.inLen = 0;
  11984. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  11985. c.input = NULL;
  11986. c.inLen = RIPEMD_DIGEST_SIZE;
  11987. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  11988. BAD_FUNC_ARG);
  11989. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11990. BAD_FUNC_ARG);
  11991. #endif
  11992. return EXPECT_RESULT();
  11993. } /* END test_wc_RipeMdUdpate */
  11994. /*
  11995. * Unit test function for wc_RipeMdFinal()
  11996. */
  11997. static int test_wc_RipeMdFinal(void)
  11998. {
  11999. EXPECT_DECLS;
  12000. #ifdef WOLFSSL_RIPEMD
  12001. RipeMd ripemd;
  12002. byte* hash_test[3];
  12003. byte hash1[RIPEMD_DIGEST_SIZE];
  12004. byte hash2[2*RIPEMD_DIGEST_SIZE];
  12005. byte hash3[5*RIPEMD_DIGEST_SIZE];
  12006. int times, i;
  12007. /* Initialize */
  12008. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12009. hash_test[0] = hash1;
  12010. hash_test[1] = hash2;
  12011. hash_test[2] = hash3;
  12012. times = sizeof(hash_test) / sizeof(byte*);
  12013. /* Testing oversized buffers. */
  12014. for (i = 0; i < times; i++) {
  12015. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  12016. }
  12017. /* Test bad args. */
  12018. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  12019. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  12020. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  12021. #endif
  12022. return EXPECT_RESULT();
  12023. } /* END test_wc_RipeMdFinal */
  12024. /*
  12025. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  12026. * wc_InitSha3_512
  12027. */
  12028. static int test_wc_InitSha3(void)
  12029. {
  12030. EXPECT_DECLS;
  12031. #if defined(WOLFSSL_SHA3)
  12032. wc_Sha3 sha3;
  12033. (void)sha3;
  12034. #if !defined(WOLFSSL_NOSHA3_224)
  12035. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12036. /* Test bad args. */
  12037. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12038. wc_Sha3_224_Free(&sha3);
  12039. #endif /* NOSHA3_224 */
  12040. #if !defined(WOLFSSL_NOSHA3_256)
  12041. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12042. /* Test bad args. */
  12043. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12044. wc_Sha3_256_Free(&sha3);
  12045. #endif /* NOSHA3_256 */
  12046. #if !defined(WOLFSSL_NOSHA3_384)
  12047. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12048. /* Test bad args. */
  12049. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12050. wc_Sha3_384_Free(&sha3);
  12051. #endif /* NOSHA3_384 */
  12052. #if !defined(WOLFSSL_NOSHA3_512)
  12053. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12054. /* Test bad args. */
  12055. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12056. wc_Sha3_512_Free(&sha3);
  12057. #endif /* NOSHA3_512 */
  12058. #endif
  12059. return EXPECT_RESULT();
  12060. } /* END test_wc_InitSha3 */
  12061. /*
  12062. * Testing wc_Sha3_Update()
  12063. */
  12064. static int testing_wc_Sha3_Update(void)
  12065. {
  12066. EXPECT_DECLS;
  12067. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  12068. !defined(WOLFSSL_AFALG_XILINX)
  12069. wc_Sha3 sha3;
  12070. byte msg[] = "Everybody's working for the weekend.";
  12071. byte msg2[] = "Everybody gets Friday off.";
  12072. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12073. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12074. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12075. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12076. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12077. word32 msglen = sizeof(msg) - 1;
  12078. word32 msg2len = sizeof(msg2);
  12079. word32 msgCmplen = sizeof(msgCmp);
  12080. #if !defined(WOLFSSL_NOSHA3_224)
  12081. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12082. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  12083. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12084. ExpectTrue(sha3.i == msglen);
  12085. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12086. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12087. /* Pass bad args. */
  12088. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12089. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12090. wc_Sha3_224_Free(&sha3);
  12091. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12092. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  12093. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  12094. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12095. wc_Sha3_224_Free(&sha3);
  12096. #endif /* SHA3_224 */
  12097. #if !defined(WOLFSSL_NOSHA3_256)
  12098. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12099. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  12100. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12101. ExpectTrue(sha3.i == msglen);
  12102. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12103. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12104. /* Pass bad args. */
  12105. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12106. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12107. wc_Sha3_256_Free(&sha3);
  12108. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12109. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  12110. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  12111. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12112. wc_Sha3_256_Free(&sha3);
  12113. #endif /* SHA3_256 */
  12114. #if !defined(WOLFSSL_NOSHA3_384)
  12115. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12116. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  12117. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12118. ExpectTrue(sha3.i == msglen);
  12119. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12120. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12121. /* Pass bad args. */
  12122. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12123. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12124. wc_Sha3_384_Free(&sha3);
  12125. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12126. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  12127. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  12128. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12129. wc_Sha3_384_Free(&sha3);
  12130. #endif /* SHA3_384 */
  12131. #if !defined(WOLFSSL_NOSHA3_512)
  12132. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12133. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  12134. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  12135. ExpectTrue(sha3.i == msglen);
  12136. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12137. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  12138. /* Pass bad args. */
  12139. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12140. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  12141. wc_Sha3_512_Free(&sha3);
  12142. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12143. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  12144. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  12145. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  12146. wc_Sha3_512_Free(&sha3);
  12147. #endif /* SHA3_512 */
  12148. #endif /* WOLFSSL_SHA3 */
  12149. return EXPECT_RESULT();
  12150. } /* END testing_wc_Sha3_Update */
  12151. /*
  12152. * Testing wc_Sha3_224_Final()
  12153. */
  12154. static int test_wc_Sha3_224_Final(void)
  12155. {
  12156. EXPECT_DECLS;
  12157. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12158. wc_Sha3 sha3;
  12159. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12160. "nopnopq";
  12161. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  12162. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  12163. "\x64\xea\xd0\xfc\xce\x33";
  12164. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12165. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  12166. /* Init stack variables. */
  12167. XMEMSET(hash, 0, sizeof(hash));
  12168. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12169. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12170. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12171. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  12172. /* Test bad args. */
  12173. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  12174. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  12175. wc_Sha3_224_Free(&sha3);
  12176. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12177. /* Init stack variables. */
  12178. XMEMSET(hash, 0, sizeof(hash));
  12179. XMEMSET(hashRet, 0, sizeof(hashRet));
  12180. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12181. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  12182. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12183. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  12184. /* Test bad args. */
  12185. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12186. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12187. wc_Sha3_224_Free(&sha3);
  12188. #endif
  12189. return EXPECT_RESULT();
  12190. } /* END test_wc_Sha3_224_Final */
  12191. /*
  12192. * Testing wc_Sha3_256_Final()
  12193. */
  12194. static int test_wc_Sha3_256_Final(void)
  12195. {
  12196. EXPECT_DECLS;
  12197. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12198. wc_Sha3 sha3;
  12199. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12200. "nopnopq";
  12201. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  12202. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  12203. "\xdd\x97\x49\x6d\x33\x76";
  12204. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12205. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  12206. /* Init stack variables. */
  12207. XMEMSET(hash, 0, sizeof(hash));
  12208. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12209. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12210. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12211. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  12212. /* Test bad args. */
  12213. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  12214. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  12215. wc_Sha3_256_Free(&sha3);
  12216. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12217. /* Init stack variables. */
  12218. XMEMSET(hash, 0, sizeof(hash));
  12219. XMEMSET(hashRet, 0, sizeof(hashRet));
  12220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12221. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  12222. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12223. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  12224. /* Test bad args. */
  12225. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12226. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12227. wc_Sha3_256_Free(&sha3);
  12228. #endif
  12229. return EXPECT_RESULT();
  12230. } /* END test_wc_Sha3_256_Final */
  12231. /*
  12232. * Testing wc_Sha3_384_Final()
  12233. */
  12234. static int test_wc_Sha3_384_Final(void)
  12235. {
  12236. EXPECT_DECLS;
  12237. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12238. wc_Sha3 sha3;
  12239. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12240. "nopnopq";
  12241. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  12242. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  12243. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  12244. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  12245. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12246. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  12247. /* Init stack variables. */
  12248. XMEMSET(hash, 0, sizeof(hash));
  12249. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12250. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12251. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12252. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  12253. /* Test bad args. */
  12254. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  12255. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  12256. wc_Sha3_384_Free(&sha3);
  12257. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12258. /* Init stack variables. */
  12259. XMEMSET(hash, 0, sizeof(hash));
  12260. XMEMSET(hashRet, 0, sizeof(hashRet));
  12261. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12262. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  12263. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12264. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  12265. /* Test bad args. */
  12266. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12267. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12268. wc_Sha3_384_Free(&sha3);
  12269. #endif
  12270. return EXPECT_RESULT();
  12271. } /* END test_wc_Sha3_384_Final */
  12272. /*
  12273. * Testing wc_Sha3_512_Final()
  12274. */
  12275. static int test_wc_Sha3_512_Final(void)
  12276. {
  12277. EXPECT_DECLS;
  12278. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  12279. !defined(WOLFSSL_NOSHA3_384)
  12280. wc_Sha3 sha3;
  12281. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12282. "nopnopq";
  12283. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  12284. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  12285. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  12286. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  12287. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  12288. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12289. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  12290. /* Init stack variables. */
  12291. XMEMSET(hash, 0, sizeof(hash));
  12292. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12293. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12294. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12295. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  12296. /* Test bad args. */
  12297. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  12298. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  12299. wc_Sha3_512_Free(&sha3);
  12300. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12301. /* Init stack variables. */
  12302. XMEMSET(hash, 0, sizeof(hash));
  12303. XMEMSET(hashRet, 0, sizeof(hashRet));
  12304. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  12305. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  12306. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12307. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  12308. /* Test bad args. */
  12309. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  12310. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  12311. wc_Sha3_512_Free(&sha3);
  12312. #endif
  12313. return EXPECT_RESULT();
  12314. } /* END test_wc_Sha3_512_Final */
  12315. /*
  12316. * Testing wc_Sha3_224_Copy()
  12317. */
  12318. static int test_wc_Sha3_224_Copy(void)
  12319. {
  12320. EXPECT_DECLS;
  12321. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  12322. wc_Sha3 sha3, sha3Cpy;
  12323. const char* msg = TEST_STRING;
  12324. word32 msglen = (word32)TEST_STRING_SZ;
  12325. byte hash[WC_SHA3_224_DIGEST_SIZE];
  12326. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  12327. XMEMSET(hash, 0, sizeof(hash));
  12328. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12329. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12330. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12331. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12332. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12333. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  12334. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  12335. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  12336. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  12337. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12338. /* Test bad args. */
  12339. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12340. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12341. wc_Sha3_224_Free(&sha3);
  12342. wc_Sha3_224_Free(&sha3Cpy);
  12343. #endif
  12344. return EXPECT_RESULT();
  12345. } /* END test_wc_Sha3_224_Copy */
  12346. /*
  12347. * Testing wc_Sha3_256_Copy()
  12348. */
  12349. static int test_wc_Sha3_256_Copy(void)
  12350. {
  12351. EXPECT_DECLS;
  12352. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12353. wc_Sha3 sha3, sha3Cpy;
  12354. const char* msg = TEST_STRING;
  12355. word32 msglen = (word32)TEST_STRING_SZ;
  12356. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12357. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12358. XMEMSET(hash, 0, sizeof(hash));
  12359. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12360. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12361. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12362. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  12363. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12364. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  12365. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  12366. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  12367. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  12368. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12369. /* Test bad args. */
  12370. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12371. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12372. wc_Sha3_256_Free(&sha3);
  12373. wc_Sha3_256_Free(&sha3Cpy);
  12374. #endif
  12375. return EXPECT_RESULT();
  12376. } /* END test_wc_Sha3_256_Copy */
  12377. /*
  12378. * Testing wc_Sha3_384_Copy()
  12379. */
  12380. static int test_wc_Sha3_384_Copy(void)
  12381. {
  12382. EXPECT_DECLS;
  12383. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12384. wc_Sha3 sha3, sha3Cpy;
  12385. const char* msg = TEST_STRING;
  12386. word32 msglen = (word32)TEST_STRING_SZ;
  12387. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12388. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12389. XMEMSET(hash, 0, sizeof(hash));
  12390. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12391. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12392. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12393. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  12394. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12395. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  12396. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  12397. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  12398. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  12399. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12400. /* Test bad args. */
  12401. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12402. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12403. wc_Sha3_384_Free(&sha3);
  12404. wc_Sha3_384_Free(&sha3Cpy);
  12405. #endif
  12406. return EXPECT_RESULT();
  12407. } /* END test_wc_Sha3_384_Copy */
  12408. /*
  12409. * Testing wc_Sha3_512_Copy()
  12410. */
  12411. static int test_wc_Sha3_512_Copy(void)
  12412. {
  12413. EXPECT_DECLS;
  12414. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12415. wc_Sha3 sha3, sha3Cpy;
  12416. const char* msg = TEST_STRING;
  12417. word32 msglen = (word32)TEST_STRING_SZ;
  12418. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12419. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12420. XMEMSET(hash, 0, sizeof(hash));
  12421. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12422. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  12423. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  12424. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  12425. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  12426. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  12427. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  12428. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  12429. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  12430. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12431. /* Test bad args. */
  12432. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  12433. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  12434. wc_Sha3_512_Free(&sha3);
  12435. wc_Sha3_512_Free(&sha3Cpy);
  12436. #endif
  12437. return EXPECT_RESULT();
  12438. } /* END test_wc_Sha3_512_Copy */
  12439. /*
  12440. * Unit test function for wc_Sha3_GetFlags()
  12441. */
  12442. static int test_wc_Sha3_GetFlags(void)
  12443. {
  12444. EXPECT_DECLS;
  12445. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12446. wc_Sha3 sha3;
  12447. word32 flags = 0;
  12448. /* Initialize */
  12449. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  12450. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  12451. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12452. wc_Sha3_224_Free(&sha3);
  12453. #endif
  12454. return EXPECT_RESULT();
  12455. } /* END test_wc_Sha3_GetFlags */
  12456. static int test_wc_InitShake256(void)
  12457. {
  12458. EXPECT_DECLS;
  12459. #ifdef WOLFSSL_SHAKE256
  12460. wc_Shake shake;
  12461. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12462. /* Test bad args. */
  12463. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  12464. wc_Shake256_Free(&shake);
  12465. #endif
  12466. return EXPECT_RESULT();
  12467. }
  12468. static int testing_wc_Shake256_Update(void)
  12469. {
  12470. EXPECT_DECLS;
  12471. #ifdef WOLFSSL_SHAKE256
  12472. wc_Shake shake;
  12473. byte msg[] = "Everybody's working for the weekend.";
  12474. byte msg2[] = "Everybody gets Friday off.";
  12475. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12476. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12477. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12478. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12479. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12480. word32 msglen = sizeof(msg) - 1;
  12481. word32 msg2len = sizeof(msg2);
  12482. word32 msgCmplen = sizeof(msgCmp);
  12483. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12484. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  12485. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  12486. ExpectTrue(shake.i == msglen);
  12487. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12488. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  12489. /* Pass bad args. */
  12490. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  12491. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  12492. wc_Shake256_Free(&shake);
  12493. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12494. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  12495. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  12496. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  12497. wc_Shake256_Free(&shake);
  12498. #endif /* WOLFSSL_SHAKE256 */
  12499. return EXPECT_RESULT();
  12500. }
  12501. static int test_wc_Shake256_Final(void)
  12502. {
  12503. EXPECT_DECLS;
  12504. #ifdef WOLFSSL_SHAKE256
  12505. wc_Shake shake;
  12506. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12507. "nopnopq";
  12508. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12509. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12510. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12511. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12512. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12513. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12514. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12515. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12516. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12517. byte hash[114];
  12518. /* Init stack variables. */
  12519. XMEMSET(hash, 0, sizeof(hash));
  12520. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12521. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  12522. 0);
  12523. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  12524. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  12525. /* Test bad args. */
  12526. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  12527. BAD_FUNC_ARG);
  12528. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  12529. BAD_FUNC_ARG);
  12530. wc_Shake256_Free(&shake);
  12531. #endif
  12532. return EXPECT_RESULT();
  12533. }
  12534. /*
  12535. * Testing wc_Shake256_Copy()
  12536. */
  12537. static int test_wc_Shake256_Copy(void)
  12538. {
  12539. EXPECT_DECLS;
  12540. #ifdef WOLFSSL_SHAKE256
  12541. wc_Shake shake, shakeCpy;
  12542. const char* msg = TEST_STRING;
  12543. word32 msglen = (word32)TEST_STRING_SZ;
  12544. byte hash[144];
  12545. byte hashCpy[144];
  12546. word32 hashLen = sizeof(hash);
  12547. word32 hashLenCpy = sizeof(hashCpy);
  12548. XMEMSET(hash, 0, sizeof(hash));
  12549. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12550. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  12551. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  12552. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  12553. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  12554. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  12555. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  12556. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  12557. /* Test bad args. */
  12558. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  12559. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  12560. wc_Shake256_Free(&shake);
  12561. wc_Shake256_Free(&shakeCpy);
  12562. #endif
  12563. return EXPECT_RESULT();
  12564. } /* END test_wc_Shake256_Copy */
  12565. /*
  12566. * Unit test function for wc_Shake256Hash()
  12567. */
  12568. static int test_wc_Shake256Hash(void)
  12569. {
  12570. EXPECT_DECLS;
  12571. #ifdef WOLFSSL_SHAKE256
  12572. const byte data[] = { /* Hello World */
  12573. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12574. 0x72,0x6c,0x64
  12575. };
  12576. word32 len = sizeof(data);
  12577. byte hash[144];
  12578. word32 hashLen = sizeof(hash);
  12579. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  12580. #endif
  12581. return EXPECT_RESULT();
  12582. } /* END test_wc_Shake256Hash */
  12583. /*
  12584. * Testing wc_InitSm3(), wc_Sm3Free()
  12585. */
  12586. static int test_wc_InitSm3Free(void)
  12587. {
  12588. EXPECT_DECLS;
  12589. #ifdef WOLFSSL_SM3
  12590. wc_Sm3 sm3;
  12591. /* Invalid Parameters */
  12592. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  12593. /* Valid Parameters */
  12594. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12595. wc_Sm3Free(NULL);
  12596. wc_Sm3Free(&sm3);
  12597. #endif
  12598. return EXPECT_RESULT();
  12599. } /* END test_wc_InitSm3 */
  12600. /*
  12601. * Testing wc_Sm3Update(), wc_Sm3Final()
  12602. */
  12603. static int test_wc_Sm3UpdateFinal(void)
  12604. {
  12605. EXPECT_DECLS;
  12606. #ifdef WOLFSSL_SM3
  12607. wc_Sm3 sm3;
  12608. byte data[WC_SM3_BLOCK_SIZE * 4];
  12609. byte hash[WC_SM3_DIGEST_SIZE];
  12610. byte calcHash[WC_SM3_DIGEST_SIZE];
  12611. byte expHash[WC_SM3_DIGEST_SIZE] = {
  12612. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  12613. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  12614. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  12615. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  12616. };
  12617. word32 chunk;
  12618. word32 i;
  12619. XMEMSET(data, 0, sizeof(data));
  12620. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12621. /* Invalid Parameters */
  12622. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  12623. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  12624. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  12625. /* Valid Parameters */
  12626. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  12627. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12628. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12629. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  12630. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  12631. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  12632. /* Ensure too many bytes for lengths. */
  12633. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  12634. /* Invalid Parameters */
  12635. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  12636. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  12637. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  12638. /* Valid Parameters */
  12639. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12640. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  12641. /* Chunk tests. */
  12642. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12643. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12644. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  12645. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  12646. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  12647. }
  12648. if (i < (word32)sizeof(data)) {
  12649. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  12650. 0);
  12651. }
  12652. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12653. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12654. }
  12655. /* Not testing when the low 32-bit length overflows. */
  12656. wc_Sm3Free(&sm3);
  12657. #endif
  12658. return EXPECT_RESULT();
  12659. } /* END test_wc_Sm3Update */
  12660. /*
  12661. * Testing wc_Sm3GetHash()
  12662. */
  12663. static int test_wc_Sm3GetHash(void)
  12664. {
  12665. EXPECT_DECLS;
  12666. #ifdef WOLFSSL_SM3
  12667. wc_Sm3 sm3;
  12668. byte hash[WC_SM3_DIGEST_SIZE];
  12669. byte calcHash[WC_SM3_DIGEST_SIZE];
  12670. byte data[WC_SM3_BLOCK_SIZE];
  12671. XMEMSET(data, 0, sizeof(data));
  12672. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12673. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12674. /* Invalid Parameters */
  12675. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  12676. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  12677. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  12678. /* Valid Parameters */
  12679. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12680. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12681. /* With update. */
  12682. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  12683. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  12684. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  12685. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  12686. wc_Sm3Free(&sm3);
  12687. #endif
  12688. return EXPECT_RESULT();
  12689. } /* END test_wc_Sm3Update */
  12690. /*
  12691. * Testing wc_Sm3Copy()
  12692. */
  12693. static int test_wc_Sm3Copy(void)
  12694. {
  12695. EXPECT_DECLS;
  12696. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12697. wc_Sm3 sm3;
  12698. wc_Sm3 sm3Copy;
  12699. byte hash[WC_SM3_DIGEST_SIZE];
  12700. byte hashCopy[WC_SM3_DIGEST_SIZE];
  12701. byte data[WC_SM3_BLOCK_SIZE + 1];
  12702. int i;
  12703. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12704. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12705. /* Invalid Parameters */
  12706. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  12707. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  12708. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  12709. /* Valid Parameters */
  12710. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12711. /* Ensure all parts of data updated during hashing are copied. */
  12712. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  12713. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  12714. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12715. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  12716. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  12717. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  12718. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  12719. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  12720. }
  12721. wc_Sm3Free(&sm3Copy);
  12722. wc_Sm3Free(&sm3);
  12723. #endif
  12724. return EXPECT_RESULT();
  12725. } /* END test_wc_Sm3Copy */
  12726. /*
  12727. * Testing wc_Sm3FinalRaw()
  12728. */
  12729. static int test_wc_Sm3FinalRaw(void)
  12730. {
  12731. EXPECT_DECLS;
  12732. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  12733. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  12734. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  12735. !defined(WOLFSSL_NO_HASH_RAW)
  12736. wc_Sm3 sm3;
  12737. byte hash1[WC_SM3_DIGEST_SIZE];
  12738. byte hash2[WC_SM3_DIGEST_SIZE];
  12739. byte hash3[WC_SM3_DIGEST_SIZE];
  12740. byte* hash_test[3] = { hash1, hash2, hash3 };
  12741. int times;
  12742. int i;
  12743. XMEMSET(&sm3, 0, sizeof(sm3));
  12744. /* Initialize */
  12745. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12746. /* Invalid Parameters */
  12747. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12748. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  12749. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12750. times = sizeof(hash_test) / sizeof(byte*);
  12751. for (i = 0; i < times; i++) {
  12752. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  12753. }
  12754. wc_Sm3Free(&sm3);
  12755. #endif
  12756. return EXPECT_RESULT();
  12757. } /* END test_wc_Sm3FinalRaw */
  12758. /*
  12759. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  12760. */
  12761. static int test_wc_Sm3GetSetFlags(void)
  12762. {
  12763. EXPECT_DECLS;
  12764. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12765. wc_Sm3 sm3;
  12766. wc_Sm3 sm3Copy;
  12767. word32 flags = 0;
  12768. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  12769. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  12770. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12771. ExpectIntEQ(flags, 0);
  12772. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  12773. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  12774. ExpectIntEQ(flags, 0);
  12775. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12776. ExpectIntEQ(flags, 0);
  12777. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  12778. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  12779. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  12780. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  12781. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  12782. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  12783. wc_Sm3Free(&sm3Copy);
  12784. wc_Sm3Free(&sm3);
  12785. #endif
  12786. return EXPECT_RESULT();
  12787. } /* END test_wc_Sm3Update */
  12788. /*
  12789. * Testing wc_Sm3Hash()
  12790. */
  12791. static int test_wc_Sm3Hash(void)
  12792. {
  12793. EXPECT_DECLS;
  12794. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  12795. byte data[WC_SM3_BLOCK_SIZE];
  12796. byte hash[WC_SM3_DIGEST_SIZE];
  12797. /* Invalid parameters. */
  12798. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  12799. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  12800. /* Valid parameters. */
  12801. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  12802. #endif
  12803. return EXPECT_RESULT();
  12804. } /* END test_wc_Sm3Hash */
  12805. /*
  12806. * Test function for wc_HmacSetKey
  12807. */
  12808. static int test_wc_Md5HmacSetKey(void)
  12809. {
  12810. EXPECT_DECLS;
  12811. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12812. Hmac hmac;
  12813. int ret, times, itr;
  12814. const char* keys[]=
  12815. {
  12816. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12817. #ifndef HAVE_FIPS
  12818. "Jefe", /* smaller than minimum FIPS key size */
  12819. #endif
  12820. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12821. };
  12822. times = sizeof(keys) / sizeof(char*);
  12823. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12824. for (itr = 0; itr < times; itr++) {
  12825. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12826. (word32)XSTRLEN(keys[itr]));
  12827. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12828. wc_HmacFree(&hmac);
  12829. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12830. #else
  12831. ExpectIntEQ(ret, 0);
  12832. #endif
  12833. }
  12834. /* Bad args. */
  12835. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12836. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12837. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  12838. BAD_FUNC_ARG);
  12839. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12840. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12841. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12842. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12843. ExpectIntEQ(ret, BAD_FUNC_ARG);
  12844. #elif defined(HAVE_FIPS)
  12845. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12846. #else
  12847. ExpectIntEQ(ret, 0);
  12848. #endif
  12849. wc_HmacFree(&hmac);
  12850. #endif
  12851. return EXPECT_RESULT();
  12852. } /* END test_wc_Md5HmacSetKey */
  12853. /*
  12854. * testing wc_HmacSetKey() on wc_Sha hash.
  12855. */
  12856. static int test_wc_ShaHmacSetKey(void)
  12857. {
  12858. EXPECT_DECLS;
  12859. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12860. Hmac hmac;
  12861. int ret, times, itr;
  12862. const char* keys[]=
  12863. {
  12864. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12865. "\x0b\x0b\x0b",
  12866. #ifndef HAVE_FIPS
  12867. "Jefe", /* smaller than minimum FIPS key size */
  12868. #endif
  12869. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12870. "\xAA\xAA\xAA"
  12871. };
  12872. times = sizeof(keys) / sizeof(char*);
  12873. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12874. for (itr = 0; itr < times; itr++) {
  12875. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12876. (word32)XSTRLEN(keys[itr])), 0);
  12877. }
  12878. /* Bad args. */
  12879. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12880. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12881. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  12882. BAD_FUNC_ARG);
  12883. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12884. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12885. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12886. #ifdef HAVE_FIPS
  12887. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12888. #else
  12889. ExpectIntEQ(ret, 0);
  12890. #endif
  12891. wc_HmacFree(&hmac);
  12892. #endif
  12893. return EXPECT_RESULT();
  12894. } /* END test_wc_ShaHmacSetKey() */
  12895. /*
  12896. * testing wc_HmacSetKey() on Sha224 hash.
  12897. */
  12898. static int test_wc_Sha224HmacSetKey(void)
  12899. {
  12900. EXPECT_DECLS;
  12901. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12902. Hmac hmac;
  12903. int ret, times, itr;
  12904. const char* keys[]=
  12905. {
  12906. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12907. "\x0b\x0b\x0b",
  12908. #ifndef HAVE_FIPS
  12909. "Jefe", /* smaller than minimum FIPS key size */
  12910. #endif
  12911. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12912. "\xAA\xAA\xAA"
  12913. };
  12914. times = sizeof(keys) / sizeof(char*);
  12915. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12916. for (itr = 0; itr < times; itr++) {
  12917. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12918. (word32)XSTRLEN(keys[itr])), 0);
  12919. }
  12920. /* Bad args. */
  12921. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12922. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12923. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  12924. BAD_FUNC_ARG);
  12925. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12926. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12927. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12928. #ifdef HAVE_FIPS
  12929. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12930. #else
  12931. ExpectIntEQ(ret, 0);
  12932. #endif
  12933. wc_HmacFree(&hmac);
  12934. #endif
  12935. return EXPECT_RESULT();
  12936. } /* END test_wc_Sha224HmacSetKey() */
  12937. /*
  12938. * testing wc_HmacSetKey() on Sha256 hash
  12939. */
  12940. static int test_wc_Sha256HmacSetKey(void)
  12941. {
  12942. EXPECT_DECLS;
  12943. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12944. Hmac hmac;
  12945. int ret, times, itr;
  12946. const char* keys[]=
  12947. {
  12948. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12949. "\x0b\x0b\x0b",
  12950. #ifndef HAVE_FIPS
  12951. "Jefe", /* smaller than minimum FIPS key size */
  12952. #endif
  12953. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12954. "\xAA\xAA\xAA"
  12955. };
  12956. times = sizeof(keys) / sizeof(char*);
  12957. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  12958. for (itr = 0; itr < times; itr++) {
  12959. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12960. (word32)XSTRLEN(keys[itr])), 0);
  12961. }
  12962. /* Bad args. */
  12963. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12964. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12965. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  12966. BAD_FUNC_ARG);
  12967. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  12968. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  12969. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12970. #ifdef HAVE_FIPS
  12971. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  12972. #else
  12973. ExpectIntEQ(ret, 0);
  12974. #endif
  12975. wc_HmacFree(&hmac);
  12976. #endif
  12977. return EXPECT_RESULT();
  12978. } /* END test_wc_Sha256HmacSetKey() */
  12979. /*
  12980. * testing wc_HmacSetKey on Sha384 hash.
  12981. */
  12982. static int test_wc_Sha384HmacSetKey(void)
  12983. {
  12984. EXPECT_DECLS;
  12985. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12986. Hmac hmac;
  12987. int ret, times, itr;
  12988. const char* keys[]=
  12989. {
  12990. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12991. "\x0b\x0b\x0b",
  12992. #ifndef HAVE_FIPS
  12993. "Jefe", /* smaller than minimum FIPS key size */
  12994. #endif
  12995. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12996. "\xAA\xAA\xAA"
  12997. };
  12998. times = sizeof(keys) / sizeof(char*);
  12999. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13000. for (itr = 0; itr < times; itr++) {
  13001. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  13002. (word32)XSTRLEN(keys[itr])), 0);
  13003. }
  13004. /* Bad args. */
  13005. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  13006. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13007. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  13008. BAD_FUNC_ARG);
  13009. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13010. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13011. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  13012. #ifdef HAVE_FIPS
  13013. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13014. #else
  13015. ExpectIntEQ(ret, 0);
  13016. #endif
  13017. wc_HmacFree(&hmac);
  13018. #endif
  13019. return EXPECT_RESULT();
  13020. } /* END test_wc_Sha384HmacSetKey() */
  13021. /*
  13022. * testing wc_HmacUpdate on wc_Md5 hash.
  13023. */
  13024. static int test_wc_Md5HmacUpdate(void)
  13025. {
  13026. EXPECT_DECLS;
  13027. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13028. Hmac hmac;
  13029. testVector a, b;
  13030. #ifdef HAVE_FIPS
  13031. const char* keys =
  13032. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13033. #else
  13034. const char* keys = "Jefe";
  13035. #endif
  13036. a.input = "what do ya want for nothing?";
  13037. a.inLen = XSTRLEN(a.input);
  13038. b.input = "Hi There";
  13039. b.inLen = XSTRLEN(b.input);
  13040. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13041. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  13042. (word32)XSTRLEN(keys)), 0);
  13043. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13044. /* Update Hmac. */
  13045. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13046. /* Test bad args. */
  13047. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13048. BAD_FUNC_ARG);
  13049. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13050. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13051. wc_HmacFree(&hmac);
  13052. #endif
  13053. return EXPECT_RESULT();
  13054. } /* END test_wc_Md5HmacUpdate */
  13055. /*
  13056. * testing wc_HmacUpdate on SHA hash.
  13057. */
  13058. static int test_wc_ShaHmacUpdate(void)
  13059. {
  13060. EXPECT_DECLS;
  13061. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13062. Hmac hmac;
  13063. testVector a, b;
  13064. #ifdef HAVE_FIPS
  13065. const char* keys =
  13066. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13067. #else
  13068. const char* keys = "Jefe";
  13069. #endif
  13070. a.input = "what do ya want for nothing?";
  13071. a.inLen = XSTRLEN(a.input);
  13072. b.input = "Hi There";
  13073. b.inLen = XSTRLEN(b.input);
  13074. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13075. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  13076. (word32)XSTRLEN(keys)), 0);
  13077. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13078. /* Update Hmac. */
  13079. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13080. /* Test bad args. */
  13081. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13082. BAD_FUNC_ARG);
  13083. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13084. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13085. wc_HmacFree(&hmac);
  13086. #endif
  13087. return EXPECT_RESULT();
  13088. } /* END test_wc_ShaHmacUpdate */
  13089. /*
  13090. * testing wc_HmacUpdate on SHA224 hash.
  13091. */
  13092. static int test_wc_Sha224HmacUpdate(void)
  13093. {
  13094. EXPECT_DECLS;
  13095. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13096. Hmac hmac;
  13097. testVector a, b;
  13098. #ifdef HAVE_FIPS
  13099. const char* keys =
  13100. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13101. #else
  13102. const char* keys = "Jefe";
  13103. #endif
  13104. a.input = "what do ya want for nothing?";
  13105. a.inLen = XSTRLEN(a.input);
  13106. b.input = "Hi There";
  13107. b.inLen = XSTRLEN(b.input);
  13108. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13109. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  13110. (word32)XSTRLEN(keys)), 0);
  13111. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13112. /* Update Hmac. */
  13113. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13114. /* Test bad args. */
  13115. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13116. BAD_FUNC_ARG);
  13117. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13118. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13119. wc_HmacFree(&hmac);
  13120. #endif
  13121. return EXPECT_RESULT();
  13122. } /* END test_wc_Sha224HmacUpdate */
  13123. /*
  13124. * testing wc_HmacUpdate on SHA256 hash.
  13125. */
  13126. static int test_wc_Sha256HmacUpdate(void)
  13127. {
  13128. EXPECT_DECLS;
  13129. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13130. Hmac hmac;
  13131. testVector a, b;
  13132. #ifdef HAVE_FIPS
  13133. const char* keys =
  13134. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13135. #else
  13136. const char* keys = "Jefe";
  13137. #endif
  13138. a.input = "what do ya want for nothing?";
  13139. a.inLen = XSTRLEN(a.input);
  13140. b.input = "Hi There";
  13141. b.inLen = XSTRLEN(b.input);
  13142. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13143. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  13144. (word32)XSTRLEN(keys)), 0);
  13145. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13146. /* Update Hmac. */
  13147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13148. /* Test bad args. */
  13149. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13150. BAD_FUNC_ARG);
  13151. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13152. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13153. wc_HmacFree(&hmac);
  13154. #endif
  13155. return EXPECT_RESULT();
  13156. } /* END test_wc_Sha256HmacUpdate */
  13157. /*
  13158. * testing wc_HmacUpdate on SHA384 hash.
  13159. */
  13160. static int test_wc_Sha384HmacUpdate(void)
  13161. {
  13162. EXPECT_DECLS;
  13163. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13164. Hmac hmac;
  13165. testVector a, b;
  13166. #ifdef HAVE_FIPS
  13167. const char* keys =
  13168. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13169. #else
  13170. const char* keys = "Jefe";
  13171. #endif
  13172. a.input = "what do ya want for nothing?";
  13173. a.inLen = XSTRLEN(a.input);
  13174. b.input = "Hi There";
  13175. b.inLen = XSTRLEN(b.input);
  13176. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13177. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  13178. (word32)XSTRLEN(keys)), 0);
  13179. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  13180. /* Update Hmac. */
  13181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13182. /* Test bad args. */
  13183. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13184. BAD_FUNC_ARG);
  13185. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  13186. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  13187. wc_HmacFree(&hmac);
  13188. #endif
  13189. return EXPECT_RESULT();
  13190. } /* END test_wc_Sha384HmacUpdate */
  13191. /*
  13192. * Testing wc_HmacFinal() with MD5
  13193. */
  13194. static int test_wc_Md5HmacFinal(void)
  13195. {
  13196. EXPECT_DECLS;
  13197. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13198. Hmac hmac;
  13199. byte hash[WC_MD5_DIGEST_SIZE];
  13200. testVector a;
  13201. const char* key;
  13202. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13203. a.input = "Hi There";
  13204. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13205. "\x9d";
  13206. a.inLen = XSTRLEN(a.input);
  13207. a.outLen = XSTRLEN(a.output);
  13208. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13209. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  13210. 0);
  13211. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13212. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13213. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  13214. /* Try bad parameters. */
  13215. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13216. #ifndef HAVE_FIPS
  13217. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13218. #endif
  13219. wc_HmacFree(&hmac);
  13220. #endif
  13221. return EXPECT_RESULT();
  13222. } /* END test_wc_Md5HmacFinal */
  13223. /*
  13224. * Testing wc_HmacFinal() with SHA
  13225. */
  13226. static int test_wc_ShaHmacFinal(void)
  13227. {
  13228. EXPECT_DECLS;
  13229. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13230. Hmac hmac;
  13231. byte hash[WC_SHA_DIGEST_SIZE];
  13232. testVector a;
  13233. const char* key;
  13234. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13235. "\x0b\x0b\x0b";
  13236. a.input = "Hi There";
  13237. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13238. "\x8e\xf1\x46\xbe\x00";
  13239. a.inLen = XSTRLEN(a.input);
  13240. a.outLen = XSTRLEN(a.output);
  13241. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13242. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  13243. 0);
  13244. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13245. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13246. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  13247. /* Try bad parameters. */
  13248. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13249. #ifndef HAVE_FIPS
  13250. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13251. #endif
  13252. wc_HmacFree(&hmac);
  13253. #endif
  13254. return EXPECT_RESULT();
  13255. } /* END test_wc_ShaHmacFinal */
  13256. /*
  13257. * Testing wc_HmacFinal() with SHA224
  13258. */
  13259. static int test_wc_Sha224HmacFinal(void)
  13260. {
  13261. EXPECT_DECLS;
  13262. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13263. Hmac hmac;
  13264. byte hash[WC_SHA224_DIGEST_SIZE];
  13265. testVector a;
  13266. const char* key;
  13267. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13268. "\x0b\x0b\x0b";
  13269. a.input = "Hi There";
  13270. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13271. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13272. a.inLen = XSTRLEN(a.input);
  13273. a.outLen = XSTRLEN(a.output);
  13274. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13275. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  13276. (word32)XSTRLEN(key)), 0);
  13277. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13278. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13279. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  13280. /* Try bad parameters. */
  13281. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13282. #ifndef HAVE_FIPS
  13283. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13284. #endif
  13285. wc_HmacFree(&hmac);
  13286. #endif
  13287. return EXPECT_RESULT();
  13288. } /* END test_wc_Sha224HmacFinal */
  13289. /*
  13290. * Testing wc_HmacFinal() with SHA256
  13291. */
  13292. static int test_wc_Sha256HmacFinal(void)
  13293. {
  13294. EXPECT_DECLS;
  13295. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13296. Hmac hmac;
  13297. byte hash[WC_SHA256_DIGEST_SIZE];
  13298. testVector a;
  13299. const char* key;
  13300. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13301. "\x0b\x0b\x0b";
  13302. a.input = "Hi There";
  13303. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13304. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13305. "\xcf\xf7";
  13306. a.inLen = XSTRLEN(a.input);
  13307. a.outLen = XSTRLEN(a.output);
  13308. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13309. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  13310. (word32)XSTRLEN(key)), 0);
  13311. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13312. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13313. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  13314. /* Try bad parameters. */
  13315. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13316. #ifndef HAVE_FIPS
  13317. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13318. #endif
  13319. wc_HmacFree(&hmac);
  13320. #endif
  13321. return EXPECT_RESULT();
  13322. } /* END test_wc_Sha256HmacFinal */
  13323. /*
  13324. * Testing wc_HmacFinal() with SHA384
  13325. */
  13326. static int test_wc_Sha384HmacFinal(void)
  13327. {
  13328. EXPECT_DECLS;
  13329. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13330. Hmac hmac;
  13331. byte hash[WC_SHA384_DIGEST_SIZE];
  13332. testVector a;
  13333. const char* key;
  13334. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13335. "\x0b\x0b\x0b";
  13336. a.input = "Hi There";
  13337. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13338. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13339. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13340. "\xfa\x9c\xb6";
  13341. a.inLen = XSTRLEN(a.input);
  13342. a.outLen = XSTRLEN(a.output);
  13343. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13344. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  13345. (word32)XSTRLEN(key)), 0);
  13346. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  13347. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  13348. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  13349. /* Try bad parameters. */
  13350. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  13351. #ifndef HAVE_FIPS
  13352. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  13353. #endif
  13354. wc_HmacFree(&hmac);
  13355. #endif
  13356. return EXPECT_RESULT();
  13357. } /* END test_wc_Sha384HmacFinal */
  13358. /*
  13359. * Testing wc_InitCmac()
  13360. */
  13361. static int test_wc_InitCmac(void)
  13362. {
  13363. EXPECT_DECLS;
  13364. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13365. Cmac cmac1;
  13366. Cmac cmac2;
  13367. Cmac cmac3;
  13368. /* AES 128 key. */
  13369. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13370. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13371. /* AES 192 key. */
  13372. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13373. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13374. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13375. /* AES 256 key. */
  13376. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13377. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13378. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13379. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13380. word32 key1Sz = (word32)sizeof(key1) - 1;
  13381. word32 key2Sz = (word32)sizeof(key2) - 1;
  13382. word32 key3Sz = (word32)sizeof(key3) - 1;
  13383. int type = WC_CMAC_AES;
  13384. (void)key1;
  13385. (void)key1Sz;
  13386. (void)key2;
  13387. (void)key2Sz;
  13388. XMEMSET(&cmac1, 0, sizeof(Cmac));
  13389. XMEMSET(&cmac2, 0, sizeof(Cmac));
  13390. XMEMSET(&cmac3, 0, sizeof(Cmac));
  13391. #ifdef WOLFSSL_AES_128
  13392. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  13393. #endif
  13394. #ifdef WOLFSSL_AES_192
  13395. wc_AesFree(&cmac1.aes);
  13396. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  13397. #endif
  13398. #ifdef WOLFSSL_AES_256
  13399. wc_AesFree(&cmac2.aes);
  13400. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  13401. #endif
  13402. wc_AesFree(&cmac3.aes);
  13403. /* Test bad args. */
  13404. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  13405. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  13406. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  13407. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  13408. #endif
  13409. return EXPECT_RESULT();
  13410. } /* END test_wc_InitCmac */
  13411. /*
  13412. * Testing wc_CmacUpdate()
  13413. */
  13414. static int test_wc_CmacUpdate(void)
  13415. {
  13416. EXPECT_DECLS;
  13417. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13418. Cmac cmac;
  13419. byte key[] = {
  13420. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13421. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13422. };
  13423. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13424. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13425. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13426. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13427. "\xf4";
  13428. word32 inSz = (word32)sizeof(in) - 1;
  13429. word32 keySz = (word32)sizeof(key);
  13430. int type = WC_CMAC_AES;
  13431. XMEMSET(&cmac, 0, sizeof(Cmac));
  13432. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13433. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  13434. /* Test bad args. */
  13435. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  13436. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  13437. wc_AesFree(&cmac.aes);
  13438. #endif
  13439. return EXPECT_RESULT();
  13440. } /* END test_wc_CmacUpdate */
  13441. /*
  13442. * Testing wc_CmacFinal()
  13443. */
  13444. static int test_wc_CmacFinal(void)
  13445. {
  13446. EXPECT_DECLS;
  13447. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13448. Cmac cmac;
  13449. byte key[] = {
  13450. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13451. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13452. };
  13453. byte msg[] = {
  13454. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13455. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13456. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13457. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13458. 0xf4
  13459. };
  13460. /* Test vectors from CMACGenAES128.rsp from
  13461. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13462. * Per RFC4493 truncation of lsb is possible.
  13463. */
  13464. byte expMac[] = {
  13465. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13466. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13467. };
  13468. byte mac[AES_BLOCK_SIZE];
  13469. word32 msgSz = (word32)sizeof(msg);
  13470. word32 keySz = (word32)sizeof(key);
  13471. word32 macSz = sizeof(mac);
  13472. word32 badMacSz = 17;
  13473. int expMacSz = sizeof(expMac);
  13474. int type = WC_CMAC_AES;
  13475. XMEMSET(&cmac, 0, sizeof(Cmac));
  13476. XMEMSET(mac, 0, macSz);
  13477. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  13478. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  13479. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  13480. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13481. /* Pass in bad args. */
  13482. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  13483. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  13484. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  13485. #endif
  13486. return EXPECT_RESULT();
  13487. } /* END test_wc_CmacFinal */
  13488. /*
  13489. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13490. */
  13491. static int test_wc_AesCmacGenerate(void)
  13492. {
  13493. EXPECT_DECLS;
  13494. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13495. byte key[] = {
  13496. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13497. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13498. };
  13499. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13500. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13501. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13502. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13503. byte mac[AES_BLOCK_SIZE];
  13504. word32 keySz = sizeof(key);
  13505. word32 macSz = sizeof(mac);
  13506. word32 msgSz = sizeof(msg) - 1;
  13507. word32 expMacSz = sizeof(expMac) - 1;
  13508. XMEMSET(mac, 0, macSz);
  13509. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  13510. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  13511. /* Pass in bad args. */
  13512. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  13513. BAD_FUNC_ARG);
  13514. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  13515. BAD_FUNC_ARG);
  13516. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  13517. BAD_FUNC_ARG);
  13518. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  13519. BAD_FUNC_ARG);
  13520. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  13521. /* Test bad args. */
  13522. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  13523. BAD_FUNC_ARG);
  13524. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  13525. BAD_FUNC_ARG);
  13526. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  13527. BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  13529. BAD_FUNC_ARG);
  13530. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  13531. BAD_FUNC_ARG);
  13532. #endif
  13533. return EXPECT_RESULT();
  13534. } /* END test_wc_AesCmacGenerate */
  13535. /*
  13536. * Testing streaming AES-GCM API.
  13537. */
  13538. static int test_wc_AesGcmStream(void)
  13539. {
  13540. EXPECT_DECLS;
  13541. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13542. defined(WOLFSSL_AESGCM_STREAM)
  13543. int i;
  13544. WC_RNG rng[1];
  13545. Aes aesEnc[1];
  13546. Aes aesDec[1];
  13547. byte tag[AES_BLOCK_SIZE];
  13548. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13549. byte out[AES_BLOCK_SIZE * 3 + 2];
  13550. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13551. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13552. byte key[AES_128_KEY_SIZE] = { 0, };
  13553. byte iv[AES_IV_SIZE] = { 1, };
  13554. byte ivOut[AES_IV_SIZE];
  13555. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13556. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13557. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13558. };
  13559. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13560. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13561. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13562. };
  13563. static const byte expTag[AES_BLOCK_SIZE] = {
  13564. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13565. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13566. };
  13567. XMEMSET(&rng, 0, sizeof(WC_RNG));
  13568. XMEMSET(&aesEnc, 0, sizeof(Aes));
  13569. XMEMSET(&aesDec, 0, sizeof(Aes));
  13570. /* Create a random for generating IV/nonce. */
  13571. ExpectIntEQ(wc_InitRng(rng), 0);
  13572. /* Initialize data structures. */
  13573. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13574. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13575. /* BadParameters to streaming init. */
  13576. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13577. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13578. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13579. BAD_FUNC_ARG);
  13580. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13581. BAD_FUNC_ARG);
  13582. /* Bad parameters to encrypt update. */
  13583. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13584. BAD_FUNC_ARG);
  13585. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13586. BAD_FUNC_ARG);
  13587. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13588. BAD_FUNC_ARG);
  13589. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13590. BAD_FUNC_ARG);
  13591. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13592. BAD_FUNC_ARG);
  13593. /* Bad parameters to decrypt update. */
  13594. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13595. BAD_FUNC_ARG);
  13596. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13597. BAD_FUNC_ARG);
  13598. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13599. BAD_FUNC_ARG);
  13600. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13601. BAD_FUNC_ARG);
  13602. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13603. BAD_FUNC_ARG);
  13604. /* Bad parameters to encrypt final. */
  13605. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13606. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13607. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13608. BAD_FUNC_ARG);
  13609. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13610. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13611. BAD_FUNC_ARG);
  13612. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13613. BAD_FUNC_ARG);
  13614. /* Bad parameters to decrypt final. */
  13615. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13616. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13617. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13618. BAD_FUNC_ARG);
  13619. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13620. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13621. BAD_FUNC_ARG);
  13622. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13623. BAD_FUNC_ARG);
  13624. /* Check calling final before setting key fails. */
  13625. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13626. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13627. /* Check calling update before setting key else fails. */
  13628. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13629. MISSING_KEY);
  13630. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13631. MISSING_KEY);
  13632. /* Set key but not IV. */
  13633. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13634. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13635. /* Check calling final before setting IV fails. */
  13636. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13637. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13638. /* Check calling update before setting IV else fails. */
  13639. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13640. MISSING_IV);
  13641. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13642. MISSING_IV);
  13643. /* Set IV using fixed part IV and external IV APIs. */
  13644. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13645. rng), 0);
  13646. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13647. GCM_NONCE_MID_SZ), 0);
  13648. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13649. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13650. /* Encrypt and decrypt data. */
  13651. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13652. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13653. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13654. /* Finalize and check tag matches. */
  13655. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13656. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13657. /* Set key and IV through streaming init API. */
  13658. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13659. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13660. /* Encrypt/decrypt one block and AAD of one block. */
  13661. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13662. AES_BLOCK_SIZE), 0);
  13663. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13664. AES_BLOCK_SIZE), 0);
  13665. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13666. /* Finalize and check tag matches. */
  13667. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13668. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13669. /* Set key and IV through streaming init API. */
  13670. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13671. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13672. /* No data to encrypt/decrypt one byte of AAD. */
  13673. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13674. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13675. /* Finalize and check tag matches. */
  13676. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13677. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13678. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13679. /* Set key and IV through streaming init API. */
  13680. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13681. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13682. /* Encrypt/decrypt one byte and no AAD. */
  13683. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13684. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13685. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  13686. /* Finalize and check tag matches. */
  13687. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13688. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13689. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13690. /* Set key and IV through streaming init API. */
  13691. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13692. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13693. /* Encryption AES is one byte at a time */
  13694. for (i = 0; i < (int)sizeof(aad); i++) {
  13695. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13696. 0);
  13697. }
  13698. for (i = 0; i < (int)sizeof(in); i++) {
  13699. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13700. 0);
  13701. }
  13702. /* Decryption AES is two bytes at a time */
  13703. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13704. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13705. 0);
  13706. }
  13707. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13708. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13709. 0), 0);
  13710. }
  13711. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13712. /* Finalize and check tag matches. */
  13713. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13714. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13715. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13716. /* Check streaming encryption can be decrypted with one shot. */
  13717. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13718. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13719. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13720. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13721. wc_AesFree(aesEnc);
  13722. wc_AesFree(aesDec);
  13723. wc_FreeRng(rng);
  13724. #endif
  13725. return EXPECT_RESULT();
  13726. } /* END test_wc_AesGcmStream */
  13727. /*
  13728. * Testing streaming SM4 API.
  13729. */
  13730. static int test_wc_Sm4(void)
  13731. {
  13732. int res = TEST_SKIPPED;
  13733. #ifdef WOLFSSL_SM4
  13734. EXPECT_DECLS;
  13735. wc_Sm4 sm4;
  13736. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13737. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13738. unsigned char key[SM4_KEY_SIZE];
  13739. #endif
  13740. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13741. unsigned char iv[SM4_IV_SIZE];
  13742. #endif
  13743. /* Invalid parameters - wc_Sm4Init */
  13744. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13745. /* Valid cases - wc_Sm4Init */
  13746. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13747. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  13748. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  13749. XMEMSET(key, 0, sizeof(key));
  13750. /* Invalid parameters - wc_Sm4SetKey. */
  13751. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  13752. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  13753. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  13754. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13755. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  13756. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13757. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  13758. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  13759. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  13760. /* Valid cases - wc_Sm4SetKey. */
  13761. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13762. #endif
  13763. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  13764. XMEMSET(iv, 0, sizeof(iv));
  13765. /* Invalid parameters - wc_Sm4SetIV. */
  13766. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  13767. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  13768. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  13769. /* Valid cases - wc_Sm4SetIV. */
  13770. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13771. #endif
  13772. /* Valid cases - wc_Sm4Free */
  13773. wc_Sm4Free(NULL);
  13774. wc_Sm4Free(&sm4);
  13775. res = EXPECT_RESULT();
  13776. #endif
  13777. return res;
  13778. } /* END test_wc_Sm4 */
  13779. /*
  13780. * Testing block based SM4-ECB API.
  13781. */
  13782. static int test_wc_Sm4Ecb(void)
  13783. {
  13784. int res = TEST_SKIPPED;
  13785. #ifdef WOLFSSL_SM4_ECB
  13786. EXPECT_DECLS;
  13787. wc_Sm4 sm4;
  13788. unsigned char key[SM4_KEY_SIZE];
  13789. unsigned char in[SM4_BLOCK_SIZE * 2];
  13790. unsigned char out[SM4_BLOCK_SIZE * 2];
  13791. unsigned char out2[SM4_BLOCK_SIZE];
  13792. XMEMSET(key, 0, sizeof(key));
  13793. XMEMSET(in, 0, sizeof(in));
  13794. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13795. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13796. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13797. /* Tested in test_wc_Sm4. */
  13798. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13799. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  13800. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13801. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13802. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13803. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13804. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13805. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13806. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13807. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13808. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13809. /* Valid cases - wc_Sm4EcbEncrypt. */
  13810. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  13811. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13812. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13813. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13814. /* In and out are same pointer. */
  13815. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13816. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13817. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  13818. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13819. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13820. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13821. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13822. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13823. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13824. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13825. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13826. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13827. /* Valid cases - wc_Sm4EcbDecrypt. */
  13828. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  13829. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13830. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13831. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13832. /* In and out are same pointer. */
  13833. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13834. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13835. wc_Sm4Free(&sm4);
  13836. res = EXPECT_RESULT();
  13837. #endif
  13838. return res;
  13839. } /* END test_wc_Sm4Ecb */
  13840. /*
  13841. * Testing block based SM4-CBC API.
  13842. */
  13843. static int test_wc_Sm4Cbc(void)
  13844. {
  13845. int res = TEST_SKIPPED;
  13846. #ifdef WOLFSSL_SM4_CBC
  13847. EXPECT_DECLS;
  13848. wc_Sm4 sm4;
  13849. unsigned char key[SM4_KEY_SIZE];
  13850. unsigned char iv[SM4_IV_SIZE];
  13851. unsigned char in[SM4_BLOCK_SIZE * 2];
  13852. unsigned char out[SM4_BLOCK_SIZE * 2];
  13853. unsigned char out2[SM4_BLOCK_SIZE];
  13854. XMEMSET(key, 0, sizeof(key));
  13855. XMEMSET(iv, 0, sizeof(iv));
  13856. XMEMSET(in, 0, sizeof(in));
  13857. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13858. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13859. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  13860. /* Tested in test_wc_Sm4. */
  13861. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13862. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  13863. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  13864. /* Tested in test_wc_Sm4. */
  13865. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13866. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  13867. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13868. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13869. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13870. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13871. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13872. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13873. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13874. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13875. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13876. /* Valid cases - wc_Sm4CbcEncrypt. */
  13877. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  13878. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13879. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13880. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13881. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13882. /* In and out are same pointer. */
  13883. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13884. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13885. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13886. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  13887. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  13888. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  13889. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  13890. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  13891. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13892. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13893. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13894. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13895. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  13896. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13897. /* Valid cases - wc_Sm4CbcDecrypt. */
  13898. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  13899. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13900. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13901. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13902. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13903. /* In and out are same pointer. */
  13904. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13905. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  13906. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13907. wc_Sm4Free(&sm4);
  13908. res = EXPECT_RESULT();
  13909. #endif
  13910. return res;
  13911. } /* END test_wc_Sm4Cbc */
  13912. /*
  13913. * Testing streaming SM4-CTR API.
  13914. */
  13915. static int test_wc_Sm4Ctr(void)
  13916. {
  13917. int res = TEST_SKIPPED;
  13918. #ifdef WOLFSSL_SM4_CTR
  13919. EXPECT_DECLS;
  13920. wc_Sm4 sm4;
  13921. unsigned char key[SM4_KEY_SIZE];
  13922. unsigned char iv[SM4_IV_SIZE];
  13923. unsigned char in[SM4_BLOCK_SIZE * 4];
  13924. unsigned char out[SM4_BLOCK_SIZE * 4];
  13925. unsigned char out2[SM4_BLOCK_SIZE * 4];
  13926. word32 chunk;
  13927. word32 i;
  13928. XMEMSET(key, 0, sizeof(key));
  13929. XMEMSET(iv, 0, sizeof(iv));
  13930. XMEMSET(in, 0, sizeof(in));
  13931. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  13932. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  13933. /* Tested in test_wc_Sm4. */
  13934. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  13935. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  13936. /* Tested in test_wc_Sm4. */
  13937. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13938. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  13939. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  13940. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  13941. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  13942. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  13943. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  13944. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  13945. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  13946. /* Valid cases - wc_Sm4CtrEncrypt. */
  13947. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  13948. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  13949. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13950. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  13951. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  13952. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13953. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  13954. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  13955. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13956. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13957. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  13958. /* In and out are same pointer. Also check encrypt of cipher text produces
  13959. * plaintext.
  13960. */
  13961. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13962. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  13963. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  13964. /* Chunking tests. */
  13965. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13966. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  13967. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  13968. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13969. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  13970. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  13971. }
  13972. if (i < (word32)sizeof(in)) {
  13973. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  13974. (word32)sizeof(in) - i), 0);
  13975. }
  13976. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  13977. }
  13978. for (i = 0; i < (word32)sizeof(iv); i++) {
  13979. iv[i] = 0xff;
  13980. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13981. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  13982. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  13983. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  13984. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  13985. }
  13986. wc_Sm4Free(&sm4);
  13987. res = EXPECT_RESULT();
  13988. #endif
  13989. return res;
  13990. } /* END test_wc_Sm4Ctr */
  13991. /*
  13992. * Testing stream SM4-GCM API.
  13993. */
  13994. static int test_wc_Sm4Gcm(void)
  13995. {
  13996. int res = TEST_SKIPPED;
  13997. #ifdef WOLFSSL_SM4_GCM
  13998. EXPECT_DECLS;
  13999. wc_Sm4 sm4;
  14000. unsigned char key[SM4_KEY_SIZE];
  14001. unsigned char nonce[GCM_NONCE_MAX_SZ];
  14002. unsigned char in[SM4_BLOCK_SIZE * 2];
  14003. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14004. unsigned char out[SM4_BLOCK_SIZE * 2];
  14005. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14006. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14007. unsigned char tag[SM4_BLOCK_SIZE];
  14008. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14009. word32 i;
  14010. XMEMSET(key, 0, sizeof(key));
  14011. XMEMSET(nonce, 0, sizeof(nonce));
  14012. XMEMSET(in, 0, sizeof(in));
  14013. XMEMSET(in2, 0, sizeof(in2));
  14014. XMEMSET(aad, 0, sizeof(aad));
  14015. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14016. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14017. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14018. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  14019. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14020. /* Invalid parameters - wc_Sm4GcmSetKey. */
  14021. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14022. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14023. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  14024. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14025. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14026. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14027. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14028. /* Valid parameters - wc_Sm4GcmSetKey. */
  14029. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14030. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  14031. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14032. 0), BAD_FUNC_ARG);
  14033. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14034. 0), BAD_FUNC_ARG);
  14035. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14036. 0), BAD_FUNC_ARG);
  14037. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14038. 0), BAD_FUNC_ARG);
  14039. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14040. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14041. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14042. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14043. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14044. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14045. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14046. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14047. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14048. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14049. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14050. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14051. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14052. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14053. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14054. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14055. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14056. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14057. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14058. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14059. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  14060. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14061. 0), BAD_FUNC_ARG);
  14062. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14063. 0), BAD_FUNC_ARG);
  14064. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14065. 0), BAD_FUNC_ARG);
  14066. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14067. 0), BAD_FUNC_ARG);
  14068. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14069. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14070. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14071. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14072. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14073. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14074. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  14075. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14076. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  14077. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14078. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  14079. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14080. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14081. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14082. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  14083. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14084. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14085. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14086. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  14087. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14088. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14089. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14090. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14091. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14092. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14093. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14094. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14095. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  14096. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14097. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14098. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14099. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14100. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14101. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14102. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14103. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14104. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14105. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14106. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14107. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14108. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14109. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14110. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14111. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14112. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14113. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14114. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14115. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14116. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14117. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14118. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14119. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14120. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14121. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14122. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14123. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14124. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14125. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14126. SM4_GCM_AUTH_E);
  14127. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  14128. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  14129. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14130. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14131. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14132. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  14133. }
  14134. /* Check different in/out sizes. */
  14135. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  14136. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14137. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  14138. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14139. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  14140. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14141. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14142. XMEMCPY(out2, out, i - 1);
  14143. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  14144. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14145. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14146. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  14147. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14148. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14149. }
  14150. /* Force the counter to roll over in first byte. */
  14151. {
  14152. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14153. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14154. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14155. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14156. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14157. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14158. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14159. }
  14160. wc_Sm4Free(&sm4);
  14161. res = EXPECT_RESULT();
  14162. #endif
  14163. return res;
  14164. } /* END test_wc_Sm4Gcm */
  14165. /*
  14166. * Testing stream SM4-CCM API.
  14167. */
  14168. static int test_wc_Sm4Ccm(void)
  14169. {
  14170. int res = TEST_SKIPPED;
  14171. #ifdef WOLFSSL_SM4_CCM
  14172. EXPECT_DECLS;
  14173. wc_Sm4 sm4;
  14174. unsigned char key[SM4_KEY_SIZE];
  14175. unsigned char nonce[CCM_NONCE_MAX_SZ];
  14176. unsigned char in[SM4_BLOCK_SIZE * 2];
  14177. unsigned char in2[SM4_BLOCK_SIZE * 2];
  14178. unsigned char out[SM4_BLOCK_SIZE * 2];
  14179. unsigned char out2[SM4_BLOCK_SIZE * 2];
  14180. unsigned char dec[SM4_BLOCK_SIZE * 2];
  14181. unsigned char tag[SM4_BLOCK_SIZE];
  14182. unsigned char aad[SM4_BLOCK_SIZE * 2];
  14183. word32 i;
  14184. XMEMSET(key, 0, sizeof(key));
  14185. XMEMSET(nonce, 0, sizeof(nonce));
  14186. XMEMSET(in, 0, sizeof(in));
  14187. XMEMSET(in2, 0, sizeof(in2));
  14188. XMEMSET(aad, 0, sizeof(aad));
  14189. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14190. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14191. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14192. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  14193. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  14194. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14195. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  14196. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14197. 0), BAD_FUNC_ARG);
  14198. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14199. 0), BAD_FUNC_ARG);
  14200. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14201. 0), BAD_FUNC_ARG);
  14202. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14203. 0), BAD_FUNC_ARG);
  14204. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14205. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14206. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14207. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14208. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14209. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14210. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14211. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14212. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14213. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14214. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14215. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14216. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  14217. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14218. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14219. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14220. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14221. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14223. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14224. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  14225. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14226. 0), BAD_FUNC_ARG);
  14227. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  14228. 0), BAD_FUNC_ARG);
  14229. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  14230. 0), BAD_FUNC_ARG);
  14231. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  14232. 0), BAD_FUNC_ARG);
  14233. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14234. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14235. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  14236. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  14237. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14238. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14239. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14240. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14241. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  14242. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14243. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  14244. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14245. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  14246. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14247. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  14248. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  14249. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14250. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14251. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  14252. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14253. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14254. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14255. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14256. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14257. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14258. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14259. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14260. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  14261. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14262. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14263. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14264. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14265. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14266. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14267. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14268. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14269. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  14270. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  14271. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14272. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  14273. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14274. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14275. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14276. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  14277. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  14278. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14279. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  14280. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14281. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  14282. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14283. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14284. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14285. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14286. }
  14287. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14288. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  14289. SM4_CCM_AUTH_E);
  14290. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  14291. for (i = 0; i < 4; i++) {
  14292. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14293. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14294. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14295. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14296. }
  14297. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  14298. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14299. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14300. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14301. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14302. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  14303. }
  14304. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  14305. * Even values in range 4..SM4_BLOCK_SIZE.
  14306. */
  14307. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  14308. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  14309. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14310. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  14311. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  14312. }
  14313. /* Check different in/out sizes. */
  14314. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  14315. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14316. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  14317. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14318. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  14319. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  14320. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  14321. XMEMCPY(out2, out, i - 1);
  14322. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  14323. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14324. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  14325. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  14326. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14327. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  14328. }
  14329. /* Force the counter to roll over in first byte. */
  14330. {
  14331. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  14332. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  14333. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  14334. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14335. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  14336. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14337. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  14338. }
  14339. wc_Sm4Free(&sm4);
  14340. res = EXPECT_RESULT();
  14341. #endif
  14342. return res;
  14343. } /* END test_wc_Sm4Ccm */
  14344. /*
  14345. * unit test for wc_Des3_SetIV()
  14346. */
  14347. static int test_wc_Des3_SetIV(void)
  14348. {
  14349. EXPECT_DECLS;
  14350. #ifndef NO_DES3
  14351. Des3 des;
  14352. const byte key[] = {
  14353. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14354. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14355. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14356. };
  14357. const byte iv[] = {
  14358. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14359. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14360. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14361. };
  14362. XMEMSET(&des, 0, sizeof(Des3));
  14363. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14364. /* DES_ENCRYPTION or DES_DECRYPTION */
  14365. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14366. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14367. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  14368. /* Test explicitly wc_Des3_SetIV() */
  14369. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  14370. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  14371. #endif
  14372. wc_Des3Free(&des);
  14373. #endif
  14374. return EXPECT_RESULT();
  14375. } /* END test_wc_Des3_SetIV */
  14376. /*
  14377. * unit test for wc_Des3_SetKey()
  14378. */
  14379. static int test_wc_Des3_SetKey(void)
  14380. {
  14381. EXPECT_DECLS;
  14382. #ifndef NO_DES3
  14383. Des3 des;
  14384. const byte key[] = {
  14385. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14386. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14387. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14388. };
  14389. const byte iv[] = {
  14390. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14391. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14392. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14393. };
  14394. XMEMSET(&des, 0, sizeof(Des3));
  14395. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14396. /* DES_ENCRYPTION or DES_DECRYPTION */
  14397. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14398. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  14399. /* Test bad args. */
  14400. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14401. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  14402. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  14403. /* Default case. Should return 0. */
  14404. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  14405. wc_Des3Free(&des);
  14406. #endif
  14407. return EXPECT_RESULT();
  14408. } /* END test_wc_Des3_SetKey */
  14409. /*
  14410. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  14411. */
  14412. static int test_wc_Des3_CbcEncryptDecrypt(void)
  14413. {
  14414. EXPECT_DECLS;
  14415. #ifndef NO_DES3
  14416. Des3 des;
  14417. byte cipher[24];
  14418. byte plain[24];
  14419. const byte key[] = {
  14420. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14421. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14422. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14423. };
  14424. const byte iv[] = {
  14425. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14426. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14427. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14428. };
  14429. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14430. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14431. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14432. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14433. };
  14434. XMEMSET(&des, 0, sizeof(Des3));
  14435. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14436. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14437. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  14438. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  14439. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  14440. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14441. /* Pass in bad args. */
  14442. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  14443. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  14444. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  14445. BAD_FUNC_ARG);
  14446. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  14447. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  14448. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  14449. wc_Des3Free(&des);
  14450. #endif
  14451. return EXPECT_RESULT();
  14452. } /* END wc_Des3_CbcEncrypt */
  14453. /*
  14454. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14455. */
  14456. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14457. {
  14458. EXPECT_DECLS;
  14459. #ifndef NO_DES3
  14460. word32 vectorSz, cipherSz;
  14461. byte cipher[24];
  14462. byte plain[24];
  14463. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  14464. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14465. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14466. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14467. };
  14468. byte key[] = {
  14469. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14470. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14471. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14472. };
  14473. byte iv[] = {
  14474. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14475. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14476. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14477. };
  14478. vectorSz = sizeof(byte) * 24;
  14479. cipherSz = sizeof(byte) * 24;
  14480. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  14481. 0);
  14482. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  14483. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  14484. /* pass in bad args. */
  14485. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  14486. BAD_FUNC_ARG);
  14487. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  14488. BAD_FUNC_ARG);
  14489. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  14490. BAD_FUNC_ARG);
  14491. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  14492. 0);
  14493. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  14494. BAD_FUNC_ARG);
  14495. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  14496. BAD_FUNC_ARG);
  14497. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  14498. BAD_FUNC_ARG);
  14499. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  14500. 0);
  14501. #endif
  14502. return EXPECT_RESULT();
  14503. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14504. /*
  14505. * Unit test for wc_Des3_EcbEncrypt
  14506. */
  14507. static int test_wc_Des3_EcbEncrypt(void)
  14508. {
  14509. EXPECT_DECLS;
  14510. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14511. Des3 des;
  14512. byte cipher[24];
  14513. word32 cipherSz = sizeof(cipher);
  14514. const byte key[] = {
  14515. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14516. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14517. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14518. };
  14519. const byte iv[] = {
  14520. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14521. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14522. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14523. };
  14524. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14525. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14526. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14527. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14528. };
  14529. XMEMSET(&des, 0, sizeof(Des3));
  14530. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  14531. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  14532. /* Bad Cases */
  14533. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14534. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  14535. BAD_FUNC_ARG);
  14536. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  14537. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  14538. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  14539. /* Good Cases */
  14540. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  14541. wc_Des3Free(&des);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_Des3_EcbEncrypt */
  14545. /*
  14546. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14547. */
  14548. static int test_wc_Chacha_SetKey(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #ifdef HAVE_CHACHA
  14552. ChaCha ctx;
  14553. const byte key[] = {
  14554. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14555. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14556. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14557. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14558. };
  14559. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14560. byte cipher[128];
  14561. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  14562. /* Test bad args. */
  14563. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  14564. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14565. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  14566. /* Test bad args. */
  14567. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  14568. #endif
  14569. return EXPECT_RESULT();
  14570. } /* END test_wc_Chacha_SetKey */
  14571. /*
  14572. * unit test for wc_Poly1305SetKey()
  14573. */
  14574. static int test_wc_Poly1305SetKey(void)
  14575. {
  14576. EXPECT_DECLS;
  14577. #ifdef HAVE_POLY1305
  14578. Poly1305 ctx;
  14579. const byte key[] =
  14580. {
  14581. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14585. };
  14586. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  14587. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  14588. /* Test bad args. */
  14589. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  14590. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  14591. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  14592. #endif
  14593. return EXPECT_RESULT();
  14594. } /* END test_wc_Poly1305_SetKey() */
  14595. /*
  14596. * Testing wc_Chacha_Process()
  14597. */
  14598. static int test_wc_Chacha_Process(void)
  14599. {
  14600. EXPECT_DECLS;
  14601. #ifdef HAVE_CHACHA
  14602. ChaCha enc, dec;
  14603. byte cipher[128];
  14604. byte plain[128];
  14605. const byte key[] =
  14606. {
  14607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14608. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14611. };
  14612. const char* input = "Everybody gets Friday off.";
  14613. word32 keySz = sizeof(key)/sizeof(byte);
  14614. unsigned long int inlen = XSTRLEN(input);
  14615. /* Initialize stack variables. */
  14616. XMEMSET(cipher, 0, 128);
  14617. XMEMSET(plain, 0, 128);
  14618. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  14619. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  14620. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14621. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14622. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  14623. 0);
  14624. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  14625. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14626. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14627. /* test checking and using leftovers, currently just in C code */
  14628. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  14629. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  14630. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  14631. (word32)inlen - 2), 0);
  14632. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14633. (byte*)input + (inlen - 2), 2), 0);
  14634. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  14635. (word32)inlen - 2), 0);
  14636. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14637. (byte*)input + (inlen - 2), 2), 0);
  14638. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  14639. /* check edge cases with counter increment */
  14640. {
  14641. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14642. const byte expected[] = {
  14643. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14644. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14645. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14646. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14647. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14648. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14649. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14650. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14651. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14652. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14653. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14654. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14655. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14656. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14657. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14658. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14659. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14660. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14661. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14662. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14663. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14664. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14665. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14666. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14667. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14668. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14669. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14670. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14671. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14672. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14673. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14674. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14675. };
  14676. const byte iv2[] = {
  14677. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14678. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14679. };
  14680. byte input2[256];
  14681. int i;
  14682. for (i = 0; i < 256; i++)
  14683. input2[i] = i;
  14684. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  14685. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  14686. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14687. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  14688. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14689. /* partial */
  14690. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  14691. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14692. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  14693. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14694. }
  14695. #endif
  14696. /* Test bad args. */
  14697. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  14698. BAD_FUNC_ARG);
  14699. #endif
  14700. return EXPECT_RESULT();
  14701. } /* END test_wc_Chacha_Process */
  14702. /*
  14703. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14704. */
  14705. static int test_wc_ChaCha20Poly1305_aead(void)
  14706. {
  14707. EXPECT_DECLS;
  14708. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14709. const byte key[] = {
  14710. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14711. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14712. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14713. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14714. };
  14715. const byte plaintext[] = {
  14716. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14717. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14718. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14719. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14720. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14721. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14722. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14723. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14724. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14725. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14726. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14727. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14728. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14729. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14730. 0x74, 0x2e
  14731. };
  14732. const byte iv[] = {
  14733. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14734. 0x44, 0x45, 0x46, 0x47
  14735. };
  14736. const byte aad[] = { /* additional data */
  14737. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14738. 0xc4, 0xc5, 0xc6, 0xc7
  14739. };
  14740. const byte cipher[] = { /* expected output from operation */
  14741. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14742. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14743. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14744. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14745. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14746. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14747. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14748. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14749. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14750. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14751. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14752. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14753. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14754. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14755. 0x61, 0x16
  14756. };
  14757. const byte authTag[] = { /* expected output from operation */
  14758. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14759. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14760. };
  14761. byte generatedCiphertext[272];
  14762. byte generatedPlaintext[272];
  14763. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14764. /* Initialize stack variables. */
  14765. XMEMSET(generatedCiphertext, 0, 272);
  14766. XMEMSET(generatedPlaintext, 0, 272);
  14767. /* Test Encrypt */
  14768. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14769. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14770. 0);
  14771. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  14772. sizeof(cipher)/sizeof(byte)), 0);
  14773. /* Test bad args. */
  14774. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  14775. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14776. BAD_FUNC_ARG);
  14777. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14778. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14779. BAD_FUNC_ARG);
  14780. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14781. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14782. BAD_FUNC_ARG);
  14783. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14784. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  14785. BAD_FUNC_ARG);
  14786. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14787. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  14788. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14789. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  14790. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14791. sizeof(cipher), authTag, generatedPlaintext), 0);
  14792. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  14793. sizeof(plaintext)/sizeof(byte)), 0);
  14794. /* Test bad args. */
  14795. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14796. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14797. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14798. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14799. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14800. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14801. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14802. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  14803. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14804. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  14805. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14806. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  14807. #endif
  14808. return EXPECT_RESULT();
  14809. } /* END test_wc_ChaCha20Poly1305_aead */
  14810. /*
  14811. * Testing function for wc_Rc2SetKey().
  14812. */
  14813. static int test_wc_Rc2SetKey(void)
  14814. {
  14815. EXPECT_DECLS;
  14816. #ifdef WC_RC2
  14817. Rc2 rc2;
  14818. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14819. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14820. /* valid key and IV */
  14821. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14822. iv, 40), 0);
  14823. /* valid key, no IV */
  14824. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14825. NULL, 40), 0);
  14826. /* bad arguments */
  14827. /* null Rc2 struct */
  14828. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14829. iv, 40), BAD_FUNC_ARG);
  14830. /* null key */
  14831. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14832. iv, 40), BAD_FUNC_ARG);
  14833. /* key size == 0 */
  14834. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  14835. /* key size > 128 */
  14836. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  14837. /* effective bits == 0 */
  14838. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14839. iv, 0), WC_KEY_SIZE_E);
  14840. /* effective bits > 1024 */
  14841. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14842. iv, 1025), WC_KEY_SIZE_E);
  14843. #endif
  14844. return EXPECT_RESULT();
  14845. } /* END test_wc_Rc2SetKey */
  14846. /*
  14847. * Testing function for wc_Rc2SetIV().
  14848. */
  14849. static int test_wc_Rc2SetIV(void)
  14850. {
  14851. EXPECT_DECLS;
  14852. #ifdef WC_RC2
  14853. Rc2 rc2;
  14854. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14855. /* valid IV */
  14856. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14857. /* valid NULL IV */
  14858. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  14859. /* bad arguments */
  14860. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  14861. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  14862. #endif
  14863. return EXPECT_RESULT();
  14864. } /* END test_wc_Rc2SetIV */
  14865. /*
  14866. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  14867. */
  14868. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14869. {
  14870. EXPECT_DECLS;
  14871. #ifdef WC_RC2
  14872. Rc2 rc2;
  14873. int effectiveKeyBits = 63;
  14874. byte cipher[RC2_BLOCK_SIZE];
  14875. byte plain[RC2_BLOCK_SIZE];
  14876. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14877. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14878. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14879. XMEMSET(cipher, 0, sizeof(cipher));
  14880. XMEMSET(plain, 0, sizeof(plain));
  14881. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14882. NULL, effectiveKeyBits), 0);
  14883. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  14884. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  14885. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  14886. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  14887. /* Rc2EcbEncrypt bad arguments */
  14888. /* null Rc2 struct */
  14889. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  14890. BAD_FUNC_ARG);
  14891. /* null out buffer */
  14892. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  14893. BAD_FUNC_ARG);
  14894. /* null input buffer */
  14895. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  14896. BAD_FUNC_ARG);
  14897. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14898. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  14899. /* Rc2EcbDecrypt bad arguments */
  14900. /* null Rc2 struct */
  14901. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  14902. BAD_FUNC_ARG);
  14903. /* null out buffer */
  14904. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  14905. BAD_FUNC_ARG);
  14906. /* null input buffer */
  14907. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  14908. BAD_FUNC_ARG);
  14909. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14910. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  14911. #endif
  14912. return EXPECT_RESULT();
  14913. } /* END test_wc_Rc2EcbEncryptDecrypt */
  14914. /*
  14915. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  14916. */
  14917. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14918. {
  14919. EXPECT_DECLS;
  14920. #ifdef WC_RC2
  14921. Rc2 rc2;
  14922. int effectiveKeyBits = 63;
  14923. byte cipher[RC2_BLOCK_SIZE*2];
  14924. byte plain[RC2_BLOCK_SIZE*2];
  14925. /* vector taken from test.c */
  14926. byte key[] = {
  14927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14928. };
  14929. byte iv[] = {
  14930. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14931. };
  14932. byte input[] = {
  14933. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14934. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14935. };
  14936. byte output[] = {
  14937. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14938. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14939. };
  14940. XMEMSET(cipher, 0, sizeof(cipher));
  14941. XMEMSET(plain, 0, sizeof(plain));
  14942. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14943. iv, effectiveKeyBits), 0);
  14944. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  14945. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  14946. /* reset IV for decrypt */
  14947. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  14948. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  14949. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  14950. /* Rc2CbcEncrypt bad arguments */
  14951. /* null Rc2 struct */
  14952. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  14953. BAD_FUNC_ARG);
  14954. /* null out buffer */
  14955. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  14956. BAD_FUNC_ARG);
  14957. /* null input buffer */
  14958. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  14959. BAD_FUNC_ARG);
  14960. /* Rc2CbcDecrypt bad arguments */
  14961. /* in size is 0 */
  14962. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  14963. /* null Rc2 struct */
  14964. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  14965. BAD_FUNC_ARG);
  14966. /* null out buffer */
  14967. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  14968. BAD_FUNC_ARG);
  14969. /* null input buffer */
  14970. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  14971. BAD_FUNC_ARG);
  14972. #endif
  14973. return EXPECT_RESULT();
  14974. } /* END test_wc_Rc2CbcEncryptDecrypt */
  14975. /*
  14976. * Testing function for wc_AesSetIV
  14977. */
  14978. static int test_wc_AesSetIV(void)
  14979. {
  14980. int res = TEST_SKIPPED;
  14981. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14982. Aes aes;
  14983. int ret = 0;
  14984. byte key16[] =
  14985. {
  14986. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14987. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14988. };
  14989. byte iv1[] = "1234567890abcdef";
  14990. byte iv2[] = "0987654321fedcba";
  14991. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14992. if (ret != 0)
  14993. return ret;
  14994. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14995. iv1, AES_ENCRYPTION);
  14996. if (ret == 0) {
  14997. ret = wc_AesSetIV(&aes, iv2);
  14998. }
  14999. /* Test bad args. */
  15000. if (ret == 0) {
  15001. ret = wc_AesSetIV(NULL, iv1);
  15002. if (ret == BAD_FUNC_ARG) {
  15003. /* NULL iv should return 0. */
  15004. ret = wc_AesSetIV(&aes, NULL);
  15005. }
  15006. else {
  15007. ret = WOLFSSL_FATAL_ERROR;
  15008. }
  15009. }
  15010. wc_AesFree(&aes);
  15011. res = TEST_RES_CHECK(ret == 0);
  15012. #endif
  15013. return res;
  15014. } /* test_wc_AesSetIV */
  15015. /*
  15016. * Testing function for wc_AesSetKey().
  15017. */
  15018. static int test_wc_AesSetKey(void)
  15019. {
  15020. EXPECT_DECLS;
  15021. #ifndef NO_AES
  15022. Aes aes;
  15023. byte key16[] = {
  15024. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15025. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15026. };
  15027. #ifdef WOLFSSL_AES_192
  15028. byte key24[] = {
  15029. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15030. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15031. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15032. };
  15033. #endif
  15034. #ifdef WOLFSSL_AES_256
  15035. byte key32[] = {
  15036. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15037. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15038. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15039. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15040. };
  15041. #endif
  15042. byte badKey16[] = {
  15043. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15044. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15045. };
  15046. byte iv[] = "1234567890abcdef";
  15047. XMEMSET(&aes, 0, sizeof(Aes));
  15048. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15049. #ifdef WOLFSSL_AES_128
  15050. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  15051. iv, AES_ENCRYPTION), 0);
  15052. #endif
  15053. #ifdef WOLFSSL_AES_192
  15054. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  15055. iv, AES_ENCRYPTION), 0);
  15056. #endif
  15057. #ifdef WOLFSSL_AES_256
  15058. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  15059. iv, AES_ENCRYPTION), 0);
  15060. #endif
  15061. /* Pass in bad args. */
  15062. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  15063. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  15064. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  15065. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  15066. BAD_FUNC_ARG);
  15067. wc_AesFree(&aes);
  15068. #endif
  15069. return EXPECT_RESULT();
  15070. } /* END test_wc_AesSetKey */
  15071. /*
  15072. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  15073. * and wc_AesCbcDecryptWithKey()
  15074. */
  15075. static int test_wc_AesCbcEncryptDecrypt(void)
  15076. {
  15077. EXPECT_DECLS;
  15078. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  15079. defined(WOLFSSL_AES_256)
  15080. Aes aes;
  15081. byte key32[] = {
  15082. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15083. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15084. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15085. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15086. };
  15087. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  15088. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  15089. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  15090. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  15091. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  15092. };
  15093. byte iv[] = "1234567890abcdef";
  15094. byte enc[sizeof(vector)];
  15095. byte dec[sizeof(vector)];
  15096. byte dec2[sizeof(vector)];
  15097. /* Init stack variables. */
  15098. XMEMSET(&aes, 0, sizeof(Aes));
  15099. XMEMSET(enc, 0, sizeof(enc));
  15100. XMEMSET(dec, 0, sizeof(vector));
  15101. XMEMSET(dec2, 0, sizeof(vector));
  15102. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15103. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15104. AES_ENCRYPTION), 0);
  15105. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  15106. wc_AesFree(&aes);
  15107. /* Re init for decrypt and set flag. */
  15108. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  15109. AES_DECRYPTION), 0);
  15110. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  15111. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15112. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  15113. sizeof(key32)/sizeof(byte), iv), 0);
  15114. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  15115. /* Pass in bad args */
  15116. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  15117. BAD_FUNC_ARG);
  15118. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  15119. BAD_FUNC_ARG);
  15120. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  15121. BAD_FUNC_ARG);
  15122. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15123. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  15124. BAD_LENGTH_E);
  15125. #endif
  15126. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15127. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15128. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  15129. "mode (v2), skip test");
  15130. #else
  15131. /* Test passing in size of 0 */
  15132. XMEMSET(enc, 0, sizeof(enc));
  15133. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  15134. /* Check enc was not modified */
  15135. {
  15136. int i;
  15137. for (i = 0; i < (int)sizeof(enc); i++)
  15138. ExpectIntEQ(enc[i], 0);
  15139. }
  15140. #endif
  15141. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  15142. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  15143. BAD_FUNC_ARG);
  15144. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  15145. BAD_FUNC_ARG);
  15146. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15147. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15148. BAD_LENGTH_E);
  15149. #else
  15150. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  15151. BAD_FUNC_ARG);
  15152. #endif
  15153. /* Test passing in size of 0 */
  15154. XMEMSET(dec, 0, sizeof(dec));
  15155. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  15156. /* Check dec was not modified */
  15157. {
  15158. int i;
  15159. for (i = 0; i < (int)sizeof(dec); i++)
  15160. ExpectIntEQ(dec[i], 0);
  15161. }
  15162. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15163. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15164. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15165. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15166. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15167. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  15168. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15169. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  15170. wc_AesFree(&aes);
  15171. #endif
  15172. return EXPECT_RESULT();
  15173. } /* END test_wc_AesCbcEncryptDecrypt */
  15174. /*
  15175. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15176. */
  15177. static int test_wc_AesCtrEncryptDecrypt(void)
  15178. {
  15179. EXPECT_DECLS;
  15180. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15181. Aes aesEnc;
  15182. Aes aesDec;
  15183. byte key32[] = {
  15184. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15185. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15186. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15187. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15188. };
  15189. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15190. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15191. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15192. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15193. };
  15194. byte iv[] = "1234567890abcdef";
  15195. byte enc[AES_BLOCK_SIZE * 2];
  15196. byte dec[AES_BLOCK_SIZE * 2];
  15197. /* Init stack variables. */
  15198. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15199. XMEMSET(&aesDec, 0, sizeof(Aes));
  15200. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15201. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15202. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15203. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15204. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  15205. AES_ENCRYPTION), 0);
  15206. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15207. sizeof(vector)/sizeof(byte)), 0);
  15208. /* Decrypt with wc_AesCtrEncrypt() */
  15209. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  15210. AES_ENCRYPTION), 0);
  15211. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  15212. 0);
  15213. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15214. /* Test bad args. */
  15215. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  15216. BAD_FUNC_ARG);
  15217. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  15218. BAD_FUNC_ARG);
  15219. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  15220. BAD_FUNC_ARG);
  15221. wc_AesFree(&aesEnc);
  15222. wc_AesFree(&aesDec);
  15223. #endif
  15224. return EXPECT_RESULT();
  15225. } /* END test_wc_AesCtrEncryptDecrypt */
  15226. /*
  15227. * test function for wc_AesGcmSetKey()
  15228. */
  15229. static int test_wc_AesGcmSetKey(void)
  15230. {
  15231. EXPECT_DECLS;
  15232. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15233. Aes aes;
  15234. #ifdef WOLFSSL_AES_128
  15235. byte key16[] = {
  15236. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15237. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15238. };
  15239. #endif
  15240. #ifdef WOLFSSL_AES_192
  15241. byte key24[] = {
  15242. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15243. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15244. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15245. };
  15246. #endif
  15247. #ifdef WOLFSSL_AES_256
  15248. byte key32[] = {
  15249. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 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, 0x66
  15253. };
  15254. #endif
  15255. byte badKey16[] = {
  15256. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15257. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15258. };
  15259. byte badKey24[] = {
  15260. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15261. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15262. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15263. };
  15264. byte badKey32[] = {
  15265. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15267. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15268. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15269. };
  15270. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15271. #ifdef WOLFSSL_AES_128
  15272. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  15273. #endif
  15274. #ifdef WOLFSSL_AES_192
  15275. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  15276. #endif
  15277. #ifdef WOLFSSL_AES_256
  15278. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15279. #endif
  15280. /* Pass in bad args. */
  15281. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  15282. BAD_FUNC_ARG);
  15283. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  15284. BAD_FUNC_ARG);
  15285. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  15286. BAD_FUNC_ARG);
  15287. wc_AesFree(&aes);
  15288. #endif
  15289. return EXPECT_RESULT();
  15290. } /* END test_wc_AesGcmSetKey */
  15291. /*
  15292. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15293. */
  15294. static int test_wc_AesGcmEncryptDecrypt(void)
  15295. {
  15296. EXPECT_DECLS;
  15297. /* WOLFSSL_AFALG requires 12 byte IV */
  15298. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15299. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15300. Aes aes;
  15301. byte key32[] = {
  15302. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15303. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15304. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15305. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15306. };
  15307. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15308. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15309. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15310. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15311. };
  15312. const byte a[] = {
  15313. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15314. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15315. 0xab, 0xad, 0xda, 0xd2
  15316. };
  15317. byte iv[] = "1234567890a";
  15318. byte longIV[] = "1234567890abcdefghij";
  15319. byte enc[sizeof(vector)];
  15320. byte resultT[AES_BLOCK_SIZE];
  15321. byte dec[sizeof(vector)];
  15322. /* Init stack variables. */
  15323. XMEMSET(&aes, 0, sizeof(Aes));
  15324. XMEMSET(enc, 0, sizeof(vector));
  15325. XMEMSET(dec, 0, sizeof(vector));
  15326. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15327. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  15328. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  15329. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15330. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15331. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  15332. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  15333. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  15334. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15335. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  15336. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15337. BAD_FUNC_ARG);
  15338. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15339. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15340. BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  15342. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  15343. BAD_FUNC_ARG);
  15344. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15345. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15346. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15347. /* FIPS does not check the lower bound of ivSz */
  15348. #else
  15349. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  15350. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15351. #endif
  15352. /* This case is now considered good. Long IVs are now allowed.
  15353. * Except for the original FIPS release, it still has an upper
  15354. * bound on the IV length. */
  15355. #if (!defined(HAVE_FIPS) || \
  15356. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15357. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15358. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15359. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15360. 0);
  15361. #else
  15362. (void)longIV;
  15363. #endif /* Old FIPS */
  15364. /* END wc_AesGcmEncrypt */
  15365. #ifdef HAVE_AES_DECRYPT
  15366. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15367. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15368. BAD_FUNC_ARG);
  15369. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  15370. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15371. BAD_FUNC_ARG);
  15372. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  15373. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15374. BAD_FUNC_ARG);
  15375. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  15376. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  15377. BAD_FUNC_ARG);
  15378. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15379. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  15380. BAD_FUNC_ARG);
  15381. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  15382. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  15383. BAD_FUNC_ARG);
  15384. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15385. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15386. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15387. /* FIPS does not check the lower bound of ivSz */
  15388. #else
  15389. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15390. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  15391. #endif
  15392. #endif /* HAVE_AES_DECRYPT */
  15393. wc_AesFree(&aes);
  15394. #endif
  15395. return EXPECT_RESULT();
  15396. } /* END test_wc_AesGcmEncryptDecrypt */
  15397. /*
  15398. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  15399. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  15400. */
  15401. static int test_wc_AesGcmMixedEncDecLongIV(void)
  15402. {
  15403. EXPECT_DECLS;
  15404. #if (!defined(HAVE_FIPS) || \
  15405. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15406. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  15407. const byte key[] = {
  15408. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15409. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15410. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15411. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15412. };
  15413. const byte in[] = {
  15414. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15415. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15416. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15417. };
  15418. const byte aad[] = {
  15419. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15420. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15421. 0xab, 0xad, 0xda, 0xd2
  15422. };
  15423. Aes aesEnc;
  15424. Aes aesDec;
  15425. byte iv[] = "1234567890abcdefghij";
  15426. byte out[sizeof(in)];
  15427. byte plain[sizeof(in)];
  15428. byte tag[AES_BLOCK_SIZE];
  15429. XMEMSET(&aesEnc, 0, sizeof(Aes));
  15430. XMEMSET(&aesDec, 0, sizeof(Aes));
  15431. XMEMSET(out, 0, sizeof(out));
  15432. XMEMSET(plain, 0, sizeof(plain));
  15433. XMEMSET(tag, 0, sizeof(tag));
  15434. /* Perform one-shot encryption using long IV */
  15435. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  15436. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  15437. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  15438. tag, sizeof(tag), aad, sizeof(aad)), 0);
  15439. /* Perform streaming decryption using long IV */
  15440. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  15441. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  15442. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  15443. sizeof(aad)), 0);
  15444. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  15445. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  15446. /* Free resources */
  15447. wc_AesFree(&aesEnc);
  15448. wc_AesFree(&aesDec);
  15449. #endif
  15450. return EXPECT_RESULT();
  15451. } /* END wc_AesGcmMixedEncDecLongIV */
  15452. /*
  15453. * unit test for wc_GmacSetKey()
  15454. */
  15455. static int test_wc_GmacSetKey(void)
  15456. {
  15457. EXPECT_DECLS;
  15458. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15459. Gmac gmac;
  15460. byte key16[] = {
  15461. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15462. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15463. };
  15464. #ifdef WOLFSSL_AES_192
  15465. byte key24[] = {
  15466. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15467. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15468. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15469. };
  15470. #endif
  15471. #ifdef WOLFSSL_AES_256
  15472. byte key32[] = {
  15473. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15474. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15475. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15476. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15477. };
  15478. #endif
  15479. byte badKey16[] = {
  15480. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15481. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15482. };
  15483. byte badKey24[] = {
  15484. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15485. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15486. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15487. };
  15488. byte badKey32[] = {
  15489. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15490. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15491. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15492. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15493. };
  15494. XMEMSET(&gmac, 0, sizeof(Gmac));
  15495. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15496. #ifdef WOLFSSL_AES_128
  15497. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  15498. #endif
  15499. #ifdef WOLFSSL_AES_192
  15500. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15501. #endif
  15502. #ifdef WOLFSSL_AES_256
  15503. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15504. #endif
  15505. /* Pass in bad args. */
  15506. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  15507. BAD_FUNC_ARG);
  15508. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  15509. BAD_FUNC_ARG);
  15510. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  15511. BAD_FUNC_ARG);
  15512. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  15513. BAD_FUNC_ARG);
  15514. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  15515. BAD_FUNC_ARG);
  15516. wc_AesFree(&gmac.aes);
  15517. #endif
  15518. return EXPECT_RESULT();
  15519. } /* END test_wc_GmacSetKey */
  15520. /*
  15521. * unit test for wc_GmacUpdate
  15522. */
  15523. static int test_wc_GmacUpdate(void)
  15524. {
  15525. EXPECT_DECLS;
  15526. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15527. Gmac gmac;
  15528. #ifdef WOLFSSL_AES_128
  15529. const byte key16[] = {
  15530. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15531. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15532. };
  15533. #endif
  15534. #ifdef WOLFSSL_AES_192
  15535. byte key24[] = {
  15536. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15537. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15538. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15539. };
  15540. #endif
  15541. #ifdef WOLFSSL_AES_256
  15542. byte key32[] = {
  15543. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15544. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15545. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15546. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15547. };
  15548. #endif
  15549. #ifdef WOLFSSL_AES_128
  15550. const byte authIn[] = {
  15551. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15552. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15553. };
  15554. #endif
  15555. #ifdef WOLFSSL_AES_192
  15556. const byte authIn2[] = {
  15557. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15558. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15559. };
  15560. #endif
  15561. const byte authIn3[] = {
  15562. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15563. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15564. };
  15565. #ifdef WOLFSSL_AES_128
  15566. const byte tag1[] = { /* Known. */
  15567. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15568. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15569. };
  15570. #endif
  15571. #ifdef WOLFSSL_AES_192
  15572. const byte tag2[] = { /* Known */
  15573. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15574. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15575. };
  15576. #endif
  15577. const byte tag3[] = { /* Known */
  15578. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15579. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15580. };
  15581. #ifdef WOLFSSL_AES_128
  15582. const byte iv[] = {
  15583. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15584. 0xe2, 0x8c, 0x8f, 0x16
  15585. };
  15586. #endif
  15587. #ifdef WOLFSSL_AES_192
  15588. const byte iv2[] = {
  15589. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15590. 0x7e, 0x1a, 0x6f, 0xbc
  15591. };
  15592. #endif
  15593. const byte iv3[] = {
  15594. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15595. 0xc3, 0xfb, 0x6c, 0x8a
  15596. };
  15597. byte tagOut[16];
  15598. byte tagOut2[24];
  15599. byte tagOut3[32];
  15600. /* Init stack variables. */
  15601. XMEMSET(&gmac, 0, sizeof(Gmac));
  15602. XMEMSET(tagOut, 0, sizeof(tagOut));
  15603. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15604. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15605. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  15606. #ifdef WOLFSSL_AES_128
  15607. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  15608. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15609. tagOut, sizeof(tag1)), 0);
  15610. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  15611. #endif
  15612. #ifdef WOLFSSL_AES_192
  15613. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15614. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  15615. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  15616. tagOut2, sizeof(tag2)), 0);
  15617. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  15618. #endif
  15619. #ifdef WOLFSSL_AES_256
  15620. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  15621. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  15622. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15623. tagOut3, sizeof(tag3)), 0);
  15624. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  15625. #endif
  15626. /* Pass bad args. */
  15627. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15628. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  15629. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15630. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  15631. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  15632. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  15633. wc_AesFree(&gmac.aes);
  15634. #endif
  15635. return EXPECT_RESULT();
  15636. } /* END test_wc_GmacUpdate */
  15637. /*
  15638. * testing wc_CamelliaSetKey
  15639. */
  15640. static int test_wc_CamelliaSetKey(void)
  15641. {
  15642. EXPECT_DECLS;
  15643. #ifdef HAVE_CAMELLIA
  15644. Camellia camellia;
  15645. /*128-bit key*/
  15646. static const byte key16[] = {
  15647. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15648. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15649. };
  15650. /* 192-bit key */
  15651. static const byte key24[] = {
  15652. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15653. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15654. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15655. };
  15656. /* 256-bit key */
  15657. static const byte key32[] = {
  15658. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15659. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15660. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15661. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15662. };
  15663. static const byte iv[] = {
  15664. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15665. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15666. };
  15667. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  15668. 0);
  15669. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  15670. NULL), 0);
  15671. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15672. 0);
  15673. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15674. NULL), 0);
  15675. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  15676. 0);
  15677. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  15678. NULL), 0);
  15679. /* Bad args. */
  15680. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  15681. BAD_FUNC_ARG);
  15682. #endif
  15683. return EXPECT_RESULT();
  15684. } /* END test_wc_CammeliaSetKey */
  15685. /*
  15686. * Testing wc_CamelliaSetIV()
  15687. */
  15688. static int test_wc_CamelliaSetIV(void)
  15689. {
  15690. EXPECT_DECLS;
  15691. #ifdef HAVE_CAMELLIA
  15692. Camellia camellia;
  15693. static const byte iv[] = {
  15694. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15695. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15696. };
  15697. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  15698. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  15699. /* Bad args. */
  15700. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  15701. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  15702. #endif
  15703. return EXPECT_RESULT();
  15704. } /* END test_wc_CamelliaSetIV*/
  15705. /*
  15706. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15707. */
  15708. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15709. {
  15710. EXPECT_DECLS;
  15711. #ifdef HAVE_CAMELLIA
  15712. Camellia camellia;
  15713. static const byte key24[] = {
  15714. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15715. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15716. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15717. };
  15718. static const byte iv[] = {
  15719. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15720. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15721. };
  15722. static const byte plainT[] = {
  15723. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15724. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15725. };
  15726. byte enc[sizeof(plainT)];
  15727. byte dec[sizeof(enc)];
  15728. /* Init stack variables.*/
  15729. XMEMSET(enc, 0, 16);
  15730. XMEMSET(enc, 0, 16);
  15731. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  15732. 0);
  15733. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  15734. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  15735. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15736. /* Pass bad args. */
  15737. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  15738. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  15739. BAD_FUNC_ARG);
  15740. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  15741. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  15742. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  15743. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  15744. #endif
  15745. return EXPECT_RESULT();
  15746. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15747. /*
  15748. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15749. */
  15750. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15751. {
  15752. EXPECT_DECLS;
  15753. #ifdef HAVE_CAMELLIA
  15754. Camellia camellia;
  15755. static const byte key24[] = {
  15756. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15757. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15758. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15759. };
  15760. static const byte plainT[] = {
  15761. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15762. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15763. };
  15764. byte enc[CAMELLIA_BLOCK_SIZE];
  15765. byte dec[CAMELLIA_BLOCK_SIZE];
  15766. /* Init stack variables. */
  15767. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15768. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15769. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15770. NULL), 0);
  15771. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  15772. CAMELLIA_BLOCK_SIZE), 0);
  15773. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  15774. NULL), 0);
  15775. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  15776. 0);
  15777. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  15778. /* Pass in bad args. */
  15779. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  15780. BAD_FUNC_ARG);
  15781. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15782. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15783. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15784. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15785. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  15786. BAD_FUNC_ARG);
  15787. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15788. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15789. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15790. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  15791. #endif
  15792. return EXPECT_RESULT();
  15793. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15794. /*
  15795. * Testing wc_Arc4SetKey()
  15796. */
  15797. static int test_wc_Arc4SetKey(void)
  15798. {
  15799. EXPECT_DECLS;
  15800. #ifndef NO_RC4
  15801. Arc4 arc;
  15802. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15803. int keyLen = 8;
  15804. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  15805. /* Test bad args. */
  15806. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  15807. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  15808. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  15809. #endif
  15810. return EXPECT_RESULT();
  15811. } /* END test_wc_Arc4SetKey */
  15812. /*
  15813. * Testing wc_Arc4Process for ENC/DEC.
  15814. */
  15815. static int test_wc_Arc4Process(void)
  15816. {
  15817. EXPECT_DECLS;
  15818. #ifndef NO_RC4
  15819. Arc4 enc;
  15820. Arc4 dec;
  15821. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15822. int keyLen = 8;
  15823. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15824. byte cipher[8];
  15825. byte plain[8];
  15826. /* Init stack variables */
  15827. XMEMSET(&enc, 0, sizeof(Arc4));
  15828. XMEMSET(&dec, 0, sizeof(Arc4));
  15829. XMEMSET(cipher, 0, sizeof(cipher));
  15830. XMEMSET(plain, 0, sizeof(plain));
  15831. /* Use for async. */
  15832. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  15833. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  15834. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  15835. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  15836. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  15837. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  15838. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  15839. /* Bad args. */
  15840. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  15841. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  15842. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  15843. wc_Arc4Free(&enc);
  15844. wc_Arc4Free(&dec);
  15845. #endif
  15846. return EXPECT_RESULT();
  15847. } /* END test_wc_Arc4Process */
  15848. /*
  15849. * Testing wc_Init RsaKey()
  15850. */
  15851. static int test_wc_InitRsaKey(void)
  15852. {
  15853. EXPECT_DECLS;
  15854. #ifndef NO_RSA
  15855. RsaKey key;
  15856. XMEMSET(&key, 0, sizeof(RsaKey));
  15857. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15858. /* Test bad args. */
  15859. #ifndef HAVE_USER_RSA
  15860. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  15861. #else
  15862. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), USER_CRYPTO_ERROR);
  15863. #endif
  15864. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15865. #endif
  15866. return EXPECT_RESULT();
  15867. } /* END test_wc_InitRsaKey */
  15868. /*
  15869. * Testing wc_RsaPrivateKeyDecode()
  15870. */
  15871. static int test_wc_RsaPrivateKeyDecode(void)
  15872. {
  15873. EXPECT_DECLS;
  15874. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15875. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15876. RsaKey key;
  15877. byte* tmp = NULL;
  15878. word32 idx = 0;
  15879. int bytes = 0;
  15880. XMEMSET(&key, 0, sizeof(RsaKey));
  15881. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  15882. DYNAMIC_TYPE_TMP_BUFFER));
  15883. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  15884. if (tmp != NULL) {
  15885. #ifdef USE_CERT_BUFFERS_1024
  15886. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15887. bytes = sizeof_client_key_der_1024;
  15888. #else
  15889. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15890. bytes = sizeof_client_key_der_2048;
  15891. #endif /* Use cert buffers. */
  15892. }
  15893. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  15894. #ifndef HAVE_USER_RSA
  15895. /* Test bad args. */
  15896. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  15897. BAD_FUNC_ARG);
  15898. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  15899. BAD_FUNC_ARG);
  15900. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15901. BAD_FUNC_ARG);
  15902. #else
  15903. /* Test bad args. User RSA. */
  15904. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  15905. USER_CRYPTO_ERROR);
  15906. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  15907. USER_CRYPTO_ERROR);
  15908. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15909. USER_CRYPTO_ERROR);
  15910. #endif
  15911. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15912. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  15913. #endif
  15914. return EXPECT_RESULT();
  15915. } /* END test_wc_RsaPrivateKeyDecode */
  15916. /*
  15917. * Testing wc_RsaPublicKeyDecode()
  15918. */
  15919. static int test_wc_RsaPublicKeyDecode(void)
  15920. {
  15921. EXPECT_DECLS;
  15922. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15923. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15924. RsaKey keyPub;
  15925. byte* tmp = NULL;
  15926. word32 idx = 0;
  15927. int bytes = 0;
  15928. word32 keySz = 0;
  15929. word32 tstKeySz = 0;
  15930. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15931. XFILE f = XBADFILE;
  15932. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  15933. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  15934. byte buf[4096];
  15935. #endif
  15936. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  15937. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  15938. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  15939. if (tmp != NULL) {
  15940. #ifdef USE_CERT_BUFFERS_1024
  15941. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15942. bytes = sizeof_client_keypub_der_1024;
  15943. keySz = 1024;
  15944. #else
  15945. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15946. bytes = sizeof_client_keypub_der_2048;
  15947. keySz = 2048;
  15948. #endif
  15949. }
  15950. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  15951. #ifndef HAVE_USER_RSA
  15952. /* Pass in bad args. */
  15953. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  15954. BAD_FUNC_ARG);
  15955. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  15956. BAD_FUNC_ARG);
  15957. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15958. BAD_FUNC_ARG);
  15959. #else
  15960. /* Pass in bad args. */
  15961. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  15962. USER_CRYPTO_ERROR);
  15963. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  15964. USER_CRYPTO_ERROR);
  15965. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  15966. USER_CRYPTO_ERROR);
  15967. #endif
  15968. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  15969. /* Test for getting modulus key size */
  15970. idx = 0;
  15971. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15972. &tstKeySz, NULL, NULL), 0);
  15973. ExpectIntEQ(tstKeySz, keySz/8);
  15974. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15975. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  15976. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  15977. if (f != XBADFILE) {
  15978. XFCLOSE(f);
  15979. f = XBADFILE;
  15980. }
  15981. idx = 0;
  15982. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15983. NULL), 0);
  15984. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  15985. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  15986. if (f != XBADFILE)
  15987. XFCLOSE(f);
  15988. idx = 0;
  15989. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15990. NULL), 0);
  15991. #endif
  15992. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15993. #endif
  15994. return EXPECT_RESULT();
  15995. } /* END test_wc_RsaPublicKeyDecode */
  15996. /*
  15997. * Testing wc_RsaPublicKeyDecodeRaw()
  15998. */
  15999. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16000. {
  16001. EXPECT_DECLS;
  16002. #if !defined(NO_RSA)
  16003. RsaKey key;
  16004. const byte n = 0x23;
  16005. const byte e = 0x03;
  16006. int nSz = sizeof(n);
  16007. int eSz = sizeof(e);
  16008. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16009. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  16010. #ifndef HAVE_USER_RSA
  16011. /* Pass in bad args. */
  16012. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16013. BAD_FUNC_ARG);
  16014. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16015. BAD_FUNC_ARG);
  16016. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16017. BAD_FUNC_ARG);
  16018. #else
  16019. /* Pass in bad args. User RSA. */
  16020. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  16021. USER_CRYPTO_ERROR);
  16022. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  16023. USER_CRYPTO_ERROR);
  16024. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  16025. USER_CRYPTO_ERROR);
  16026. #endif
  16027. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16028. #endif
  16029. return EXPECT_RESULT();
  16030. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16031. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16032. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16033. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16034. * trying until it gets a probable prime. */
  16035. #ifdef HAVE_FIPS
  16036. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16037. {
  16038. int ret;
  16039. for (;;) {
  16040. ret = wc_MakeRsaKey(key, size, e, rng);
  16041. if (ret != PRIME_GEN_E) break;
  16042. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  16043. "trying again.\n");
  16044. }
  16045. return ret;
  16046. }
  16047. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16048. #else
  16049. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16050. #endif
  16051. #endif
  16052. /*
  16053. * Testing wc_MakeRsaKey()
  16054. */
  16055. static int test_wc_MakeRsaKey(void)
  16056. {
  16057. EXPECT_DECLS;
  16058. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16059. RsaKey genKey;
  16060. WC_RNG rng;
  16061. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16062. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16063. int bits = 1024;
  16064. #else
  16065. int bits = 2048;
  16066. #endif
  16067. XMEMSET(&genKey, 0, sizeof(RsaKey));
  16068. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16069. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16070. ExpectIntEQ(wc_InitRng(&rng), 0);
  16071. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16072. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16073. #ifndef HAVE_USER_RSA
  16074. /* Test bad args. */
  16075. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  16076. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16077. BAD_FUNC_ARG);
  16078. /* e < 3 */
  16079. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  16080. /* e & 1 == 0 */
  16081. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  16082. #else
  16083. /* Test bad args. */
  16084. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng),
  16085. USER_CRYPTO_ERROR);
  16086. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  16087. USER_CRYPTO_ERROR);
  16088. /* e < 3 */
  16089. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), USER_CRYPTO_ERROR);
  16090. /* e & 1 == 0 */
  16091. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), USER_CRYPTO_ERROR);
  16092. #endif /* HAVE_USER_RSA */
  16093. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16094. #endif
  16095. return EXPECT_RESULT();
  16096. } /* END test_wc_MakeRsaKey */
  16097. /*
  16098. * Test the bounds checking on the cipher text versus the key modulus.
  16099. * 1. Make a new RSA key.
  16100. * 2. Set c to 1.
  16101. * 3. Decrypt c into k. (error)
  16102. * 4. Copy the key modulus to c and sub 1 from the copy.
  16103. * 5. Decrypt c into k. (error)
  16104. * Valid bounds test cases are covered by all the other RSA tests.
  16105. */
  16106. static int test_RsaDecryptBoundsCheck(void)
  16107. {
  16108. EXPECT_DECLS;
  16109. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16110. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16111. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16112. WC_RNG rng;
  16113. RsaKey key;
  16114. byte flatC[256];
  16115. word32 flatCSz;
  16116. byte out[256];
  16117. word32 outSz = sizeof(out);
  16118. XMEMSET(&key, 0, sizeof(RsaKey));
  16119. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16120. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16121. ExpectIntEQ(wc_InitRng(&rng), 0);
  16122. if (EXPECT_SUCCESS()) {
  16123. const byte* derKey;
  16124. word32 derKeySz;
  16125. word32 idx = 0;
  16126. #ifdef USE_CERT_BUFFERS_1024
  16127. derKey = server_key_der_1024;
  16128. derKeySz = (word32)sizeof_server_key_der_1024;
  16129. flatCSz = 128;
  16130. #else
  16131. derKey = server_key_der_2048;
  16132. derKeySz = (word32)sizeof_server_key_der_2048;
  16133. flatCSz = 256;
  16134. #endif
  16135. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  16136. }
  16137. if (EXPECT_SUCCESS()) {
  16138. XMEMSET(flatC, 0, flatCSz);
  16139. flatC[flatCSz-1] = 1;
  16140. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16141. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  16142. if (EXPECT_SUCCESS()) {
  16143. mp_int c;
  16144. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  16145. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  16146. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  16147. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16148. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  16149. mp_clear(&c);
  16150. }
  16151. }
  16152. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16153. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16154. #endif
  16155. return EXPECT_RESULT();
  16156. } /* END test_wc_RsaDecryptBoundsCheck */
  16157. /*
  16158. * Testing wc_SetKeyUsage()
  16159. */
  16160. static int test_wc_SetKeyUsage(void)
  16161. {
  16162. EXPECT_DECLS;
  16163. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16164. Cert myCert;
  16165. ExpectIntEQ(wc_InitCert(&myCert), 0);
  16166. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  16167. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  16168. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  16169. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  16170. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  16171. /* Test bad args. */
  16172. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  16173. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  16174. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  16175. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  16176. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  16177. KEYUSAGE_E);
  16178. #endif
  16179. return EXPECT_RESULT();
  16180. } /* END test_wc_SetKeyUsage */
  16181. /*
  16182. * Testing wc_CheckProbablePrime()
  16183. */
  16184. static int test_wc_CheckProbablePrime(void)
  16185. {
  16186. EXPECT_DECLS;
  16187. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16188. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16189. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16190. RsaKey key;
  16191. WC_RNG rng;
  16192. byte e[3];
  16193. word32 eSz = (word32)sizeof(e);
  16194. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16195. word32 nSz = (word32)sizeof(n);
  16196. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16197. word32 dSz = (word32)sizeof(d);
  16198. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16199. word32 pSz = (word32)sizeof(p);
  16200. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16201. word32 qSz = (word32)sizeof(q);
  16202. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16203. int* isPrime;
  16204. int test[5];
  16205. isPrime = test;
  16206. XMEMSET(&key, 0, sizeof(RsaKey));
  16207. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16208. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16209. ExpectIntEQ(wc_InitRng(&rng), 0);
  16210. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16211. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  16212. WC_RSA_EXPONENT, &rng), 0);
  16213. PRIVATE_KEY_UNLOCK();
  16214. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  16215. &qSz), 0);
  16216. PRIVATE_KEY_LOCK();
  16217. /* Bad cases */
  16218. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  16219. BAD_FUNC_ARG);
  16220. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  16221. BAD_FUNC_ARG);
  16222. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  16223. BAD_FUNC_ARG);
  16224. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  16225. BAD_FUNC_ARG);
  16226. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  16227. BAD_FUNC_ARG);
  16228. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  16229. BAD_FUNC_ARG);
  16230. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  16231. BAD_FUNC_ARG);
  16232. /* Good case */
  16233. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  16234. 0);
  16235. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16236. wc_FreeRng(&rng);
  16237. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16238. #endif
  16239. return EXPECT_RESULT();
  16240. } /* END test_wc_CheckProbablePrime */
  16241. /*
  16242. * Testing wc_RsaPSS_Verify()
  16243. */
  16244. static int test_wc_RsaPSS_Verify(void)
  16245. {
  16246. EXPECT_DECLS;
  16247. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16248. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16249. RsaKey key;
  16250. WC_RNG rng;
  16251. int sz = 256;
  16252. const char* szMessage = "This is the string to be signed";
  16253. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16254. unsigned char pDecrypted[2048/8];
  16255. byte* pt = pDecrypted;
  16256. word32 outLen = sizeof(pDecrypted);
  16257. XMEMSET(&key, 0, sizeof(RsaKey));
  16258. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16259. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16260. ExpectIntEQ(wc_InitRng(&rng), 0);
  16261. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16262. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16263. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  16264. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  16265. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16266. /* Bad cases */
  16267. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16268. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16269. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16270. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16271. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16272. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16273. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16274. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16275. /* Good case */
  16276. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16277. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16278. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16279. wc_FreeRng(&rng);
  16280. #endif
  16281. return EXPECT_RESULT();
  16282. } /* END test_wc_RsaPSS_Verify */
  16283. /*
  16284. * Testing wc_RsaPSS_VerifyCheck()
  16285. */
  16286. static int test_wc_RsaPSS_VerifyCheck(void)
  16287. {
  16288. EXPECT_DECLS;
  16289. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16290. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16291. RsaKey key;
  16292. WC_RNG rng;
  16293. int sz = 256; /* 2048/8 */
  16294. byte digest[32];
  16295. word32 digestSz = sizeof(digest);
  16296. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16297. word32 pSignatureSz = sizeof(pSignature);
  16298. unsigned char pDecrypted[2048/8];
  16299. byte* pt = pDecrypted;
  16300. word32 outLen = sizeof(pDecrypted);
  16301. XMEMSET(&key, 0, sizeof(RsaKey));
  16302. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16303. XMEMSET(digest, 0, sizeof(digest));
  16304. XMEMSET(pSignature, 0, sizeof(pSignature));
  16305. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16306. ExpectIntEQ(wc_InitRng(&rng), 0);
  16307. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16308. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16309. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16310. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16311. 0);
  16312. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16313. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16314. /* Bad cases */
  16315. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  16316. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16317. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  16318. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16319. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  16320. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16321. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  16322. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16323. /* Good case */
  16324. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  16325. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16326. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16327. wc_FreeRng(&rng);
  16328. #endif
  16329. return EXPECT_RESULT();
  16330. } /* END test_wc_RsaPSS_VerifyCheck */
  16331. /*
  16332. * Testing wc_RsaPSS_VerifyCheckInline()
  16333. */
  16334. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16335. {
  16336. EXPECT_DECLS;
  16337. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16338. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16339. RsaKey key;
  16340. WC_RNG rng;
  16341. int sz = 256;
  16342. byte digest[32];
  16343. word32 digestSz = sizeof(digest);
  16344. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16345. unsigned char pDecrypted[2048/8];
  16346. byte* pt = pDecrypted;
  16347. XMEMSET(&key, 0, sizeof(RsaKey));
  16348. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16349. XMEMSET(digest, 0, sizeof(digest));
  16350. XMEMSET(pSignature, 0, sizeof(pSignature));
  16351. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16352. ExpectIntEQ(wc_InitRng(&rng), 0);
  16353. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16354. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16355. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  16356. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  16357. 0);
  16358. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  16359. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  16360. /* Bad Cases */
  16361. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  16362. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16363. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  16364. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16365. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  16366. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16367. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16368. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  16369. /* Good case */
  16370. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  16371. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  16372. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16373. wc_FreeRng(&rng);
  16374. #endif
  16375. return EXPECT_RESULT();
  16376. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16377. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16378. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16379. {
  16380. (void)flag;
  16381. (void)type;
  16382. (void)file;
  16383. (void)line;
  16384. }
  16385. #endif
  16386. /*
  16387. * Testing wc_LockMutex_ex
  16388. */
  16389. static int test_wc_LockMutex_ex(void)
  16390. {
  16391. EXPECT_DECLS;
  16392. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16393. int flag = CRYPTO_LOCK;
  16394. int type = 0;
  16395. const char* file = "./test-LockMutex_ex.txt";
  16396. int line = 0;
  16397. /* without SetMutexCb */
  16398. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  16399. /* with SetMutexCb */
  16400. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16401. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  16402. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16403. #endif
  16404. return EXPECT_RESULT();
  16405. } /* End test_wc_LockMutex_ex*/
  16406. /*
  16407. * Testing wc_SetMutexCb
  16408. */
  16409. static int test_wc_SetMutexCb(void)
  16410. {
  16411. EXPECT_DECLS;
  16412. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16413. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  16414. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  16415. #endif
  16416. return EXPECT_RESULT();
  16417. } /* End test_wc_SetMutexCb*/
  16418. /*
  16419. * Testing wc_RsaKeyToDer()
  16420. */
  16421. static int test_wc_RsaKeyToDer(void)
  16422. {
  16423. EXPECT_DECLS;
  16424. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16425. RsaKey genKey;
  16426. WC_RNG rng;
  16427. byte* der = NULL;
  16428. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16429. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16430. int bits = 1024;
  16431. word32 derSz = 611;
  16432. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16433. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16434. #else
  16435. int bits = 2048;
  16436. word32 derSz = 1196;
  16437. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16438. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16439. #endif
  16440. XMEMSET(&rng, 0, sizeof(rng));
  16441. XMEMSET(&genKey, 0, sizeof(genKey));
  16442. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16443. /* Init structures. */
  16444. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  16445. ExpectIntEQ(wc_InitRng(&rng), 0);
  16446. /* Make key. */
  16447. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  16448. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  16449. #ifndef HAVE_USER_RSA
  16450. /* Pass good/bad args. */
  16451. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  16452. /* Get just the output length */
  16453. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16454. /* Try Public Key. */
  16455. genKey.type = 0;
  16456. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  16457. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16458. /* Put back to Private Key */
  16459. genKey.type = 1;
  16460. #endif
  16461. #else
  16462. /* Pass good/bad args. */
  16463. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16464. /* Get just the output length */
  16465. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  16466. /* Try Public Key. */
  16467. genKey.type = 0;
  16468. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), USER_CRYPTO_ERROR);
  16469. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16470. /* Put back to Private Key */
  16471. genKey.type = 1;
  16472. #endif
  16473. #endif
  16474. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16475. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  16476. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16477. #endif
  16478. return EXPECT_RESULT();
  16479. } /* END test_wc_RsaKeyToDer */
  16480. /*
  16481. * Testing wc_RsaKeyToPublicDer()
  16482. */
  16483. static int test_wc_RsaKeyToPublicDer(void)
  16484. {
  16485. EXPECT_DECLS;
  16486. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16487. RsaKey key;
  16488. WC_RNG rng;
  16489. byte* der = NULL;
  16490. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16491. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16492. int bits = 1024;
  16493. word32 derLen = 162;
  16494. #else
  16495. int bits = 2048;
  16496. word32 derLen = 294;
  16497. #endif
  16498. #ifndef HAVE_USER_RSA
  16499. int ret;
  16500. #endif
  16501. XMEMSET(&rng, 0, sizeof(rng));
  16502. XMEMSET(&key, 0, sizeof(key));
  16503. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16504. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16505. ExpectIntEQ(wc_InitRng(&rng), 0);
  16506. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16507. /* test getting size only */
  16508. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  16509. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  16510. /* test getting size only */
  16511. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  16512. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  16513. #ifndef HAVE_USER_RSA
  16514. /* Pass in bad args. */
  16515. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  16516. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  16517. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  16518. #else
  16519. /* Pass in bad args. */
  16520. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), USER_CRYPTO_ERROR);
  16521. ExpectIntEQ(wc_RsaKeyToPublicDer(&key, der, -1), USER_CRYPTO_ERROR);
  16522. #endif
  16523. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16524. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16525. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16526. #endif
  16527. return EXPECT_RESULT();
  16528. } /* END test_wc_RsaKeyToPublicDer */
  16529. /*
  16530. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16531. */
  16532. static int test_wc_RsaPublicEncryptDecrypt(void)
  16533. {
  16534. EXPECT_DECLS;
  16535. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16536. RsaKey key;
  16537. WC_RNG rng;
  16538. const char inStr[] = TEST_STRING;
  16539. const word32 plainLen = (word32)TEST_STRING_SZ;
  16540. const word32 inLen = (word32)TEST_STRING_SZ;
  16541. int bits = TEST_RSA_BITS;
  16542. const word32 cipherLen = TEST_RSA_BYTES;
  16543. word32 cipherLenResult = cipherLen;
  16544. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16545. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16546. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16547. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16548. ExpectNotNull(in);
  16549. ExpectNotNull(plain);
  16550. ExpectNotNull(cipher);
  16551. #endif
  16552. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16553. /* Initialize stack structures. */
  16554. XMEMSET(&key, 0, sizeof(RsaKey));
  16555. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16556. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16557. ExpectIntEQ(wc_InitRng(&rng), 0);
  16558. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16559. /* Encrypt. */
  16560. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  16561. cipherLen, &key, &rng), 0);
  16562. /* Pass bad args - tested in another testing function.*/
  16563. /* Decrypt */
  16564. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16565. /* Bind rng */
  16566. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16567. #endif
  16568. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  16569. &key), 0);
  16570. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  16571. /* Pass bad args - tested in another testing function.*/
  16572. WC_FREE_VAR(in, NULL);
  16573. WC_FREE_VAR(plain, NULL);
  16574. WC_FREE_VAR(cipher, NULL);
  16575. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16576. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16577. #endif
  16578. return EXPECT_RESULT();
  16579. } /* END test_wc_RsaPublicEncryptDecrypt */
  16580. /*
  16581. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16582. */
  16583. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16584. {
  16585. EXPECT_DECLS;
  16586. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16587. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16588. && !defined(NO_SHA256)
  16589. RsaKey key;
  16590. WC_RNG rng;
  16591. const char inStr[] = TEST_STRING;
  16592. const word32 inLen = (word32)TEST_STRING_SZ;
  16593. const word32 plainSz = (word32)TEST_STRING_SZ;
  16594. byte* res = NULL;
  16595. int idx = 0;
  16596. int bits = TEST_RSA_BITS;
  16597. const word32 cipherSz = TEST_RSA_BYTES;
  16598. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16599. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16600. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16601. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16602. ExpectNotNull(in);
  16603. ExpectNotNull(plain);
  16604. ExpectNotNull(cipher);
  16605. #endif
  16606. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16607. /* Initialize stack structures. */
  16608. XMEMSET(&key, 0, sizeof(RsaKey));
  16609. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16610. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  16611. ExpectIntEQ(wc_InitRng(&rng), 0);
  16612. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16613. /* Encrypt */
  16614. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  16615. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16616. /* Pass bad args - tested in another testing function.*/
  16617. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16618. /* Decrypt */
  16619. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16620. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  16621. #endif
  16622. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  16623. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16624. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  16625. /* Pass bad args - tested in another testing function.*/
  16626. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  16627. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  16628. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  16629. #endif
  16630. WC_FREE_VAR(in, NULL);
  16631. WC_FREE_VAR(plain, NULL);
  16632. WC_FREE_VAR(cipher, NULL);
  16633. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16634. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16635. #endif
  16636. return EXPECT_RESULT();
  16637. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16638. /*
  16639. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16640. */
  16641. static int test_wc_RsaSSL_SignVerify(void)
  16642. {
  16643. EXPECT_DECLS;
  16644. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16645. RsaKey key;
  16646. WC_RNG rng;
  16647. const char inStr[] = TEST_STRING;
  16648. const word32 plainSz = (word32)TEST_STRING_SZ;
  16649. const word32 inLen = (word32)TEST_STRING_SZ;
  16650. word32 idx = 0;
  16651. int bits = TEST_RSA_BITS;
  16652. const word32 outSz = TEST_RSA_BYTES;
  16653. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16654. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16655. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16656. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16657. ExpectNotNull(in);
  16658. ExpectNotNull(out);
  16659. ExpectNotNull(plain);
  16660. #endif
  16661. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  16662. XMEMSET(&key, 0, sizeof(RsaKey));
  16663. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16664. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16665. ExpectIntEQ(wc_InitRng(&rng), 0);
  16666. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16667. /* Sign. */
  16668. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  16669. idx = (int)outSz;
  16670. #ifndef HAVE_USER_RSA
  16671. /* Test bad args. */
  16672. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16673. BAD_FUNC_ARG);
  16674. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16675. BAD_FUNC_ARG);
  16676. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16677. BAD_FUNC_ARG);
  16678. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16679. BAD_FUNC_ARG);
  16680. #else
  16681. /* Test bad args. */
  16682. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  16683. USER_CRYPTO_ERROR);
  16684. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  16685. USER_CRYPTO_ERROR);
  16686. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  16687. USER_CRYPTO_ERROR);
  16688. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  16689. USER_CRYPTO_ERROR);
  16690. #endif
  16691. /* Verify. */
  16692. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  16693. #ifndef HAVE_USER_RSA
  16694. /* Pass bad args. */
  16695. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16696. BAD_FUNC_ARG);
  16697. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16698. BAD_FUNC_ARG);
  16699. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16700. BAD_FUNC_ARG);
  16701. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16702. BAD_FUNC_ARG);
  16703. #else
  16704. /* Pass bad args. */
  16705. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  16706. USER_CRYPTO_ERROR);
  16707. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  16708. USER_CRYPTO_ERROR);
  16709. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  16710. USER_CRYPTO_ERROR);
  16711. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  16712. USER_CRYPTO_ERROR);
  16713. #endif
  16714. WC_FREE_VAR(in, NULL);
  16715. WC_FREE_VAR(out, NULL);
  16716. WC_FREE_VAR(plain, NULL);
  16717. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16718. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16719. #endif
  16720. return EXPECT_RESULT();
  16721. } /* END test_wc_RsaSSL_SignVerify */
  16722. /*
  16723. * Testing wc_RsaEncryptSize()
  16724. */
  16725. static int test_wc_RsaEncryptSize(void)
  16726. {
  16727. EXPECT_DECLS;
  16728. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16729. RsaKey key;
  16730. WC_RNG rng;
  16731. XMEMSET(&key, 0, sizeof(RsaKey));
  16732. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16733. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16734. ExpectIntEQ(wc_InitRng(&rng), 0);
  16735. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16736. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16737. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  16738. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  16739. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16740. #endif
  16741. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  16742. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  16743. /* Pass in bad arg. */
  16744. #ifndef HAVE_USER_RSA
  16745. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  16746. #else
  16747. ExpectIntEQ(wc_RsaEncryptSize(NULL), 0);
  16748. #endif
  16749. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16750. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16751. #endif
  16752. return EXPECT_RESULT();
  16753. } /* END test_wc_RsaEncryptSize*/
  16754. /*
  16755. * Testing wc_RsaFlattenPublicKey()
  16756. */
  16757. static int test_wc_RsaFlattenPublicKey(void)
  16758. {
  16759. EXPECT_DECLS;
  16760. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16761. RsaKey key;
  16762. WC_RNG rng;
  16763. byte e[256];
  16764. byte n[256];
  16765. word32 eSz = sizeof(e);
  16766. word32 nSz = sizeof(n);
  16767. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16768. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16769. int bits = 1024;
  16770. #else
  16771. int bits = 2048;
  16772. #endif
  16773. XMEMSET(&key, 0, sizeof(RsaKey));
  16774. XMEMSET(&rng, 0, sizeof(WC_RNG));
  16775. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16776. ExpectIntEQ(wc_InitRng(&rng), 0);
  16777. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  16778. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  16779. #ifndef HAVE_USER_RSA
  16780. /* Pass bad args. */
  16781. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16782. BAD_FUNC_ARG);
  16783. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16784. BAD_FUNC_ARG);
  16785. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16786. BAD_FUNC_ARG);
  16787. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16788. BAD_FUNC_ARG);
  16789. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16790. BAD_FUNC_ARG);
  16791. #else
  16792. /* Pass bad args. */
  16793. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  16794. USER_CRYPTO_ERROR);
  16795. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  16796. USER_CRYPTO_ERROR);
  16797. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  16798. USER_CRYPTO_ERROR);
  16799. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  16800. USER_CRYPTO_ERROR);
  16801. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  16802. USER_CRYPTO_ERROR);
  16803. #endif
  16804. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16805. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  16806. #endif
  16807. return EXPECT_RESULT();
  16808. } /* END test_wc_RsaFlattenPublicKey */
  16809. /*
  16810. * unit test for wc_AesCcmSetKey
  16811. */
  16812. static int test_wc_AesCcmSetKey(void)
  16813. {
  16814. EXPECT_DECLS;
  16815. #ifdef HAVE_AESCCM
  16816. Aes aes;
  16817. const byte key16[] = {
  16818. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16819. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16820. };
  16821. const byte key24[] = {
  16822. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16823. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16824. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16825. };
  16826. const byte key32[] = {
  16827. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16828. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16829. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16830. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16831. };
  16832. XMEMSET(&aes, 0, sizeof(Aes));
  16833. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16834. #ifdef WOLFSSL_AES_128
  16835. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16836. #endif
  16837. #ifdef WOLFSSL_AES_192
  16838. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  16839. #endif
  16840. #ifdef WOLFSSL_AES_256
  16841. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  16842. #endif
  16843. /* Test bad args. */
  16844. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  16845. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  16846. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  16847. wc_AesFree(&aes);
  16848. #endif
  16849. return EXPECT_RESULT();
  16850. } /* END test_wc_AesCcmSetKey */
  16851. /*
  16852. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  16853. */
  16854. static int test_wc_AesCcmEncryptDecrypt(void)
  16855. {
  16856. EXPECT_DECLS;
  16857. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  16858. Aes aes;
  16859. const byte key16[] = {
  16860. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  16861. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  16862. };
  16863. /* plaintext */
  16864. const byte plainT[] = {
  16865. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  16866. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  16867. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  16868. };
  16869. /* nonce */
  16870. const byte iv[] = {
  16871. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  16872. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  16873. };
  16874. const byte c[] = { /* cipher text. */
  16875. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  16876. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  16877. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  16878. };
  16879. const byte t[] = { /* Auth tag */
  16880. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  16881. };
  16882. const byte authIn[] = {
  16883. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  16884. };
  16885. byte cipherOut[sizeof(plainT)];
  16886. byte authTag[sizeof(t)];
  16887. #ifdef HAVE_AES_DECRYPT
  16888. byte plainOut[sizeof(cipherOut)];
  16889. #endif
  16890. XMEMSET(&aes, 0, sizeof(Aes));
  16891. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16892. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  16893. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16894. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  16895. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  16896. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  16897. #ifdef HAVE_AES_DECRYPT
  16898. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16899. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  16900. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  16901. #endif
  16902. /* Pass in bad args. Encrypt*/
  16903. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  16904. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16905. BAD_FUNC_ARG);
  16906. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  16907. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16908. BAD_FUNC_ARG);
  16909. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  16910. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16911. BAD_FUNC_ARG);
  16912. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16913. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16914. BAD_FUNC_ARG);
  16915. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16916. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  16917. BAD_FUNC_ARG);
  16918. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16919. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16920. BAD_FUNC_ARG);
  16921. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  16922. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  16923. BAD_FUNC_ARG);
  16924. #ifdef HAVE_AES_DECRYPT
  16925. /* Pass in bad args. Decrypt*/
  16926. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  16927. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16928. BAD_FUNC_ARG);
  16929. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  16930. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16931. BAD_FUNC_ARG);
  16932. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  16933. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16934. BAD_FUNC_ARG);
  16935. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16936. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16937. BAD_FUNC_ARG);
  16938. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16939. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  16940. BAD_FUNC_ARG);
  16941. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16942. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16943. BAD_FUNC_ARG);
  16944. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  16945. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  16946. BAD_FUNC_ARG);
  16947. #endif
  16948. wc_AesFree(&aes);
  16949. #endif /* HAVE_AESCCM */
  16950. return EXPECT_RESULT();
  16951. } /* END test_wc_AesCcmEncryptDecrypt */
  16952. /*
  16953. * Testing wc_InitDsaKey()
  16954. */
  16955. static int test_wc_InitDsaKey(void)
  16956. {
  16957. EXPECT_DECLS;
  16958. #ifndef NO_DSA
  16959. DsaKey key;
  16960. XMEMSET(&key, 0, sizeof(DsaKey));
  16961. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  16962. /* Pass in bad args. */
  16963. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  16964. wc_FreeDsaKey(&key);
  16965. #endif
  16966. return EXPECT_RESULT();
  16967. } /* END test_wc_InitDsaKey */
  16968. /*
  16969. * Testing wc_DsaSign() and wc_DsaVerify()
  16970. */
  16971. static int test_wc_DsaSignVerify(void)
  16972. {
  16973. EXPECT_DECLS;
  16974. #if !defined(NO_DSA)
  16975. DsaKey key;
  16976. WC_RNG rng;
  16977. wc_Sha sha;
  16978. byte signature[DSA_SIG_SIZE];
  16979. byte hash[WC_SHA_DIGEST_SIZE];
  16980. word32 idx = 0;
  16981. word32 bytes;
  16982. int answer;
  16983. #ifdef USE_CERT_BUFFERS_1024
  16984. byte tmp[ONEK_BUF];
  16985. XMEMSET(tmp, 0, sizeof(tmp));
  16986. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  16987. bytes = sizeof_dsa_key_der_1024;
  16988. #elif defined(USE_CERT_BUFFERS_2048)
  16989. byte tmp[TWOK_BUF];
  16990. XMEMSET(tmp, 0, sizeof(tmp));
  16991. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  16992. bytes = sizeof_dsa_key_der_2048;
  16993. #else
  16994. byte tmp[TWOK_BUF];
  16995. XFILE fp = XBADFILE;
  16996. XMEMSET(tmp, 0, sizeof(tmp));
  16997. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  16998. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  16999. if (fp != XBADFILE)
  17000. XFCLOSE(fp);
  17001. #endif /* END USE_CERT_BUFFERS_1024 */
  17002. ExpectIntEQ(wc_InitSha(&sha), 0);
  17003. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  17004. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  17005. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17006. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17007. ExpectIntEQ(wc_InitRng(&rng), 0);
  17008. /* Sign. */
  17009. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  17010. /* Test bad args. */
  17011. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  17012. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  17013. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  17014. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  17015. /* Verify. */
  17016. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  17017. ExpectIntEQ(answer, 1);
  17018. /* Pass in bad args. */
  17019. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  17020. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  17021. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  17022. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  17023. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17024. /* hard set q to 0 and test fail case */
  17025. mp_free(&key.q);
  17026. mp_init(&key.q);
  17027. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17028. mp_set(&key.q, 1);
  17029. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17030. #endif
  17031. DoExpectIntEQ(wc_FreeRng(&rng),0);
  17032. wc_FreeDsaKey(&key);
  17033. wc_ShaFree(&sha);
  17034. #endif
  17035. return EXPECT_RESULT();
  17036. } /* END test_wc_DsaSign */
  17037. /*
  17038. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17039. */
  17040. static int test_wc_DsaPublicPrivateKeyDecode(void)
  17041. {
  17042. EXPECT_DECLS;
  17043. #if !defined(NO_DSA)
  17044. DsaKey key;
  17045. word32 bytes;
  17046. word32 idx = 0;
  17047. int ret;
  17048. #ifdef USE_CERT_BUFFERS_1024
  17049. byte tmp[ONEK_BUF];
  17050. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17051. bytes = sizeof_dsa_key_der_1024;
  17052. #elif defined(USE_CERT_BUFFERS_2048)
  17053. byte tmp[TWOK_BUF];
  17054. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17055. bytes = sizeof_dsa_key_der_2048;
  17056. #else
  17057. byte tmp[TWOK_BUF];
  17058. XFILE fp = XBADFILE;
  17059. XMEMSET(tmp, 0, sizeof(tmp));
  17060. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  17061. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  17062. if (fp != XBADFILE)
  17063. XFCLOSE(fp);
  17064. #endif /* END USE_CERT_BUFFERS_1024 */
  17065. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17066. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17067. /* Test bad args. */
  17068. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  17069. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  17070. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  17071. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17072. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  17073. wc_FreeDsaKey(&key);
  17074. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17075. idx = 0; /* Reset */
  17076. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  17077. /* Test bad args. */
  17078. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  17079. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  17080. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  17081. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  17082. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  17083. wc_FreeDsaKey(&key);
  17084. #endif /* !NO_DSA */
  17085. return EXPECT_RESULT();
  17086. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17087. /*
  17088. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17089. */
  17090. static int test_wc_MakeDsaKey(void)
  17091. {
  17092. EXPECT_DECLS;
  17093. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17094. DsaKey genKey;
  17095. WC_RNG rng;
  17096. XMEMSET(&genKey, 0, sizeof(genKey));
  17097. XMEMSET(&rng, 0, sizeof(rng));
  17098. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  17099. ExpectIntEQ(wc_InitRng(&rng), 0);
  17100. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  17101. /* Test bad args. */
  17102. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  17103. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  17104. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  17105. BAD_FUNC_ARG);
  17106. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  17107. /* Test bad args. */
  17108. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  17109. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  17110. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17111. wc_FreeDsaKey(&genKey);
  17112. #endif
  17113. return EXPECT_RESULT();
  17114. } /* END test_wc_MakeDsaKey */
  17115. /*
  17116. * Testing wc_DsaKeyToDer()
  17117. */
  17118. static int test_wc_DsaKeyToDer(void)
  17119. {
  17120. EXPECT_DECLS;
  17121. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17122. DsaKey key;
  17123. word32 bytes;
  17124. word32 idx = 0;
  17125. #ifdef USE_CERT_BUFFERS_1024
  17126. byte tmp[ONEK_BUF];
  17127. byte der[ONEK_BUF];
  17128. XMEMSET(tmp, 0, sizeof(tmp));
  17129. XMEMSET(der, 0, sizeof(der));
  17130. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17131. bytes = sizeof_dsa_key_der_1024;
  17132. #elif defined(USE_CERT_BUFFERS_2048)
  17133. byte tmp[TWOK_BUF];
  17134. byte der[TWOK_BUF];
  17135. XMEMSET(tmp, 0, sizeof(tmp));
  17136. XMEMSET(der, 0, sizeof(der));
  17137. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17138. bytes = sizeof_dsa_key_der_2048;
  17139. #else
  17140. byte tmp[TWOK_BUF];
  17141. byte der[TWOK_BUF];
  17142. XFILE fp = XBADFILE;
  17143. XMEMSET(tmp, 0, sizeof(tmp));
  17144. XMEMSET(der, 0, sizeof(der));
  17145. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  17146. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  17147. if (fp != XBADFILE)
  17148. XFCLOSE(fp);
  17149. #endif /* END USE_CERT_BUFFERS_1024 */
  17150. XMEMSET(&key, 0, sizeof(DsaKey));
  17151. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17152. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  17153. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  17154. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  17155. /* Test bad args. */
  17156. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17157. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  17158. wc_FreeDsaKey(&key);
  17159. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17160. return EXPECT_RESULT();
  17161. } /* END test_wc_DsaKeyToDer */
  17162. /*
  17163. * Testing wc_DsaKeyToPublicDer()
  17164. * (indirectly testing setDsaPublicKey())
  17165. */
  17166. static int test_wc_DsaKeyToPublicDer(void)
  17167. {
  17168. EXPECT_DECLS;
  17169. #ifndef HAVE_SELFTEST
  17170. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17171. DsaKey key;
  17172. WC_RNG rng;
  17173. byte* der = NULL;
  17174. word32 sz;
  17175. word32 idx = 0;
  17176. XMEMSET(&key, 0, sizeof(DsaKey));
  17177. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17178. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  17179. DYNAMIC_TYPE_TMP_BUFFER));
  17180. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17181. ExpectIntEQ(wc_InitRng(&rng), 0);
  17182. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  17183. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  17184. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  17185. wc_FreeDsaKey(&key);
  17186. idx = 0;
  17187. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  17188. /* Test without the SubjectPublicKeyInfo header */
  17189. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  17190. wc_FreeDsaKey(&key);
  17191. idx = 0;
  17192. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  17193. /* Test bad args. */
  17194. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17195. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  17196. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17197. wc_FreeDsaKey(&key);
  17198. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17199. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17200. #endif /* !HAVE_SELFTEST */
  17201. return EXPECT_RESULT();
  17202. } /* END test_wc_DsaKeyToPublicDer */
  17203. /*
  17204. * Testing wc_DsaImportParamsRaw()
  17205. */
  17206. static int test_wc_DsaImportParamsRaw(void)
  17207. {
  17208. EXPECT_DECLS;
  17209. #if !defined(NO_DSA)
  17210. DsaKey key;
  17211. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17212. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17213. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17214. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17215. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17216. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17217. "80a0093113a8bd73";
  17218. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17219. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17220. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17221. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17222. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17223. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17224. "76341a7e7d9";
  17225. /* invalid p and q parameters */
  17226. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17227. const char* invalidQ = "96c5390a";
  17228. XMEMSET(&key, 0, sizeof(DsaKey));
  17229. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17230. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  17231. /* test bad args */
  17232. /* null key struct */
  17233. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  17234. /* null param pointers */
  17235. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  17236. /* illegal p length */
  17237. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  17238. /* illegal q length */
  17239. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  17240. wc_FreeDsaKey(&key);
  17241. #endif
  17242. return EXPECT_RESULT();
  17243. } /* END test_wc_DsaImportParamsRaw */
  17244. /*
  17245. * Testing wc_DsaImportParamsRawCheck()
  17246. */
  17247. static int test_wc_DsaImportParamsRawCheck(void)
  17248. {
  17249. EXPECT_DECLS;
  17250. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17251. DsaKey key;
  17252. int trusted = 0;
  17253. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17254. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17255. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17256. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17257. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17258. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17259. "80a0093113a8bd73";
  17260. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17261. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17262. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17263. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17264. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17265. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17266. "76341a7e7d9";
  17267. /* invalid p and q parameters */
  17268. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17269. const char* invalidQ = "96c5390a";
  17270. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17271. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  17272. /* test bad args */
  17273. /* null key struct */
  17274. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  17275. BAD_FUNC_ARG);
  17276. /* null param pointers */
  17277. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  17278. NULL), BAD_FUNC_ARG);
  17279. /* illegal p length */
  17280. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  17281. BAD_FUNC_ARG);
  17282. /* illegal q length */
  17283. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  17284. BAD_FUNC_ARG);
  17285. wc_FreeDsaKey(&key);
  17286. #endif
  17287. return EXPECT_RESULT();
  17288. } /* END test_wc_DsaImportParamsRawCheck */
  17289. /*
  17290. * Testing wc_DsaExportParamsRaw()
  17291. */
  17292. static int test_wc_DsaExportParamsRaw(void)
  17293. {
  17294. EXPECT_DECLS;
  17295. #if !defined(NO_DSA)
  17296. DsaKey key;
  17297. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17298. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17299. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17300. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17301. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17302. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17303. "80a0093113a8bd73";
  17304. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17305. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17306. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17307. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17308. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17309. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17310. "76341a7e7d9";
  17311. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  17312. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  17313. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  17314. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  17315. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  17316. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  17317. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  17318. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  17319. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  17320. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  17321. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  17322. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  17323. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  17324. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  17325. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  17326. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  17327. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  17328. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  17329. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  17330. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  17331. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  17332. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  17333. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  17334. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  17335. byte pOut[MAX_DSA_PARAM_SIZE];
  17336. byte qOut[MAX_DSA_PARAM_SIZE];
  17337. byte gOut[MAX_DSA_PARAM_SIZE];
  17338. word32 pOutSz;
  17339. word32 qOutSz;
  17340. word32 gOutSz;
  17341. XMEMSET(&key, 0, sizeof(DsaKey));
  17342. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17343. /* first test using imported raw parameters, for expected */
  17344. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  17345. pOutSz = sizeof(pOut);
  17346. qOutSz = sizeof(qOut);
  17347. gOutSz = sizeof(gOut);
  17348. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17349. &gOutSz), 0);
  17350. /* validate exported parameters are correct */
  17351. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  17352. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  17353. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  17354. /* test bad args */
  17355. /* null key struct */
  17356. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17357. &gOutSz), BAD_FUNC_ARG);
  17358. /* null output pointers */
  17359. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  17360. &gOutSz), LENGTH_ONLY_E);
  17361. /* null output size pointers */
  17362. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  17363. NULL), BAD_FUNC_ARG);
  17364. /* p output buffer size too small */
  17365. pOutSz = 1;
  17366. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17367. &gOutSz), BUFFER_E);
  17368. pOutSz = sizeof(pOut);
  17369. /* q output buffer size too small */
  17370. qOutSz = 1;
  17371. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17372. &gOutSz), BUFFER_E);
  17373. qOutSz = sizeof(qOut);
  17374. /* g output buffer size too small */
  17375. gOutSz = 1;
  17376. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  17377. &gOutSz), BUFFER_E);
  17378. wc_FreeDsaKey(&key);
  17379. #endif
  17380. return EXPECT_RESULT();
  17381. } /* END test_wc_DsaExportParamsRaw */
  17382. /*
  17383. * Testing wc_DsaExportKeyRaw()
  17384. */
  17385. static int test_wc_DsaExportKeyRaw(void)
  17386. {
  17387. EXPECT_DECLS;
  17388. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17389. DsaKey key;
  17390. WC_RNG rng;
  17391. byte xOut[MAX_DSA_PARAM_SIZE];
  17392. byte yOut[MAX_DSA_PARAM_SIZE];
  17393. word32 xOutSz, yOutSz;
  17394. XMEMSET(&key, 0, sizeof(key));
  17395. XMEMSET(&rng, 0, sizeof(rng));
  17396. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  17397. ExpectIntEQ(wc_InitRng(&rng), 0);
  17398. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  17399. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  17400. /* try successful export */
  17401. xOutSz = sizeof(xOut);
  17402. yOutSz = sizeof(yOut);
  17403. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  17404. /* test bad args */
  17405. /* null key struct */
  17406. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  17407. BAD_FUNC_ARG);
  17408. /* null output pointers */
  17409. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  17410. LENGTH_ONLY_E);
  17411. /* null output size pointers */
  17412. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  17413. BAD_FUNC_ARG);
  17414. /* x output buffer size too small */
  17415. xOutSz = 1;
  17416. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  17417. BUFFER_E);
  17418. xOutSz = sizeof(xOut);
  17419. /* y output buffer size too small */
  17420. yOutSz = 1;
  17421. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  17422. BUFFER_E);
  17423. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17424. wc_FreeDsaKey(&key);
  17425. #endif
  17426. return EXPECT_RESULT();
  17427. } /* END test_wc_DsaExportParamsRaw */
  17428. /*
  17429. * Testing wc_ed25519_make_key().
  17430. */
  17431. static int test_wc_ed25519_make_key(void)
  17432. {
  17433. EXPECT_DECLS;
  17434. #if defined(HAVE_ED25519)
  17435. ed25519_key key;
  17436. WC_RNG rng;
  17437. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  17438. XMEMSET(&key, 0, sizeof(ed25519_key));
  17439. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17440. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17441. ExpectIntEQ(wc_InitRng(&rng), 0);
  17442. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, sizeof(pubkey)),
  17443. ECC_PRIV_KEY_E);
  17444. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17445. /* Test bad args. */
  17446. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  17447. BAD_FUNC_ARG);
  17448. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  17449. BAD_FUNC_ARG);
  17450. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  17451. BAD_FUNC_ARG);
  17452. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  17453. BAD_FUNC_ARG);
  17454. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17455. wc_ed25519_free(&key);
  17456. #endif
  17457. return EXPECT_RESULT();
  17458. } /* END test_wc_ed25519_make_key */
  17459. /*
  17460. * Testing wc_ed25519_init()
  17461. */
  17462. static int test_wc_ed25519_init(void)
  17463. {
  17464. EXPECT_DECLS;
  17465. #if defined(HAVE_ED25519)
  17466. ed25519_key key;
  17467. XMEMSET(&key, 0, sizeof(ed25519_key));
  17468. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17469. /* Test bad args. */
  17470. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  17471. wc_ed25519_free(&key);
  17472. #endif
  17473. return EXPECT_RESULT();
  17474. } /* END test_wc_ed25519_init */
  17475. /*
  17476. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  17477. */
  17478. static int test_wc_ed25519_sign_msg(void)
  17479. {
  17480. EXPECT_DECLS;
  17481. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  17482. WC_RNG rng;
  17483. ed25519_key key;
  17484. byte msg[] = "Everybody gets Friday off.\n";
  17485. byte sig[ED25519_SIG_SIZE];
  17486. word32 msglen = sizeof(msg);
  17487. word32 siglen = sizeof(sig);
  17488. word32 badSigLen = sizeof(sig) - 1;
  17489. #ifdef HAVE_ED25519_VERIFY
  17490. int verify_ok = 0; /*1 = Verify success.*/
  17491. #endif
  17492. /* Initialize stack variables. */
  17493. XMEMSET(&key, 0, sizeof(ed25519_key));
  17494. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17495. XMEMSET(sig, 0, siglen);
  17496. /* Initialize key. */
  17497. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17498. ExpectIntEQ(wc_InitRng(&rng), 0);
  17499. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17500. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  17501. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  17502. /* Test bad args. */
  17503. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  17504. BAD_FUNC_ARG);
  17505. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  17506. BAD_FUNC_ARG);
  17507. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  17508. BAD_FUNC_ARG);
  17509. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  17510. BAD_FUNC_ARG);
  17511. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  17512. BUFFER_E);
  17513. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  17514. badSigLen -= 1;
  17515. #ifdef HAVE_ED25519_VERIFY
  17516. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  17517. &key), 0);
  17518. ExpectIntEQ(verify_ok, 1);
  17519. /* Test bad args. */
  17520. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  17521. &key), BAD_FUNC_ARG);
  17522. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  17523. &key), BAD_FUNC_ARG);
  17524. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  17525. &key), BAD_FUNC_ARG);
  17526. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  17527. &key), BAD_FUNC_ARG);
  17528. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen, msg, msglen, NULL, &key),
  17529. BAD_FUNC_ARG);
  17530. ExpectIntEQ(wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  17531. NULL), BAD_FUNC_ARG);
  17532. ExpectIntEQ(wc_ed25519_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  17533. &key), BAD_FUNC_ARG);
  17534. #endif /* Verify. */
  17535. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17536. wc_ed25519_free(&key);
  17537. #endif
  17538. return EXPECT_RESULT();
  17539. } /* END test_wc_ed25519_sign_msg */
  17540. /*
  17541. * Testing wc_ed25519_import_public()
  17542. */
  17543. static int test_wc_ed25519_import_public(void)
  17544. {
  17545. EXPECT_DECLS;
  17546. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  17547. ed25519_key pubKey;
  17548. WC_RNG rng;
  17549. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  17550. word32 inlen = sizeof(in);
  17551. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  17552. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17553. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  17554. ExpectIntEQ(wc_InitRng(&rng), 0);
  17555. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  17556. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  17557. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  17558. /* Test bad args. */
  17559. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  17560. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  17561. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  17562. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17563. wc_ed25519_free(&pubKey);
  17564. #endif
  17565. return EXPECT_RESULT();
  17566. } /* END wc_ed25519_import_public */
  17567. /*
  17568. * Testing wc_ed25519_import_private_key()
  17569. */
  17570. static int test_wc_ed25519_import_private_key(void)
  17571. {
  17572. EXPECT_DECLS;
  17573. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  17574. ed25519_key key;
  17575. WC_RNG rng;
  17576. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  17577. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  17578. word32 privKeySz = sizeof(privKey);
  17579. word32 pubKeySz = sizeof(pubKey);
  17580. #ifdef HAVE_ED25519_KEY_EXPORT
  17581. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  17582. word32 bothKeysSz = sizeof(bothKeys);
  17583. #endif
  17584. XMEMSET(&key, 0, sizeof(ed25519_key));
  17585. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17586. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17587. ExpectIntEQ(wc_InitRng(&rng), 0);
  17588. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17589. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  17590. pubKeySz, &key, 1), 0);
  17591. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  17592. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  17593. #ifdef HAVE_ED25519_KEY_EXPORT
  17594. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  17595. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  17596. &key, 1), 0);
  17597. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  17598. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  17599. #endif
  17600. /* Test bad args. */
  17601. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  17602. &key), BAD_FUNC_ARG);
  17603. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  17604. pubKeySz, &key), BAD_FUNC_ARG);
  17605. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  17606. pubKeySz, NULL), BAD_FUNC_ARG);
  17607. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  17608. pubKeySz, &key), BAD_FUNC_ARG);
  17609. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  17610. pubKeySz - 1, &key), BAD_FUNC_ARG);
  17611. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  17612. &key), BAD_FUNC_ARG);
  17613. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17614. wc_ed25519_free(&key);
  17615. #endif
  17616. return EXPECT_RESULT();
  17617. } /* END test_wc_ed25519_import_private_key */
  17618. /*
  17619. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  17620. */
  17621. static int test_wc_ed25519_export(void)
  17622. {
  17623. EXPECT_DECLS;
  17624. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  17625. ed25519_key key;
  17626. WC_RNG rng;
  17627. byte priv[ED25519_PRV_KEY_SIZE];
  17628. byte pub[ED25519_PUB_KEY_SIZE];
  17629. word32 privSz = sizeof(priv);
  17630. word32 pubSz = sizeof(pub);
  17631. XMEMSET(&key, 0, sizeof(ed25519_key));
  17632. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17633. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17634. ExpectIntEQ(wc_InitRng(&rng), 0);
  17635. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17636. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  17637. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  17638. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  17639. /* Test bad args. */
  17640. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  17641. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  17642. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  17643. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  17644. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  17645. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  17646. /* Test bad args. */
  17647. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  17648. BAD_FUNC_ARG);
  17649. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  17650. BAD_FUNC_ARG);
  17651. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  17652. BAD_FUNC_ARG);
  17653. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17654. wc_ed25519_free(&key);
  17655. #endif
  17656. return EXPECT_RESULT();
  17657. } /* END test_wc_ed25519_export */
  17658. /*
  17659. * Testing wc_ed25519_size()
  17660. */
  17661. static int test_wc_ed25519_size(void)
  17662. {
  17663. EXPECT_DECLS;
  17664. #if defined(HAVE_ED25519)
  17665. ed25519_key key;
  17666. WC_RNG rng;
  17667. XMEMSET(&key, 0, sizeof(ed25519_key));
  17668. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17669. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17670. ExpectIntEQ(wc_InitRng(&rng), 0);
  17671. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17672. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  17673. /* Test bad args. */
  17674. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  17675. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  17676. /* Test bad args. */
  17677. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  17678. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  17679. /* Test bad args. */
  17680. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  17681. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  17682. /* Test bad args. */
  17683. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  17684. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17685. wc_ed25519_free(&key);
  17686. #endif
  17687. return EXPECT_RESULT();
  17688. } /* END test_wc_ed25519_size */
  17689. /*
  17690. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  17691. */
  17692. static int test_wc_ed25519_exportKey(void)
  17693. {
  17694. EXPECT_DECLS;
  17695. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  17696. WC_RNG rng;
  17697. ed25519_key key;
  17698. byte priv[ED25519_PRV_KEY_SIZE];
  17699. byte pub[ED25519_PUB_KEY_SIZE];
  17700. byte privOnly[ED25519_PRV_KEY_SIZE];
  17701. word32 privSz = sizeof(priv);
  17702. word32 pubSz = sizeof(pub);
  17703. word32 privOnlySz = sizeof(privOnly);
  17704. XMEMSET(&key, 0, sizeof(ed25519_key));
  17705. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17706. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17707. ExpectIntEQ(wc_InitRng(&rng), 0);
  17708. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17709. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  17710. /* Test bad args. */
  17711. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  17712. BAD_FUNC_ARG);
  17713. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  17714. BAD_FUNC_ARG);
  17715. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  17716. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  17717. /* Test bad args. */
  17718. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  17719. BAD_FUNC_ARG);
  17720. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  17721. BAD_FUNC_ARG);
  17722. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  17723. BAD_FUNC_ARG);
  17724. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  17725. BAD_FUNC_ARG);
  17726. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  17727. BAD_FUNC_ARG);
  17728. /* Cross check output. */
  17729. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  17730. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17731. wc_ed25519_free(&key);
  17732. #endif
  17733. return EXPECT_RESULT();
  17734. } /* END test_wc_ed25519_exportKey */
  17735. /*
  17736. * Testing wc_Ed25519PublicKeyToDer
  17737. */
  17738. static int test_wc_Ed25519PublicKeyToDer(void)
  17739. {
  17740. EXPECT_DECLS;
  17741. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  17742. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  17743. ed25519_key key;
  17744. byte derBuf[1024];
  17745. XMEMSET(&key, 0, sizeof(ed25519_key));
  17746. /* Test bad args */
  17747. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  17748. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17749. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  17750. wc_ed25519_free(&key);
  17751. /* Test good args */
  17752. if (EXPECT_SUCCESS()) {
  17753. WC_RNG rng;
  17754. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17755. ExpectIntEQ(wc_ed25519_init(&key), 0);
  17756. ExpectIntEQ(wc_InitRng(&rng), 0);
  17757. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  17758. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  17759. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17760. wc_ed25519_free(&key);
  17761. }
  17762. #endif
  17763. return EXPECT_RESULT();
  17764. } /* END testing wc_Ed25519PublicKeyToDer */
  17765. /*
  17766. * Testing wc_curve25519_init and wc_curve25519_free.
  17767. */
  17768. static int test_wc_curve25519_init(void)
  17769. {
  17770. EXPECT_DECLS;
  17771. #if defined(HAVE_CURVE25519)
  17772. curve25519_key key;
  17773. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17774. /* Test bad args for wc_curve25519_init */
  17775. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  17776. /* Test good args for wc_curve_25519_free */
  17777. wc_curve25519_free(&key);
  17778. /* Test bad args for wc_curve25519 free. */
  17779. wc_curve25519_free(NULL);
  17780. #endif
  17781. return EXPECT_RESULT();
  17782. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  17783. /*
  17784. * Testing test_wc_curve25519_size.
  17785. */
  17786. static int test_wc_curve25519_size(void)
  17787. {
  17788. EXPECT_DECLS;
  17789. #if defined(HAVE_CURVE25519)
  17790. curve25519_key key;
  17791. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17792. /* Test good args for wc_curve25519_size */
  17793. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  17794. /* Test bad args for wc_curve25519_size */
  17795. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  17796. wc_curve25519_free(&key);
  17797. #endif
  17798. return EXPECT_RESULT();
  17799. } /* END test_wc_curve25519_size*/
  17800. /*
  17801. * Testing test_wc_curve25519_export_key_raw().
  17802. */
  17803. static int test_wc_curve25519_export_key_raw(void)
  17804. {
  17805. EXPECT_DECLS;
  17806. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17807. curve25519_key key;
  17808. WC_RNG rng;
  17809. byte privateKey[CURVE25519_KEYSIZE];
  17810. byte publicKey[CURVE25519_KEYSIZE];
  17811. word32 prvkSz;
  17812. word32 pubkSz;
  17813. byte prik[CURVE25519_KEYSIZE];
  17814. byte pubk[CURVE25519_KEYSIZE];
  17815. word32 prksz;
  17816. word32 pbksz;
  17817. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17818. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17819. ExpectIntEQ(wc_InitRng(&rng), 0);
  17820. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  17821. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  17822. prvkSz = CURVE25519_KEYSIZE;
  17823. pubkSz = CURVE25519_KEYSIZE;
  17824. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  17825. publicKey, &pubkSz), BAD_FUNC_ARG);
  17826. prvkSz = CURVE25519_KEYSIZE;
  17827. pubkSz = CURVE25519_KEYSIZE;
  17828. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  17829. &pubkSz), BAD_FUNC_ARG);
  17830. prvkSz = CURVE25519_KEYSIZE;
  17831. pubkSz = CURVE25519_KEYSIZE;
  17832. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  17833. publicKey, &pubkSz), BAD_FUNC_ARG);
  17834. /* prvkSz = CURVE25519_KEYSIZE; */
  17835. pubkSz = CURVE25519_KEYSIZE;
  17836. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17837. NULL, &pubkSz), BAD_FUNC_ARG);
  17838. prvkSz = CURVE25519_KEYSIZE;
  17839. pubkSz = CURVE25519_KEYSIZE;
  17840. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17841. publicKey, NULL), BAD_FUNC_ARG);
  17842. /* cross-testing */
  17843. prksz = CURVE25519_KEYSIZE;
  17844. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  17845. pbksz = CURVE25519_KEYSIZE;
  17846. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  17847. prvkSz = CURVE25519_KEYSIZE;
  17848. /* pubkSz = CURVE25519_KEYSIZE; */
  17849. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  17850. publicKey, &pubkSz), 0);
  17851. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  17852. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  17853. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17854. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17855. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  17856. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  17857. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17858. wc_curve25519_free(&key);
  17859. #endif
  17860. return EXPECT_RESULT();
  17861. } /* end of test_wc_curve25519_export_key_raw */
  17862. /*
  17863. * Testing test_wc_curve25519_export_key_raw_ex().
  17864. */
  17865. static int test_wc_curve25519_export_key_raw_ex(void)
  17866. {
  17867. EXPECT_DECLS;
  17868. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  17869. curve25519_key key;
  17870. WC_RNG rng;
  17871. byte privateKey[CURVE25519_KEYSIZE];
  17872. byte publicKey[CURVE25519_KEYSIZE];
  17873. word32 prvkSz;
  17874. word32 pubkSz;
  17875. byte prik[CURVE25519_KEYSIZE];
  17876. byte pubk[CURVE25519_KEYSIZE];
  17877. word32 prksz;
  17878. word32 pbksz;
  17879. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17880. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17881. ExpectIntEQ(wc_InitRng(&rng), 0);
  17882. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  17883. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  17884. prvkSz = CURVE25519_KEYSIZE;
  17885. pubkSz = CURVE25519_KEYSIZE;
  17886. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  17887. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17888. prvkSz = CURVE25519_KEYSIZE;
  17889. pubkSz = CURVE25519_KEYSIZE;
  17890. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  17891. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17892. prvkSz = CURVE25519_KEYSIZE;
  17893. pubkSz = CURVE25519_KEYSIZE;
  17894. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17895. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17896. /* prvkSz = CURVE25519_KEYSIZE; */
  17897. pubkSz = CURVE25519_KEYSIZE;
  17898. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17899. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17900. prvkSz = CURVE25519_KEYSIZE;
  17901. pubkSz = CURVE25519_KEYSIZE;
  17902. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17903. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  17904. prvkSz = CURVE25519_KEYSIZE;
  17905. /* pubkSz = CURVE25519_KEYSIZE; */
  17906. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  17907. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17908. prvkSz = CURVE25519_KEYSIZE;
  17909. pubkSz = CURVE25519_KEYSIZE;
  17910. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  17911. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17912. prvkSz = CURVE25519_KEYSIZE;
  17913. pubkSz = CURVE25519_KEYSIZE;
  17914. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17915. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17916. /* prvkSz = CURVE25519_KEYSIZE; */
  17917. pubkSz = CURVE25519_KEYSIZE;
  17918. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17919. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17920. prvkSz = CURVE25519_KEYSIZE;
  17921. pubkSz = CURVE25519_KEYSIZE;
  17922. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  17923. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  17924. /* illegal value for endian */
  17925. prvkSz = CURVE25519_KEYSIZE;
  17926. /* pubkSz = CURVE25519_KEYSIZE; */
  17927. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17928. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  17929. /* cross-testing */
  17930. prksz = CURVE25519_KEYSIZE;
  17931. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  17932. pbksz = CURVE25519_KEYSIZE;
  17933. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  17934. prvkSz = CURVE25519_KEYSIZE;
  17935. /* pubkSz = CURVE25519_KEYSIZE; */
  17936. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17937. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  17938. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  17939. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  17940. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17941. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17942. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  17943. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  17944. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  17945. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  17946. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17947. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17948. /* try once with another endian */
  17949. prvkSz = CURVE25519_KEYSIZE;
  17950. pubkSz = CURVE25519_KEYSIZE;
  17951. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  17952. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  17953. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  17954. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  17955. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17956. wc_curve25519_free(&key);
  17957. #endif
  17958. return EXPECT_RESULT();
  17959. } /* end of test_wc_curve25519_export_key_raw_ex */
  17960. /*
  17961. * Testing wc_curve25519_make_key
  17962. */
  17963. static int test_wc_curve25519_make_key(void)
  17964. {
  17965. EXPECT_DECLS;
  17966. #if defined(HAVE_CURVE25519)
  17967. curve25519_key key;
  17968. WC_RNG rng;
  17969. int keysize;
  17970. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17971. ExpectIntEQ(wc_curve25519_init(&key), 0);
  17972. ExpectIntEQ(wc_InitRng(&rng), 0);
  17973. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  17974. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  17975. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  17976. /* test bad cases*/
  17977. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  17978. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  17979. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  17980. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  17981. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17982. wc_curve25519_free(&key);
  17983. #endif
  17984. return EXPECT_RESULT();
  17985. } /* END test_wc_curve25519_make_key*/
  17986. /*
  17987. * Testing wc_curve25519_shared_secret_ex
  17988. */
  17989. static int test_wc_curve25519_shared_secret_ex(void)
  17990. {
  17991. EXPECT_DECLS;
  17992. #if defined(HAVE_CURVE25519)
  17993. curve25519_key private_key;
  17994. curve25519_key public_key;
  17995. WC_RNG rng;
  17996. byte out[CURVE25519_KEYSIZE];
  17997. word32 outLen = sizeof(out);
  17998. int endian = EC25519_BIG_ENDIAN;
  17999. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  18000. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  18001. ExpectIntEQ(wc_InitRng(&rng), 0);
  18002. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  18003. 0);
  18004. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  18005. 0);
  18006. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18007. &outLen, endian), 0);
  18008. /* test bad cases*/
  18009. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  18010. BAD_FUNC_ARG);
  18011. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  18012. endian), BAD_FUNC_ARG);
  18013. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  18014. endian), BAD_FUNC_ARG);
  18015. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  18016. &outLen, endian), BAD_FUNC_ARG);
  18017. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18018. NULL, endian), BAD_FUNC_ARG);
  18019. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  18020. * increasing to 0x8f checks for error being returned*/
  18021. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  18022. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18023. &outLen, endian), ECC_BAD_ARG_E);
  18024. outLen = outLen - 2;
  18025. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  18026. &outLen, endian), BAD_FUNC_ARG);
  18027. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18028. wc_curve25519_free(&private_key);
  18029. wc_curve25519_free(&public_key);
  18030. #endif
  18031. return EXPECT_RESULT();
  18032. } /* END test_wc_curve25519_shared_secret_ex*/
  18033. /*
  18034. * Testing wc_curve25519_make_pub
  18035. */
  18036. static int test_wc_curve25519_make_pub(void)
  18037. {
  18038. EXPECT_DECLS;
  18039. #ifdef HAVE_CURVE25519
  18040. curve25519_key key;
  18041. WC_RNG rng;
  18042. byte out[CURVE25519_KEYSIZE];
  18043. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18044. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18045. ExpectIntEQ(wc_InitRng(&rng), 0);
  18046. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18047. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  18048. (int)sizeof(key.k), key.k), 0);
  18049. /* test bad cases*/
  18050. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  18051. (int)sizeof out, out), ECC_BAD_ARG_E);
  18052. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18053. NULL), ECC_BAD_ARG_E);
  18054. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  18055. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  18056. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  18057. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  18058. /* verify clamping test */
  18059. key.k[0] |= ~248;
  18060. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18061. key.k), ECC_BAD_ARG_E);
  18062. key.k[0] &= 248;
  18063. /* repeat the expected-to-succeed test. */
  18064. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  18065. key.k), 0);
  18066. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18067. wc_curve25519_free(&key);
  18068. #endif
  18069. return EXPECT_RESULT();
  18070. } /* END test_wc_curve25519_make_pub */
  18071. /*
  18072. * Testing test_wc_curve25519_export_public_ex
  18073. */
  18074. static int test_wc_curve25519_export_public_ex(void)
  18075. {
  18076. EXPECT_DECLS;
  18077. #if defined(HAVE_CURVE25519)
  18078. curve25519_key key;
  18079. WC_RNG rng;
  18080. byte out[CURVE25519_KEYSIZE];
  18081. word32 outLen = sizeof(out);
  18082. int endian = EC25519_BIG_ENDIAN;
  18083. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18084. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18085. ExpectIntEQ(wc_InitRng(&rng), 0);
  18086. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18087. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  18088. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  18089. /* test bad cases*/
  18090. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  18091. BAD_FUNC_ARG);
  18092. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  18093. BAD_FUNC_ARG);
  18094. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  18095. BAD_FUNC_ARG);
  18096. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  18097. BAD_FUNC_ARG);
  18098. outLen = outLen - 2;
  18099. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  18100. ECC_BAD_ARG_E);
  18101. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18102. wc_curve25519_free(&key);
  18103. #endif
  18104. return EXPECT_RESULT();
  18105. } /* END test_wc_curve25519_export_public_ex*/
  18106. /*
  18107. * Testing test_wc_curve25519_import_private_raw_ex
  18108. */
  18109. static int test_wc_curve25519_import_private_raw_ex(void)
  18110. {
  18111. EXPECT_DECLS;
  18112. #if defined(HAVE_CURVE25519)
  18113. curve25519_key key;
  18114. WC_RNG rng;
  18115. byte priv[CURVE25519_KEYSIZE];
  18116. byte pub[CURVE25519_KEYSIZE];
  18117. word32 privSz = sizeof(priv);
  18118. word32 pubSz = sizeof(pub);
  18119. int endian = EC25519_BIG_ENDIAN;
  18120. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18121. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18122. ExpectIntEQ(wc_InitRng(&rng), 0);
  18123. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18124. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  18125. endian), 0);
  18126. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  18127. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18128. &key, endian), 0);
  18129. /* test bad cases*/
  18130. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  18131. endian), BAD_FUNC_ARG);
  18132. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  18133. &key, endian), BAD_FUNC_ARG);
  18134. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  18135. &key, endian), BAD_FUNC_ARG);
  18136. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18137. NULL, endian), BAD_FUNC_ARG);
  18138. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  18139. &key, endian), ECC_BAD_ARG_E);
  18140. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  18141. &key, endian), ECC_BAD_ARG_E);
  18142. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  18143. &key, EC25519_LITTLE_ENDIAN), 0);
  18144. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18145. wc_curve25519_free(&key);
  18146. #endif
  18147. return EXPECT_RESULT();
  18148. } /* END test_wc_curve25519_import_private_raw_ex*/
  18149. /*
  18150. * Testing test_wc_curve25519_import_private
  18151. */
  18152. static int test_wc_curve25519_import_private(void)
  18153. {
  18154. EXPECT_DECLS;
  18155. #if defined(HAVE_CURVE25519)
  18156. curve25519_key key;
  18157. WC_RNG rng;
  18158. byte priv[CURVE25519_KEYSIZE];
  18159. word32 privSz = sizeof(priv);
  18160. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18161. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18162. ExpectIntEQ(wc_InitRng(&rng), 0);
  18163. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  18164. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  18165. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  18166. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18167. wc_curve25519_free(&key);
  18168. #endif
  18169. return EXPECT_RESULT();
  18170. } /* END test_wc_curve25519_import*/
  18171. /*
  18172. * Testing test_wc_curve25519_export_private_raw_ex
  18173. */
  18174. static int test_wc_curve25519_export_private_raw_ex(void)
  18175. {
  18176. EXPECT_DECLS;
  18177. #if defined(HAVE_CURVE25519)
  18178. curve25519_key key;
  18179. byte out[CURVE25519_KEYSIZE];
  18180. word32 outLen = sizeof(out);
  18181. int endian = EC25519_BIG_ENDIAN;
  18182. ExpectIntEQ(wc_curve25519_init(&key), 0);
  18183. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  18184. 0);
  18185. /* test bad cases*/
  18186. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  18187. BAD_FUNC_ARG);
  18188. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  18189. BAD_FUNC_ARG);
  18190. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  18191. endian), BAD_FUNC_ARG);
  18192. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  18193. BAD_FUNC_ARG);
  18194. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  18195. EC25519_LITTLE_ENDIAN), 0);
  18196. outLen = outLen - 2;
  18197. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  18198. ECC_BAD_ARG_E);
  18199. wc_curve25519_free(&key);
  18200. #endif
  18201. return EXPECT_RESULT();
  18202. } /* END test_wc_curve25519_export_private_raw_ex*/
  18203. /*
  18204. * Testing wc_ed448_make_key().
  18205. */
  18206. static int test_wc_ed448_make_key(void)
  18207. {
  18208. EXPECT_DECLS;
  18209. #if defined(HAVE_ED448)
  18210. ed448_key key;
  18211. WC_RNG rng;
  18212. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  18213. XMEMSET(&key, 0, sizeof(ed448_key));
  18214. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18215. ExpectIntEQ(wc_ed448_init(&key), 0);
  18216. ExpectIntEQ(wc_InitRng(&rng), 0);
  18217. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  18218. ECC_PRIV_KEY_E);
  18219. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18220. /* Test bad args. */
  18221. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  18222. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  18223. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  18224. BAD_FUNC_ARG);
  18225. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  18226. BAD_FUNC_ARG);
  18227. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18228. wc_ed448_free(&key);
  18229. #endif
  18230. return EXPECT_RESULT();
  18231. } /* END test_wc_ed448_make_key */
  18232. /*
  18233. * Testing wc_ed448_init()
  18234. */
  18235. static int test_wc_ed448_init(void)
  18236. {
  18237. EXPECT_DECLS;
  18238. #if defined(HAVE_ED448)
  18239. ed448_key key;
  18240. XMEMSET(&key, 0, sizeof(ed448_key));
  18241. ExpectIntEQ(wc_ed448_init(&key), 0);
  18242. /* Test bad args. */
  18243. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  18244. wc_ed448_free(&key);
  18245. #endif
  18246. return EXPECT_RESULT();
  18247. } /* END test_wc_ed448_init */
  18248. /*
  18249. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  18250. */
  18251. static int test_wc_ed448_sign_msg(void)
  18252. {
  18253. EXPECT_DECLS;
  18254. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  18255. ed448_key key;
  18256. WC_RNG rng;
  18257. byte msg[] = "Everybody gets Friday off.\n";
  18258. byte sig[ED448_SIG_SIZE];
  18259. word32 msglen = sizeof(msg);
  18260. word32 siglen = sizeof(sig);
  18261. word32 badSigLen = sizeof(sig) - 1;
  18262. #ifdef HAVE_ED448_VERIFY
  18263. int verify_ok = 0; /*1 = Verify success.*/
  18264. #endif
  18265. /* Initialize stack variables. */
  18266. XMEMSET(&key, 0, sizeof(ed448_key));
  18267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18268. XMEMSET(sig, 0, siglen);
  18269. /* Initialize key. */
  18270. ExpectIntEQ(wc_ed448_init(&key), 0);
  18271. ExpectIntEQ(wc_InitRng(&rng), 0);
  18272. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18273. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  18274. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  18275. /* Test bad args. */
  18276. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  18277. BAD_FUNC_ARG);
  18278. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  18279. BAD_FUNC_ARG);
  18280. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  18281. BAD_FUNC_ARG);
  18282. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  18283. BAD_FUNC_ARG);
  18284. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  18285. BUFFER_E);
  18286. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  18287. badSigLen -= 1;
  18288. #ifdef HAVE_ED448_VERIFY
  18289. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  18290. NULL, 0), 0);
  18291. ExpectIntEQ(verify_ok, 1);
  18292. /* Test bad args. */
  18293. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  18294. &key, NULL, 0), BAD_FUNC_ARG);
  18295. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  18296. &key, NULL, 0), BAD_FUNC_ARG);
  18297. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18298. &key, NULL, 0), BAD_FUNC_ARG);
  18299. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  18300. &key, NULL, 0), BAD_FUNC_ARG);
  18301. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  18302. &key, NULL, 0), BAD_FUNC_ARG);
  18303. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  18304. NULL, NULL, 0), BAD_FUNC_ARG);
  18305. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  18306. &key, NULL, 0), BAD_FUNC_ARG);
  18307. #endif /* Verify. */
  18308. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18309. wc_ed448_free(&key);
  18310. #endif
  18311. return EXPECT_RESULT();
  18312. } /* END test_wc_ed448_sign_msg */
  18313. /*
  18314. * Testing wc_ed448_import_public()
  18315. */
  18316. static int test_wc_ed448_import_public(void)
  18317. {
  18318. EXPECT_DECLS;
  18319. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18320. ed448_key pubKey;
  18321. WC_RNG rng;
  18322. const byte in[] =
  18323. "Ed448PublicKeyUnitTest.................................\n";
  18324. word32 inlen = sizeof(in);
  18325. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  18326. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18327. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  18328. ExpectIntEQ(wc_InitRng(&rng), 0);
  18329. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  18330. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  18331. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  18332. /* Test bad args. */
  18333. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  18334. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  18335. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  18336. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18337. wc_ed448_free(&pubKey);
  18338. #endif
  18339. return EXPECT_RESULT();
  18340. } /* END wc_ed448_import_public */
  18341. /*
  18342. * Testing wc_ed448_import_private_key()
  18343. */
  18344. static int test_wc_ed448_import_private_key(void)
  18345. {
  18346. EXPECT_DECLS;
  18347. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  18348. ed448_key key;
  18349. WC_RNG rng;
  18350. const byte privKey[] =
  18351. "Ed448PrivateKeyUnitTest................................\n";
  18352. const byte pubKey[] =
  18353. "Ed448PublicKeyUnitTest.................................\n";
  18354. word32 privKeySz = sizeof(privKey);
  18355. word32 pubKeySz = sizeof(pubKey);
  18356. #ifdef HAVE_ED448_KEY_EXPORT
  18357. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18358. word32 bothKeysSz = sizeof(bothKeys);
  18359. #endif
  18360. XMEMSET(&key, 0, sizeof(ed448_key));
  18361. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18362. ExpectIntEQ(wc_ed448_init(&key), 0);
  18363. ExpectIntEQ(wc_InitRng(&rng), 0);
  18364. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18365. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  18366. pubKeySz, &key, 1), 0);
  18367. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  18368. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  18369. #ifdef HAVE_ED448_KEY_EXPORT
  18370. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  18371. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  18372. &key, 1), 0);
  18373. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  18374. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  18375. #endif
  18376. /* Test bad args. */
  18377. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18378. &key), BAD_FUNC_ARG);
  18379. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  18380. &key), BAD_FUNC_ARG);
  18381. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18382. pubKeySz, NULL), BAD_FUNC_ARG);
  18383. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  18384. pubKeySz, &key), BAD_FUNC_ARG);
  18385. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  18386. pubKeySz - 1, &key), BAD_FUNC_ARG);
  18387. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  18388. BAD_FUNC_ARG);
  18389. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18390. wc_ed448_free(&key);
  18391. #endif
  18392. return EXPECT_RESULT();
  18393. } /* END test_wc_ed448_import_private_key */
  18394. /*
  18395. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  18396. */
  18397. static int test_wc_ed448_export(void)
  18398. {
  18399. EXPECT_DECLS;
  18400. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18401. ed448_key key;
  18402. WC_RNG rng;
  18403. byte priv[ED448_PRV_KEY_SIZE];
  18404. byte pub[ED448_PUB_KEY_SIZE];
  18405. word32 privSz = sizeof(priv);
  18406. word32 pubSz = sizeof(pub);
  18407. XMEMSET(&key, 0, sizeof(ed448_key));
  18408. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18409. ExpectIntEQ(wc_ed448_init(&key), 0);
  18410. ExpectIntEQ(wc_InitRng(&rng), 0);
  18411. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18412. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  18413. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  18414. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  18415. /* Test bad args. */
  18416. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  18417. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  18418. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  18419. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  18420. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  18421. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  18422. /* Test bad args. */
  18423. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  18424. BAD_FUNC_ARG);
  18425. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  18426. BAD_FUNC_ARG);
  18427. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  18428. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18429. wc_ed448_free(&key);
  18430. #endif
  18431. return EXPECT_RESULT();
  18432. } /* END test_wc_ed448_export */
  18433. /*
  18434. * Testing wc_ed448_size()
  18435. */
  18436. static int test_wc_ed448_size(void)
  18437. {
  18438. EXPECT_DECLS;
  18439. #if defined(HAVE_ED448)
  18440. ed448_key key;
  18441. WC_RNG rng;
  18442. XMEMSET(&key, 0, sizeof(ed448_key));
  18443. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18444. ExpectIntEQ(wc_ed448_init(&key), 0);
  18445. ExpectIntEQ(wc_InitRng(&rng), 0);
  18446. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18447. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  18448. /* Test bad args. */
  18449. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  18450. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  18451. /* Test bad args. */
  18452. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  18453. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  18454. /* Test bad args. */
  18455. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  18456. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  18457. /* Test bad args. */
  18458. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  18459. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18460. wc_ed448_free(&key);
  18461. #endif
  18462. return EXPECT_RESULT();
  18463. } /* END test_wc_ed448_size */
  18464. /*
  18465. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  18466. */
  18467. static int test_wc_ed448_exportKey(void)
  18468. {
  18469. EXPECT_DECLS;
  18470. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  18471. ed448_key key;
  18472. WC_RNG rng;
  18473. byte priv[ED448_PRV_KEY_SIZE];
  18474. byte pub[ED448_PUB_KEY_SIZE];
  18475. byte privOnly[ED448_PRV_KEY_SIZE];
  18476. word32 privSz = sizeof(priv);
  18477. word32 pubSz = sizeof(pub);
  18478. word32 privOnlySz = sizeof(privOnly);
  18479. XMEMSET(&key, 0, sizeof(ed448_key));
  18480. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18481. ExpectIntEQ(wc_ed448_init(&key), 0);
  18482. ExpectIntEQ(wc_InitRng(&rng), 0);
  18483. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18484. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  18485. /* Test bad args. */
  18486. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  18487. BAD_FUNC_ARG);
  18488. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  18489. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  18490. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  18491. /* Test bad args. */
  18492. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  18493. BAD_FUNC_ARG);
  18494. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  18495. BAD_FUNC_ARG);
  18496. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  18497. BAD_FUNC_ARG);
  18498. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  18499. BAD_FUNC_ARG);
  18500. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  18501. BAD_FUNC_ARG);
  18502. /* Cross check output. */
  18503. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  18504. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18505. wc_ed448_free(&key);
  18506. #endif
  18507. return EXPECT_RESULT();
  18508. } /* END test_wc_ed448_exportKey */
  18509. /*
  18510. * Testing wc_Ed448PublicKeyToDer
  18511. */
  18512. static int test_wc_Ed448PublicKeyToDer(void)
  18513. {
  18514. EXPECT_DECLS;
  18515. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  18516. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18517. ed448_key key;
  18518. byte derBuf[1024];
  18519. XMEMSET(&key, 0, sizeof(ed448_key));
  18520. /* Test bad args */
  18521. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  18522. ExpectIntEQ(wc_ed448_init(&key), 0);
  18523. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  18524. wc_ed448_free(&key);
  18525. /* Test good args */
  18526. if (EXPECT_SUCCESS()) {
  18527. WC_RNG rng;
  18528. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18529. ExpectIntEQ(wc_ed448_init(&key), 0);
  18530. ExpectIntEQ(wc_InitRng(&rng), 0);
  18531. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  18532. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  18533. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18534. wc_ed448_free(&key);
  18535. }
  18536. #endif
  18537. return EXPECT_RESULT();
  18538. } /* END testing wc_Ed448PublicKeyToDer */
  18539. /*
  18540. * Testing wc_curve448_init and wc_curve448_free.
  18541. */
  18542. static int test_wc_curve448_init(void)
  18543. {
  18544. EXPECT_DECLS;
  18545. #if defined(HAVE_CURVE448)
  18546. curve448_key key;
  18547. /* Test bad args for wc_curve448_init */
  18548. ExpectIntEQ(wc_curve448_init(&key), 0);
  18549. /* Test bad args for wc_curve448_init */
  18550. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  18551. /* Test good args for wc_curve_448_free */
  18552. wc_curve448_free(&key);
  18553. /* Test bad args for wc_curve448_free */
  18554. wc_curve448_free(NULL);
  18555. #endif
  18556. return EXPECT_RESULT();
  18557. } /* END test_wc_curve448_init and wc_curve_448_free*/
  18558. /*
  18559. * Testing wc_curve448_make_key
  18560. */
  18561. static int test_wc_curve448_make_key(void)
  18562. {
  18563. EXPECT_DECLS;
  18564. #if defined(HAVE_CURVE448)
  18565. curve448_key key;
  18566. WC_RNG rng;
  18567. int keysize;
  18568. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18569. ExpectIntEQ(wc_curve448_init(&key), 0);
  18570. ExpectIntEQ(wc_InitRng(&rng), 0);
  18571. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18572. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  18573. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  18574. /* test bad cases */
  18575. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  18576. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  18577. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  18578. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  18579. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18580. wc_curve448_free(&key);
  18581. #endif
  18582. return EXPECT_RESULT();
  18583. } /* END test_wc_curve448_make_key*/
  18584. /*
  18585. * Testing test_wc_curve448_shared_secret_ex
  18586. */
  18587. static int test_wc_curve448_shared_secret_ex(void)
  18588. {
  18589. EXPECT_DECLS;
  18590. #if defined(HAVE_CURVE448)
  18591. curve448_key private_key;
  18592. curve448_key public_key;
  18593. WC_RNG rng;
  18594. byte out[CURVE448_KEY_SIZE];
  18595. word32 outLen = sizeof(out);
  18596. int endian = EC448_BIG_ENDIAN;
  18597. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18598. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  18599. ExpectIntEQ(wc_InitRng(&rng), 0);
  18600. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  18601. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  18602. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  18603. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18604. &outLen, endian), 0);
  18605. /* test bad cases */
  18606. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  18607. BAD_FUNC_ARG);
  18608. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  18609. endian), BAD_FUNC_ARG);
  18610. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  18611. endian), BAD_FUNC_ARG);
  18612. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  18613. &outLen, endian), BAD_FUNC_ARG);
  18614. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18615. NULL, endian), BAD_FUNC_ARG);
  18616. outLen = outLen - 2;
  18617. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  18618. &outLen, endian), BAD_FUNC_ARG);
  18619. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18620. wc_curve448_free(&private_key);
  18621. wc_curve448_free(&public_key);
  18622. #endif
  18623. return EXPECT_RESULT();
  18624. } /* END test_wc_curve448_shared_secret_ex*/
  18625. /*
  18626. * Testing test_wc_curve448_export_public_ex
  18627. */
  18628. static int test_wc_curve448_export_public_ex(void)
  18629. {
  18630. EXPECT_DECLS;
  18631. #if defined(HAVE_CURVE448)
  18632. WC_RNG rng;
  18633. curve448_key key;
  18634. byte out[CURVE448_KEY_SIZE];
  18635. word32 outLen = sizeof(out);
  18636. int endian = EC448_BIG_ENDIAN;
  18637. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18638. ExpectIntEQ(wc_curve448_init(&key), 0);
  18639. ExpectIntEQ(wc_InitRng(&rng), 0);
  18640. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18641. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  18642. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  18643. /* test bad cases*/
  18644. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  18645. BAD_FUNC_ARG);
  18646. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  18647. BAD_FUNC_ARG);
  18648. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  18649. BAD_FUNC_ARG);
  18650. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  18651. BAD_FUNC_ARG);
  18652. outLen = outLen - 2;
  18653. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  18654. ECC_BAD_ARG_E);
  18655. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18656. wc_curve448_free(&key);
  18657. #endif
  18658. return EXPECT_RESULT();
  18659. } /* END test_wc_curve448_export_public_ex*/
  18660. /*
  18661. * Testing test_wc_curve448_export_private_raw_ex
  18662. */
  18663. static int test_wc_curve448_export_private_raw_ex(void)
  18664. {
  18665. EXPECT_DECLS;
  18666. #if defined(HAVE_CURVE448)
  18667. curve448_key key;
  18668. byte out[CURVE448_KEY_SIZE];
  18669. word32 outLen = sizeof(out);
  18670. int endian = EC448_BIG_ENDIAN;
  18671. ExpectIntEQ(wc_curve448_init(&key), 0);
  18672. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  18673. 0);
  18674. /* test bad cases*/
  18675. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  18676. BAD_FUNC_ARG);
  18677. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  18678. BAD_FUNC_ARG);
  18679. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  18680. BAD_FUNC_ARG);
  18681. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  18682. BAD_FUNC_ARG);
  18683. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  18684. EC448_LITTLE_ENDIAN), 0);
  18685. outLen = outLen - 2;
  18686. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  18687. ECC_BAD_ARG_E);
  18688. wc_curve448_free(&key);
  18689. #endif
  18690. return EXPECT_RESULT();
  18691. } /* END test_wc_curve448_export_private_raw_ex*/
  18692. /*
  18693. * Testing test_wc_curve448_import_private_raw_ex
  18694. */
  18695. static int test_wc_curve448_import_private_raw_ex(void)
  18696. {
  18697. EXPECT_DECLS;
  18698. #if defined(HAVE_CURVE448)
  18699. curve448_key key;
  18700. WC_RNG rng;
  18701. byte priv[CURVE448_KEY_SIZE];
  18702. byte pub[CURVE448_KEY_SIZE];
  18703. word32 privSz = sizeof(priv);
  18704. word32 pubSz = sizeof(pub);
  18705. int endian = EC448_BIG_ENDIAN;
  18706. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18707. ExpectIntEQ(wc_curve448_init(&key), 0);
  18708. ExpectIntEQ(wc_InitRng(&rng), 0);
  18709. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18710. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18711. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  18712. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18713. &key, endian), 0);
  18714. /* test bad cases */
  18715. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  18716. BAD_FUNC_ARG);
  18717. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  18718. &key, endian), BAD_FUNC_ARG);
  18719. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  18720. &key, endian), BAD_FUNC_ARG);
  18721. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18722. NULL, endian), BAD_FUNC_ARG);
  18723. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  18724. &key, endian), ECC_BAD_ARG_E);
  18725. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  18726. &key, endian), ECC_BAD_ARG_E);
  18727. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  18728. &key, EC448_LITTLE_ENDIAN), 0);
  18729. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18730. wc_curve448_free(&key);
  18731. #endif
  18732. return EXPECT_RESULT();
  18733. } /* END test_wc_curve448_import_private_raw_ex*/
  18734. /*
  18735. * Testing test_curve448_export_key_raw
  18736. */
  18737. static int test_wc_curve448_export_key_raw(void)
  18738. {
  18739. EXPECT_DECLS;
  18740. #if defined(HAVE_CURVE448)
  18741. curve448_key key;
  18742. WC_RNG rng;
  18743. byte priv[CURVE448_KEY_SIZE];
  18744. byte pub[CURVE448_KEY_SIZE];
  18745. word32 privSz = sizeof(priv);
  18746. word32 pubSz = sizeof(pub);
  18747. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18748. ExpectIntEQ(wc_curve448_init(&key), 0);
  18749. ExpectIntEQ(wc_InitRng(&rng), 0);
  18750. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18751. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18752. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  18753. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  18754. 0);
  18755. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18756. wc_curve448_free(&key);
  18757. #endif
  18758. return EXPECT_RESULT();
  18759. } /* END test_wc_curve448_import_private_raw_ex*/
  18760. /*
  18761. * Testing test_wc_curve448_import_private
  18762. */
  18763. static int test_wc_curve448_import_private(void)
  18764. {
  18765. EXPECT_DECLS;
  18766. #if defined(HAVE_CURVE448)
  18767. curve448_key key;
  18768. WC_RNG rng;
  18769. byte priv[CURVE448_KEY_SIZE];
  18770. word32 privSz = sizeof(priv);
  18771. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18772. ExpectIntEQ(wc_curve448_init(&key), 0);
  18773. ExpectIntEQ(wc_InitRng(&rng), 0);
  18774. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  18775. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  18776. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  18777. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18778. wc_curve448_free(&key);
  18779. #endif
  18780. return EXPECT_RESULT();
  18781. } /* END test_wc_curve448_import*/
  18782. /*
  18783. * Testing test_wc_curve448_size.
  18784. */
  18785. static int test_wc_curve448_size(void)
  18786. {
  18787. EXPECT_DECLS;
  18788. #if defined(HAVE_CURVE448)
  18789. curve448_key key;
  18790. ExpectIntEQ(wc_curve448_init(&key), 0);
  18791. /* Test good args for wc_curve448_size */
  18792. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  18793. /* Test bad args for wc_curve448_size */
  18794. ExpectIntEQ(wc_curve448_size(NULL), 0);
  18795. wc_curve448_free(&key);
  18796. #endif
  18797. return EXPECT_RESULT();
  18798. } /* END test_wc_curve448_size*/
  18799. /*
  18800. * Testing wc_ecc_make_key.
  18801. */
  18802. static int test_wc_ecc_make_key(void)
  18803. {
  18804. EXPECT_DECLS;
  18805. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18806. ecc_key key;
  18807. WC_RNG rng;
  18808. int ret;
  18809. XMEMSET(&key, 0, sizeof(ecc_key));
  18810. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18811. ExpectIntEQ(wc_ecc_init(&key), 0);
  18812. ExpectIntEQ(wc_InitRng(&rng), 0);
  18813. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18814. #if defined(WOLFSSL_ASYNC_CRYPT)
  18815. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18816. #endif
  18817. ExpectIntEQ(ret, 0);
  18818. /* Pass in bad args. */
  18819. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  18820. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  18821. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18822. wc_ecc_free(&key);
  18823. #ifdef FP_ECC
  18824. wc_ecc_fp_free();
  18825. #endif
  18826. #endif
  18827. return EXPECT_RESULT();
  18828. } /* END test_wc_ecc_make_key */
  18829. /*
  18830. * Testing wc_ecc_init()
  18831. */
  18832. static int test_wc_ecc_init(void)
  18833. {
  18834. EXPECT_DECLS;
  18835. #ifdef HAVE_ECC
  18836. ecc_key key;
  18837. XMEMSET(&key, 0, sizeof(ecc_key));
  18838. ExpectIntEQ(wc_ecc_init(&key), 0);
  18839. /* Pass in bad args. */
  18840. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  18841. wc_ecc_free(&key);
  18842. #endif
  18843. return EXPECT_RESULT();
  18844. } /* END test_wc_ecc_init */
  18845. /*
  18846. * Testing wc_ecc_check_key()
  18847. */
  18848. static int test_wc_ecc_check_key(void)
  18849. {
  18850. EXPECT_DECLS;
  18851. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18852. ecc_key key;
  18853. WC_RNG rng;
  18854. int ret;
  18855. XMEMSET(&rng, 0, sizeof(rng));
  18856. XMEMSET(&key, 0, sizeof(key));
  18857. ExpectIntEQ(wc_ecc_init(&key), 0);
  18858. ExpectIntEQ(wc_InitRng(&rng), 0);
  18859. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18860. #if defined(WOLFSSL_ASYNC_CRYPT)
  18861. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18862. #endif
  18863. ExpectIntEQ(ret, 0);
  18864. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  18865. /* Pass in bad args. */
  18866. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  18867. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18868. wc_ecc_free(&key);
  18869. #ifdef FP_ECC
  18870. wc_ecc_fp_free();
  18871. #endif
  18872. #endif
  18873. return EXPECT_RESULT();
  18874. } /* END test_wc_ecc_check_key */
  18875. /*
  18876. * Testing wc_ecc_get_generator()
  18877. */
  18878. static int test_wc_ecc_get_generator(void)
  18879. {
  18880. EXPECT_DECLS;
  18881. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  18882. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  18883. ecc_point* pt = NULL;
  18884. ExpectNotNull(pt = wc_ecc_new_point());
  18885. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  18886. MP_OKAY);
  18887. /* Test bad args. */
  18888. /* Returns Zero for bad arg. */
  18889. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  18890. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  18891. MP_OKAY);
  18892. /* If we ever get to 1000 curves increase this number */
  18893. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  18894. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  18895. wc_ecc_del_point(pt);
  18896. #endif
  18897. return EXPECT_RESULT();
  18898. } /* END test_wc_ecc_get_generator */
  18899. /*
  18900. * Testing wc_ecc_size()
  18901. */
  18902. static int test_wc_ecc_size(void)
  18903. {
  18904. EXPECT_DECLS;
  18905. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  18906. WC_RNG rng;
  18907. ecc_key key;
  18908. int ret;
  18909. XMEMSET(&key, 0, sizeof(ecc_key));
  18910. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18911. ExpectIntEQ(wc_ecc_init(&key), 0);
  18912. ExpectIntEQ(wc_InitRng(&rng), 0);
  18913. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18914. #if defined(WOLFSSL_ASYNC_CRYPT)
  18915. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18916. #endif
  18917. ExpectIntEQ(ret, 0);
  18918. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  18919. /* Test bad args. */
  18920. /* Returns Zero for bad arg. */
  18921. ExpectIntEQ(wc_ecc_size(NULL), 0);
  18922. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  18923. wc_ecc_free(&key);
  18924. #endif
  18925. return EXPECT_RESULT();
  18926. } /* END test_wc_ecc_size */
  18927. static int test_wc_ecc_params(void)
  18928. {
  18929. EXPECT_DECLS;
  18930. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  18931. It was added after certifications */
  18932. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18933. const ecc_set_type* ecc_set;
  18934. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  18935. /* Test for SECP256R1 curve */
  18936. int curve_id = ECC_SECP256R1;
  18937. int curve_idx;
  18938. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  18939. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  18940. ExpectIntEQ(ecc_set->id, curve_id);
  18941. #endif
  18942. /* Test case when SECP256R1 is not enabled */
  18943. /* Test that we get curve params for index 0 */
  18944. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  18945. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  18946. return EXPECT_RESULT();
  18947. }
  18948. /*
  18949. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  18950. */
  18951. static int test_wc_ecc_signVerify_hash(void)
  18952. {
  18953. EXPECT_DECLS;
  18954. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  18955. ecc_key key;
  18956. WC_RNG rng;
  18957. int ret;
  18958. #ifdef HAVE_ECC_VERIFY
  18959. int verify = 0;
  18960. #endif
  18961. word32 siglen = ECC_BUFSIZE;
  18962. byte sig[ECC_BUFSIZE];
  18963. byte adjustedSig[ECC_BUFSIZE+1];
  18964. byte digest[] = TEST_STRING;
  18965. word32 digestlen = (word32)TEST_STRING_SZ;
  18966. /* Init stack var */
  18967. XMEMSET(&key, 0, sizeof(ecc_key));
  18968. XMEMSET(&rng, 0, sizeof(WC_RNG));
  18969. XMEMSET(sig, 0, siglen);
  18970. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  18971. /* Init structs. */
  18972. ExpectIntEQ(wc_ecc_init(&key), 0);
  18973. ExpectIntEQ(wc_InitRng(&rng), 0);
  18974. ret = wc_ecc_make_key(&rng, KEY14, &key);
  18975. #if defined(WOLFSSL_ASYNC_CRYPT)
  18976. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  18977. #endif
  18978. ExpectIntEQ(ret, 0);
  18979. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  18980. 0);
  18981. /* Check bad args. */
  18982. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  18983. ECC_BAD_ARG_E);
  18984. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  18985. ECC_BAD_ARG_E);
  18986. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  18987. ECC_BAD_ARG_E);
  18988. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  18989. ECC_BAD_ARG_E);
  18990. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  18991. ECC_BAD_ARG_E);
  18992. #ifdef HAVE_ECC_VERIFY
  18993. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  18994. &key), 0);
  18995. ExpectIntEQ(verify, 1);
  18996. /* test check on length of signature passed in */
  18997. XMEMCPY(adjustedSig, sig, siglen);
  18998. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  18999. #ifndef NO_STRICT_ECDSA_LEN
  19000. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19001. &verify, &key), 0);
  19002. #else
  19003. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  19004. * is allowed */
  19005. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  19006. &verify, &key), 0);
  19007. #endif
  19008. /* Test bad args. */
  19009. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  19010. &key), ECC_BAD_ARG_E);
  19011. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  19012. ECC_BAD_ARG_E);
  19013. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  19014. ECC_BAD_ARG_E);
  19015. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  19016. NULL), ECC_BAD_ARG_E);
  19017. #endif /* HAVE_ECC_VERIFY */
  19018. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19019. wc_ecc_free(&key);
  19020. #ifdef FP_ECC
  19021. wc_ecc_fp_free();
  19022. #endif
  19023. #endif
  19024. return EXPECT_RESULT();
  19025. } /* END test_wc_ecc_sign_hash */
  19026. /*
  19027. * Testing wc_ecc_shared_secret()
  19028. */
  19029. static int test_wc_ecc_shared_secret(void)
  19030. {
  19031. EXPECT_DECLS;
  19032. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  19033. ecc_key key;
  19034. ecc_key pubKey;
  19035. WC_RNG rng;
  19036. #if defined(NO_ECC256)
  19037. int ret;
  19038. #endif
  19039. byte out[KEY32];
  19040. int keySz = sizeof(out);
  19041. word32 outlen = (word32)sizeof(out);
  19042. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19043. const char* qx =
  19044. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  19045. const char* qy =
  19046. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  19047. const char* d =
  19048. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  19049. const char* curveName = "SECP256R1";
  19050. const byte expected_shared_secret[] =
  19051. {
  19052. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  19053. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  19054. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  19055. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  19056. };
  19057. #endif
  19058. PRIVATE_KEY_UNLOCK();
  19059. /* Initialize variables. */
  19060. XMEMSET(&key, 0, sizeof(ecc_key));
  19061. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  19062. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19063. XMEMSET(out, 0, keySz);
  19064. ExpectIntEQ(wc_ecc_init(&key), 0);
  19065. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  19066. ExpectIntEQ(wc_InitRng(&rng), 0);
  19067. #if !defined(NO_ECC256)
  19068. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  19069. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  19070. #else
  19071. ret = wc_ecc_make_key(&rng, keySz, &key);
  19072. #if defined(WOLFSSL_ASYNC_CRYPT)
  19073. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19074. #endif
  19075. ExpectIntEQ(ret, 0);
  19076. ret = wc_ecc_make_key(&rng, keySz, &key);
  19077. #if defined(WOLFSSL_ASYNC_CRYPT)
  19078. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19079. #endif
  19080. ExpectIntEQ(ret, 0);
  19081. #endif
  19082. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19083. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19084. !defined(HAVE_SELFTEST)
  19085. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  19086. #endif
  19087. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  19088. #if !defined(NO_ECC256)
  19089. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  19090. #endif
  19091. /* Test bad args. */
  19092. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  19093. BAD_FUNC_ARG);
  19094. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  19095. BAD_FUNC_ARG);
  19096. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  19097. BAD_FUNC_ARG);
  19098. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  19099. BAD_FUNC_ARG);
  19100. /* Invalid length */
  19101. outlen = 1;
  19102. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  19103. BUFFER_E);
  19104. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19105. wc_ecc_free(&pubKey);
  19106. wc_ecc_free(&key);
  19107. #ifdef FP_ECC
  19108. wc_ecc_fp_free();
  19109. #endif
  19110. PRIVATE_KEY_LOCK();
  19111. #endif
  19112. return EXPECT_RESULT();
  19113. } /* END tests_wc_ecc_shared_secret */
  19114. /*
  19115. * testint wc_ecc_export_x963()
  19116. */
  19117. static int test_wc_ecc_export_x963(void)
  19118. {
  19119. EXPECT_DECLS;
  19120. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19121. ecc_key key;
  19122. WC_RNG rng;
  19123. byte out[ECC_ASN963_MAX_BUF_SZ];
  19124. word32 outlen = sizeof(out);
  19125. int ret;
  19126. PRIVATE_KEY_UNLOCK();
  19127. /* Initialize variables. */
  19128. XMEMSET(&key, 0, sizeof(ecc_key));
  19129. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19130. XMEMSET(out, 0, outlen);
  19131. ExpectIntEQ(wc_ecc_init(&key), 0);
  19132. ExpectIntEQ(wc_InitRng(&rng), 0);
  19133. ret = wc_ecc_make_key(&rng, KEY20, &key);
  19134. #if defined(WOLFSSL_ASYNC_CRYPT)
  19135. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19136. #endif
  19137. ExpectIntEQ(ret, 0);
  19138. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  19139. /* Test bad args. */
  19140. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  19141. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  19142. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  19143. key.idx = -4;
  19144. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  19145. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19146. wc_ecc_free(&key);
  19147. #ifdef FP_ECC
  19148. wc_ecc_fp_free();
  19149. #endif
  19150. PRIVATE_KEY_LOCK();
  19151. #endif
  19152. return EXPECT_RESULT();
  19153. } /* END test_wc_ecc_export_x963 */
  19154. /*
  19155. * Testing wc_ecc_export_x963_ex()
  19156. * compile with --enable-compkey will use compression.
  19157. */
  19158. static int test_wc_ecc_export_x963_ex(void)
  19159. {
  19160. EXPECT_DECLS;
  19161. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19162. ecc_key key;
  19163. WC_RNG rng;
  19164. int ret;
  19165. byte out[ECC_ASN963_MAX_BUF_SZ];
  19166. word32 outlen = sizeof(out);
  19167. #ifdef HAVE_COMP_KEY
  19168. word32 badOutLen = 5;
  19169. #endif
  19170. /* Init stack variables. */
  19171. XMEMSET(&key, 0, sizeof(ecc_key));
  19172. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19173. XMEMSET(out, 0, outlen);
  19174. ExpectIntEQ(wc_ecc_init(&key), 0);
  19175. ExpectIntEQ(wc_InitRng(&rng), 0);
  19176. ret = wc_ecc_make_key(&rng, KEY64, &key);
  19177. #if defined(WOLFSSL_ASYNC_CRYPT)
  19178. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19179. #endif
  19180. ExpectIntEQ(ret, 0);
  19181. #ifdef HAVE_COMP_KEY
  19182. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  19183. #else
  19184. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  19185. #endif
  19186. /* Test bad args. */
  19187. #ifdef HAVE_COMP_KEY
  19188. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  19189. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  19190. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  19191. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  19192. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  19193. #else
  19194. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  19195. LENGTH_ONLY_E);
  19196. #endif
  19197. key.idx = -4;
  19198. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  19199. #else
  19200. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  19201. ECC_BAD_ARG_E);
  19202. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  19203. LENGTH_ONLY_E);
  19204. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  19205. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  19206. ECC_BAD_ARG_E);
  19207. key.idx = -4;
  19208. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  19209. ECC_BAD_ARG_E);
  19210. #endif
  19211. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19212. wc_ecc_free(&key);
  19213. #ifdef FP_ECC
  19214. wc_ecc_fp_free();
  19215. #endif
  19216. #endif
  19217. return EXPECT_RESULT();
  19218. } /* END test_wc_ecc_export_x963_ex */
  19219. /*
  19220. * testing wc_ecc_import_x963()
  19221. */
  19222. static int test_wc_ecc_import_x963(void)
  19223. {
  19224. EXPECT_DECLS;
  19225. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19226. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19227. ecc_key pubKey;
  19228. ecc_key key;
  19229. WC_RNG rng;
  19230. byte x963[ECC_ASN963_MAX_BUF_SZ];
  19231. word32 x963Len = (word32)sizeof(x963);
  19232. int ret;
  19233. /* Init stack variables. */
  19234. XMEMSET(&key, 0, sizeof(ecc_key));
  19235. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  19236. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19237. XMEMSET(x963, 0, x963Len);
  19238. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  19239. ExpectIntEQ(wc_ecc_init(&key), 0);
  19240. ExpectIntEQ(wc_InitRng(&rng), 0);
  19241. ret = wc_ecc_make_key(&rng, KEY24, &key);
  19242. #if defined(WOLFSSL_ASYNC_CRYPT)
  19243. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19244. #endif
  19245. ExpectIntEQ(ret, 0);
  19246. PRIVATE_KEY_UNLOCK();
  19247. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  19248. PRIVATE_KEY_LOCK();
  19249. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  19250. /* Test bad args. */
  19251. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  19252. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  19253. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  19254. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19255. wc_ecc_free(&key);
  19256. wc_ecc_free(&pubKey);
  19257. #ifdef FP_ECC
  19258. wc_ecc_fp_free();
  19259. #endif
  19260. #endif
  19261. return EXPECT_RESULT();
  19262. } /* END wc_ecc_import_x963 */
  19263. /*
  19264. * testing wc_ecc_import_private_key()
  19265. */
  19266. static int test_wc_ecc_import_private_key(void)
  19267. {
  19268. EXPECT_DECLS;
  19269. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  19270. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19271. ecc_key key;
  19272. ecc_key keyImp;
  19273. WC_RNG rng;
  19274. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  19275. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  19276. word32 privKeySz = (word32)sizeof(privKey);
  19277. word32 x963KeySz = (word32)sizeof(x963Key);
  19278. int ret;
  19279. /* Init stack variables. */
  19280. XMEMSET(&key, 0, sizeof(ecc_key));
  19281. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  19282. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19283. XMEMSET(privKey, 0, privKeySz);
  19284. XMEMSET(x963Key, 0, x963KeySz);
  19285. ExpectIntEQ(wc_ecc_init(&key), 0);
  19286. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  19287. ExpectIntEQ(wc_InitRng(&rng), 0);
  19288. ret = wc_ecc_make_key(&rng, KEY48, &key);
  19289. #if defined(WOLFSSL_ASYNC_CRYPT)
  19290. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19291. #endif
  19292. ExpectIntEQ(ret, 0);
  19293. PRIVATE_KEY_UNLOCK();
  19294. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  19295. PRIVATE_KEY_LOCK();
  19296. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  19297. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19298. x963KeySz, &keyImp), 0);
  19299. /* Pass in bad args. */
  19300. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  19301. x963KeySz, NULL), BAD_FUNC_ARG);
  19302. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  19303. &keyImp), BAD_FUNC_ARG);
  19304. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19305. wc_ecc_free(&keyImp);
  19306. wc_ecc_free(&key);
  19307. #ifdef FP_ECC
  19308. wc_ecc_fp_free();
  19309. #endif
  19310. #endif
  19311. return EXPECT_RESULT();
  19312. } /* END test_wc_ecc_import_private_key */
  19313. /*
  19314. * Testing wc_ecc_export_private_only()
  19315. */
  19316. static int test_wc_ecc_export_private_only(void)
  19317. {
  19318. EXPECT_DECLS;
  19319. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  19320. ecc_key key;
  19321. WC_RNG rng;
  19322. byte out[ECC_PRIV_KEY_BUF];
  19323. word32 outlen = sizeof(out);
  19324. int ret;
  19325. /* Init stack variables. */
  19326. XMEMSET(&key, 0, sizeof(ecc_key));
  19327. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19328. XMEMSET(out, 0, outlen);
  19329. ExpectIntEQ(wc_ecc_init(&key), 0);
  19330. ExpectIntEQ(wc_InitRng(&rng), 0);
  19331. ret = wc_ecc_make_key(&rng, KEY32, &key);
  19332. #if defined(WOLFSSL_ASYNC_CRYPT)
  19333. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19334. #endif
  19335. ExpectIntEQ(ret, 0);
  19336. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  19337. /* Pass in bad args. */
  19338. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  19339. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  19340. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  19341. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19342. wc_ecc_free(&key);
  19343. #ifdef FP_ECC
  19344. wc_ecc_fp_free();
  19345. #endif
  19346. #endif
  19347. return EXPECT_RESULT();
  19348. } /* END test_wc_ecc_export_private_only */
  19349. /*
  19350. * Testing wc_ecc_rs_to_sig()
  19351. */
  19352. static int test_wc_ecc_rs_to_sig(void)
  19353. {
  19354. EXPECT_DECLS;
  19355. #if defined(HAVE_ECC) && !defined(NO_ASN)
  19356. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19357. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  19358. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  19359. const char* zeroStr = "0";
  19360. byte sig[ECC_MAX_SIG_SIZE];
  19361. word32 siglen = (word32)sizeof(sig);
  19362. /* R and S max size is the order of curve. 2^192.*/
  19363. int keySz = KEY24;
  19364. byte r[KEY24];
  19365. byte s[KEY24];
  19366. word32 rlen = (word32)sizeof(r);
  19367. word32 slen = (word32)sizeof(s);
  19368. /* Init stack variables. */
  19369. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  19370. XMEMSET(r, 0, keySz);
  19371. XMEMSET(s, 0, keySz);
  19372. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  19373. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  19374. /* Test bad args. */
  19375. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  19376. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  19377. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  19378. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  19379. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  19380. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  19381. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  19382. ECC_BAD_ARG_E);
  19383. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  19384. ECC_BAD_ARG_E);
  19385. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  19386. ECC_BAD_ARG_E);
  19387. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  19388. ECC_BAD_ARG_E);
  19389. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  19390. ECC_BAD_ARG_E);
  19391. #endif
  19392. return EXPECT_RESULT();
  19393. } /* END test_wc_ecc_rs_to_sig */
  19394. static int test_wc_ecc_import_raw(void)
  19395. {
  19396. EXPECT_DECLS;
  19397. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  19398. ecc_key key;
  19399. const char* qx =
  19400. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  19401. const char* qy =
  19402. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  19403. const char* d =
  19404. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  19405. const char* curveName = "SECP256R1";
  19406. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19407. const char* kNullStr = "";
  19408. int ret;
  19409. #endif
  19410. XMEMSET(&key, 0, sizeof(ecc_key));
  19411. ExpectIntEQ(wc_ecc_init(&key), 0);
  19412. /* Test good import */
  19413. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  19414. /* Test bad args. */
  19415. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  19416. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  19417. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  19418. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  19419. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19420. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19421. wc_ecc_free(&key);
  19422. #endif
  19423. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  19424. curveName), 0);
  19425. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  19426. #endif
  19427. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  19428. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19429. wc_ecc_free(&key);
  19430. #endif
  19431. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19432. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  19433. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  19434. #else
  19435. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  19436. #endif
  19437. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19438. wc_ecc_free(&key);
  19439. #endif
  19440. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19441. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  19442. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  19443. #else
  19444. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  19445. #endif
  19446. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  19447. wc_ecc_free(&key);
  19448. #endif
  19449. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  19450. #endif
  19451. wc_ecc_free(&key);
  19452. #endif
  19453. return EXPECT_RESULT();
  19454. } /* END test_wc_ecc_import_raw */
  19455. static int test_wc_ecc_import_unsigned(void)
  19456. {
  19457. EXPECT_DECLS;
  19458. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  19459. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  19460. HAVE_FIPS_VERSION >= 2))
  19461. ecc_key key;
  19462. const byte qx[] = {
  19463. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  19464. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  19465. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  19466. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  19467. };
  19468. const byte qy[] = {
  19469. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  19470. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  19471. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  19472. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  19473. };
  19474. const byte d[] = {
  19475. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  19476. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  19477. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  19478. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  19479. };
  19480. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19481. const byte nullBytes[32] = {0};
  19482. int ret;
  19483. #endif
  19484. int curveId = ECC_SECP256R1;
  19485. XMEMSET(&key, 0, sizeof(ecc_key));
  19486. ExpectIntEQ(wc_ecc_init(&key), 0);
  19487. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  19488. curveId), 0);
  19489. /* Test bad args. */
  19490. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  19491. curveId), BAD_FUNC_ARG);
  19492. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  19493. curveId), BAD_FUNC_ARG);
  19494. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  19495. curveId), BAD_FUNC_ARG);
  19496. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  19497. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  19498. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  19499. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  19500. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  19501. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  19502. #endif
  19503. wc_ecc_free(&key);
  19504. #endif
  19505. return EXPECT_RESULT();
  19506. } /* END test_wc_ecc_import_unsigned */
  19507. /*
  19508. * Testing wc_ecc_sig_size()
  19509. */
  19510. static int test_wc_ecc_sig_size(void)
  19511. {
  19512. EXPECT_DECLS;
  19513. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  19514. ecc_key key;
  19515. WC_RNG rng;
  19516. int keySz = KEY16;
  19517. int ret;
  19518. XMEMSET(&rng, 0, sizeof(rng));
  19519. XMEMSET(&key, 0, sizeof(key));
  19520. ExpectIntEQ(wc_ecc_init(&key), 0);
  19521. ExpectIntEQ(wc_InitRng(&rng), 0);
  19522. ret = wc_ecc_make_key(&rng, keySz, &key);
  19523. #if defined(WOLFSSL_ASYNC_CRYPT)
  19524. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19525. #endif
  19526. ExpectIntEQ(ret, 0);
  19527. ExpectIntLE(wc_ecc_sig_size(&key),
  19528. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  19529. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19530. wc_ecc_free(&key);
  19531. #endif
  19532. return EXPECT_RESULT();
  19533. } /* END test_wc_ecc_sig_size */
  19534. /*
  19535. * Testing wc_ecc_ctx_new()
  19536. */
  19537. static int test_wc_ecc_ctx_new(void)
  19538. {
  19539. EXPECT_DECLS;
  19540. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19541. WC_RNG rng;
  19542. ecEncCtx* cli = NULL;
  19543. ecEncCtx* srv = NULL;
  19544. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19545. ExpectIntEQ(wc_InitRng(&rng), 0);
  19546. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19547. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  19548. wc_ecc_ctx_free(cli);
  19549. cli = NULL;
  19550. wc_ecc_ctx_free(srv);
  19551. /* Test bad args. */
  19552. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  19553. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  19554. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  19555. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19556. wc_ecc_ctx_free(cli);
  19557. #endif
  19558. return EXPECT_RESULT();
  19559. } /* END test_wc_ecc_ctx_new */
  19560. /*
  19561. * Tesing wc_ecc_reset()
  19562. */
  19563. static int test_wc_ecc_ctx_reset(void)
  19564. {
  19565. EXPECT_DECLS;
  19566. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19567. ecEncCtx* ctx = NULL;
  19568. WC_RNG rng;
  19569. XMEMSET(&rng, 0, sizeof(rng));
  19570. ExpectIntEQ(wc_InitRng(&rng), 0);
  19571. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19572. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  19573. /* Pass in bad args. */
  19574. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  19575. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  19576. wc_ecc_ctx_free(ctx);
  19577. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19578. #endif
  19579. return EXPECT_RESULT();
  19580. } /* END test_wc_ecc_ctx_reset */
  19581. /*
  19582. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  19583. */
  19584. static int test_wc_ecc_ctx_set_peer_salt(void)
  19585. {
  19586. EXPECT_DECLS;
  19587. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19588. WC_RNG rng;
  19589. ecEncCtx* cliCtx = NULL;
  19590. ecEncCtx* servCtx = NULL;
  19591. const byte* cliSalt = NULL;
  19592. const byte* servSalt = NULL;
  19593. XMEMSET(&rng, 0, sizeof(rng));
  19594. ExpectIntEQ(wc_InitRng(&rng), 0);
  19595. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19596. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  19597. /* Test bad args. */
  19598. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  19599. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  19600. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  19601. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  19602. /* Test bad args. */
  19603. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  19604. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  19605. wc_ecc_ctx_free(cliCtx);
  19606. wc_ecc_ctx_free(servCtx);
  19607. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19608. #endif
  19609. return EXPECT_RESULT();
  19610. } /* END test_wc_ecc_ctx_set_peer_salt */
  19611. /*
  19612. * Testing wc_ecc_ctx_set_info()
  19613. */
  19614. static int test_wc_ecc_ctx_set_info(void)
  19615. {
  19616. EXPECT_DECLS;
  19617. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  19618. ecEncCtx* ctx = NULL;
  19619. WC_RNG rng;
  19620. const char* optInfo = "Optional Test Info.";
  19621. int optInfoSz = (int)XSTRLEN(optInfo);
  19622. const char* badOptInfo = NULL;
  19623. XMEMSET(&rng, 0, sizeof(rng));
  19624. ExpectIntEQ(wc_InitRng(&rng), 0);
  19625. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  19626. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  19627. /* Test bad args. */
  19628. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  19629. BAD_FUNC_ARG);
  19630. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  19631. BAD_FUNC_ARG);
  19632. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  19633. wc_ecc_ctx_free(ctx);
  19634. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19635. #endif
  19636. return EXPECT_RESULT();
  19637. } /* END test_wc_ecc_ctx_set_info */
  19638. /*
  19639. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  19640. */
  19641. static int test_wc_ecc_encryptDecrypt(void)
  19642. {
  19643. EXPECT_DECLS;
  19644. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  19645. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  19646. ecc_key srvKey;
  19647. ecc_key cliKey;
  19648. ecc_key tmpKey;
  19649. WC_RNG rng;
  19650. int ret;
  19651. const char* msg = "EccBlock Size 16";
  19652. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  19653. #ifdef WOLFSSL_ECIES_OLD
  19654. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  19655. #elif defined(WOLFSSL_ECIES_GEN_IV)
  19656. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  19657. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  19658. #else
  19659. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  19660. WC_SHA256_DIGEST_SIZE];
  19661. #endif
  19662. word32 outSz = (word32)sizeof(out);
  19663. byte plain[sizeof("EccBlock Size 16")];
  19664. word32 plainSz = (word32)sizeof(plain);
  19665. int keySz = KEY20;
  19666. /* Init stack variables. */
  19667. XMEMSET(out, 0, outSz);
  19668. XMEMSET(plain, 0, plainSz);
  19669. XMEMSET(&rng, 0, sizeof(rng));
  19670. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  19671. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  19672. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  19673. ExpectIntEQ(wc_InitRng(&rng), 0);
  19674. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  19675. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  19676. #if defined(WOLFSSL_ASYNC_CRYPT)
  19677. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  19678. #endif
  19679. ExpectIntEQ(ret, 0);
  19680. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  19681. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  19682. #if defined(WOLFSSL_ASYNC_CRYPT)
  19683. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  19684. #endif
  19685. ExpectIntEQ(ret, 0);
  19686. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  19687. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19688. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19689. !defined(HAVE_SELFTEST)
  19690. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  19691. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  19692. #endif
  19693. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  19694. &outSz, NULL), 0);
  19695. /* Test bad args. */
  19696. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  19697. NULL), BAD_FUNC_ARG);
  19698. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  19699. NULL), BAD_FUNC_ARG);
  19700. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  19701. NULL), BAD_FUNC_ARG);
  19702. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  19703. &outSz, NULL), BAD_FUNC_ARG);
  19704. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  19705. NULL), BAD_FUNC_ARG);
  19706. #ifdef WOLFSSL_ECIES_OLD
  19707. tmpKey.dp = cliKey.dp;
  19708. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  19709. #endif
  19710. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  19711. NULL), 0);
  19712. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  19713. NULL), BAD_FUNC_ARG);
  19714. #ifdef WOLFSSL_ECIES_OLD
  19715. /* NULL parameter allowed in new implementations - public key comes from
  19716. * the message. */
  19717. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  19718. NULL), BAD_FUNC_ARG);
  19719. #endif
  19720. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  19721. NULL), BAD_FUNC_ARG);
  19722. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  19723. NULL), BAD_FUNC_ARG);
  19724. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  19725. BAD_FUNC_ARG);
  19726. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  19727. wc_ecc_free(&tmpKey);
  19728. wc_ecc_free(&srvKey);
  19729. wc_ecc_free(&cliKey);
  19730. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19731. #endif
  19732. return EXPECT_RESULT();
  19733. } /* END test_wc_ecc_encryptDecrypt */
  19734. /*
  19735. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  19736. */
  19737. static int test_wc_ecc_del_point(void)
  19738. {
  19739. EXPECT_DECLS;
  19740. #if defined(HAVE_ECC)
  19741. ecc_point* pt = NULL;
  19742. ExpectNotNull(pt = wc_ecc_new_point());
  19743. wc_ecc_del_point(pt);
  19744. #endif
  19745. return EXPECT_RESULT();
  19746. } /* END test_wc_ecc_del_point */
  19747. /*
  19748. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  19749. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  19750. * and wc_ecc_cmp_point()
  19751. */
  19752. static int test_wc_ecc_pointFns(void)
  19753. {
  19754. EXPECT_DECLS;
  19755. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  19756. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19757. !defined(WOLFSSL_ATECC608A)
  19758. ecc_key key;
  19759. WC_RNG rng;
  19760. int ret;
  19761. ecc_point* point = NULL;
  19762. ecc_point* cpypt = NULL;
  19763. int idx = 0;
  19764. int keySz = KEY32;
  19765. byte der[DER_SZ(KEY32)];
  19766. word32 derlenChk = 0;
  19767. word32 derSz = DER_SZ(KEY32);
  19768. /* Init stack variables. */
  19769. XMEMSET(der, 0, derSz);
  19770. XMEMSET(&key, 0, sizeof(ecc_key));
  19771. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19772. ExpectIntEQ(wc_InitRng(&rng), 0);
  19773. ExpectIntEQ(wc_ecc_init(&key), 0);
  19774. ret = wc_ecc_make_key(&rng, keySz, &key);
  19775. #if defined(WOLFSSL_ASYNC_CRYPT)
  19776. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19777. #endif
  19778. ExpectIntEQ(ret, 0);
  19779. ExpectNotNull(point = wc_ecc_new_point());
  19780. ExpectNotNull(cpypt = wc_ecc_new_point());
  19781. /* Export */
  19782. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  19783. &derlenChk), LENGTH_ONLY_E);
  19784. /* Check length value. */
  19785. ExpectIntEQ(derSz, derlenChk);
  19786. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  19787. &derSz), 0);
  19788. /* Test bad args. */
  19789. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  19790. ECC_BAD_ARG_E);
  19791. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  19792. ECC_BAD_ARG_E);
  19793. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  19794. NULL), ECC_BAD_ARG_E);
  19795. /* Import */
  19796. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  19797. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  19798. /* Test bad args. */
  19799. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  19800. ECC_BAD_ARG_E);
  19801. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  19802. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  19803. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  19804. ECC_BAD_ARG_E);
  19805. /* Copy */
  19806. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  19807. /* Test bad args. */
  19808. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  19809. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  19810. /* Compare point */
  19811. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  19812. /* Test bad args. */
  19813. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  19814. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  19815. /* At infinity if return == 1, otherwise return == 0. */
  19816. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  19817. /* Test bad args. */
  19818. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  19819. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  19820. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  19821. #ifdef USE_ECC_B_PARAM
  19822. /* On curve if ret == 0 */
  19823. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  19824. /* Test bad args. */
  19825. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  19826. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  19827. #endif /* USE_ECC_B_PARAM */
  19828. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  19829. /* Free */
  19830. wc_ecc_del_point(point);
  19831. wc_ecc_del_point(cpypt);
  19832. wc_ecc_free(&key);
  19833. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19834. #endif
  19835. return EXPECT_RESULT();
  19836. } /* END test_wc_ecc_pointFns */
  19837. /*
  19838. * Testing wc_ecc_sahred_secret_ssh()
  19839. */
  19840. static int test_wc_ecc_shared_secret_ssh(void)
  19841. {
  19842. EXPECT_DECLS;
  19843. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  19844. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19845. !defined(WOLFSSL_ATECC608A)
  19846. ecc_key key;
  19847. ecc_key key2;
  19848. WC_RNG rng;
  19849. int ret;
  19850. int keySz = KEY32;
  19851. int key2Sz = KEY24;
  19852. byte secret[KEY32];
  19853. word32 secretLen = keySz;
  19854. /* Init stack variables. */
  19855. XMEMSET(&key, 0, sizeof(ecc_key));
  19856. XMEMSET(&key2, 0, sizeof(ecc_key));
  19857. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19858. XMEMSET(secret, 0, secretLen);
  19859. /* Make keys */
  19860. ExpectIntEQ(wc_ecc_init(&key), 0);
  19861. ExpectIntEQ(wc_InitRng(&rng), 0);
  19862. ret = wc_ecc_make_key(&rng, keySz, &key);
  19863. #if defined(WOLFSSL_ASYNC_CRYPT)
  19864. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19865. #endif
  19866. ExpectIntEQ(ret, 0);
  19867. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19868. ExpectIntEQ(wc_ecc_init(&key2), 0);
  19869. ExpectIntEQ(wc_InitRng(&rng), 0);
  19870. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  19871. #if defined(WOLFSSL_ASYNC_CRYPT)
  19872. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  19873. #endif
  19874. ExpectIntEQ(ret, 0);
  19875. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  19876. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  19877. !defined(HAVE_SELFTEST)
  19878. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  19879. #endif
  19880. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  19881. &secretLen), 0);
  19882. /* Pass in bad args. */
  19883. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  19884. &secretLen), BAD_FUNC_ARG);
  19885. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  19886. BAD_FUNC_ARG);
  19887. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  19888. BAD_FUNC_ARG);
  19889. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  19890. BAD_FUNC_ARG);
  19891. key.type = ECC_PUBLICKEY;
  19892. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  19893. &secretLen), ECC_BAD_ARG_E);
  19894. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19895. wc_ecc_free(&key);
  19896. wc_ecc_free(&key2);
  19897. #ifdef FP_ECC
  19898. wc_ecc_fp_free();
  19899. #endif
  19900. #endif
  19901. return EXPECT_RESULT();
  19902. } /* END test_wc_ecc_shared_secret_ssh */
  19903. /*
  19904. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  19905. */
  19906. static int test_wc_ecc_verify_hash_ex(void)
  19907. {
  19908. EXPECT_DECLS;
  19909. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  19910. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  19911. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  19912. ecc_key key;
  19913. WC_RNG rng;
  19914. int ret;
  19915. mp_int r;
  19916. mp_int s;
  19917. mp_int z;
  19918. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  19919. unsigned char iHash[] = "Everyone gets Friday off.......";
  19920. unsigned char shortHash[] = TEST_STRING;
  19921. word32 hashlen = sizeof(hash);
  19922. word32 iHashLen = sizeof(iHash);
  19923. word32 shortHashLen = sizeof(shortHash);
  19924. int keySz = KEY32;
  19925. int verify_ok = 0;
  19926. XMEMSET(&key, 0, sizeof(ecc_key));
  19927. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19928. XMEMSET(&r, 0, sizeof(mp_int));
  19929. XMEMSET(&s, 0, sizeof(mp_int));
  19930. XMEMSET(&z, 0, sizeof(mp_int));
  19931. /* Initialize r, s and z. */
  19932. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  19933. ExpectIntEQ(wc_ecc_init(&key), 0);
  19934. ExpectIntEQ(wc_InitRng(&rng), 0);
  19935. ret = wc_ecc_make_key(&rng, keySz, &key);
  19936. #if defined(WOLFSSL_ASYNC_CRYPT)
  19937. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  19938. #endif
  19939. ExpectIntEQ(ret, 0);
  19940. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  19941. /* verify_ok should be 1. */
  19942. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  19943. 0);
  19944. ExpectIntEQ(verify_ok, 1);
  19945. /* verify_ok should be 0 */
  19946. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  19947. &key), 0);
  19948. ExpectIntEQ(verify_ok, 0);
  19949. /* verify_ok should be 0. */
  19950. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  19951. &verify_ok, &key), 0);
  19952. ExpectIntEQ(verify_ok, 0);
  19953. /* Test bad args. */
  19954. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  19955. ECC_BAD_ARG_E);
  19956. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  19957. ECC_BAD_ARG_E);
  19958. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  19959. ECC_BAD_ARG_E);
  19960. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  19961. ECC_BAD_ARG_E);
  19962. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  19963. ECC_BAD_ARG_E);
  19964. /* Test bad args. */
  19965. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  19966. &verify_ok, &key), ECC_BAD_ARG_E);
  19967. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  19968. &verify_ok, &key), ECC_BAD_ARG_E);
  19969. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  19970. &verify_ok, &key), MP_ZERO_E);
  19971. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  19972. &verify_ok, &key), MP_ZERO_E);
  19973. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  19974. &verify_ok, &key), MP_ZERO_E);
  19975. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  19976. &key), ECC_BAD_ARG_E);
  19977. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  19978. &key), ECC_BAD_ARG_E);
  19979. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  19980. &verify_ok, NULL), ECC_BAD_ARG_E);
  19981. wc_ecc_free(&key);
  19982. mp_free(&r);
  19983. mp_free(&s);
  19984. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19985. #endif
  19986. return EXPECT_RESULT();
  19987. } /* END test_wc_ecc_verify_hash_ex */
  19988. /*
  19989. * Testing wc_ecc_mulmod()
  19990. */
  19991. static int test_wc_ecc_mulmod(void)
  19992. {
  19993. EXPECT_DECLS;
  19994. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  19995. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  19996. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  19997. ecc_key key1;
  19998. ecc_key key2;
  19999. ecc_key key3;
  20000. WC_RNG rng;
  20001. int ret;
  20002. XMEMSET(&key1, 0, sizeof(ecc_key));
  20003. XMEMSET(&key2, 0, sizeof(ecc_key));
  20004. XMEMSET(&key3, 0, sizeof(ecc_key));
  20005. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20006. ExpectIntEQ(wc_ecc_init(&key1), 0);
  20007. ExpectIntEQ(wc_ecc_init(&key2), 0);
  20008. ExpectIntEQ(wc_ecc_init(&key3), 0);
  20009. ExpectIntEQ(wc_InitRng(&rng), 0);
  20010. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  20011. #if defined(WOLFSSL_ASYNC_CRYPT)
  20012. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  20013. #endif
  20014. ExpectIntEQ(ret, 0);
  20015. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20016. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  20017. key1.dp->Af, ECC_SECP256R1), 0);
  20018. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  20019. key1.dp->prime, ECC_SECP256R1), 0);
  20020. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  20021. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  20022. 1), 0);
  20023. /* Test bad args. */
  20024. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  20025. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20026. ECC_BAD_ARG_E);
  20027. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  20028. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20029. ECC_BAD_ARG_E);
  20030. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  20031. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  20032. ECC_BAD_ARG_E);
  20033. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  20034. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  20035. wc_ecc_free(&key1);
  20036. wc_ecc_free(&key2);
  20037. wc_ecc_free(&key3);
  20038. #ifdef FP_ECC
  20039. wc_ecc_fp_free();
  20040. #endif
  20041. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  20042. return EXPECT_RESULT();
  20043. } /* END test_wc_ecc_mulmod */
  20044. /*
  20045. * Testing wc_ecc_is_valid_idx()
  20046. */
  20047. static int test_wc_ecc_is_valid_idx(void)
  20048. {
  20049. EXPECT_DECLS;
  20050. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20051. ecc_key key;
  20052. WC_RNG rng;
  20053. int ret;
  20054. int iVal = -2;
  20055. int iVal2 = 3000;
  20056. XMEMSET(&key, 0, sizeof(ecc_key));
  20057. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20058. ExpectIntEQ(wc_ecc_init(&key), 0);
  20059. ExpectIntEQ(wc_InitRng(&rng), 0);
  20060. ret = wc_ecc_make_key(&rng, 32, &key);
  20061. #if defined(WOLFSSL_ASYNC_CRYPT)
  20062. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20063. #endif
  20064. ExpectIntEQ(ret, 0);
  20065. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  20066. /* Test bad args. */
  20067. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  20068. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  20069. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20070. wc_ecc_free(&key);
  20071. #ifdef FP_ECC
  20072. wc_ecc_fp_free();
  20073. #endif
  20074. #endif
  20075. return EXPECT_RESULT();
  20076. } /* END test_wc_ecc_is_valid_idx */
  20077. /*
  20078. * Testing wc_ecc_get_curve_id_from_oid()
  20079. */
  20080. static int test_wc_ecc_get_curve_id_from_oid(void)
  20081. {
  20082. EXPECT_DECLS;
  20083. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  20084. !defined(HAVE_FIPS)
  20085. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  20086. word32 len = sizeof(oid);
  20087. /* Bad Cases */
  20088. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  20089. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  20090. /* Good Case */
  20091. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  20092. #endif
  20093. return EXPECT_RESULT();
  20094. } /* END test_wc_ecc_get_curve_id_from_oid */
  20095. /*
  20096. * Testing wc_ecc_sig_size_calc()
  20097. */
  20098. static int test_wc_ecc_sig_size_calc(void)
  20099. {
  20100. EXPECT_DECLS;
  20101. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  20102. ecc_key key;
  20103. WC_RNG rng;
  20104. int sz = 0;
  20105. int ret;
  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, 16, &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. sz = key.dp->size;
  20116. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  20117. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20118. wc_ecc_free(&key);
  20119. #endif
  20120. return EXPECT_RESULT();
  20121. } /* END test_wc_ecc_sig_size_calc */
  20122. /*
  20123. * Testing wc_ecc_sm2_make_key()
  20124. */
  20125. static int test_wc_ecc_sm2_make_key(void)
  20126. {
  20127. int res = TEST_SKIPPED;
  20128. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  20129. EXPECT_DECLS;
  20130. WC_RNG rng[1];
  20131. ecc_key key[1];
  20132. XMEMSET(rng, 0, sizeof(*rng));
  20133. XMEMSET(key, 0, sizeof(*key));
  20134. ExpectIntEQ(wc_InitRng(rng), 0);
  20135. ExpectIntEQ(wc_ecc_init(key), 0);
  20136. /* Test invalid parameters. */
  20137. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  20138. BAD_FUNC_ARG);
  20139. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  20140. BAD_FUNC_ARG);
  20141. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  20142. BAD_FUNC_ARG);
  20143. /* Test valid parameters. */
  20144. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20145. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  20146. wc_ecc_free(key);
  20147. wc_FreeRng(rng);
  20148. #ifdef FP_ECC
  20149. wc_ecc_fp_free();
  20150. #endif
  20151. res = EXPECT_RESULT();
  20152. #endif
  20153. return res;
  20154. }
  20155. /*
  20156. * Testing wc_ecc_sm2_shared_secret()
  20157. */
  20158. static int test_wc_ecc_sm2_shared_secret(void)
  20159. {
  20160. int res = TEST_SKIPPED;
  20161. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  20162. EXPECT_DECLS;
  20163. WC_RNG rng[1];
  20164. ecc_key keyA[1];
  20165. ecc_key keyB[1];
  20166. byte outA[32];
  20167. byte outB[32];
  20168. word32 outALen = 32;
  20169. word32 outBLen = 32;
  20170. XMEMSET(rng, 0, sizeof(*rng));
  20171. XMEMSET(keyA, 0, sizeof(*keyA));
  20172. XMEMSET(keyB, 0, sizeof(*keyB));
  20173. ExpectIntEQ(wc_InitRng(rng), 0);
  20174. ExpectIntEQ(wc_ecc_init(keyA), 0);
  20175. ExpectIntEQ(wc_ecc_init(keyB), 0);
  20176. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  20177. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  20178. #ifdef ECC_TIMING_RESISTANT
  20179. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  20180. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  20181. #endif
  20182. /* Test invalid parameters. */
  20183. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  20184. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  20185. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  20186. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  20187. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  20188. BAD_FUNC_ARG);
  20189. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  20190. BAD_FUNC_ARG);
  20191. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  20192. BAD_FUNC_ARG);
  20193. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  20194. BAD_FUNC_ARG);
  20195. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  20196. /* Test valid parameters. */
  20197. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  20198. ExpectIntLE(outALen, 32);
  20199. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  20200. ExpectIntLE(outBLen, 32);
  20201. ExpectIntEQ(outALen, outBLen);
  20202. ExpectBufEQ(outA, outB, outALen);
  20203. wc_ecc_free(keyB);
  20204. wc_ecc_free(keyA);
  20205. wc_FreeRng(rng);
  20206. #ifdef FP_ECC
  20207. wc_ecc_fp_free();
  20208. #endif
  20209. res = EXPECT_RESULT();
  20210. #endif
  20211. return res;
  20212. }
  20213. /*
  20214. * Testing wc_ecc_sm2_create_digest()
  20215. */
  20216. static int test_wc_ecc_sm2_create_digest(void)
  20217. {
  20218. int res = TEST_SKIPPED;
  20219. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  20220. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  20221. EXPECT_DECLS;
  20222. ecc_key key[1];
  20223. enum wc_HashType hashType;
  20224. unsigned char pub[] = {
  20225. 0x04,
  20226. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20227. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20228. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20229. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20230. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20231. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20232. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20233. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20234. };
  20235. unsigned char id[] = {
  20236. 0x01, 0x02, 0x03,
  20237. };
  20238. unsigned char msg[] = {
  20239. 0x01, 0x02, 0x03,
  20240. };
  20241. unsigned char hash[32];
  20242. #ifdef WOLFSSL_SM3
  20243. unsigned char expHash[32] = {
  20244. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  20245. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  20246. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  20247. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  20248. };
  20249. #else
  20250. unsigned char expHash[32] = {
  20251. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  20252. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  20253. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  20254. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  20255. };
  20256. #endif
  20257. #ifdef WOLFSSL_SM3
  20258. hashType = WC_HASH_TYPE_SM3;
  20259. #else
  20260. hashType = WC_HASH_TYPE_SHA256;
  20261. #endif
  20262. XMEMSET(key, 0, sizeof(*key));
  20263. ExpectIntEQ(wc_ecc_init(key), 0);
  20264. /* Test with no curve set. */
  20265. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20266. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20267. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20268. /* Test invalid parameters. */
  20269. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20270. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20271. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  20272. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20273. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  20274. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  20275. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20276. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  20277. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  20278. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  20279. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  20280. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20281. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  20282. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  20283. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20284. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  20285. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20286. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  20287. /* Bad hash type. */
  20288. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20289. -1, hash, 0, key), BAD_FUNC_ARG);
  20290. /* Bad hash size. */
  20291. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20292. hashType, hash, 0, key), BUFFER_E);
  20293. /* Test valid parameters. */
  20294. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  20295. hashType, hash, sizeof(hash), key), 0);
  20296. ExpectBufEQ(hash, expHash, sizeof(expHash));
  20297. wc_ecc_free(key);
  20298. res = EXPECT_RESULT();
  20299. #endif
  20300. return res;
  20301. }
  20302. /*
  20303. * Testing wc_ecc_sm2_verify_hash_ex()
  20304. */
  20305. static int test_wc_ecc_sm2_verify_hash_ex(void)
  20306. {
  20307. int res = TEST_SKIPPED;
  20308. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  20309. defined(WOLFSSL_PUBLIC_MP)
  20310. EXPECT_DECLS;
  20311. ecc_key key[1];
  20312. mp_int r[1];
  20313. mp_int s[1];
  20314. int verified;
  20315. unsigned char pub[] = {
  20316. 0x04,
  20317. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20318. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20319. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20320. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20321. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20322. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20323. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20324. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20325. };
  20326. unsigned char hash[] = {
  20327. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  20328. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  20329. 0x50, 0x69, 0x5B, 0x20
  20330. };
  20331. unsigned char rData[] = {
  20332. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  20333. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  20334. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  20335. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  20336. };
  20337. unsigned char sData[] = {
  20338. 0x1D,
  20339. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20340. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20341. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20342. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  20343. };
  20344. unsigned char rBadData[] = {
  20345. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  20346. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  20347. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  20348. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  20349. };
  20350. XMEMSET(key, 0, sizeof(*key));
  20351. XMEMSET(r, 0, sizeof(*r));
  20352. XMEMSET(s, 0, sizeof(*s));
  20353. ExpectIntEQ(mp_init(r), 0);
  20354. ExpectIntEQ(mp_init(s), 0);
  20355. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  20356. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  20357. ExpectIntEQ(wc_ecc_init(key), 0);
  20358. /* Test with no curve set. */
  20359. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20360. &verified, key), BAD_FUNC_ARG);
  20361. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20362. /* Test invalid parameters. */
  20363. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20364. NULL, NULL), BAD_FUNC_ARG);
  20365. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  20366. NULL, NULL), BAD_FUNC_ARG);
  20367. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  20368. NULL, NULL), BAD_FUNC_ARG);
  20369. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  20370. NULL, NULL), BAD_FUNC_ARG);
  20371. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20372. &verified, NULL), BAD_FUNC_ARG);
  20373. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  20374. NULL, key), BAD_FUNC_ARG);
  20375. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  20376. &verified, key), BAD_FUNC_ARG);
  20377. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  20378. &verified, key), BAD_FUNC_ARG);
  20379. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  20380. &verified, key), BAD_FUNC_ARG);
  20381. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20382. NULL, key), BAD_FUNC_ARG);
  20383. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20384. &verified, NULL), BAD_FUNC_ARG);
  20385. /* Make key not on the SM2 curve. */
  20386. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20387. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20388. &verified, key), BAD_FUNC_ARG);
  20389. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20390. /* Test valid parameters. */
  20391. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20392. &verified, key), 0);
  20393. ExpectIntEQ(verified, 1);
  20394. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  20395. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  20396. &verified, key), 0);
  20397. ExpectIntEQ(verified, 0);
  20398. mp_free(s);
  20399. mp_free(r);
  20400. wc_ecc_free(key);
  20401. #ifdef FP_ECC
  20402. wc_ecc_fp_free();
  20403. #endif
  20404. res = EXPECT_RESULT();
  20405. #endif
  20406. return res;
  20407. }
  20408. /*
  20409. * Testing wc_ecc_sm2_verify_hash()
  20410. */
  20411. static int test_wc_ecc_sm2_verify_hash(void)
  20412. {
  20413. int res = TEST_SKIPPED;
  20414. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  20415. EXPECT_DECLS;
  20416. ecc_key key[1];
  20417. int verified;
  20418. unsigned char pub[] = {
  20419. 0x04,
  20420. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  20421. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  20422. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  20423. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  20424. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  20425. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  20426. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  20427. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  20428. };
  20429. unsigned char hash[] = {
  20430. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  20431. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  20432. 0x50, 0x69, 0x5B, 0x20
  20433. };
  20434. unsigned char sig[] = {
  20435. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  20436. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  20437. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  20438. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  20439. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  20440. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20441. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20442. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20443. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  20444. };
  20445. unsigned char sigBad[] = {
  20446. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  20447. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  20448. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  20449. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  20450. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  20451. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  20452. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  20453. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  20454. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  20455. };
  20456. XMEMSET(key, 0, sizeof(*key));
  20457. ExpectIntEQ(wc_ecc_init(key), 0);
  20458. /* Test with no curve set. */
  20459. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20460. &verified, key), BAD_FUNC_ARG);
  20461. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  20462. /* Test invalid parameters. */
  20463. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20464. NULL, NULL), BAD_FUNC_ARG);
  20465. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  20466. NULL, NULL), BAD_FUNC_ARG);
  20467. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  20468. NULL, NULL), BAD_FUNC_ARG);
  20469. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20470. &verified, NULL), BAD_FUNC_ARG);
  20471. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  20472. NULL, key), BAD_FUNC_ARG);
  20473. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  20474. &verified, key), BAD_FUNC_ARG);
  20475. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  20476. &verified, key), BAD_FUNC_ARG);
  20477. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20478. NULL, key), BAD_FUNC_ARG);
  20479. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20480. &verified, NULL), BAD_FUNC_ARG);
  20481. /* Make key not on the SM2 curve. */
  20482. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20483. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20484. &verified, key), BAD_FUNC_ARG);
  20485. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20486. /* Test valid parameters. */
  20487. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  20488. &verified, key), 0);
  20489. ExpectIntEQ(verified, 1);
  20490. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  20491. sizeof(hash), &verified, key), 0);
  20492. ExpectIntEQ(verified, 0);
  20493. wc_ecc_free(key);
  20494. #ifdef FP_ECC
  20495. wc_ecc_fp_free();
  20496. #endif
  20497. res = EXPECT_RESULT();
  20498. #endif
  20499. return res;
  20500. }
  20501. /*
  20502. * Testing wc_ecc_sm2_verify_hash_ex()
  20503. */
  20504. static int test_wc_ecc_sm2_sign_hash_ex(void)
  20505. {
  20506. int res = TEST_SKIPPED;
  20507. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  20508. defined(WOLFSSL_PUBLIC_MP)
  20509. EXPECT_DECLS;
  20510. WC_RNG rng[1];
  20511. ecc_key key[1];
  20512. mp_int r[1];
  20513. mp_int s[1];
  20514. unsigned char hash[32];
  20515. #ifdef HAVE_ECC_VERIFY
  20516. int verified;
  20517. #endif
  20518. XMEMSET(rng, 0, sizeof(*rng));
  20519. XMEMSET(key, 0, sizeof(*key));
  20520. XMEMSET(r, 0, sizeof(*r));
  20521. XMEMSET(s, 0, sizeof(*s));
  20522. ExpectIntEQ(wc_InitRng(rng), 0);
  20523. ExpectIntEQ(mp_init(r), 0);
  20524. ExpectIntEQ(mp_init(s), 0);
  20525. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  20526. ExpectIntEQ(wc_ecc_init(key), 0);
  20527. /* Test with no curve set. */
  20528. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20529. BAD_FUNC_ARG);
  20530. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20531. /* Test invalid parameters. */
  20532. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  20533. NULL), BAD_FUNC_ARG);
  20534. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  20535. NULL), BAD_FUNC_ARG);
  20536. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  20537. NULL), BAD_FUNC_ARG);
  20538. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  20539. NULL), BAD_FUNC_ARG);
  20540. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  20541. NULL), BAD_FUNC_ARG);
  20542. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  20543. s), BAD_FUNC_ARG);
  20544. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  20545. BAD_FUNC_ARG);
  20546. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  20547. BAD_FUNC_ARG);
  20548. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  20549. BAD_FUNC_ARG);
  20550. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  20551. BAD_FUNC_ARG);
  20552. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  20553. BAD_FUNC_ARG);
  20554. /* Make key not on the SM2 curve. */
  20555. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20556. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20557. BAD_FUNC_ARG);
  20558. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20559. #ifdef WOLFSSL_SP_MATH_ALL
  20560. {
  20561. mp_int smallR[1];
  20562. sp_init_size(smallR, 1);
  20563. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  20564. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  20565. smallR, s), MP_VAL);
  20566. }
  20567. #endif
  20568. /* Test valid parameters. */
  20569. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  20570. 0);
  20571. #ifdef HAVE_ECC_VERIFY
  20572. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  20573. key), 0);
  20574. ExpectIntEQ(verified, 1);
  20575. #endif
  20576. mp_free(s);
  20577. mp_free(r);
  20578. wc_ecc_free(key);
  20579. wc_FreeRng(rng);
  20580. #ifdef FP_ECC
  20581. wc_ecc_fp_free();
  20582. #endif
  20583. res = EXPECT_RESULT();
  20584. #endif
  20585. return res;
  20586. }
  20587. /*
  20588. * Testing wc_ecc_sm2_verify_hash()
  20589. */
  20590. static int test_wc_ecc_sm2_sign_hash(void)
  20591. {
  20592. int res = TEST_SKIPPED;
  20593. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  20594. EXPECT_DECLS;
  20595. WC_RNG rng[1];
  20596. ecc_key key[1];
  20597. unsigned char hash[32];
  20598. unsigned char sig[72];
  20599. word32 sigSz = sizeof(sig);
  20600. #ifdef HAVE_ECC_VERIFY
  20601. int verified;
  20602. #endif
  20603. XMEMSET(rng, 0, sizeof(*rng));
  20604. XMEMSET(key, 0, sizeof(*key));
  20605. ExpectIntEQ(wc_InitRng(rng), 0);
  20606. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  20607. ExpectIntEQ(wc_ecc_init(key), 0);
  20608. /* Test with no curve set. */
  20609. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20610. BAD_FUNC_ARG);
  20611. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  20612. /* Test invalid parameters. */
  20613. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  20614. NULL), BAD_FUNC_ARG);
  20615. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  20616. NULL), BAD_FUNC_ARG);
  20617. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  20618. NULL), BAD_FUNC_ARG);
  20619. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  20620. NULL), BAD_FUNC_ARG);
  20621. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  20622. NULL), BAD_FUNC_ARG);
  20623. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  20624. key), BAD_FUNC_ARG);
  20625. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  20626. key), BAD_FUNC_ARG);
  20627. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  20628. key), BAD_FUNC_ARG);
  20629. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  20630. key), BAD_FUNC_ARG);
  20631. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  20632. key), BAD_FUNC_ARG);
  20633. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  20634. NULL), BAD_FUNC_ARG);
  20635. /* Make key not on the SM2 curve. */
  20636. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  20637. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20638. BAD_FUNC_ARG);
  20639. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  20640. /* Test valid parameters. */
  20641. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  20642. 0);
  20643. #ifdef HAVE_ECC_VERIFY
  20644. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  20645. &verified, key), 0);
  20646. ExpectIntEQ(verified, 1);
  20647. #endif
  20648. wc_ecc_free(key);
  20649. wc_FreeRng(rng);
  20650. #ifdef FP_ECC
  20651. wc_ecc_fp_free();
  20652. #endif
  20653. res = EXPECT_RESULT();
  20654. #endif
  20655. return res;
  20656. }
  20657. /*
  20658. * Testing ToTraditional
  20659. */
  20660. static int test_ToTraditional(void)
  20661. {
  20662. EXPECT_DECLS;
  20663. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  20664. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  20665. defined(OPENSSL_EXTRA_X509_SMALL))
  20666. XFILE f = XBADFILE;
  20667. byte input[TWOK_BUF];
  20668. word32 sz;
  20669. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  20670. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  20671. if (f != XBADFILE)
  20672. XFCLOSE(f);
  20673. /* Good case */
  20674. ExpectIntGT(ToTraditional(input, sz), 0);
  20675. /* Bad cases */
  20676. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  20677. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  20678. #ifdef WOLFSSL_ASN_TEMPLATE
  20679. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  20680. #else
  20681. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  20682. #endif
  20683. #endif
  20684. return EXPECT_RESULT();
  20685. } /* End test_ToTraditional*/
  20686. /*
  20687. * Testing wc_EccPrivateKeyToDer
  20688. */
  20689. static int test_wc_EccPrivateKeyToDer(void)
  20690. {
  20691. EXPECT_DECLS;
  20692. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20693. byte output[ONEK_BUF];
  20694. ecc_key eccKey;
  20695. WC_RNG rng;
  20696. word32 inLen;
  20697. word32 outLen = 0;
  20698. int ret;
  20699. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  20700. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20701. ExpectIntEQ(wc_InitRng(&rng), 0);
  20702. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  20703. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  20704. #if defined(WOLFSSL_ASYNC_CRYPT)
  20705. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  20706. #endif
  20707. ExpectIntEQ(ret, 0);
  20708. inLen = (word32)sizeof(output);
  20709. /* Bad Cases */
  20710. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20711. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20712. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  20713. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  20714. /* Good Case */
  20715. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  20716. wc_ecc_free(&eccKey);
  20717. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20718. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  20719. {
  20720. /* test importing private only into a PKEY struct */
  20721. EC_KEY* ec = NULL;
  20722. EVP_PKEY* pkey = NULL;
  20723. const unsigned char* der;
  20724. der = output;
  20725. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  20726. der = output;
  20727. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  20728. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  20729. if (EXPECT_FAIL()) {
  20730. EC_KEY_free(ec);
  20731. }
  20732. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  20733. }
  20734. #endif
  20735. #endif
  20736. return EXPECT_RESULT();
  20737. } /* End test_wc_EccPrivateKeyToDer*/
  20738. /*
  20739. * Testing wc_DhPublicKeyDecode
  20740. */
  20741. static int test_wc_DhPublicKeyDecode(void)
  20742. {
  20743. EXPECT_DECLS;
  20744. #ifndef NO_DH
  20745. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  20746. DhKey key;
  20747. word32 inOutIdx;
  20748. XMEMSET(&key, 0, sizeof(DhKey));
  20749. ExpectIntEQ(wc_InitDhKey(&key), 0);
  20750. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  20751. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  20752. BAD_FUNC_ARG);
  20753. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  20754. BAD_FUNC_ARG);
  20755. inOutIdx = 0;
  20756. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  20757. BAD_FUNC_ARG);
  20758. inOutIdx = 0;
  20759. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  20760. BAD_FUNC_ARG);
  20761. inOutIdx = 0;
  20762. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  20763. sizeof_dh_pub_key_der_2048), 0);
  20764. ExpectIntNE(key.p.used, 0);
  20765. ExpectIntNE(key.g.used, 0);
  20766. ExpectIntEQ(key.q.used, 0);
  20767. ExpectIntNE(key.pub.used, 0);
  20768. ExpectIntEQ(key.priv.used, 0);
  20769. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  20770. #endif
  20771. #endif /* !NO_DH */
  20772. return EXPECT_RESULT();
  20773. }
  20774. /*
  20775. * Testing wc_Ed25519KeyToDer
  20776. */
  20777. static int test_wc_Ed25519KeyToDer(void)
  20778. {
  20779. EXPECT_DECLS;
  20780. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20781. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20782. byte output[ONEK_BUF];
  20783. ed25519_key ed25519Key;
  20784. WC_RNG rng;
  20785. word32 inLen;
  20786. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  20787. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20788. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  20789. ExpectIntEQ(wc_InitRng(&rng), 0);
  20790. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  20791. inLen = (word32)sizeof(output);
  20792. /* Bad Cases */
  20793. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20794. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20795. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  20796. /* Good Cases */
  20797. /* length only */
  20798. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  20799. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  20800. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20801. wc_ed25519_free(&ed25519Key);
  20802. #endif
  20803. return EXPECT_RESULT();
  20804. } /* End test_wc_Ed25519KeyToDer*/
  20805. /*
  20806. * Testing wc_Ed25519PrivateKeyToDer
  20807. */
  20808. static int test_wc_Ed25519PrivateKeyToDer(void)
  20809. {
  20810. EXPECT_DECLS;
  20811. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20812. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20813. byte output[ONEK_BUF];
  20814. ed25519_key ed25519PrivKey;
  20815. WC_RNG rng;
  20816. word32 inLen;
  20817. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  20818. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20819. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  20820. ExpectIntEQ(wc_InitRng(&rng), 0);
  20821. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  20822. 0);
  20823. inLen = (word32)sizeof(output);
  20824. /* Bad Cases */
  20825. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20826. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20827. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  20828. BAD_FUNC_ARG);
  20829. /* Good Cases */
  20830. /* length only */
  20831. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  20832. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  20833. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20834. wc_ed25519_free(&ed25519PrivKey);
  20835. #endif
  20836. return EXPECT_RESULT();
  20837. } /* End test_wc_Ed25519PrivateKeyToDer*/
  20838. /*
  20839. * Testing wc_Ed448KeyToDer
  20840. */
  20841. static int test_wc_Ed448KeyToDer(void)
  20842. {
  20843. EXPECT_DECLS;
  20844. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20845. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20846. byte output[ONEK_BUF];
  20847. ed448_key ed448Key;
  20848. WC_RNG rng;
  20849. word32 inLen;
  20850. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  20851. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20852. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  20853. ExpectIntEQ(wc_InitRng(&rng), 0);
  20854. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  20855. inLen = (word32)sizeof(output);
  20856. /* Bad Cases */
  20857. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20858. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20859. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  20860. /* Good Cases */
  20861. /* length only */
  20862. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  20863. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  20864. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20865. wc_ed448_free(&ed448Key);
  20866. #endif
  20867. return EXPECT_RESULT();
  20868. } /* End test_wc_Ed448KeyToDer*/
  20869. /*
  20870. * Testing wc_Ed448PrivateKeyToDer
  20871. */
  20872. static int test_wc_Ed448PrivateKeyToDer(void)
  20873. {
  20874. EXPECT_DECLS;
  20875. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20876. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20877. byte output[ONEK_BUF];
  20878. ed448_key ed448PrivKey;
  20879. WC_RNG rng;
  20880. word32 inLen;
  20881. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  20882. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20883. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  20884. ExpectIntEQ(wc_InitRng(&rng), 0);
  20885. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  20886. 0);
  20887. inLen = (word32)sizeof(output);
  20888. /* Bad Cases */
  20889. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  20890. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  20891. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  20892. BAD_FUNC_ARG);
  20893. /* Good cases */
  20894. /* length only */
  20895. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  20896. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  20897. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20898. wc_ed448_free(&ed448PrivKey);
  20899. #endif
  20900. return EXPECT_RESULT();
  20901. } /* End test_wc_Ed448PrivateKeyToDer*/
  20902. /*
  20903. * Testing wc_SetSubjectBuffer
  20904. */
  20905. static int test_wc_SetSubjectBuffer(void)
  20906. {
  20907. EXPECT_DECLS;
  20908. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  20909. Cert cert;
  20910. XFILE file = XBADFILE;
  20911. byte* der = NULL;
  20912. word32 derSz;
  20913. derSz = FOURK_BUF;
  20914. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  20915. DYNAMIC_TYPE_TMP_BUFFER));
  20916. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  20917. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  20918. if (file != XBADFILE)
  20919. XFCLOSE(file);
  20920. ExpectIntEQ(wc_InitCert(&cert), 0);
  20921. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  20922. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  20923. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20924. #endif
  20925. return EXPECT_RESULT();
  20926. } /* End test_wc_SetSubjectBuffer*/
  20927. /*
  20928. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  20929. */
  20930. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  20931. {
  20932. EXPECT_DECLS;
  20933. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  20934. WC_RNG rng;
  20935. Cert cert;
  20936. #if !defined(NO_RSA) && defined(HAVE_RSA)
  20937. RsaKey rsaKey;
  20938. int bits = 2048;
  20939. #endif
  20940. #if defined(HAVE_ECC)
  20941. ecc_key eccKey;
  20942. int ret;
  20943. #endif
  20944. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20945. ed25519_key ed25519Key;
  20946. #endif
  20947. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20948. ed448_key ed448Key;
  20949. #endif
  20950. #ifndef HAVE_FIPS
  20951. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  20952. #else
  20953. ExpectIntEQ(wc_InitRng(&rng), 0);
  20954. #endif
  20955. ExpectIntEQ(wc_InitCert(&cert), 0);
  20956. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  20957. /* RSA */
  20958. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  20959. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  20960. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  20961. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  20962. 0);
  20963. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  20964. #endif
  20965. #if defined(HAVE_ECC)
  20966. /* ECC */
  20967. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  20968. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  20969. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  20970. #if defined(WOLFSSL_ASYNC_CRYPT)
  20971. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  20972. #endif
  20973. ExpectIntEQ(ret, 0);
  20974. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  20975. 0);
  20976. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  20977. #endif
  20978. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20979. /* ED25519 */
  20980. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  20981. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  20982. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  20983. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  20984. &ed25519Key), 0);
  20985. wc_ed25519_free(&ed25519Key);
  20986. #endif
  20987. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20988. /* ED448 */
  20989. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  20990. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  20991. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  20992. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  20993. &ed448Key), 0);
  20994. wc_ed448_free(&ed448Key);
  20995. #endif
  20996. wc_FreeRng(&rng);
  20997. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20998. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  20999. return EXPECT_RESULT();
  21000. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  21001. /*
  21002. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  21003. */
  21004. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  21005. {
  21006. EXPECT_DECLS;
  21007. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  21008. WC_RNG rng;
  21009. Cert cert;
  21010. #if !defined(NO_RSA) && defined(HAVE_RSA)
  21011. RsaKey rsaKey;
  21012. int bits = 2048;
  21013. #endif
  21014. #if defined(HAVE_ECC)
  21015. ecc_key eccKey;
  21016. int ret;
  21017. #endif
  21018. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21019. ed25519_key ed25519Key;
  21020. #endif
  21021. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21022. ed448_key ed448Key;
  21023. #endif
  21024. #ifndef HAVE_FIPS
  21025. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  21026. #else
  21027. ExpectIntEQ(wc_InitRng(&rng), 0);
  21028. #endif
  21029. ExpectIntEQ(wc_InitCert(&cert), 0);
  21030. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  21031. /* RSA */
  21032. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  21033. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  21034. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  21035. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  21036. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  21037. #endif
  21038. #if defined(HAVE_ECC)
  21039. /* ECC */
  21040. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  21041. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  21042. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  21043. #if defined(WOLFSSL_ASYNC_CRYPT)
  21044. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21045. #endif
  21046. ExpectIntEQ(ret, 0);
  21047. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  21048. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  21049. #endif
  21050. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  21051. /* ED25519 */
  21052. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  21053. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  21054. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  21055. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  21056. &ed25519Key), 0);
  21057. wc_ed25519_free(&ed25519Key);
  21058. #endif
  21059. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21060. /* ED448 */
  21061. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  21062. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  21063. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  21064. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  21065. 0);
  21066. wc_ed448_free(&ed448Key);
  21067. #endif
  21068. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21069. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  21070. return EXPECT_RESULT();
  21071. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  21072. /*
  21073. * Testing wc_PKCS7_New()
  21074. */
  21075. static int test_wc_PKCS7_New(void)
  21076. {
  21077. EXPECT_DECLS;
  21078. #if defined(HAVE_PKCS7)
  21079. PKCS7* pkcs7 = NULL;
  21080. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  21081. wc_PKCS7_Free(pkcs7);
  21082. #endif
  21083. return EXPECT_RESULT();
  21084. } /* END test-wc_PKCS7_New */
  21085. /*
  21086. * Testing wc_PKCS7_Init()
  21087. */
  21088. static int test_wc_PKCS7_Init(void)
  21089. {
  21090. EXPECT_DECLS;
  21091. #if defined(HAVE_PKCS7)
  21092. PKCS7* pkcs7 = NULL;
  21093. void* heap = NULL;
  21094. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  21095. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  21096. /* Pass in bad args. */
  21097. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  21098. wc_PKCS7_Free(pkcs7);
  21099. #endif
  21100. return EXPECT_RESULT();
  21101. } /* END test-wc_PKCS7_Init */
  21102. /*
  21103. * Testing wc_PKCS7_InitWithCert()
  21104. */
  21105. static int test_wc_PKCS7_InitWithCert(void)
  21106. {
  21107. EXPECT_DECLS;
  21108. #if defined(HAVE_PKCS7)
  21109. PKCS7* pkcs7 = NULL;
  21110. #ifndef NO_RSA
  21111. #if defined(USE_CERT_BUFFERS_2048)
  21112. unsigned char cert[sizeof(client_cert_der_2048)];
  21113. int certSz = (int)sizeof(cert);
  21114. XMEMSET(cert, 0, certSz);
  21115. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  21116. #elif defined(USE_CERT_BUFFERS_1024)
  21117. unsigned char cert[sizeof(client_cert_der_1024)];
  21118. int certSz = (int)sizeof(cert);
  21119. XMEMSET(cert, 0, certSz);
  21120. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  21121. #else
  21122. unsigned char cert[ONEK_BUF];
  21123. XFILE fp = XBADFILE;
  21124. int certSz;
  21125. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21126. XBADFILE);
  21127. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21128. fp), 0);
  21129. if (fp != XBADFILE)
  21130. XFCLOSE(fp);
  21131. #endif
  21132. #elif defined(HAVE_ECC)
  21133. #if defined(USE_CERT_BUFFERS_256)
  21134. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21135. int certSz = (int)sizeof(cert);
  21136. XMEMSET(cert, 0, certSz);
  21137. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  21138. #else
  21139. unsigned char cert[ONEK_BUF];
  21140. XFILE fp = XBADFILE;
  21141. int certSz;
  21142. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21143. XBADFILE);
  21144. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  21145. fp), 0);
  21146. if (fp != XBADFILE)
  21147. XFCLOSE(fp);
  21148. #endif
  21149. #else
  21150. #error PKCS7 requires ECC or RSA
  21151. #endif
  21152. #ifdef HAVE_ECC
  21153. {
  21154. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  21155. static unsigned char certWithInvalidEccKey[] = {
  21156. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  21157. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  21158. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  21159. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  21160. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21161. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  21162. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21163. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21164. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21165. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21166. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21167. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21168. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21169. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21170. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21171. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  21172. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  21173. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  21174. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  21175. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  21176. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  21177. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  21178. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  21179. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  21180. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  21181. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  21182. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  21183. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  21184. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  21185. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  21186. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  21187. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  21188. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  21189. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  21190. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  21191. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  21192. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  21193. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  21194. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  21195. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  21196. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21197. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  21198. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  21199. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  21200. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  21201. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  21202. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  21203. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  21204. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  21205. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  21206. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  21207. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  21208. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  21209. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  21210. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  21211. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  21212. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  21213. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  21214. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  21215. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  21216. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  21217. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  21218. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  21219. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  21220. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  21221. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  21222. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  21223. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  21224. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  21225. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  21226. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  21227. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  21228. 0x08, 0xA8, 0xB4
  21229. };
  21230. #endif
  21231. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21232. /* If initialization is not successful, it's free'd in init func. */
  21233. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  21234. 0);
  21235. wc_PKCS7_Free(pkcs7);
  21236. pkcs7 = NULL;
  21237. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21238. /* Valid initialization usage. */
  21239. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21240. /* Pass in bad args. No need free for null checks, free at end.*/
  21241. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  21242. BAD_FUNC_ARG);
  21243. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  21244. BAD_FUNC_ARG);
  21245. #ifdef HAVE_ECC
  21246. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  21247. sizeof(certWithInvalidEccKey)), 0);
  21248. }
  21249. #endif
  21250. wc_PKCS7_Free(pkcs7);
  21251. #endif
  21252. return EXPECT_RESULT();
  21253. } /* END test_wc_PKCS7_InitWithCert */
  21254. /*
  21255. * Testing wc_PKCS7_EncodeData()
  21256. */
  21257. static int test_wc_PKCS7_EncodeData(void)
  21258. {
  21259. EXPECT_DECLS;
  21260. #if defined(HAVE_PKCS7)
  21261. PKCS7* pkcs7 = NULL;
  21262. byte output[FOURK_BUF];
  21263. byte data[] = "My encoded DER cert.";
  21264. #ifndef NO_RSA
  21265. #if defined(USE_CERT_BUFFERS_2048)
  21266. unsigned char cert[sizeof(client_cert_der_2048)];
  21267. unsigned char key[sizeof(client_key_der_2048)];
  21268. int certSz = (int)sizeof(cert);
  21269. int keySz = (int)sizeof(key);
  21270. XMEMSET(cert, 0, certSz);
  21271. XMEMSET(key, 0, keySz);
  21272. XMEMCPY(cert, client_cert_der_2048, certSz);
  21273. XMEMCPY(key, client_key_der_2048, keySz);
  21274. #elif defined(USE_CERT_BUFFERS_1024)
  21275. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  21276. unsigned char key[sizeof_client_key_der_1024];
  21277. int certSz = (int)sizeof(cert);
  21278. int keySz = (int)sizeof(key);
  21279. XMEMSET(cert, 0, certSz);
  21280. XMEMSET(key, 0, keySz);
  21281. XMEMCPY(cert, client_cert_der_1024, certSz);
  21282. XMEMCPY(key, client_key_der_1024, keySz);
  21283. #else
  21284. unsigned char cert[ONEK_BUF];
  21285. unsigned char key[ONEK_BUF];
  21286. XFILE fp = XBADFILE;
  21287. int certSz;
  21288. int keySz;
  21289. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21290. XBADFILE);
  21291. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21292. fp), 0);
  21293. if (fp != XBADFILE) {
  21294. XFCLOSE(fp);
  21295. fp = XBADFILE;
  21296. }
  21297. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21298. XBADFILE);
  21299. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21300. 0);
  21301. if (fp != XBADFILE)
  21302. XFCLOSE(fp);
  21303. #endif
  21304. #elif defined(HAVE_ECC)
  21305. #if defined(USE_CERT_BUFFERS_256)
  21306. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21307. unsigned char key[sizeof(ecc_clikey_der_256)];
  21308. int certSz = (int)sizeof(cert);
  21309. int keySz = (int)sizeof(key);
  21310. XMEMSET(cert, 0, certSz);
  21311. XMEMSET(key, 0, keySz);
  21312. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21313. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21314. #else
  21315. unsigned char cert[ONEK_BUF];
  21316. unsigned char key[ONEK_BUF];
  21317. XFILE fp = XBADFILE;
  21318. int certSz, keySz;
  21319. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21320. XBADFILE);
  21321. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  21322. fp), 0);
  21323. if (fp != XBADFILE) {
  21324. XFCLOSE(fp);
  21325. fp = XBADFILE;
  21326. }
  21327. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21328. XBADFILE);
  21329. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  21330. 0);
  21331. if (fp != XBADFILE)
  21332. XFCLOSE(fp);
  21333. #endif
  21334. #endif
  21335. XMEMSET(output, 0, sizeof(output));
  21336. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21337. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21338. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  21339. if (pkcs7 != NULL) {
  21340. pkcs7->content = data;
  21341. pkcs7->contentSz = sizeof(data);
  21342. pkcs7->privateKey = key;
  21343. pkcs7->privateKeySz = keySz;
  21344. }
  21345. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  21346. /* Test bad args. */
  21347. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  21348. BAD_FUNC_ARG);
  21349. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  21350. BAD_FUNC_ARG);
  21351. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  21352. wc_PKCS7_Free(pkcs7);
  21353. #endif
  21354. return EXPECT_RESULT();
  21355. } /* END test_wc_PKCS7_EncodeData */
  21356. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  21357. !defined(NO_RSA) && !defined(NO_SHA256)
  21358. /* RSA sign raw digest callback */
  21359. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  21360. byte* out, word32 outSz, byte* privateKey,
  21361. word32 privateKeySz, int devid, int hashOID)
  21362. {
  21363. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  21364. byte digInfoEncoding[] = {
  21365. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  21366. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  21367. 0x00, 0x04, 0x20
  21368. };
  21369. int ret;
  21370. byte digestInfo[ONEK_BUF];
  21371. byte sig[FOURK_BUF];
  21372. word32 digestInfoSz = 0;
  21373. word32 idx = 0;
  21374. RsaKey rsa;
  21375. /* SHA-256 required only for this example callback due to above
  21376. * digInfoEncoding[] */
  21377. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  21378. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  21379. (hashOID != SHA256h)) {
  21380. return -1;
  21381. }
  21382. /* build DigestInfo */
  21383. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  21384. digestInfoSz += sizeof(digInfoEncoding);
  21385. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  21386. digestInfoSz += digestSz;
  21387. /* set up RSA key */
  21388. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  21389. if (ret != 0) {
  21390. return ret;
  21391. }
  21392. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  21393. /* sign DigestInfo */
  21394. if (ret == 0) {
  21395. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  21396. &rsa, pkcs7->rng);
  21397. if (ret > 0) {
  21398. if (ret > (int)outSz) {
  21399. /* output buffer too small */
  21400. ret = -1;
  21401. }
  21402. else {
  21403. /* success, ret holds sig size */
  21404. XMEMCPY(out, sig, ret);
  21405. }
  21406. }
  21407. }
  21408. wc_FreeRsaKey(&rsa);
  21409. return ret;
  21410. }
  21411. #endif
  21412. /*
  21413. * Testing wc_PKCS7_EncodeSignedData()
  21414. */
  21415. static int test_wc_PKCS7_EncodeSignedData(void)
  21416. {
  21417. EXPECT_DECLS;
  21418. #if defined(HAVE_PKCS7)
  21419. PKCS7* pkcs7 = NULL;
  21420. WC_RNG rng;
  21421. byte output[FOURK_BUF];
  21422. byte badOut[1];
  21423. word32 outputSz = (word32)sizeof(output);
  21424. word32 badOutSz = 0;
  21425. byte data[] = "Test data to encode.";
  21426. #ifndef NO_RSA
  21427. #if defined(USE_CERT_BUFFERS_2048)
  21428. byte key[sizeof(client_key_der_2048)];
  21429. byte cert[sizeof(client_cert_der_2048)];
  21430. word32 keySz = (word32)sizeof(key);
  21431. word32 certSz = (word32)sizeof(cert);
  21432. XMEMSET(key, 0, keySz);
  21433. XMEMSET(cert, 0, certSz);
  21434. XMEMCPY(key, client_key_der_2048, keySz);
  21435. XMEMCPY(cert, client_cert_der_2048, certSz);
  21436. #elif defined(USE_CERT_BUFFERS_1024)
  21437. byte key[sizeof_client_key_der_1024];
  21438. byte cert[sizeof(sizeof_client_cert_der_1024)];
  21439. word32 keySz = (word32)sizeof(key);
  21440. word32 certSz = (word32)sizeof(cert);
  21441. XMEMSET(key, 0, keySz);
  21442. XMEMSET(cert, 0, certSz);
  21443. XMEMCPY(key, client_key_der_1024, keySz);
  21444. XMEMCPY(cert, client_cert_der_1024, certSz);
  21445. #else
  21446. unsigned char cert[ONEK_BUF];
  21447. unsigned char key[ONEK_BUF];
  21448. XFILE fp = XBADFILE;
  21449. int certSz;
  21450. int keySz;
  21451. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21452. XBADFILE);
  21453. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21454. fp), 0);
  21455. if (fp != XBADFILE) {
  21456. XFCLOSE(fp);
  21457. fp = XBADFILE;
  21458. }
  21459. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21460. XBADFILE);
  21461. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21462. 0);
  21463. if (fp != XBADFILE)
  21464. XFCLOSE(fp);
  21465. #endif
  21466. #elif defined(HAVE_ECC)
  21467. #if defined(USE_CERT_BUFFERS_256)
  21468. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21469. unsigned char key[sizeof(ecc_clikey_der_256)];
  21470. int certSz = (int)sizeof(cert);
  21471. int keySz = (int)sizeof(key);
  21472. XMEMSET(cert, 0, certSz);
  21473. XMEMSET(key, 0, keySz);
  21474. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  21475. XMEMCPY(key, ecc_clikey_der_256, keySz);
  21476. #else
  21477. unsigned char cert[ONEK_BUF];
  21478. unsigned char key[ONEK_BUF];
  21479. XFILE fp = XBADFILE;
  21480. int certSz;
  21481. int keySz;
  21482. ExpectTrue((fp = XOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21483. XBADFILE);
  21484. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  21485. if (fp != XBADFILE) {
  21486. XFCLOSE(fp);
  21487. fp = XBADFILE;
  21488. }
  21489. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21490. XBADFILE);
  21491. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  21492. if (fp != XBADFILE)
  21493. XFCLOSE(fp);
  21494. #endif
  21495. #endif
  21496. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21497. XMEMSET(output, 0, outputSz);
  21498. ExpectIntEQ(wc_InitRng(&rng), 0);
  21499. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21500. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21501. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21502. if (pkcs7 != NULL) {
  21503. pkcs7->content = data;
  21504. pkcs7->contentSz = (word32)sizeof(data);
  21505. pkcs7->privateKey = key;
  21506. pkcs7->privateKeySz = (word32)sizeof(key);
  21507. pkcs7->encryptOID = RSAk;
  21508. #ifdef NO_SHA
  21509. pkcs7->hashOID = SHA256h;
  21510. #else
  21511. pkcs7->hashOID = SHAh;
  21512. #endif
  21513. pkcs7->rng = &rng;
  21514. }
  21515. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  21516. wc_PKCS7_Free(pkcs7);
  21517. pkcs7 = NULL;
  21518. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21519. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21520. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  21521. /* Pass in bad args. */
  21522. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  21523. BAD_FUNC_ARG);
  21524. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  21525. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  21526. badOutSz), BAD_FUNC_ARG);
  21527. if (pkcs7 != NULL) {
  21528. pkcs7->hashOID = 0; /* bad hashOID */
  21529. }
  21530. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  21531. BAD_FUNC_ARG);
  21532. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  21533. !defined(NO_RSA) && !defined(NO_SHA256)
  21534. /* test RSA sign raw digest callback, if using RSA and compiled in.
  21535. * Example callback assumes SHA-256, so only run test if compiled in. */
  21536. wc_PKCS7_Free(pkcs7);
  21537. pkcs7 = NULL;
  21538. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21539. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21540. if (pkcs7 != NULL) {
  21541. pkcs7->content = data;
  21542. pkcs7->contentSz = (word32)sizeof(data);
  21543. pkcs7->privateKey = key;
  21544. pkcs7->privateKeySz = (word32)sizeof(key);
  21545. pkcs7->encryptOID = RSAk;
  21546. pkcs7->hashOID = SHA256h;
  21547. pkcs7->rng = &rng;
  21548. }
  21549. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  21550. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  21551. #endif
  21552. wc_PKCS7_Free(pkcs7);
  21553. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21554. #endif
  21555. return EXPECT_RESULT();
  21556. } /* END test_wc_PKCS7_EncodeSignedData */
  21557. /*
  21558. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  21559. */
  21560. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  21561. {
  21562. EXPECT_DECLS;
  21563. #if defined(HAVE_PKCS7)
  21564. int i;
  21565. PKCS7* pkcs7 = NULL;
  21566. WC_RNG rng;
  21567. byte outputHead[FOURK_BUF/2];
  21568. byte outputFoot[FOURK_BUF/2];
  21569. word32 outputHeadSz = (word32)sizeof(outputHead);
  21570. word32 outputFootSz = (word32)sizeof(outputFoot);
  21571. byte data[FOURK_BUF];
  21572. wc_HashAlg hash;
  21573. #ifdef NO_SHA
  21574. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  21575. #else
  21576. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  21577. #endif
  21578. byte hashBuf[WC_MAX_DIGEST_SIZE];
  21579. word32 hashSz = wc_HashGetDigestSize(hashType);
  21580. #ifndef NO_RSA
  21581. #if defined(USE_CERT_BUFFERS_2048)
  21582. byte key[sizeof(client_key_der_2048)];
  21583. byte cert[sizeof(client_cert_der_2048)];
  21584. word32 keySz = (word32)sizeof(key);
  21585. word32 certSz = (word32)sizeof(cert);
  21586. XMEMSET(key, 0, keySz);
  21587. XMEMSET(cert, 0, certSz);
  21588. XMEMCPY(key, client_key_der_2048, keySz);
  21589. XMEMCPY(cert, client_cert_der_2048, certSz);
  21590. #elif defined(USE_CERT_BUFFERS_1024)
  21591. byte key[sizeof_client_key_der_1024];
  21592. byte cert[sizeof(sizeof_client_cert_der_1024)];
  21593. word32 keySz = (word32)sizeof(key);
  21594. word32 certSz = (word32)sizeof(cert);
  21595. XMEMSET(key, 0, keySz);
  21596. XMEMSET(cert, 0, certSz);
  21597. XMEMCPY(key, client_key_der_1024, keySz);
  21598. XMEMCPY(cert, client_cert_der_1024, certSz);
  21599. #else
  21600. unsigned char cert[ONEK_BUF];
  21601. unsigned char key[ONEK_BUF];
  21602. XFILE fp = XBADFILE;
  21603. int certSz;
  21604. int keySz;
  21605. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  21606. XBADFILE);
  21607. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  21608. fp), 0);
  21609. if (fp != XBADFILE) {
  21610. XFCLOSE(fp);
  21611. fp = XBADFILE;
  21612. }
  21613. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  21614. XBADFILE);
  21615. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  21616. 0);
  21617. if (fp != XBADFILE)
  21618. XFCLOSE(fp);
  21619. #endif
  21620. #elif defined(HAVE_ECC)
  21621. #if defined(USE_CERT_BUFFERS_256)
  21622. unsigned char cert[sizeof(cliecc_cert_der_256)];
  21623. unsigned char key[sizeof(ecc_clikey_der_256)];
  21624. int certSz = (int)sizeof(cert);
  21625. int keySz = (int)sizeof(key);
  21626. XMEMSET(cert, 0, certSz);
  21627. XMEMSET(key, 0, keySz);
  21628. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  21629. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  21630. #else
  21631. unsigned char cert[ONEK_BUF];
  21632. unsigned char key[ONEK_BUF];
  21633. XFILE fp = XBADFILE;
  21634. int certSz;
  21635. int keySz;
  21636. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  21637. XBADFILE);
  21638. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  21639. fp), 0);
  21640. if (fp != XBADFILE) {
  21641. XFCLOSE(fp);
  21642. fp = XBADFILE;
  21643. }
  21644. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  21645. XBADFILE);
  21646. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  21647. 0);
  21648. if (fp != XBADFILE)
  21649. XFCLOSE(fp);
  21650. #endif
  21651. #endif
  21652. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21653. /* initialize large data with sequence */
  21654. for (i=0; i<(int)sizeof(data); i++)
  21655. data[i] = i & 0xff;
  21656. XMEMSET(outputHead, 0, outputHeadSz);
  21657. XMEMSET(outputFoot, 0, outputFootSz);
  21658. ExpectIntEQ(wc_InitRng(&rng), 0);
  21659. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21660. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  21661. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  21662. if (pkcs7 != NULL) {
  21663. pkcs7->content = NULL; /* not used for ex */
  21664. pkcs7->contentSz = (word32)sizeof(data);
  21665. pkcs7->privateKey = key;
  21666. pkcs7->privateKeySz = (word32)sizeof(key);
  21667. pkcs7->encryptOID = RSAk;
  21668. #ifdef NO_SHA
  21669. pkcs7->hashOID = SHA256h;
  21670. #else
  21671. pkcs7->hashOID = SHAh;
  21672. #endif
  21673. pkcs7->rng = &rng;
  21674. }
  21675. /* calculate hash for content */
  21676. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  21677. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  21678. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  21679. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  21680. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  21681. /* Perform PKCS7 sign using hash directly */
  21682. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21683. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  21684. ExpectIntGT(outputHeadSz, 0);
  21685. ExpectIntGT(outputFootSz, 0);
  21686. wc_PKCS7_Free(pkcs7);
  21687. pkcs7 = NULL;
  21688. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21689. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21690. /* required parameter even on verify when using _ex, if using outputHead
  21691. * and outputFoot */
  21692. if (pkcs7 != NULL) {
  21693. pkcs7->contentSz = (word32)sizeof(data);
  21694. }
  21695. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21696. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  21697. wc_PKCS7_Free(pkcs7);
  21698. pkcs7 = NULL;
  21699. /* assembly complete PKCS7 sign and use normal verify */
  21700. {
  21701. byte* output = NULL;
  21702. word32 outputSz = 0;
  21703. ExpectNotNull(output = (byte*)XMALLOC(
  21704. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  21705. DYNAMIC_TYPE_TMP_BUFFER));
  21706. if (output != NULL) {
  21707. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  21708. outputSz += outputHeadSz;
  21709. XMEMCPY(&output[outputSz], data, sizeof(data));
  21710. outputSz += sizeof(data);
  21711. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  21712. outputSz += outputFootSz;
  21713. }
  21714. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  21715. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  21716. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  21717. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21718. }
  21719. /* Pass in bad args. */
  21720. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  21721. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21722. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  21723. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21724. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21725. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21726. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  21727. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21728. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21729. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21730. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21731. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  21732. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21733. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  21734. if (pkcs7 != NULL) {
  21735. pkcs7->hashOID = 0; /* bad hashOID */
  21736. }
  21737. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  21738. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  21739. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  21740. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21741. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  21742. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21743. #ifndef NO_PKCS7_STREAM
  21744. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21745. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  21746. #else
  21747. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  21748. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  21749. #endif
  21750. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  21751. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21752. #ifndef NO_PKCS7_STREAM
  21753. /* can pass in 0 buffer length with streaming API */
  21754. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21755. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  21756. #else
  21757. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21758. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  21759. #endif
  21760. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21761. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  21762. #ifndef NO_PKCS7_STREAM
  21763. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21764. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  21765. #else
  21766. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  21767. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  21768. #endif
  21769. wc_PKCS7_Free(pkcs7);
  21770. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21771. #endif
  21772. return EXPECT_RESULT();
  21773. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  21774. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  21775. /**
  21776. * Loads certs/keys from files or buffers into the argument buffers,
  21777. * helper function called by CreatePKCS7SignedData().
  21778. *
  21779. * Returns 0 on success, negative on error.
  21780. */
  21781. static int LoadPKCS7SignedDataCerts(
  21782. int useIntermediateCertChain, int pkAlgoType,
  21783. byte* intCARoot, word32* intCARootSz,
  21784. byte* intCA1, word32* intCA1Sz,
  21785. byte* intCA2, word32* intCA2Sz,
  21786. byte* cert, word32* certSz,
  21787. byte* key, word32* keySz)
  21788. {
  21789. EXPECT_DECLS;
  21790. int ret = 0;
  21791. XFILE fp = XBADFILE;
  21792. #ifndef NO_RSA
  21793. const char* intCARootRSA = "./certs/ca-cert.der";
  21794. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  21795. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  21796. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  21797. const char* intServKeyRSA = "./certs/server-key.der";
  21798. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  21799. const char* cli1024Cert = "./certs/1024/client-cert.der";
  21800. const char* cli1024Key = "./certs/1024/client-key.der";
  21801. #endif
  21802. #endif
  21803. #ifdef HAVE_ECC
  21804. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  21805. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  21806. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  21807. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  21808. const char* intServKeyECC = "./certs/ecc-key.der";
  21809. #ifndef USE_CERT_BUFFERS_256
  21810. const char* cliEccCert = "./certs/client-ecc-cert.der";
  21811. const char* cliEccKey = "./certs/client-ecc-key.der";
  21812. #endif
  21813. #endif
  21814. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  21815. ((useIntermediateCertChain == 1) &&
  21816. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  21817. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  21818. return BAD_FUNC_ARG;
  21819. }
  21820. /* Read/load certs and keys to use for signing based on PK type and chain */
  21821. switch (pkAlgoType) {
  21822. #ifndef NO_RSA
  21823. case RSA_TYPE:
  21824. if (useIntermediateCertChain == 1) {
  21825. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  21826. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  21827. if (fp != XBADFILE) {
  21828. XFCLOSE(fp);
  21829. fp = XBADFILE;
  21830. }
  21831. ExpectIntGT(*intCARootSz, 0);
  21832. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  21833. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  21834. if (fp != XBADFILE) {
  21835. XFCLOSE(fp);
  21836. fp = XBADFILE;
  21837. }
  21838. ExpectIntGT(*intCA1Sz, 0);
  21839. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  21840. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  21841. if (fp != XBADFILE) {
  21842. XFCLOSE(fp);
  21843. fp = XBADFILE;
  21844. }
  21845. ExpectIntGT(*intCA2Sz, 0);
  21846. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  21847. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21848. if (fp != XBADFILE) {
  21849. XFCLOSE(fp);
  21850. fp = XBADFILE;
  21851. }
  21852. ExpectIntGT(*certSz, 0);
  21853. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  21854. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21855. if (fp != XBADFILE) {
  21856. XFCLOSE(fp);
  21857. fp = XBADFILE;
  21858. }
  21859. ExpectIntGT(*keySz, 0);
  21860. }
  21861. else {
  21862. #if defined(USE_CERT_BUFFERS_2048)
  21863. *keySz = sizeof_client_key_der_2048;
  21864. *certSz = sizeof_client_cert_der_2048;
  21865. XMEMCPY(key, client_key_der_2048, *keySz);
  21866. XMEMCPY(cert, client_cert_der_2048, *certSz);
  21867. #elif defined(USE_CERT_BUFFERS_1024)
  21868. *keySz = sizeof_client_key_der_1024;
  21869. *certSz = sizeof_client_cert_der_1024;
  21870. XMEMCPY(key, client_key_der_1024, *keySz);
  21871. XMEMCPY(cert, client_cert_der_1024, *certSz);
  21872. #else
  21873. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  21874. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21875. if (fp != XBADFILE) {
  21876. XFCLOSE(fp);
  21877. fp = XBADFILE;
  21878. }
  21879. ExpectIntGT(*keySz, 0);
  21880. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  21881. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21882. if (fp != XBADFILE) {
  21883. XFCLOSE(fp);
  21884. fp = XBADFILE;
  21885. }
  21886. ExpectIntGT(*certSz, 0);
  21887. #endif /* USE_CERT_BUFFERS_2048 */
  21888. }
  21889. break;
  21890. #endif /* !NO_RSA */
  21891. #ifdef HAVE_ECC
  21892. case ECC_TYPE:
  21893. if (useIntermediateCertChain == 1) {
  21894. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  21895. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  21896. if (fp != XBADFILE) {
  21897. XFCLOSE(fp);
  21898. fp = XBADFILE;
  21899. }
  21900. ExpectIntGT(*intCARootSz, 0);
  21901. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  21902. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  21903. if (fp != XBADFILE) {
  21904. XFCLOSE(fp);
  21905. fp = XBADFILE;
  21906. }
  21907. ExpectIntGT(*intCA1Sz, 0);
  21908. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  21909. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  21910. if (fp != XBADFILE) {
  21911. XFCLOSE(fp);
  21912. fp = XBADFILE;
  21913. }
  21914. ExpectIntGT(*intCA2Sz, 0);
  21915. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  21916. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21917. if (fp != XBADFILE) {
  21918. XFCLOSE(fp);
  21919. fp = XBADFILE;
  21920. }
  21921. ExpectIntGT(*certSz, 0);
  21922. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  21923. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21924. if (fp != XBADFILE) {
  21925. XFCLOSE(fp);
  21926. fp = XBADFILE;
  21927. }
  21928. ExpectIntGT(*keySz, 0);
  21929. }
  21930. else {
  21931. #if defined(USE_CERT_BUFFERS_256)
  21932. *keySz = sizeof_ecc_clikey_der_256;
  21933. *certSz = sizeof_cliecc_cert_der_256;
  21934. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  21935. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  21936. #else
  21937. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  21938. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  21939. if (fp != XBADFILE) {
  21940. XFCLOSE(fp);
  21941. fp = XBADFILE;
  21942. }
  21943. ExpectIntGT(*keySz, 0);
  21944. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  21945. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  21946. if (fp != XBADFILE) {
  21947. XFCLOSE(fp);
  21948. fp = XBADFILE;
  21949. }
  21950. ExpectIntGT(*certSz, 0);
  21951. #endif /* USE_CERT_BUFFERS_256 */
  21952. }
  21953. break;
  21954. #endif /* HAVE_ECC */
  21955. default:
  21956. WOLFSSL_MSG("Unsupported SignedData PK type");
  21957. ret = BAD_FUNC_ARG;
  21958. break;
  21959. }
  21960. if (EXPECT_FAIL() && (ret == 0)) {
  21961. ret = BAD_FUNC_ARG;
  21962. }
  21963. return ret;
  21964. }
  21965. /**
  21966. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  21967. *
  21968. * output output buffer to place SignedData
  21969. * outputSz size of output buffer
  21970. * data data buffer to be signed
  21971. * dataSz size of data buffer
  21972. * withAttribs [1/0] include attributes in SignedData message
  21973. * detachedSig [1/0] create detached signature, no content
  21974. * useIntCertChain [1/0] use certificate chain and include intermediate and
  21975. * root CAs in bundle
  21976. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  21977. *
  21978. * Return size of bundle created on success, negative on error */
  21979. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  21980. byte* data, word32 dataSz,
  21981. int withAttribs, int detachedSig,
  21982. int useIntermediateCertChain,
  21983. int pkAlgoType)
  21984. {
  21985. EXPECT_DECLS;
  21986. int ret = 0;
  21987. WC_RNG rng;
  21988. PKCS7* pkcs7 = NULL;
  21989. static byte messageTypeOid[] =
  21990. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  21991. 0x09, 0x02 };
  21992. static byte messageType[] = { 0x13, 2, '1', '9' };
  21993. PKCS7Attrib attribs[] =
  21994. {
  21995. { messageTypeOid, sizeof(messageTypeOid), messageType,
  21996. sizeof(messageType) }
  21997. };
  21998. byte intCARoot[TWOK_BUF];
  21999. byte intCA1[TWOK_BUF];
  22000. byte intCA2[TWOK_BUF];
  22001. byte cert[TWOK_BUF];
  22002. byte key[TWOK_BUF];
  22003. word32 intCARootSz = sizeof(intCARoot);
  22004. word32 intCA1Sz = sizeof(intCA1);
  22005. word32 intCA2Sz = sizeof(intCA2);
  22006. word32 certSz = sizeof(cert);
  22007. word32 keySz = sizeof(key);
  22008. XMEMSET(intCARoot, 0, intCARootSz);
  22009. XMEMSET(intCA1, 0, intCA1Sz);
  22010. XMEMSET(intCA2, 0, intCA2Sz);
  22011. XMEMSET(cert, 0, certSz);
  22012. XMEMSET(key, 0, keySz);
  22013. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  22014. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  22015. cert, &certSz, key, &keySz);
  22016. ExpectIntEQ(ret, 0);
  22017. XMEMSET(output, 0, outputSz);
  22018. ExpectIntEQ(wc_InitRng(&rng), 0);
  22019. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22020. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22021. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  22022. if (useIntermediateCertChain == 1) {
  22023. /* Add intermediate and root CA certs into SignedData Certs SET */
  22024. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  22025. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  22026. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  22027. }
  22028. if (pkcs7 != NULL) {
  22029. pkcs7->content = data;
  22030. pkcs7->contentSz = dataSz;
  22031. pkcs7->privateKey = key;
  22032. pkcs7->privateKeySz = (word32)sizeof(key);
  22033. if (pkAlgoType == RSA_TYPE) {
  22034. pkcs7->encryptOID = RSAk;
  22035. }
  22036. else {
  22037. pkcs7->encryptOID = ECDSAk;
  22038. }
  22039. #ifdef NO_SHA
  22040. pkcs7->hashOID = SHA256h;
  22041. #else
  22042. pkcs7->hashOID = SHAh;
  22043. #endif
  22044. pkcs7->rng = &rng;
  22045. if (withAttribs) {
  22046. /* include a signed attribute */
  22047. pkcs7->signedAttribs = attribs;
  22048. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  22049. }
  22050. }
  22051. if (detachedSig) {
  22052. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  22053. }
  22054. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  22055. ExpectIntGT(outputSz, 0);
  22056. wc_PKCS7_Free(pkcs7);
  22057. pkcs7 = NULL;
  22058. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22059. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22060. if (detachedSig && (pkcs7 != NULL)) {
  22061. pkcs7->content = data;
  22062. pkcs7->contentSz = dataSz;
  22063. }
  22064. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22065. wc_PKCS7_Free(pkcs7);
  22066. wc_FreeRng(&rng);
  22067. if (EXPECT_FAIL()) {
  22068. outputSz = 0;
  22069. }
  22070. return outputSz;
  22071. }
  22072. #endif
  22073. /*
  22074. * Testing wc_PKCS_VerifySignedData()
  22075. */
  22076. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  22077. {
  22078. EXPECT_DECLS;
  22079. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  22080. PKCS7* pkcs7 = NULL;
  22081. byte output[6000]; /* Large size needed for bundles with int CA certs */
  22082. word32 outputSz = sizeof(output);
  22083. byte data[] = "Test data to encode.";
  22084. byte badOut[1];
  22085. word32 badOutSz = 0;
  22086. byte badContent[] = "This is different content than was signed";
  22087. wc_HashAlg hash;
  22088. #ifdef NO_SHA
  22089. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  22090. #else
  22091. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  22092. #endif
  22093. byte hashBuf[WC_MAX_DIGEST_SIZE];
  22094. word32 hashSz = wc_HashGetDigestSize(hashType);
  22095. #ifndef NO_RSA
  22096. PKCS7DecodedAttrib* decodedAttrib = NULL;
  22097. /* contentType OID (1.2.840.113549.1.9.3) */
  22098. static const byte contentTypeOid[] =
  22099. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  22100. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  22101. static const byte dataType[] =
  22102. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  22103. /* messageDigest OID (1.2.840.113549.1.9.4) */
  22104. static const byte messageDigestOid[] =
  22105. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  22106. #ifndef NO_ASN_TIME
  22107. /* signingTime OID () */
  22108. static const byte signingTimeOid[] =
  22109. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  22110. #endif
  22111. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  22112. int dateLength = 0;
  22113. byte dateFormat;
  22114. const byte* datePart = NULL;
  22115. struct tm timearg;
  22116. time_t now;
  22117. struct tm* nowTm = NULL;
  22118. #ifdef NEED_TMP_TIME
  22119. struct tm tmpTimeStorage;
  22120. struct tm* tmpTime = &tmpTimeStorage;
  22121. #endif
  22122. #endif /* !NO_ASN && !NO_ASN_TIME */
  22123. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  22124. /* Success test with RSA certs/key */
  22125. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22126. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  22127. /* calculate hash for content, used later */
  22128. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  22129. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  22130. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  22131. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  22132. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22133. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22134. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22135. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22136. /* Check that decoded signed attributes are correct */
  22137. /* messageDigest should be first */
  22138. if (pkcs7 != NULL) {
  22139. decodedAttrib = pkcs7->decodedAttrib;
  22140. }
  22141. ExpectNotNull(decodedAttrib);
  22142. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  22143. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  22144. decodedAttrib->oidSz), 0);
  22145. /* + 2 for OCTET STRING and length bytes */
  22146. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  22147. ExpectNotNull(decodedAttrib->value);
  22148. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  22149. #ifndef NO_ASN_TIME
  22150. /* signingTime should be second */
  22151. if (decodedAttrib != NULL) {
  22152. decodedAttrib = decodedAttrib->next;
  22153. }
  22154. ExpectNotNull(decodedAttrib);
  22155. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  22156. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  22157. decodedAttrib->oidSz), 0);
  22158. ExpectIntGT(decodedAttrib->valueSz, 0);
  22159. ExpectNotNull(decodedAttrib->value);
  22160. #endif
  22161. /* Verify signingTime if ASN and time are available */
  22162. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  22163. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  22164. &datePart, &dateFormat, &dateLength), 0);
  22165. ExpectNotNull(datePart);
  22166. ExpectIntGT(dateLength, 0);
  22167. XMEMSET(&timearg, 0, sizeof(timearg));
  22168. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  22169. &timearg), 0);
  22170. /* Get current time and compare year/month/day against attribute value */
  22171. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  22172. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  22173. ExpectNotNull(nowTm);
  22174. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  22175. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  22176. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  22177. #endif /* !NO_ASN && !NO_ASN_TIME */
  22178. /* contentType should be third */
  22179. if (decodedAttrib != NULL) {
  22180. decodedAttrib = decodedAttrib->next;
  22181. }
  22182. ExpectNotNull(decodedAttrib);
  22183. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  22184. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  22185. decodedAttrib->oidSz), 0);
  22186. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  22187. ExpectNotNull(decodedAttrib->value);
  22188. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  22189. 0);
  22190. #endif /* !NO_RSA */
  22191. /* Test bad args. */
  22192. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  22193. BAD_FUNC_ARG);
  22194. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  22195. BAD_FUNC_ARG);
  22196. #ifndef NO_PKCS7_STREAM
  22197. /* can pass in 0 buffer length with streaming API */
  22198. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22199. badOutSz), WC_PKCS7_WANT_READ_E);
  22200. #else
  22201. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  22202. badOutSz), BAD_FUNC_ARG);
  22203. #endif
  22204. wc_PKCS7_Free(pkcs7);
  22205. pkcs7 = NULL;
  22206. #ifndef NO_RSA
  22207. /* Try RSA certs/key/sig first */
  22208. outputSz = sizeof(output);
  22209. XMEMSET(output, 0, outputSz);
  22210. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22211. (word32)sizeof(data),
  22212. 1, 1, 0, RSA_TYPE)), 0);
  22213. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22214. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22215. if (pkcs7 != NULL) {
  22216. pkcs7->content = badContent;
  22217. pkcs7->contentSz = sizeof(badContent);
  22218. }
  22219. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  22220. SIG_VERIFY_E);
  22221. wc_PKCS7_Free(pkcs7);
  22222. pkcs7 = NULL;
  22223. /* Test success case with detached signature and valid content */
  22224. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22225. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22226. if (pkcs7 != NULL) {
  22227. pkcs7->content = data;
  22228. pkcs7->contentSz = sizeof(data);
  22229. }
  22230. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22231. wc_PKCS7_Free(pkcs7);
  22232. pkcs7 = NULL;
  22233. /* verify using pre-computed content digest only (no content) */
  22234. {
  22235. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22236. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  22237. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22238. output, outputSz, NULL, 0), 0);
  22239. wc_PKCS7_Free(pkcs7);
  22240. pkcs7 = NULL;
  22241. }
  22242. #endif /* !NO_RSA */
  22243. /* Test verify on signedData containing intermediate/root CA certs */
  22244. #ifndef NO_RSA
  22245. outputSz = sizeof(output);
  22246. XMEMSET(output, 0, outputSz);
  22247. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22248. (word32)sizeof(data),
  22249. 0, 0, 1, RSA_TYPE)), 0);
  22250. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22251. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22252. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22253. wc_PKCS7_Free(pkcs7);
  22254. pkcs7 = NULL;
  22255. #endif /* !NO_RSA */
  22256. #endif
  22257. return EXPECT_RESULT();
  22258. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  22259. /*
  22260. * Testing wc_PKCS_VerifySignedData()
  22261. */
  22262. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  22263. {
  22264. EXPECT_DECLS;
  22265. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  22266. PKCS7* pkcs7 = NULL;
  22267. byte output[6000]; /* Large size needed for bundles with int CA certs */
  22268. word32 outputSz = sizeof(output);
  22269. byte data[] = "Test data to encode.";
  22270. byte badContent[] = "This is different content than was signed";
  22271. wc_HashAlg hash;
  22272. #ifdef NO_SHA
  22273. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  22274. #else
  22275. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  22276. #endif
  22277. byte hashBuf[WC_MAX_DIGEST_SIZE];
  22278. word32 hashSz = wc_HashGetDigestSize(hashType);
  22279. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  22280. /* Success test with ECC certs/key */
  22281. outputSz = sizeof(output);
  22282. XMEMSET(output, 0, outputSz);
  22283. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22284. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  22285. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22286. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  22287. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22288. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22289. wc_PKCS7_Free(pkcs7);
  22290. pkcs7 = NULL;
  22291. /* Invalid content should error, use detached signature so we can
  22292. * easily change content */
  22293. outputSz = sizeof(output);
  22294. XMEMSET(output, 0, outputSz);
  22295. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22296. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  22297. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22298. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22299. if (pkcs7 != NULL) {
  22300. pkcs7->content = badContent;
  22301. pkcs7->contentSz = sizeof(badContent);
  22302. }
  22303. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  22304. SIG_VERIFY_E);
  22305. wc_PKCS7_Free(pkcs7);
  22306. pkcs7 = NULL;
  22307. /* Test success case with detached signature and valid content */
  22308. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22309. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  22310. if (pkcs7 != NULL) {
  22311. pkcs7->content = data;
  22312. pkcs7->contentSz = sizeof(data);
  22313. }
  22314. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  22315. wc_PKCS7_Free(pkcs7);
  22316. pkcs7 = NULL;
  22317. /* verify using pre-computed content digest only (no content) */
  22318. {
  22319. /* calculate hash for content */
  22320. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  22321. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  22322. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  22323. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  22324. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22325. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  22326. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  22327. output, outputSz, NULL, 0), 0);
  22328. wc_PKCS7_Free(pkcs7);
  22329. pkcs7 = NULL;
  22330. }
  22331. /* Test verify on signedData containing intermediate/root CA certs */
  22332. outputSz = sizeof(output);
  22333. XMEMSET(output, 0, outputSz);
  22334. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  22335. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  22336. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  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. #endif
  22342. return EXPECT_RESULT();
  22343. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  22344. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  22345. !defined(NO_AES_256)
  22346. static const byte defKey[] = {
  22347. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22348. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22349. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22350. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22351. };
  22352. static byte aesHandle[32]; /* simulated hardware key handle */
  22353. /* return 0 on success */
  22354. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  22355. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  22356. byte* in, int inSz, byte* out, void* usrCtx)
  22357. {
  22358. int ret;
  22359. Aes aes;
  22360. if (usrCtx == NULL) {
  22361. /* no simulated handle passed in */
  22362. return -1;
  22363. }
  22364. switch (encryptOID) {
  22365. case AES256CBCb:
  22366. if (ivSz != AES_BLOCK_SIZE)
  22367. return BAD_FUNC_ARG;
  22368. break;
  22369. default:
  22370. WOLFSSL_MSG("Unsupported content cipher type for test");
  22371. return ALGO_ID_E;
  22372. };
  22373. /* simulate using handle to get key */
  22374. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  22375. if (ret == 0) {
  22376. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  22377. if (ret == 0)
  22378. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  22379. wc_AesFree(&aes);
  22380. }
  22381. (void)aad;
  22382. (void)aadSz;
  22383. (void)authTag;
  22384. (void)authTagSz;
  22385. (void)pkcs7;
  22386. return ret;
  22387. }
  22388. /* returns key size on success */
  22389. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  22390. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  22391. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  22392. {
  22393. int ret = -1;
  22394. if (out == NULL)
  22395. return BAD_FUNC_ARG;
  22396. if (keyId[0] != 0x00) {
  22397. return -1;
  22398. }
  22399. if (type != (int)PKCS7_KEKRI) {
  22400. return -1;
  22401. }
  22402. switch (keyWrapAlgo) {
  22403. case AES256_WRAP:
  22404. /* simulate setting a handle for later decryption but use key
  22405. * as handle in the test case here */
  22406. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  22407. aesHandle, sizeof(aesHandle), NULL);
  22408. if (ret < 0)
  22409. return ret;
  22410. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  22411. if (ret < 0)
  22412. return ret;
  22413. /* return key size on success */
  22414. return sizeof(defKey);
  22415. default:
  22416. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  22417. return BAD_KEYWRAP_ALG_E;
  22418. };
  22419. (void)cekSz;
  22420. (void)cek;
  22421. (void)outSz;
  22422. (void)keyIdSz;
  22423. (void)direction;
  22424. (void)orginKey; /* used with KAKRI */
  22425. (void)orginKeySz;
  22426. return ret;
  22427. }
  22428. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  22429. /*
  22430. * Testing wc_PKCS7_EncodeEnvelopedData()
  22431. */
  22432. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  22433. {
  22434. EXPECT_DECLS;
  22435. #if defined(HAVE_PKCS7)
  22436. PKCS7* pkcs7 = NULL;
  22437. #ifdef ECC_TIMING_RESISTANT
  22438. WC_RNG rng;
  22439. #endif
  22440. word32 tempWrd32 = 0;
  22441. byte* tmpBytePtr = NULL;
  22442. const char input[] = "Test data to encode.";
  22443. int i;
  22444. int testSz = 0;
  22445. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  22446. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22447. byte* rsaCert = NULL;
  22448. byte* rsaPrivKey = NULL;
  22449. word32 rsaCertSz;
  22450. word32 rsaPrivKeySz;
  22451. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  22452. !defined(USE_CERT_BUFFERS_2048) )
  22453. static const char* rsaClientCert = "./certs/client-cert.der";
  22454. static const char* rsaClientKey = "./certs/client-key.der";
  22455. rsaCertSz = (word32)sizeof(rsaClientCert);
  22456. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  22457. #endif
  22458. #endif
  22459. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22460. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22461. byte* eccCert = NULL;
  22462. byte* eccPrivKey = NULL;
  22463. word32 eccCertSz;
  22464. word32 eccPrivKeySz;
  22465. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  22466. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  22467. static const char* eccClientKey = "./certs/ecc-client-key.der";
  22468. #endif
  22469. #endif
  22470. /* Generic buffer size. */
  22471. byte output[ONEK_BUF];
  22472. byte decoded[sizeof(input)/sizeof(char)];
  22473. int decodedSz = 0;
  22474. #ifndef NO_FILESYSTEM
  22475. XFILE certFile = XBADFILE;
  22476. XFILE keyFile = XBADFILE;
  22477. #endif
  22478. #ifdef ECC_TIMING_RESISTANT
  22479. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22480. #endif
  22481. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22482. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22483. /* RSA certs and keys. */
  22484. #if defined(USE_CERT_BUFFERS_1024)
  22485. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  22486. /* Allocate buffer space. */
  22487. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  22488. DYNAMIC_TYPE_TMP_BUFFER));
  22489. /* Init buffer. */
  22490. if (rsaCert != NULL) {
  22491. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  22492. }
  22493. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  22494. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  22495. DYNAMIC_TYPE_TMP_BUFFER));
  22496. if (rsaPrivKey != NULL) {
  22497. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  22498. }
  22499. #elif defined(USE_CERT_BUFFERS_2048)
  22500. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  22501. /* Allocate buffer */
  22502. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  22503. DYNAMIC_TYPE_TMP_BUFFER));
  22504. /* Init buffer. */
  22505. if (rsaCert != NULL) {
  22506. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  22507. }
  22508. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  22509. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  22510. DYNAMIC_TYPE_TMP_BUFFER));
  22511. if (rsaPrivKey != NULL) {
  22512. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  22513. }
  22514. #else
  22515. /* File system. */
  22516. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  22517. rsaCertSz = (word32)FOURK_BUF;
  22518. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22519. DYNAMIC_TYPE_TMP_BUFFER));
  22520. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  22521. certFile)) > 0);
  22522. if (certFile != XBADFILE)
  22523. XFCLOSE(certFile);
  22524. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  22525. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22526. DYNAMIC_TYPE_TMP_BUFFER));
  22527. rsaPrivKeySz = (word32)FOURK_BUF;
  22528. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  22529. keyFile)) > 0);
  22530. if (keyFile != XBADFILE)
  22531. XFCLOSE(keyFile);
  22532. #endif /* USE_CERT_BUFFERS */
  22533. #endif /* NO_RSA */
  22534. /* ECC */
  22535. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  22536. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  22537. #ifdef USE_CERT_BUFFERS_256
  22538. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22539. DYNAMIC_TYPE_TMP_BUFFER));
  22540. /* Init buffer. */
  22541. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  22542. if (eccCert != NULL) {
  22543. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  22544. }
  22545. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  22546. DYNAMIC_TYPE_TMP_BUFFER));
  22547. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  22548. if (eccPrivKey != NULL) {
  22549. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  22550. }
  22551. #else /* File system. */
  22552. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  22553. eccCertSz = (word32)FOURK_BUF;
  22554. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22555. DYNAMIC_TYPE_TMP_BUFFER));
  22556. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  22557. certFile)) > 0);
  22558. if (certFile != XBADFILE) {
  22559. XFCLOSE(certFile);
  22560. }
  22561. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  22562. eccPrivKeySz = (word32)FOURK_BUF;
  22563. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22564. DYNAMIC_TYPE_TMP_BUFFER));
  22565. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  22566. keyFile)) > 0);
  22567. if (keyFile != XBADFILE) {
  22568. XFCLOSE(keyFile);
  22569. }
  22570. #endif /* USE_CERT_BUFFERS_256 */
  22571. #endif /* END HAVE_ECC */
  22572. #ifndef NO_FILESYSTEM
  22573. /* Silence. */
  22574. (void)keyFile;
  22575. (void)certFile;
  22576. #endif
  22577. {
  22578. const pkcs7EnvelopedVector testVectors[] = {
  22579. /* DATA is a global variable defined in the makefile. */
  22580. #if !defined(NO_RSA)
  22581. #ifndef NO_DES3
  22582. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  22583. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22584. #endif /* NO_DES3 */
  22585. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22586. #ifndef NO_AES_128
  22587. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  22588. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22589. #endif
  22590. #ifndef NO_AES_192
  22591. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  22592. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22593. #endif
  22594. #ifndef NO_AES_256
  22595. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  22596. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  22597. #endif
  22598. #endif /* NO_AES && HAVE_AES_CBC */
  22599. #endif /* NO_RSA */
  22600. #if defined(HAVE_ECC)
  22601. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22602. #if !defined(NO_SHA) && !defined(NO_AES_128)
  22603. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22604. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  22605. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22606. #endif
  22607. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  22608. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22609. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  22610. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22611. #endif
  22612. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  22613. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  22614. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  22615. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  22616. #endif
  22617. #endif /* NO_AES && HAVE_AES_CBC*/
  22618. #endif /* END HAVE_ECC */
  22619. }; /* END pkcs7EnvelopedVector */
  22620. #ifdef ECC_TIMING_RESISTANT
  22621. ExpectIntEQ(wc_InitRng(&rng), 0);
  22622. #endif
  22623. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22624. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22625. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  22626. for (i = 0; i < testSz; i++) {
  22627. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  22628. (word32)(testVectors + i)->certSz), 0);
  22629. if (pkcs7 != NULL) {
  22630. #ifdef ECC_TIMING_RESISTANT
  22631. pkcs7->rng = &rng;
  22632. #endif
  22633. pkcs7->content = (byte*)(testVectors + i)->content;
  22634. pkcs7->contentSz = (testVectors + i)->contentSz;
  22635. pkcs7->contentOID = (testVectors + i)->contentOID;
  22636. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  22637. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  22638. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  22639. pkcs7->privateKey = (testVectors + i)->privateKey;
  22640. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  22641. }
  22642. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22643. (word32)sizeof(output)), 0);
  22644. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22645. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  22646. ExpectIntGE(decodedSz, 0);
  22647. /* Verify the size of each buffer. */
  22648. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  22649. /* Don't free the last time through the loop. */
  22650. if (i < testSz - 1) {
  22651. wc_PKCS7_Free(pkcs7);
  22652. pkcs7 = NULL;
  22653. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22654. }
  22655. } /* END test loop. */
  22656. }
  22657. /* Test bad args. */
  22658. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  22659. (word32)sizeof(output)), BAD_FUNC_ARG);
  22660. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  22661. (word32)sizeof(output)), BAD_FUNC_ARG);
  22662. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  22663. /* Decode. */
  22664. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  22665. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22666. BAD_FUNC_ARG);
  22667. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22668. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22669. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22670. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  22671. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  22672. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22673. BAD_FUNC_ARG);
  22674. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  22675. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  22676. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  22677. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  22678. /* only a failure for KARI test cases */
  22679. if (pkcs7 != NULL) {
  22680. tempWrd32 = pkcs7->singleCertSz;
  22681. pkcs7->singleCertSz = 0;
  22682. }
  22683. #if defined(WOLFSSL_ASN_TEMPLATE)
  22684. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22685. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22686. BUFFER_E);
  22687. #else
  22688. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22689. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22690. ASN_PARSE_E);
  22691. #endif
  22692. if (pkcs7 != NULL) {
  22693. pkcs7->singleCertSz = tempWrd32;
  22694. tmpBytePtr = pkcs7->singleCert;
  22695. pkcs7->singleCert = NULL;
  22696. }
  22697. #if defined(NO_PKCS7_STREAM)
  22698. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  22699. * is returned from kari parse which gets set to bad func arg */
  22700. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22701. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22702. BAD_FUNC_ARG);
  22703. #else
  22704. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22705. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22706. ASN_PARSE_E);
  22707. #endif
  22708. if (pkcs7 != NULL) {
  22709. pkcs7->singleCert = tmpBytePtr;
  22710. }
  22711. #endif
  22712. if (pkcs7 != NULL) {
  22713. tempWrd32 = pkcs7->privateKeySz;
  22714. pkcs7->privateKeySz = 0;
  22715. }
  22716. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22717. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22718. BAD_FUNC_ARG);
  22719. if (pkcs7 != NULL) {
  22720. pkcs7->privateKeySz = tempWrd32;
  22721. tmpBytePtr = pkcs7->privateKey;
  22722. pkcs7->privateKey = NULL;
  22723. }
  22724. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22725. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  22726. BAD_FUNC_ARG);
  22727. if (pkcs7 != NULL) {
  22728. pkcs7->privateKey = tmpBytePtr;
  22729. }
  22730. wc_PKCS7_Free(pkcs7);
  22731. pkcs7 = NULL;
  22732. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  22733. /* test of decrypt callback with KEKRI enveloped data */
  22734. {
  22735. int envelopedSz;
  22736. const byte keyId[] = { 0x00 };
  22737. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22738. if (pkcs7 != NULL) {
  22739. pkcs7->content = (byte*)input;
  22740. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  22741. pkcs7->contentOID = DATA;
  22742. pkcs7->encryptOID = AES256CBCb;
  22743. }
  22744. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  22745. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  22746. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  22747. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  22748. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  22749. (word32)sizeof(output))), 0);
  22750. wc_PKCS7_Free(pkcs7);
  22751. pkcs7 = NULL;
  22752. /* decode envelopedData */
  22753. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22754. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  22755. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  22756. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  22757. envelopedSz, decoded, sizeof(decoded))), 0);
  22758. wc_PKCS7_Free(pkcs7);
  22759. pkcs7 = NULL;
  22760. }
  22761. #endif /* !NO_AES && !NO_AES_256 */
  22762. #ifndef NO_RSA
  22763. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22764. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22765. #endif /* NO_RSA */
  22766. #ifdef HAVE_ECC
  22767. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22768. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22769. #endif /* HAVE_ECC */
  22770. #ifdef ECC_TIMING_RESISTANT
  22771. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22772. #endif
  22773. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  22774. !defined(NO_RSA) && !defined(NO_SHA)
  22775. {
  22776. byte out[7];
  22777. byte *cms = NULL;
  22778. word32 cmsSz;
  22779. XFILE cmsFile = XBADFILE;
  22780. XMEMSET(out, 0, sizeof(out));
  22781. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22782. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  22783. != XBADFILE);
  22784. cmsSz = (word32)FOURK_BUF;
  22785. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  22786. DYNAMIC_TYPE_TMP_BUFFER));
  22787. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  22788. if (cmsFile != XBADFILE)
  22789. XFCLOSE(cmsFile);
  22790. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  22791. sizeof_client_cert_der_2048), 0);
  22792. if (pkcs7 != NULL) {
  22793. pkcs7->privateKey = (byte*)client_key_der_2048;
  22794. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  22795. }
  22796. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  22797. 2), 0);
  22798. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  22799. sizeof(out)), 0);
  22800. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22801. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  22802. wc_PKCS7_Free(pkcs7);
  22803. pkcs7 = NULL;
  22804. }
  22805. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  22806. #endif /* HAVE_PKCS7 */
  22807. return EXPECT_RESULT();
  22808. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  22809. /*
  22810. * Testing wc_PKCS7_EncodeEncryptedData()
  22811. */
  22812. static int test_wc_PKCS7_EncodeEncryptedData(void)
  22813. {
  22814. EXPECT_DECLS;
  22815. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  22816. PKCS7* pkcs7 = NULL;
  22817. byte* tmpBytePtr = NULL;
  22818. byte encrypted[TWOK_BUF];
  22819. byte decoded[TWOK_BUF];
  22820. word32 tmpWrd32 = 0;
  22821. int tmpInt = 0;
  22822. int decodedSz;
  22823. int encryptedSz = 0;
  22824. int testSz;
  22825. int i;
  22826. const byte data[] = { /* Hello World */
  22827. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  22828. 0x72,0x6c,0x64
  22829. };
  22830. #ifndef NO_DES3
  22831. byte desKey[] = {
  22832. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  22833. };
  22834. byte des3Key[] = {
  22835. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  22836. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  22837. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  22838. };
  22839. #endif
  22840. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22841. #ifndef NO_AES_128
  22842. byte aes128Key[] = {
  22843. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22844. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22845. };
  22846. #endif
  22847. #ifndef NO_AES_192
  22848. byte aes192Key[] = {
  22849. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22850. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22851. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22852. };
  22853. #endif
  22854. #ifndef NO_AES_256
  22855. byte aes256Key[] = {
  22856. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22857. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22858. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  22859. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  22860. };
  22861. #endif
  22862. #endif /* !NO_AES && HAVE_AES_CBC */
  22863. const pkcs7EncryptedVector testVectors[] =
  22864. {
  22865. #ifndef NO_DES3
  22866. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  22867. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  22868. #endif /* !NO_DES3 */
  22869. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  22870. #ifndef NO_AES_128
  22871. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  22872. sizeof(aes128Key)},
  22873. #endif
  22874. #ifndef NO_AES_192
  22875. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  22876. sizeof(aes192Key)},
  22877. #endif
  22878. #ifndef NO_AES_256
  22879. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  22880. sizeof(aes256Key)},
  22881. #endif
  22882. #endif /* !NO_AES && HAVE_AES_CBC */
  22883. };
  22884. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  22885. for (i = 0; i < testSz; i++) {
  22886. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22887. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22888. if (pkcs7 != NULL) {
  22889. pkcs7->content = (byte*)testVectors[i].content;
  22890. pkcs7->contentSz = testVectors[i].contentSz;
  22891. pkcs7->contentOID = testVectors[i].contentOID;
  22892. pkcs7->encryptOID = testVectors[i].encryptOID;
  22893. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  22894. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  22895. pkcs7->heap = HEAP_HINT;
  22896. }
  22897. /* encode encryptedData */
  22898. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22899. sizeof(encrypted)), 0);
  22900. /* Decode encryptedData */
  22901. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  22902. encryptedSz, decoded, sizeof(decoded)), 0);
  22903. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  22904. /* Keep values for last itr. */
  22905. if (i < testSz - 1) {
  22906. wc_PKCS7_Free(pkcs7);
  22907. pkcs7 = NULL;
  22908. }
  22909. }
  22910. if (pkcs7 == NULL || testSz == 0) {
  22911. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  22912. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  22913. }
  22914. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  22915. sizeof(encrypted)),BAD_FUNC_ARG);
  22916. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  22917. sizeof(encrypted)), BAD_FUNC_ARG);
  22918. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22919. 0), BAD_FUNC_ARG);
  22920. /* Testing the struct. */
  22921. if (pkcs7 != NULL) {
  22922. tmpBytePtr = pkcs7->content;
  22923. pkcs7->content = NULL;
  22924. }
  22925. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22926. sizeof(encrypted)), BAD_FUNC_ARG);
  22927. if (pkcs7 != NULL) {
  22928. pkcs7->content = tmpBytePtr;
  22929. tmpWrd32 = pkcs7->contentSz;
  22930. pkcs7->contentSz = 0;
  22931. }
  22932. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22933. sizeof(encrypted)), BAD_FUNC_ARG);
  22934. if (pkcs7 != NULL) {
  22935. pkcs7->contentSz = tmpWrd32;
  22936. tmpInt = pkcs7->encryptOID;
  22937. pkcs7->encryptOID = 0;
  22938. }
  22939. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22940. sizeof(encrypted)), BAD_FUNC_ARG);
  22941. if (pkcs7 != NULL) {
  22942. pkcs7->encryptOID = tmpInt;
  22943. tmpBytePtr = pkcs7->encryptionKey;
  22944. pkcs7->encryptionKey = NULL;
  22945. }
  22946. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22947. sizeof(encrypted)), BAD_FUNC_ARG);
  22948. if (pkcs7 != NULL) {
  22949. pkcs7->encryptionKey = tmpBytePtr;
  22950. tmpWrd32 = pkcs7->encryptionKeySz;
  22951. pkcs7->encryptionKeySz = 0;
  22952. }
  22953. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  22954. sizeof(encrypted)), BAD_FUNC_ARG);
  22955. if (pkcs7 != NULL) {
  22956. pkcs7->encryptionKeySz = tmpWrd32;
  22957. }
  22958. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  22959. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22960. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  22961. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22962. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  22963. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22964. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22965. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  22966. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22967. decoded, 0), BAD_FUNC_ARG);
  22968. /* Test struct fields */
  22969. if (pkcs7 != NULL) {
  22970. tmpBytePtr = pkcs7->encryptionKey;
  22971. pkcs7->encryptionKey = NULL;
  22972. }
  22973. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22974. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22975. if (pkcs7 != NULL) {
  22976. pkcs7->encryptionKey = tmpBytePtr;
  22977. pkcs7->encryptionKeySz = 0;
  22978. }
  22979. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  22980. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  22981. wc_PKCS7_Free(pkcs7);
  22982. #endif
  22983. return EXPECT_RESULT();
  22984. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  22985. /*
  22986. * Testing wc_PKCS7_Degenerate()
  22987. */
  22988. static int test_wc_PKCS7_Degenerate(void)
  22989. {
  22990. EXPECT_DECLS;
  22991. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  22992. PKCS7* pkcs7 = NULL;
  22993. char fName[] = "./certs/test-degenerate.p7b";
  22994. XFILE f = XBADFILE;
  22995. byte der[4096];
  22996. word32 derSz = 0;
  22997. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  22998. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  22999. if (f != XBADFILE)
  23000. XFCLOSE(f);
  23001. /* test degenerate success */
  23002. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23003. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23004. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23005. #ifndef NO_RSA
  23006. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23007. #else
  23008. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23009. #endif
  23010. wc_PKCS7_Free(pkcs7);
  23011. pkcs7 = NULL;
  23012. /* test with turning off degenerate cases */
  23013. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23014. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23015. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23016. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  23017. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  23018. PKCS7_NO_SIGNER_E);
  23019. wc_PKCS7_Free(pkcs7);
  23020. #endif
  23021. return EXPECT_RESULT();
  23022. } /* END test_wc_PKCS7_Degenerate() */
  23023. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23024. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  23025. static byte berContent[] = {
  23026. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23027. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  23028. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  23029. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  23030. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  23031. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  23032. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  23033. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  23034. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  23035. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  23036. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  23037. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  23038. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  23039. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  23040. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  23041. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  23042. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  23043. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  23044. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23045. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23046. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  23047. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23048. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23049. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  23050. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  23051. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  23052. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  23053. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  23054. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  23055. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  23056. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  23057. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  23058. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  23059. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  23060. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  23061. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  23062. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  23063. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  23064. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  23065. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  23066. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  23067. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  23068. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  23069. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  23070. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23071. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  23072. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  23073. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  23074. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  23075. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  23076. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  23077. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  23078. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  23079. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  23080. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  23081. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  23082. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  23083. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  23084. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  23085. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  23086. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  23087. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  23088. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  23089. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  23090. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  23091. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  23092. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  23093. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  23094. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  23095. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  23096. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  23097. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  23098. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  23099. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  23100. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  23101. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  23102. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  23103. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  23104. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  23105. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  23106. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  23107. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  23108. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  23109. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  23110. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  23111. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  23112. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  23113. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  23114. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  23115. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  23116. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  23117. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  23118. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  23119. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  23120. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  23121. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  23122. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  23123. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  23124. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  23125. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  23126. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  23127. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  23128. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  23129. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  23130. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  23131. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  23132. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  23133. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  23134. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  23135. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  23136. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  23137. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  23138. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  23139. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  23140. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  23141. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  23142. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  23143. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  23144. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  23145. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  23146. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  23147. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  23148. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  23149. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  23150. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  23151. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  23152. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  23153. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  23154. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  23155. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  23156. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  23157. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  23158. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  23159. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  23160. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  23161. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  23162. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  23163. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  23164. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  23165. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  23166. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  23167. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  23168. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  23169. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  23170. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  23171. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  23172. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  23173. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  23174. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  23175. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  23176. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  23177. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  23178. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  23179. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  23180. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  23181. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  23182. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  23183. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  23184. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  23185. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  23186. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  23187. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  23188. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  23189. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  23190. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  23191. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  23192. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  23193. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  23194. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  23195. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  23196. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  23197. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  23198. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  23199. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  23200. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  23201. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  23202. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  23203. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  23204. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  23205. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  23206. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  23207. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  23208. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  23209. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  23210. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  23211. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  23212. 0x00, 0x00, 0x00, 0x00, 0x00
  23213. };
  23214. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  23215. * !NO_DES3 && !NO_SHA
  23216. */
  23217. /*
  23218. * Testing wc_PKCS7_BER()
  23219. */
  23220. static int test_wc_PKCS7_BER(void)
  23221. {
  23222. EXPECT_DECLS;
  23223. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  23224. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  23225. PKCS7* pkcs7 = NULL;
  23226. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  23227. XFILE f = XBADFILE;
  23228. byte der[4096];
  23229. #ifndef NO_DES3
  23230. byte decoded[2048];
  23231. #endif
  23232. word32 derSz;
  23233. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  23234. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23235. if (f != XBADFILE) {
  23236. XFCLOSE(f);
  23237. f = XBADFILE;
  23238. }
  23239. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23240. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23241. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23242. #ifndef NO_RSA
  23243. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23244. #else
  23245. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  23246. #endif
  23247. wc_PKCS7_Free(pkcs7);
  23248. pkcs7 = NULL;
  23249. #ifndef NO_DES3
  23250. /* decode BER content */
  23251. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  23252. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23253. if (f != XBADFILE) {
  23254. XFCLOSE(f);
  23255. f = XBADFILE;
  23256. }
  23257. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23258. #ifndef NO_RSA
  23259. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23260. #else
  23261. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  23262. #endif
  23263. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  23264. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  23265. if (f != XBADFILE) {
  23266. XFCLOSE(f);
  23267. f = XBADFILE;
  23268. }
  23269. if (pkcs7 != NULL) {
  23270. pkcs7->privateKey = der;
  23271. pkcs7->privateKeySz = derSz;
  23272. }
  23273. #ifndef NO_RSA
  23274. #ifdef WOLFSSL_SP_MATH
  23275. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23276. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  23277. #else
  23278. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23279. sizeof(berContent), decoded, sizeof(decoded)), 0);
  23280. #endif
  23281. #else
  23282. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  23283. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  23284. #endif
  23285. wc_PKCS7_Free(pkcs7);
  23286. #endif /* !NO_DES3 */
  23287. #endif
  23288. return EXPECT_RESULT();
  23289. } /* END test_wc_PKCS7_BER() */
  23290. static int test_wc_PKCS7_signed_enveloped(void)
  23291. {
  23292. EXPECT_DECLS;
  23293. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  23294. !defined(NO_FILESYSTEM)
  23295. XFILE f = XBADFILE;
  23296. PKCS7* pkcs7 = NULL;
  23297. #ifdef HAVE_AES_CBC
  23298. PKCS7* inner = NULL;
  23299. #endif
  23300. void* pt = NULL;
  23301. WC_RNG rng;
  23302. unsigned char key[FOURK_BUF/2];
  23303. unsigned char cert[FOURK_BUF/2];
  23304. unsigned char env[FOURK_BUF];
  23305. int envSz = FOURK_BUF;
  23306. int keySz = 0;
  23307. int certSz = 0;
  23308. unsigned char sig[FOURK_BUF * 2];
  23309. int sigSz = FOURK_BUF * 2;
  23310. #ifdef HAVE_AES_CBC
  23311. unsigned char decoded[FOURK_BUF];
  23312. int decodedSz = FOURK_BUF;
  23313. #endif
  23314. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23315. /* load cert */
  23316. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  23317. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  23318. if (f != XBADFILE) {
  23319. XFCLOSE(f);
  23320. f = XBADFILE;
  23321. }
  23322. /* load key */
  23323. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  23324. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  23325. if (f != XBADFILE) {
  23326. XFCLOSE(f);
  23327. f = XBADFILE;
  23328. }
  23329. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  23330. /* sign cert for envelope */
  23331. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23332. ExpectIntEQ(wc_InitRng(&rng), 0);
  23333. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23334. if (pkcs7 != NULL) {
  23335. pkcs7->content = cert;
  23336. pkcs7->contentSz = certSz;
  23337. pkcs7->contentOID = DATA;
  23338. pkcs7->privateKey = key;
  23339. pkcs7->privateKeySz = keySz;
  23340. pkcs7->encryptOID = RSAk;
  23341. pkcs7->hashOID = SHA256h;
  23342. pkcs7->rng = &rng;
  23343. }
  23344. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23345. wc_PKCS7_Free(pkcs7);
  23346. pkcs7 = NULL;
  23347. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23348. #ifdef HAVE_AES_CBC
  23349. /* create envelope */
  23350. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23351. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23352. if (pkcs7 != NULL) {
  23353. pkcs7->content = sig;
  23354. pkcs7->contentSz = sigSz;
  23355. pkcs7->contentOID = DATA;
  23356. pkcs7->encryptOID = AES256CBCb;
  23357. pkcs7->privateKey = key;
  23358. pkcs7->privateKeySz = keySz;
  23359. }
  23360. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  23361. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  23362. wc_PKCS7_Free(pkcs7);
  23363. pkcs7 = NULL;
  23364. #endif
  23365. /* create bad signed enveloped data */
  23366. sigSz = FOURK_BUF * 2;
  23367. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23368. ExpectIntEQ(wc_InitRng(&rng), 0);
  23369. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23370. if (pkcs7 != NULL) {
  23371. pkcs7->content = env;
  23372. pkcs7->contentSz = envSz;
  23373. pkcs7->contentOID = DATA;
  23374. pkcs7->privateKey = key;
  23375. pkcs7->privateKeySz = keySz;
  23376. pkcs7->encryptOID = RSAk;
  23377. pkcs7->hashOID = SHA256h;
  23378. pkcs7->rng = &rng;
  23379. }
  23380. /* Set no certs in bundle for this test. Hang on to the pointer though to
  23381. * free it later. */
  23382. if (pkcs7 != NULL) {
  23383. pt = (void*)pkcs7->certList;
  23384. pkcs7->certList = NULL; /* no certs in bundle */
  23385. }
  23386. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23387. if (pkcs7 != NULL) {
  23388. /* restore pointer for PKCS7 free call */
  23389. pkcs7->certList = (Pkcs7Cert*)pt;
  23390. }
  23391. wc_PKCS7_Free(pkcs7);
  23392. pkcs7 = NULL;
  23393. /* check verify fails */
  23394. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23395. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23396. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  23397. PKCS7_SIGNEEDS_CHECK);
  23398. /* try verifying the signature manually */
  23399. {
  23400. RsaKey rKey;
  23401. word32 idx = 0;
  23402. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  23403. WC_MAX_DIGEST_SIZE];
  23404. int digestSz;
  23405. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  23406. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  23407. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  23408. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  23409. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  23410. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  23411. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  23412. /* verify was success */
  23413. }
  23414. wc_PKCS7_Free(pkcs7);
  23415. pkcs7 = NULL;
  23416. /* initializing the PKCS7 struct with the signing certificate should pass */
  23417. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23418. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23419. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23420. wc_PKCS7_Free(pkcs7);
  23421. pkcs7 = NULL;
  23422. /* create valid degenerate bundle */
  23423. sigSz = FOURK_BUF * 2;
  23424. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23425. if (pkcs7 != NULL) {
  23426. pkcs7->content = env;
  23427. pkcs7->contentSz = envSz;
  23428. pkcs7->contentOID = DATA;
  23429. pkcs7->privateKey = key;
  23430. pkcs7->privateKeySz = keySz;
  23431. pkcs7->encryptOID = RSAk;
  23432. pkcs7->hashOID = SHA256h;
  23433. pkcs7->rng = &rng;
  23434. }
  23435. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  23436. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  23437. wc_PKCS7_Free(pkcs7);
  23438. pkcs7 = NULL;
  23439. wc_FreeRng(&rng);
  23440. /* check verify */
  23441. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23442. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  23443. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  23444. ExpectNotNull(pkcs7->content);
  23445. #ifdef HAVE_AES_CBC
  23446. /* check decode */
  23447. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  23448. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  23449. if (inner != NULL) {
  23450. inner->privateKey = key;
  23451. inner->privateKeySz = keySz;
  23452. }
  23453. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  23454. pkcs7->contentSz, decoded, decodedSz)), 0);
  23455. wc_PKCS7_Free(inner);
  23456. inner = NULL;
  23457. #endif
  23458. wc_PKCS7_Free(pkcs7);
  23459. pkcs7 = NULL;
  23460. #ifdef HAVE_AES_CBC
  23461. /* check cert set */
  23462. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  23463. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23464. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  23465. ExpectNotNull(pkcs7->singleCert);
  23466. ExpectIntNE(pkcs7->singleCertSz, 0);
  23467. wc_PKCS7_Free(pkcs7);
  23468. pkcs7 = NULL;
  23469. #endif
  23470. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  23471. return EXPECT_RESULT();
  23472. }
  23473. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  23474. {
  23475. EXPECT_DECLS;
  23476. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23477. && !defined(NO_AES)
  23478. PKCS7* pkcs7 = NULL;
  23479. void* heap = NULL;
  23480. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23481. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23482. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  23483. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  23484. wc_PKCS7_Free(pkcs7);
  23485. #endif
  23486. return EXPECT_RESULT();
  23487. }
  23488. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  23489. {
  23490. EXPECT_DECLS;
  23491. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23492. && !defined(NO_AES)
  23493. PKCS7* pkcs7 = NULL;
  23494. void* heap = NULL;
  23495. WOLFSSL_CTX* ctx = NULL;
  23496. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23497. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23498. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23499. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  23500. wc_PKCS7_Free(pkcs7);
  23501. #endif
  23502. return EXPECT_RESULT();
  23503. }
  23504. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  23505. {
  23506. EXPECT_DECLS;
  23507. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23508. && !defined(NO_AES)
  23509. PKCS7* pkcs7 = NULL;
  23510. void* heap = NULL;
  23511. WOLFSSL_CTX* ctx = NULL;
  23512. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23513. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23514. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  23515. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  23516. wc_PKCS7_Free(pkcs7);
  23517. #endif
  23518. return EXPECT_RESULT();
  23519. }
  23520. static int test_wc_PKCS7_DecodeCompressedData(void)
  23521. {
  23522. EXPECT_DECLS;
  23523. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23524. && !defined(NO_AES) && defined(HAVE_LIBZ)
  23525. PKCS7* pkcs7 = NULL;
  23526. void* heap = NULL;
  23527. byte out[4096];
  23528. byte* decompressed = NULL;
  23529. int outSz;
  23530. int decompressedSz;
  23531. const char* cert = "./certs/client-cert.pem";
  23532. byte* cert_buf = NULL;
  23533. size_t cert_sz = 0;
  23534. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  23535. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  23536. DYNAMIC_TYPE_TMP_BUFFER)));
  23537. decompressedSz = (int)cert_sz;
  23538. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  23539. if (pkcs7 != NULL) {
  23540. pkcs7->content = (byte*)cert_buf;
  23541. pkcs7->contentSz = (word32)cert_sz;
  23542. pkcs7->contentOID = DATA;
  23543. }
  23544. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  23545. sizeof(out))), 0);
  23546. wc_PKCS7_Free(pkcs7);
  23547. pkcs7 = NULL;
  23548. /* compressed key should be smaller than when started */
  23549. ExpectIntLT(outSz, cert_sz);
  23550. /* test decompression */
  23551. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  23552. ExpectIntEQ(pkcs7->contentOID, 0);
  23553. /* fail case with out buffer too small */
  23554. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23555. decompressed, outSz), 0);
  23556. /* success case */
  23557. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  23558. decompressed, decompressedSz), cert_sz);
  23559. ExpectIntEQ(pkcs7->contentOID, DATA);
  23560. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23561. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23562. decompressed = NULL;
  23563. /* test decompression function with different 'max' inputs */
  23564. outSz = sizeof(out);
  23565. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  23566. 0);
  23567. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  23568. out, outSz, 0, heap), 0);
  23569. ExpectNull(decompressed);
  23570. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  23571. out, outSz, 0, heap), 0);
  23572. ExpectNotNull(decompressed);
  23573. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23574. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23575. decompressed = NULL;
  23576. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  23577. out, outSz, 0, heap), 0);
  23578. ExpectNotNull(decompressed);
  23579. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  23580. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  23581. if (cert_buf != NULL)
  23582. free(cert_buf);
  23583. wc_PKCS7_Free(pkcs7);
  23584. #endif
  23585. return EXPECT_RESULT();
  23586. }
  23587. static int test_wc_i2d_PKCS12(void)
  23588. {
  23589. EXPECT_DECLS;
  23590. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  23591. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  23592. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  23593. WC_PKCS12* pkcs12 = NULL;
  23594. unsigned char der[FOURK_BUF * 2];
  23595. unsigned char* pt;
  23596. int derSz = 0;
  23597. unsigned char out[FOURK_BUF * 2];
  23598. int outSz = FOURK_BUF * 2;
  23599. const char p12_f[] = "./certs/test-servercert.p12";
  23600. XFILE f = XBADFILE;
  23601. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  23602. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  23603. if (f != XBADFILE)
  23604. XFCLOSE(f);
  23605. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  23606. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  23607. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23608. ExpectIntEQ(outSz, derSz);
  23609. outSz = derSz - 1;
  23610. pt = out;
  23611. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  23612. outSz = derSz;
  23613. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  23614. ExpectIntEQ((pt == out), 0);
  23615. pt = NULL;
  23616. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  23617. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  23618. wc_PKCS12_free(pkcs12);
  23619. pkcs12 = NULL;
  23620. /* Run the same test but use wc_d2i_PKCS12_fp. */
  23621. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  23622. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  23623. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23624. ExpectIntEQ(outSz, derSz);
  23625. wc_PKCS12_free(pkcs12);
  23626. pkcs12 = NULL;
  23627. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  23628. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  23629. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  23630. ExpectIntEQ(outSz, derSz);
  23631. wc_PKCS12_free(pkcs12);
  23632. pkcs12 = NULL;
  23633. #endif
  23634. return EXPECT_RESULT();
  23635. }
  23636. /* Testing wc_SignatureGetSize() for signature type ECC */
  23637. static int test_wc_SignatureGetSize_ecc(void)
  23638. {
  23639. EXPECT_DECLS;
  23640. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  23641. enum wc_SignatureType sig_type;
  23642. word32 key_len;
  23643. ecc_key ecc;
  23644. const char* qx =
  23645. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  23646. const char* qy =
  23647. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  23648. const char* d =
  23649. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  23650. XMEMSET(&ecc, 0, sizeof(ecc_key));
  23651. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  23652. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  23653. /* Input for signature type ECC */
  23654. sig_type = WC_SIGNATURE_TYPE_ECC;
  23655. key_len = sizeof(ecc_key);
  23656. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  23657. /* Test bad args */
  23658. sig_type = (enum wc_SignatureType) 100;
  23659. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  23660. sig_type = WC_SIGNATURE_TYPE_ECC;
  23661. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  23662. key_len = (word32)0;
  23663. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  23664. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  23665. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  23666. return EXPECT_RESULT();
  23667. } /* END test_wc_SignatureGetSize_ecc() */
  23668. /* Testing wc_SignatureGetSize() for signature type rsa */
  23669. static int test_wc_SignatureGetSize_rsa(void)
  23670. {
  23671. EXPECT_DECLS;
  23672. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  23673. enum wc_SignatureType sig_type;
  23674. word32 key_len;
  23675. word32 idx = 0;
  23676. RsaKey rsa_key;
  23677. byte* tmp = NULL;
  23678. size_t bytes;
  23679. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  23680. #ifdef USE_CERT_BUFFERS_1024
  23681. bytes = (size_t)sizeof_client_key_der_1024;
  23682. if (bytes < (size_t)sizeof_client_key_der_1024)
  23683. bytes = (size_t)sizeof_client_cert_der_1024;
  23684. #elif defined(USE_CERT_BUFFERS_2048)
  23685. bytes = (size_t)sizeof_client_key_der_2048;
  23686. if (bytes < (size_t)sizeof_client_cert_der_2048)
  23687. bytes = (size_t)sizeof_client_cert_der_2048;
  23688. #else
  23689. bytes = FOURK_BUF;
  23690. #endif
  23691. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  23692. DYNAMIC_TYPE_TMP_BUFFER));
  23693. if (tmp != NULL) {
  23694. #ifdef USE_CERT_BUFFERS_1024
  23695. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  23696. #elif defined(USE_CERT_BUFFERS_2048)
  23697. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  23698. #elif !defined(NO_FILESYSTEM)
  23699. XFILE file = XBADFILE;
  23700. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  23701. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  23702. if (file != XBADFILE)
  23703. XFCLOSE(file);
  23704. }
  23705. #else
  23706. ExpectFail();
  23707. #endif
  23708. }
  23709. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  23710. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  23711. /* Input for signature type RSA */
  23712. sig_type = WC_SIGNATURE_TYPE_RSA;
  23713. key_len = sizeof(RsaKey);
  23714. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  23715. /* Test bad args */
  23716. sig_type = (enum wc_SignatureType)100;
  23717. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  23718. sig_type = WC_SIGNATURE_TYPE_RSA;
  23719. #ifndef HAVE_USER_RSA
  23720. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  23721. #else
  23722. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  23723. #endif
  23724. key_len = (word32)0;
  23725. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  23726. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  23727. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23728. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  23729. return EXPECT_RESULT();
  23730. } /* END test_wc_SignatureGetSize_rsa(void) */
  23731. /*----------------------------------------------------------------------------*
  23732. | hash.h Tests
  23733. *----------------------------------------------------------------------------*/
  23734. static int test_wc_HashInit(void)
  23735. {
  23736. EXPECT_DECLS;
  23737. int i; /* 0 indicates tests passed, 1 indicates failure */
  23738. wc_HashAlg hash;
  23739. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23740. enum wc_HashType enumArray[] = {
  23741. #ifndef NO_MD5
  23742. WC_HASH_TYPE_MD5,
  23743. #endif
  23744. #ifndef NO_SHA
  23745. WC_HASH_TYPE_SHA,
  23746. #endif
  23747. #ifdef WOLFSSL_SHA224
  23748. WC_HASH_TYPE_SHA224,
  23749. #endif
  23750. #ifndef NO_SHA256
  23751. WC_HASH_TYPE_SHA256,
  23752. #endif
  23753. #ifdef WOLFSSL_SHA384
  23754. WC_HASH_TYPE_SHA384,
  23755. #endif
  23756. #ifdef WOLFSSL_SHA512
  23757. WC_HASH_TYPE_SHA512,
  23758. #endif
  23759. };
  23760. /* dynamically finds the length */
  23761. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23762. /* For loop to test various arguments... */
  23763. for (i = 0; i < enumlen; i++) {
  23764. /* check for bad args */
  23765. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23766. wc_HashFree(&hash, enumArray[i]);
  23767. /* check for null ptr */
  23768. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  23769. } /* end of for loop */
  23770. return EXPECT_RESULT();
  23771. } /* end of test_wc_HashInit */
  23772. /*
  23773. * Unit test function for wc_HashSetFlags()
  23774. */
  23775. static int test_wc_HashSetFlags(void)
  23776. {
  23777. EXPECT_DECLS;
  23778. #ifdef WOLFSSL_HASH_FLAGS
  23779. wc_HashAlg hash;
  23780. word32 flags = 0;
  23781. int i, j;
  23782. int notSupportedLen;
  23783. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23784. enum wc_HashType enumArray[] = {
  23785. #ifndef NO_MD5
  23786. WC_HASH_TYPE_MD5,
  23787. #endif
  23788. #ifndef NO_SHA
  23789. WC_HASH_TYPE_SHA,
  23790. #endif
  23791. #ifdef WOLFSSL_SHA224
  23792. WC_HASH_TYPE_SHA224,
  23793. #endif
  23794. #ifndef NO_SHA256
  23795. WC_HASH_TYPE_SHA256,
  23796. #endif
  23797. #ifdef WOLFSSL_SHA384
  23798. WC_HASH_TYPE_SHA384,
  23799. #endif
  23800. #ifdef WOLFSSL_SHA512
  23801. WC_HASH_TYPE_SHA512,
  23802. #endif
  23803. #ifdef WOLFSSL_SHA3
  23804. WC_HASH_TYPE_SHA3_224,
  23805. #endif
  23806. };
  23807. enum wc_HashType notSupported[] = {
  23808. WC_HASH_TYPE_MD5_SHA,
  23809. WC_HASH_TYPE_MD2,
  23810. WC_HASH_TYPE_MD4,
  23811. WC_HASH_TYPE_BLAKE2B,
  23812. WC_HASH_TYPE_BLAKE2S,
  23813. WC_HASH_TYPE_NONE,
  23814. };
  23815. /* dynamically finds the length */
  23816. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23817. /* For loop to test various arguments... */
  23818. for (i = 0; i < enumlen; i++) {
  23819. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23820. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  23821. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  23822. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  23823. wc_HashFree(&hash, enumArray[i]);
  23824. }
  23825. /* For loop to test not supported cases */
  23826. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23827. for (j = 0; j < notSupportedLen; j++) {
  23828. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  23829. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  23830. BAD_FUNC_ARG);
  23831. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  23832. }
  23833. #endif
  23834. return EXPECT_RESULT();
  23835. } /* END test_wc_HashSetFlags */
  23836. /*
  23837. * Unit test function for wc_HashGetFlags()
  23838. */
  23839. static int test_wc_HashGetFlags(void)
  23840. {
  23841. EXPECT_DECLS;
  23842. #ifdef WOLFSSL_HASH_FLAGS
  23843. wc_HashAlg hash;
  23844. word32 flags = 0;
  23845. int i, j;
  23846. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  23847. enum wc_HashType enumArray[] = {
  23848. #ifndef NO_MD5
  23849. WC_HASH_TYPE_MD5,
  23850. #endif
  23851. #ifndef NO_SHA
  23852. WC_HASH_TYPE_SHA,
  23853. #endif
  23854. #ifdef WOLFSSL_SHA224
  23855. WC_HASH_TYPE_SHA224,
  23856. #endif
  23857. #ifndef NO_SHA256
  23858. WC_HASH_TYPE_SHA256,
  23859. #endif
  23860. #ifdef WOLFSSL_SHA384
  23861. WC_HASH_TYPE_SHA384,
  23862. #endif
  23863. #ifdef WOLFSSL_SHA512
  23864. WC_HASH_TYPE_SHA512,
  23865. #endif
  23866. #ifdef WOLFSSL_SHA3
  23867. WC_HASH_TYPE_SHA3_224,
  23868. #endif
  23869. };
  23870. enum wc_HashType notSupported[] = {
  23871. WC_HASH_TYPE_MD5_SHA,
  23872. WC_HASH_TYPE_MD2,
  23873. WC_HASH_TYPE_MD4,
  23874. WC_HASH_TYPE_BLAKE2B,
  23875. WC_HASH_TYPE_BLAKE2S,
  23876. WC_HASH_TYPE_NONE,
  23877. };
  23878. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  23879. int notSupportedLen;
  23880. /* For loop to test various arguments... */
  23881. for (i = 0; i < enumlen; i++) {
  23882. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  23883. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  23884. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  23885. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  23886. wc_HashFree(&hash, enumArray[i]);
  23887. }
  23888. /* For loop to test not supported cases */
  23889. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  23890. for (j = 0; j < notSupportedLen; j++) {
  23891. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  23892. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  23893. BAD_FUNC_ARG);
  23894. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  23895. }
  23896. #endif
  23897. return EXPECT_RESULT();
  23898. } /* END test_wc_HashGetFlags */
  23899. /*----------------------------------------------------------------------------*
  23900. | Compatibility Tests
  23901. *----------------------------------------------------------------------------*/
  23902. /*----------------------------------------------------------------------------*
  23903. | ASN.1 Tests
  23904. *----------------------------------------------------------------------------*/
  23905. static int test_wolfSSL_ASN1_BIT_STRING(void)
  23906. {
  23907. EXPECT_DECLS;
  23908. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  23909. ASN1_BIT_STRING* str = NULL;
  23910. ExpectNotNull(str = ASN1_BIT_STRING_new());
  23911. /* Empty data testing. */
  23912. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  23913. ASN1_BIT_STRING_free(str);
  23914. str = NULL;
  23915. ExpectNotNull(str = ASN1_BIT_STRING_new());
  23916. /* Invalid parameter testing. */
  23917. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  23918. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  23919. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  23920. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  23921. /* No bit string - bit is always 0. */
  23922. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  23923. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  23924. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  23925. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  23926. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  23927. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  23928. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  23929. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  23930. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  23931. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  23932. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  23933. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  23934. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  23935. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  23936. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  23937. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  23938. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  23939. ASN1_BIT_STRING_free(str);
  23940. ASN1_BIT_STRING_free(NULL);
  23941. #endif
  23942. return EXPECT_RESULT();
  23943. }
  23944. static int test_wolfSSL_ASN1_INTEGER(void)
  23945. {
  23946. EXPECT_DECLS;
  23947. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  23948. ASN1_INTEGER* a = NULL;
  23949. ASN1_INTEGER* dup = NULL;
  23950. const unsigned char invalidLenDer[] = {
  23951. 0x02, 0x20, 0x00
  23952. };
  23953. const unsigned char longDer[] = {
  23954. 0x02, 0x20,
  23955. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23956. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23957. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23958. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  23959. };
  23960. const unsigned char* p;
  23961. /* Invalid parameter testing. */
  23962. ASN1_INTEGER_free(NULL);
  23963. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  23964. ExpectNotNull(a = ASN1_INTEGER_new());
  23965. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  23966. ASN1_INTEGER_free(dup);
  23967. dup = NULL;
  23968. ASN1_INTEGER_free(a);
  23969. a = NULL;
  23970. p = longDer;
  23971. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  23972. p = longDer;
  23973. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  23974. ExpectPtrNE(p, longDer);
  23975. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  23976. ASN1_INTEGER_free(dup);
  23977. ASN1_INTEGER_free(a);
  23978. #endif
  23979. return EXPECT_RESULT();
  23980. }
  23981. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  23982. {
  23983. EXPECT_DECLS;
  23984. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  23985. ASN1_INTEGER* a = NULL;
  23986. ASN1_INTEGER* b = NULL;
  23987. ExpectNotNull(a = ASN1_INTEGER_new());
  23988. ExpectNotNull(b = ASN1_INTEGER_new());
  23989. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  23990. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  23991. /* Invalid parameter testing. */
  23992. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  23993. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  23994. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  23995. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  23996. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  23997. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  23998. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  23999. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24000. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  24001. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24002. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  24003. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  24004. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24005. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  24006. ASN1_INTEGER_free(b);
  24007. ASN1_INTEGER_free(a);
  24008. #endif
  24009. return EXPECT_RESULT();
  24010. }
  24011. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  24012. {
  24013. EXPECT_DECLS;
  24014. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24015. ASN1_INTEGER* ai = NULL;
  24016. ASN1_INTEGER* ai2 = NULL;
  24017. BIGNUM* bn = NULL;
  24018. BIGNUM* bn2 = NULL;
  24019. ExpectNotNull(ai = ASN1_INTEGER_new());
  24020. ExpectNotNull(bn2 = BN_new());
  24021. /* Invalid parameter testing. */
  24022. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  24023. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  24024. /* at the moment hard setting since no set function */
  24025. if (ai != NULL) {
  24026. ai->data[0] = 0xff; /* No DER encoding. */
  24027. ai->length = 1;
  24028. }
  24029. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24030. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24031. BN_free(bn);
  24032. bn = NULL;
  24033. #else
  24034. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  24035. #endif
  24036. if (ai != NULL) {
  24037. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24038. ai->data[1] = 0x04; /* bad length of integer */
  24039. ai->data[2] = 0x03;
  24040. ai->length = 3;
  24041. }
  24042. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24043. /* Interpreted as a number 0x020403. */
  24044. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24045. BN_free(bn);
  24046. bn = NULL;
  24047. #else
  24048. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  24049. #endif
  24050. if (ai != NULL) {
  24051. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24052. ai->data[1] = 0x01; /* length of integer */
  24053. ai->data[2] = 0x03;
  24054. ai->length = 3;
  24055. }
  24056. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  24057. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  24058. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24059. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24060. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24061. if (ai != NULL) {
  24062. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24063. ai->data[1] = 0x02; /* length of integer */
  24064. ai->data[2] = 0x00; /* padding byte to ensure positive */
  24065. ai->data[3] = 0xff;
  24066. ai->length = 4;
  24067. }
  24068. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24069. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24070. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24071. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24072. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24073. if (ai != NULL) {
  24074. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24075. ai->data[1] = 0x01; /* length of integer */
  24076. ai->data[2] = 0x00;
  24077. ai->length = 3;
  24078. }
  24079. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24080. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24081. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24082. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24083. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24084. if (ai != NULL) {
  24085. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  24086. ai->data[1] = 0x01; /* length of integer */
  24087. ai->data[2] = 0x01;
  24088. ai->length = 3;
  24089. ai->negative = 1;
  24090. }
  24091. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  24092. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  24093. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  24094. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  24095. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  24096. BN_free(bn2);
  24097. BN_free(bn);
  24098. ASN1_INTEGER_free(ai2);
  24099. ASN1_INTEGER_free(ai);
  24100. #endif
  24101. return EXPECT_RESULT();
  24102. }
  24103. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  24104. {
  24105. EXPECT_DECLS;
  24106. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24107. ASN1_INTEGER *a = NULL;
  24108. long val;
  24109. ExpectNotNull(a = ASN1_INTEGER_new());
  24110. /* Invalid parameter testing. */
  24111. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  24112. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  24113. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  24114. #else
  24115. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  24116. #endif
  24117. ASN1_INTEGER_free(a);
  24118. a = NULL;
  24119. ExpectNotNull(a = ASN1_INTEGER_new());
  24120. val = 0;
  24121. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  24122. ASN1_INTEGER_free(a);
  24123. a = NULL;
  24124. /* 0 */
  24125. ExpectNotNull(a = ASN1_INTEGER_new());
  24126. val = 0;
  24127. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24128. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24129. ASN1_INTEGER_free(a);
  24130. a = NULL;
  24131. /* 40 */
  24132. ExpectNotNull(a = ASN1_INTEGER_new());
  24133. val = 40;
  24134. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24135. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24136. ASN1_INTEGER_free(a);
  24137. a = NULL;
  24138. /* -40 */
  24139. ExpectNotNull(a = ASN1_INTEGER_new());
  24140. val = -40;
  24141. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24142. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24143. ASN1_INTEGER_free(a);
  24144. a = NULL;
  24145. /* 128 */
  24146. ExpectNotNull(a = ASN1_INTEGER_new());
  24147. val = 128;
  24148. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24149. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24150. ASN1_INTEGER_free(a);
  24151. a = NULL;
  24152. /* -128 */
  24153. ExpectNotNull(a = ASN1_INTEGER_new());
  24154. val = -128;
  24155. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24156. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24157. ASN1_INTEGER_free(a);
  24158. a = NULL;
  24159. /* 200 */
  24160. ExpectNotNull(a = ASN1_INTEGER_new());
  24161. val = 200;
  24162. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24163. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24164. ASN1_INTEGER_free(a);
  24165. a = NULL;
  24166. /* int max (2147483647) */
  24167. ExpectNotNull(a = ASN1_INTEGER_new());
  24168. val = 2147483647;
  24169. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24170. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24171. ASN1_INTEGER_free(a);
  24172. a = NULL;
  24173. /* int min (-2147483648) */
  24174. ExpectNotNull(a = ASN1_INTEGER_new());
  24175. val = -2147483647 - 1;
  24176. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24177. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24178. ASN1_INTEGER_free(a);
  24179. a = NULL;
  24180. /* long max positive */
  24181. ExpectNotNull(a = ASN1_INTEGER_new());
  24182. val = (long)(((unsigned long)-1) >> 1);
  24183. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  24184. ExpectTrue(ASN1_INTEGER_get(a) == val);
  24185. ASN1_INTEGER_free(a);
  24186. #endif
  24187. return EXPECT_RESULT();
  24188. }
  24189. #if defined(OPENSSL_EXTRA)
  24190. typedef struct ASN1IntTestVector {
  24191. const byte* der;
  24192. const size_t derSz;
  24193. const long value;
  24194. } ASN1IntTestVector;
  24195. #endif
  24196. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  24197. {
  24198. EXPECT_DECLS;
  24199. #if defined(OPENSSL_EXTRA)
  24200. size_t i;
  24201. WOLFSSL_ASN1_INTEGER* a = NULL;
  24202. WOLFSSL_ASN1_INTEGER* b = NULL;
  24203. WOLFSSL_ASN1_INTEGER* c = NULL;
  24204. const byte* p = NULL;
  24205. byte* p2 = NULL;
  24206. byte* reEncoded = NULL;
  24207. int reEncodedSz;
  24208. static const byte zeroDer[] = {
  24209. 0x02, 0x01, 0x00
  24210. };
  24211. static const byte oneDer[] = {
  24212. 0x02, 0x01, 0x01
  24213. };
  24214. static const byte negativeDer[] = {
  24215. 0x02, 0x03, 0xC1, 0x16, 0x0D
  24216. };
  24217. static const byte positiveDer[] = {
  24218. 0x02, 0x03, 0x01, 0x00, 0x01
  24219. };
  24220. static const byte primeDer[] = {
  24221. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  24222. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  24223. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  24224. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  24225. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  24226. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  24227. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  24228. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  24229. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  24230. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  24231. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  24232. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  24233. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  24234. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  24235. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  24236. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  24237. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  24238. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  24239. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  24240. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  24241. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  24242. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  24243. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  24244. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  24245. };
  24246. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  24247. static const ASN1IntTestVector testVectors[] = {
  24248. {zeroDer, sizeof(zeroDer), 0},
  24249. {oneDer, sizeof(oneDer), 1},
  24250. {negativeDer, sizeof(negativeDer), -4123123},
  24251. {positiveDer, sizeof(positiveDer), 65537},
  24252. {primeDer, sizeof(primeDer), 0}
  24253. };
  24254. static const size_t NUM_TEST_VECTORS =
  24255. sizeof(testVectors)/sizeof(testVectors[0]);
  24256. /* Check d2i error conditions */
  24257. /* NULL pointer to input. */
  24258. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  24259. ExpectNull(b);
  24260. /* NULL input. */
  24261. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  24262. ExpectNull(b);
  24263. /* 0 length. */
  24264. p = testVectors[0].der;
  24265. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  24266. ExpectNull(b);
  24267. /* Negative length. */
  24268. p = testVectors[0].der;
  24269. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  24270. ExpectNull(b);
  24271. /* Garbage DER input. */
  24272. p = garbageDer;
  24273. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  24274. ExpectNull(b);
  24275. /* Check i2d error conditions */
  24276. /* NULL input. */
  24277. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  24278. /* 0 length input data buffer (a->length == 0). */
  24279. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  24280. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  24281. if (a != NULL)
  24282. a->data = NULL;
  24283. /* NULL input data buffer. */
  24284. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  24285. if (a != NULL) {
  24286. /* Reset a->data. */
  24287. a->data = a->intData;
  24288. }
  24289. /* Set a to valid value. */
  24290. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  24291. /* NULL output buffer. */
  24292. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  24293. wolfSSL_ASN1_INTEGER_free(a);
  24294. a = NULL;
  24295. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  24296. p = testVectors[i].der;
  24297. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  24298. testVectors[i].derSz));
  24299. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  24300. if (testVectors[i].derSz <= sizeof(long)) {
  24301. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  24302. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  24303. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  24304. wolfSSL_ASN1_INTEGER_free(c);
  24305. c = NULL;
  24306. }
  24307. /* Convert to DER without a pre-allocated output buffer. */
  24308. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  24309. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  24310. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  24311. /* Convert to DER with a pre-allocated output buffer. In this case, the
  24312. * output buffer pointer should be incremented just past the end of the
  24313. * encoded data. */
  24314. p2 = reEncoded;
  24315. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  24316. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  24317. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  24318. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  24319. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  24320. reEncoded = NULL;
  24321. wolfSSL_ASN1_INTEGER_free(a);
  24322. a = NULL;
  24323. }
  24324. #endif /* OPENSSL_EXTRA */
  24325. return EXPECT_RESULT();
  24326. }
  24327. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  24328. {
  24329. EXPECT_DECLS;
  24330. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  24331. BIO* bio = NULL;
  24332. BIO* out = NULL;
  24333. BIO* fixed = NULL;
  24334. ASN1_INTEGER* ai = NULL;
  24335. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  24336. char tmp[1024];
  24337. int tmpSz;
  24338. const char expected1[] = "123456";
  24339. const char expected2[] = "112345678912345678901234567890";
  24340. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  24341. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  24342. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  24343. ExpectNotNull(ai = ASN1_INTEGER_new());
  24344. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  24345. /* Invalid parameter testing. */
  24346. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  24347. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  24348. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  24349. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  24350. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  24351. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  24352. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  24353. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  24354. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  24355. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  24356. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  24357. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  24358. /* No data to read from BIO. */
  24359. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  24360. /* read first line */
  24361. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24362. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  24363. XMEMSET(tmp, 0, 1024);
  24364. tmpSz = BIO_read(out, tmp, 1024);
  24365. ExpectIntEQ(tmpSz, 6);
  24366. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  24367. /* fail on second line (not % 2) */
  24368. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  24369. /* read 3rd long line */
  24370. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24371. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  24372. XMEMSET(tmp, 0, 1024);
  24373. tmpSz = BIO_read(out, tmp, 1024);
  24374. ExpectIntEQ(tmpSz, 30);
  24375. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  24376. /* fail on empty line */
  24377. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  24378. BIO_free(bio);
  24379. bio = NULL;
  24380. /* Make long integer, requiring dynamic memory, even longer. */
  24381. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  24382. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24383. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  24384. XMEMSET(tmp, 0, 1024);
  24385. tmpSz = BIO_read(out, tmp, 1024);
  24386. ExpectIntEQ(tmpSz, 48);
  24387. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  24388. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  24389. XMEMSET(tmp, 0, 1024);
  24390. tmpSz = BIO_read(out, tmp, 1024);
  24391. ExpectIntEQ(tmpSz, 56);
  24392. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  24393. BIO_free(bio);
  24394. BIO_free(out);
  24395. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  24396. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  24397. /* Ensure there is 0 bytes available to write into. */
  24398. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  24399. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  24400. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  24401. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  24402. BIO_free(fixed);
  24403. ASN1_INTEGER_free(ai);
  24404. #endif
  24405. return EXPECT_RESULT();
  24406. }
  24407. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  24408. {
  24409. EXPECT_DECLS;
  24410. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24411. ASN1_INTEGER *a = NULL;
  24412. unsigned char *pp,*tpp;
  24413. int ret;
  24414. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  24415. /* Invalid parameter testing. */
  24416. /* Set pp to an invalid value. */
  24417. pp = NULL;
  24418. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  24419. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  24420. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  24421. /* 40 */
  24422. if (a != NULL) {
  24423. a->intData[0] = ASN_INTEGER;
  24424. a->intData[1] = 1;
  24425. a->intData[2] = 40;
  24426. }
  24427. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24428. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24429. DYNAMIC_TYPE_TMP_BUFFER));
  24430. tpp = pp;
  24431. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24432. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24433. tpp--;
  24434. ExpectIntEQ(*tpp, 40);
  24435. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24436. pp = NULL;
  24437. /* 128 */
  24438. if (a != NULL) {
  24439. a->intData[0] = ASN_INTEGER;
  24440. a->intData[1] = 1;
  24441. a->intData[2] = 128;
  24442. }
  24443. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24444. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24445. DYNAMIC_TYPE_TMP_BUFFER));
  24446. tpp = pp;
  24447. if (tpp != NULL) {
  24448. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24449. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24450. tpp--;
  24451. ExpectIntEQ(*(tpp--), 128);
  24452. ExpectIntEQ(*tpp, 0);
  24453. }
  24454. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24455. pp = NULL;
  24456. /* -40 */
  24457. if (a != NULL) {
  24458. a->intData[0] = ASN_INTEGER;
  24459. a->intData[1] = 1;
  24460. a->intData[2] = 40;
  24461. a->negative = 1;
  24462. }
  24463. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24464. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24465. DYNAMIC_TYPE_TMP_BUFFER));
  24466. tpp = pp;
  24467. if (tpp != NULL) {
  24468. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24469. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24470. tpp--;
  24471. ExpectIntEQ(*tpp, 216);
  24472. }
  24473. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24474. pp = NULL;
  24475. /* -128 */
  24476. if (a != NULL) {
  24477. a->intData[0] = ASN_INTEGER;
  24478. a->intData[1] = 1;
  24479. a->intData[2] = 128;
  24480. a->negative = 1;
  24481. }
  24482. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24483. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24484. DYNAMIC_TYPE_TMP_BUFFER));
  24485. tpp = pp;
  24486. if (tpp != NULL) {
  24487. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24488. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24489. tpp--;
  24490. ExpectIntEQ(*tpp, 128);
  24491. }
  24492. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24493. pp = NULL;
  24494. /* -200 */
  24495. if (a != NULL) {
  24496. a->intData[0] = ASN_INTEGER;
  24497. a->intData[1] = 1;
  24498. a->intData[2] = 200;
  24499. a->negative = 1;
  24500. }
  24501. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24502. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24503. DYNAMIC_TYPE_TMP_BUFFER));
  24504. tpp = pp;
  24505. if (tpp != NULL) {
  24506. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24507. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24508. tpp--;
  24509. ExpectIntEQ(*(tpp--), 56);
  24510. ExpectIntEQ(*tpp, 255);
  24511. }
  24512. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24513. pp = NULL;
  24514. /* Empty */
  24515. if (a != NULL) {
  24516. a->intData[0] = ASN_INTEGER;
  24517. a->intData[1] = 0;
  24518. a->negative = 0;
  24519. }
  24520. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24521. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24522. DYNAMIC_TYPE_TMP_BUFFER));
  24523. tpp = pp;
  24524. if (tpp != NULL) {
  24525. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24526. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24527. tpp--;
  24528. ExpectIntEQ(*tpp, 0);
  24529. }
  24530. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24531. pp = NULL;
  24532. /* 0 */
  24533. if (a != NULL) {
  24534. a->intData[0] = ASN_INTEGER;
  24535. a->intData[1] = 1;
  24536. a->intData[2] = 0;
  24537. a->negative = 1;
  24538. }
  24539. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  24540. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24541. DYNAMIC_TYPE_TMP_BUFFER));
  24542. if (tpp != NULL) {
  24543. tpp = pp;
  24544. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24545. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  24546. tpp--;
  24547. ExpectIntEQ(*tpp, 0);
  24548. }
  24549. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24550. pp = NULL;
  24551. /* 0x100 */
  24552. if (a != NULL) {
  24553. a->intData[0] = ASN_INTEGER;
  24554. a->intData[1] = 2;
  24555. a->intData[2] = 0x01;
  24556. a->intData[3] = 0x00;
  24557. a->negative = 0;
  24558. }
  24559. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24560. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24561. DYNAMIC_TYPE_TMP_BUFFER));
  24562. if (tpp != NULL) {
  24563. tpp = pp;
  24564. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24565. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24566. tpp -= 2;
  24567. ExpectIntEQ(tpp[0], 0x01);
  24568. ExpectIntEQ(tpp[1], 0x00);
  24569. }
  24570. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24571. pp = NULL;
  24572. /* -0x8000 => 0x8000 */
  24573. if (a != NULL) {
  24574. a->intData[0] = ASN_INTEGER;
  24575. a->intData[1] = 2;
  24576. a->intData[2] = 0x80;
  24577. a->intData[3] = 0x00;
  24578. a->negative = 1;
  24579. }
  24580. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  24581. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24582. DYNAMIC_TYPE_TMP_BUFFER));
  24583. tpp = pp;
  24584. if (tpp != NULL) {
  24585. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24586. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  24587. tpp -= 2;
  24588. ExpectIntEQ(tpp[0], 0x80);
  24589. ExpectIntEQ(tpp[1], 0x00);
  24590. }
  24591. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24592. pp = NULL;
  24593. /* -0x8001 => 0xFF7FFF */
  24594. if (a != NULL) {
  24595. a->intData[0] = ASN_INTEGER;
  24596. a->intData[1] = 2;
  24597. a->intData[2] = 0x80;
  24598. a->intData[3] = 0x01;
  24599. a->negative = 1;
  24600. }
  24601. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  24602. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  24603. DYNAMIC_TYPE_TMP_BUFFER));
  24604. tpp = pp;
  24605. if (tpp != NULL) {
  24606. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  24607. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  24608. tpp -= 3;
  24609. ExpectIntEQ(tpp[0], 0xFF);
  24610. ExpectIntEQ(tpp[1], 0x7F);
  24611. ExpectIntEQ(tpp[2], 0xFF);
  24612. }
  24613. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24614. wolfSSL_ASN1_INTEGER_free(a);
  24615. #endif /* OPENSSL_EXTRA && !NO_ASN */
  24616. return EXPECT_RESULT();
  24617. }
  24618. static int test_wolfSSL_ASN1_OBJECT(void)
  24619. {
  24620. EXPECT_DECLS;
  24621. #if defined(OPENSSL_EXTRA)
  24622. ASN1_OBJECT* a = NULL;
  24623. ASN1_OBJECT s;
  24624. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  24625. /* Invalid parameter testing. */
  24626. ASN1_OBJECT_free(NULL);
  24627. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  24628. /* Test that a static ASN1_OBJECT can be freed. */
  24629. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  24630. ASN1_OBJECT_free(&s);
  24631. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  24632. ASN1_OBJECT_free(a);
  24633. a = NULL;
  24634. s.obj = der;
  24635. s.objSz = sizeof(der);
  24636. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  24637. ASN1_OBJECT_free(a);
  24638. ASN1_OBJECT_free(&s);
  24639. #endif /* OPENSSL_EXTRA */
  24640. return EXPECT_RESULT();
  24641. }
  24642. static int test_wolfSSL_ASN1_get_object(void)
  24643. {
  24644. EXPECT_DECLS;
  24645. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  24646. const unsigned char* derBuf = cliecc_cert_der_256;
  24647. const unsigned char* nullPtr = NULL;
  24648. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  24649. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  24650. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  24651. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  24652. const unsigned char* p;
  24653. unsigned char objDer[8];
  24654. unsigned char* der;
  24655. unsigned char* derPtr;
  24656. int len = sizeof_cliecc_cert_der_256;
  24657. long asnLen = 0;
  24658. int tag = 0;
  24659. int cls = 0;
  24660. ASN1_OBJECT* a = NULL;
  24661. ASN1_OBJECT s;
  24662. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  24663. /* Invalid encoding at length. */
  24664. p = objDerInvalidLen;
  24665. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  24666. 0x80);
  24667. p = objDerBadLen;
  24668. /* Error = 0x80, Constructed = 0x20 */
  24669. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  24670. 0x80 | 0x20);
  24671. /* Read a couple TLV triplets and make sure they match the expected values
  24672. */
  24673. /* SEQUENCE */
  24674. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  24675. ExpectIntEQ(asnLen, 862);
  24676. ExpectIntEQ(tag, 0x10);
  24677. ExpectIntEQ(cls, 0);
  24678. /* SEQUENCE */
  24679. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24680. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24681. ExpectIntEQ(asnLen, 772);
  24682. ExpectIntEQ(tag, 0x10);
  24683. ExpectIntEQ(cls, 0);
  24684. /* [0] */
  24685. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24686. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24687. ExpectIntEQ(asnLen, 3);
  24688. ExpectIntEQ(tag, 0);
  24689. ExpectIntEQ(cls, 0x80);
  24690. /* INTEGER */
  24691. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24692. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24693. ExpectIntEQ(asnLen, 1);
  24694. ExpectIntEQ(tag, 0x2);
  24695. ExpectIntEQ(cls, 0);
  24696. derBuf += asnLen;
  24697. /* INTEGER */
  24698. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24699. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24700. ExpectIntEQ(asnLen, 20);
  24701. ExpectIntEQ(tag, 0x2);
  24702. ExpectIntEQ(cls, 0);
  24703. derBuf += asnLen;
  24704. /* SEQUENCE */
  24705. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  24706. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  24707. ExpectIntEQ(asnLen, 10);
  24708. ExpectIntEQ(tag, 0x10);
  24709. ExpectIntEQ(cls, 0);
  24710. /* Found OBJECT_ID. */
  24711. /* Invalid parameter testing. */
  24712. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  24713. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  24714. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  24715. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  24716. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  24717. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  24718. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  24719. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  24720. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  24721. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  24722. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  24723. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  24724. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  24725. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  24726. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  24727. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  24728. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  24729. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  24730. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  24731. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  24732. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  24733. /* Invalid encoding at length. */
  24734. p = objDerInvalidLen;
  24735. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  24736. p = objDerBadLen;
  24737. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  24738. p = objDerNotObj;
  24739. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  24740. p = objDerNoData;
  24741. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  24742. /* Create an ASN OBJECT from content */
  24743. p = derBuf + 2;
  24744. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  24745. ASN1_OBJECT_free(a);
  24746. a = NULL;
  24747. /* Create an ASN OBJECT from DER */
  24748. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  24749. /* Invalid parameter testing. */
  24750. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  24751. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  24752. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  24753. der = NULL;
  24754. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  24755. derPtr = objDer;
  24756. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  24757. ExpectPtrNE(derPtr, objDer);
  24758. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  24759. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  24760. ASN1_OBJECT_free(a);
  24761. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  24762. return EXPECT_RESULT();
  24763. }
  24764. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  24765. {
  24766. EXPECT_DECLS;
  24767. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  24768. ASN1_OBJECT* obj = NULL;
  24769. ASN1_OBJECT* a = NULL;
  24770. BIO *bio = NULL;
  24771. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  24772. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  24773. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  24774. const unsigned char* p;
  24775. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  24776. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  24777. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  24778. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  24779. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  24780. /* No DER encoding in ASN1_OBJECT. */
  24781. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  24782. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24783. ASN1_OBJECT_free(a);
  24784. a = NULL;
  24785. /* DER encoding - not OBJECT_ID */
  24786. p = notObjDer;
  24787. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24788. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24789. ASN1_OBJECT_free(a);
  24790. a = NULL;
  24791. /* Bad length encoding. */
  24792. p = badLenDer;
  24793. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24794. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24795. ASN1_OBJECT_free(a);
  24796. a = NULL;
  24797. /* Good encoding - but unknown. */
  24798. p = goodDer;
  24799. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  24800. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  24801. ASN1_OBJECT_free(a);
  24802. BIO_free(bio);
  24803. ASN1_OBJECT_free(obj);
  24804. #endif
  24805. return EXPECT_RESULT();
  24806. }
  24807. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  24808. {
  24809. EXPECT_DECLS;
  24810. #if defined(OPENSSL_EXTRA) && \
  24811. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  24812. char buf[50] = {0};
  24813. ASN1_OBJECT* obj;
  24814. const char* oid = "2.5.29.19";
  24815. const char* ln = "X509v3 Basic Constraints";
  24816. obj = NULL;
  24817. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  24818. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  24819. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  24820. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  24821. XMEMSET(buf, 0, sizeof(buf));
  24822. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  24823. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  24824. ASN1_OBJECT_free(obj);
  24825. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  24826. return EXPECT_RESULT();
  24827. }
  24828. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  24829. {
  24830. EXPECT_DECLS;
  24831. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  24832. WOLFSSL_STACK* sk = NULL;
  24833. WOLFSSL_ASN1_OBJECT* obj;
  24834. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  24835. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24836. wolfSSL_sk_ASN1_OBJECT_free(sk);
  24837. sk = NULL;
  24838. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24839. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  24840. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  24841. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  24842. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  24843. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  24844. sk = NULL;
  24845. /* obj freed in pop_free call. */
  24846. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  24847. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  24848. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  24849. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  24850. wolfSSL_sk_ASN1_OBJECT_free(sk);
  24851. wolfSSL_ASN1_OBJECT_free(obj);
  24852. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  24853. return EXPECT_RESULT();
  24854. }
  24855. static int test_wolfSSL_ASN1_STRING(void)
  24856. {
  24857. EXPECT_DECLS;
  24858. #if defined(OPENSSL_EXTRA)
  24859. ASN1_STRING* str = NULL;
  24860. ASN1_STRING* c = NULL;
  24861. const char data[] = "hello wolfSSL";
  24862. const char data2[] = "Same len data";
  24863. const char longData[] =
  24864. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  24865. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  24866. ASN1_STRING_free(str);
  24867. str = NULL;
  24868. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  24869. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  24870. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  24871. /* Check setting to NULL works. */
  24872. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  24873. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  24874. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  24875. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  24876. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  24877. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  24878. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  24879. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  24880. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  24881. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  24882. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  24883. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  24884. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  24885. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  24886. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  24887. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  24888. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  24889. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  24890. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  24891. /* Check setting back to smaller size frees dynamic data. */
  24892. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  24893. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  24894. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  24895. ExpectNull(ASN1_STRING_get0_data(NULL));
  24896. ExpectNotNull(ASN1_STRING_get0_data(str));
  24897. ExpectNull(ASN1_STRING_data(NULL));
  24898. ExpectNotNull(ASN1_STRING_data(str));
  24899. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  24900. ExpectIntGT(ASN1_STRING_length(str), 0);
  24901. ASN1_STRING_free(c);
  24902. ASN1_STRING_free(str);
  24903. ASN1_STRING_free(NULL);
  24904. #ifndef NO_WOLFSSL_STUB
  24905. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  24906. #endif
  24907. #endif
  24908. return EXPECT_RESULT();
  24909. }
  24910. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  24911. {
  24912. EXPECT_DECLS;
  24913. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  24914. !defined(NO_FILESYSTEM)
  24915. WOLFSSL_X509* x509 = NULL;
  24916. WOLFSSL_X509_NAME* subject = NULL;
  24917. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  24918. WOLFSSL_ASN1_STRING* a = NULL;
  24919. FILE* file = XBADFILE;
  24920. int idx = 0;
  24921. char targetOutput[16] = "www.wolfssl.com";
  24922. unsigned char* actual_output = NULL;
  24923. int len = 0;
  24924. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  24925. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  24926. if (file != NULL)
  24927. fclose(file);
  24928. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  24929. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  24930. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  24931. NID_commonName, -1)), 5);
  24932. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  24933. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  24934. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  24935. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  24936. a = NULL;
  24937. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  24938. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  24939. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  24940. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  24941. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  24942. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  24943. wolfSSL_X509_free(x509);
  24944. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24945. ExpectNotNull(a = ASN1_STRING_new());
  24946. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  24947. ASN1_STRING_free(a);
  24948. #endif
  24949. return EXPECT_RESULT();
  24950. }
  24951. static int test_wolfSSL_i2s_ASN1_STRING(void)
  24952. {
  24953. EXPECT_DECLS;
  24954. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  24955. WOLFSSL_ASN1_STRING* str = NULL;
  24956. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  24957. char* ret = NULL;
  24958. ExpectNotNull(str = ASN1_STRING_new());
  24959. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  24960. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24961. ret = NULL;
  24962. /* No data. */
  24963. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  24964. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24965. ret = NULL;
  24966. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  24967. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  24968. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24969. ret = NULL;
  24970. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  24971. /* No type. */
  24972. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  24973. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24974. ASN1_STRING_free(str);
  24975. #endif
  24976. return EXPECT_RESULT();
  24977. }
  24978. static int test_wolfSSL_ASN1_STRING_canon(void)
  24979. {
  24980. EXPECT_DECLS;
  24981. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  24982. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  24983. defined(OPENSSL_EXTRA_X509_SMALL))
  24984. WOLFSSL_ASN1_STRING* orig = NULL;
  24985. WOLFSSL_ASN1_STRING* canon = NULL;
  24986. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  24987. const char* whitespaceOnly = "\t\r\n";
  24988. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  24989. const char* canonData = "\x01 \x02 \xff tt";
  24990. const char longData[] =
  24991. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  24992. ExpectNotNull(orig = ASN1_STRING_new());
  24993. ExpectNotNull(canon = ASN1_STRING_new());
  24994. /* Invalid parameter testing. */
  24995. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  24996. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  24997. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  24998. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  24999. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25000. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  25001. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25002. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25003. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  25004. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25005. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25006. ASN1_STRING_free(orig);
  25007. orig = NULL;
  25008. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  25009. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  25010. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25011. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  25012. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25013. ASN1_STRING_free(orig);
  25014. orig = NULL;
  25015. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  25016. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  25017. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  25018. ASN1_STRING_free(orig);
  25019. orig = NULL;
  25020. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  25021. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  25022. ASN1_STRING_free(orig);
  25023. ASN1_STRING_free(canon);
  25024. #endif
  25025. #endif
  25026. return EXPECT_RESULT();
  25027. }
  25028. static int test_wolfSSL_ASN1_STRING_print(void)
  25029. {
  25030. EXPECT_DECLS;
  25031. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  25032. !defined(NO_BIO)
  25033. ASN1_STRING* asnStr = NULL;
  25034. const char HELLO_DATA[]= \
  25035. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  25036. #define MAX_UNPRINTABLE_CHAR 32
  25037. #define MAX_BUF 255
  25038. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  25039. unsigned char expected[sizeof(unprintableData)+1];
  25040. unsigned char rbuf[MAX_BUF];
  25041. BIO *bio = NULL;
  25042. int p_len;
  25043. int i;
  25044. /* setup */
  25045. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  25046. unprintableData[i] = HELLO_DATA[i];
  25047. expected[i] = HELLO_DATA[i];
  25048. }
  25049. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  25050. unprintableData[sizeof(HELLO_DATA)+i] = i;
  25051. if (i == (int)'\n' || i == (int)'\r')
  25052. expected[sizeof(HELLO_DATA)+i] = i;
  25053. else
  25054. expected[sizeof(HELLO_DATA)+i] = '.';
  25055. }
  25056. unprintableData[sizeof(unprintableData)-1] = '\0';
  25057. expected[sizeof(expected)-1] = '\0';
  25058. XMEMSET(rbuf, 0, MAX_BUF);
  25059. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25060. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  25061. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25062. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  25063. (int)sizeof(unprintableData)), 1);
  25064. /* test */
  25065. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  25066. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  25067. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  25068. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  25069. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  25070. ExpectStrEQ((char*)rbuf, (const char*)expected);
  25071. BIO_free(bio);
  25072. bio = NULL;
  25073. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25074. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25075. /* Ensure there is 0 bytes available to write into. */
  25076. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  25077. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25078. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25079. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25080. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  25081. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  25082. BIO_free(bio);
  25083. ASN1_STRING_free(asnStr);
  25084. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  25085. return EXPECT_RESULT();
  25086. }
  25087. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  25088. {
  25089. EXPECT_DECLS;
  25090. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  25091. ASN1_STRING* asn_str = NULL;
  25092. const char data[] = "Hello wolfSSL!";
  25093. ASN1_STRING* esc_str = NULL;
  25094. const char esc_data[] = "a+;<>";
  25095. ASN1_STRING* neg_int = NULL;
  25096. const char neg_int_data[] = "\xff";
  25097. ASN1_STRING* neg_enum = NULL;
  25098. const char neg_enum_data[] = "\xff";
  25099. BIO *bio = NULL;
  25100. BIO *fixed = NULL;
  25101. unsigned long flags;
  25102. int p_len;
  25103. unsigned char rbuf[255];
  25104. /* setup */
  25105. XMEMSET(rbuf, 0, 255);
  25106. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25107. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  25108. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25109. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25110. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  25111. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25112. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  25113. sizeof(esc_data)), 1);
  25114. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  25115. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  25116. sizeof(neg_int_data) - 1), 1);
  25117. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  25118. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  25119. sizeof(neg_enum_data) - 1), 1);
  25120. /* Invalid parameter testing. */
  25121. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  25122. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  25123. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  25124. /* no flags */
  25125. XMEMSET(rbuf, 0, 255);
  25126. flags = 0;
  25127. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  25128. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  25129. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  25130. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25131. /* Ensure there is 0 bytes available to write into. */
  25132. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25133. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25134. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25135. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25136. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  25137. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25138. /* RFC2253 Escape */
  25139. XMEMSET(rbuf, 0, 255);
  25140. flags = ASN1_STRFLGS_ESC_2253;
  25141. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  25142. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  25143. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  25144. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25145. /* Ensure there is 0 bytes available to write into. */
  25146. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25147. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25148. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25149. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25150. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  25151. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  25152. /* Show type */
  25153. XMEMSET(rbuf, 0, 255);
  25154. flags = ASN1_STRFLGS_SHOW_TYPE;
  25155. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  25156. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  25157. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  25158. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25159. /* Ensure there is 0 bytes available to write into. */
  25160. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25161. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25162. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25163. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25164. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  25165. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25166. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  25167. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25168. /* Dump All */
  25169. XMEMSET(rbuf, 0, 255);
  25170. flags = ASN1_STRFLGS_DUMP_ALL;
  25171. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  25172. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  25173. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  25174. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25175. /* Ensure there is 0 bytes available to write into. */
  25176. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25177. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25178. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25179. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25180. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  25181. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25182. /* Dump Der */
  25183. XMEMSET(rbuf, 0, 255);
  25184. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  25185. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  25186. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  25187. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  25188. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25189. /* Ensure there is 0 bytes available to write into. */
  25190. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  25191. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25192. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25193. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25194. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  25195. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25196. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  25197. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  25198. /* Dump All + Show type */
  25199. XMEMSET(rbuf, 0, 255);
  25200. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25201. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  25202. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  25203. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  25204. /* Dump All + Show type - Negative Integer. */
  25205. XMEMSET(rbuf, 0, 255);
  25206. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25207. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  25208. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  25209. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  25210. /* Dump All + Show type - Negative Enumerated. */
  25211. XMEMSET(rbuf, 0, 255);
  25212. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  25213. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  25214. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  25215. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  25216. BIO_free(fixed);
  25217. BIO_free(bio);
  25218. ASN1_STRING_free(asn_str);
  25219. ASN1_STRING_free(esc_str);
  25220. ASN1_STRING_free(neg_int);
  25221. ASN1_STRING_free(neg_enum);
  25222. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  25223. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  25224. #endif
  25225. return EXPECT_RESULT();
  25226. }
  25227. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  25228. {
  25229. EXPECT_DECLS;
  25230. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  25231. ASN1_STRING* asn1str_test = NULL;
  25232. ASN1_STRING* asn1str_answer = NULL;
  25233. /* Each character is encoded using 4 bytes */
  25234. char input[] = {
  25235. 0, 0, 0, 'T',
  25236. 0, 0, 0, 'e',
  25237. 0, 0, 0, 's',
  25238. 0, 0, 0, 't',
  25239. };
  25240. char output[] = "Test";
  25241. char badInput[] = {
  25242. 1, 0, 0, 'T',
  25243. 0, 1, 0, 'e',
  25244. 0, 0, 1, 's',
  25245. };
  25246. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  25247. /* Test wrong type. */
  25248. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  25249. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25250. ASN1_STRING_free(asn1str_test);
  25251. asn1str_test = NULL;
  25252. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  25253. /* Test bad length. */
  25254. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  25255. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25256. /* Test bad input. */
  25257. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  25258. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25259. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  25260. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25261. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  25262. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  25263. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  25264. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  25265. ExpectNotNull(
  25266. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  25267. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  25268. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  25269. ASN1_STRING_free(asn1str_test);
  25270. ASN1_STRING_free(asn1str_answer);
  25271. #endif /* OPENSSL_ALL && !NO_ASN */
  25272. return EXPECT_RESULT();
  25273. }
  25274. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  25275. {
  25276. EXPECT_DECLS;
  25277. #if defined(OPENSSL_EXTRA)
  25278. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  25279. unsigned char nullstr[32];
  25280. XMEMSET(nullstr, 0, 32);
  25281. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  25282. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  25283. if (asn1_gtime != NULL) {
  25284. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  25285. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  25286. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  25287. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25288. }
  25289. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  25290. #endif /* OPENSSL_EXTRA */
  25291. return EXPECT_RESULT();
  25292. }
  25293. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  25294. {
  25295. EXPECT_DECLS;
  25296. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  25297. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  25298. BIO* bio = NULL;
  25299. unsigned char buf[24];
  25300. int i;
  25301. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25302. BIO_set_write_buf_size(bio, 24);
  25303. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  25304. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  25305. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  25306. /* Type not set. */
  25307. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25308. gtime.type = V_ASN1_GENERALIZEDTIME;
  25309. /* Invalid parameters testing. */
  25310. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  25311. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  25312. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  25313. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  25314. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  25315. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  25316. BIO_free(bio);
  25317. bio = NULL;
  25318. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25319. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  25320. /* Ensure there is 0 bytes available to write into. */
  25321. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  25322. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25323. for (i = 1; i < 20; i++) {
  25324. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  25325. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  25326. }
  25327. BIO_free(bio);
  25328. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  25329. #endif /* OPENSSL_EXTRA */
  25330. return EXPECT_RESULT();
  25331. }
  25332. static int test_wolfSSL_ASN1_TIME(void)
  25333. {
  25334. EXPECT_DECLS;
  25335. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25336. WOLFSSL_ASN1_TIME* asn_time = NULL;
  25337. unsigned char *data;
  25338. ExpectNotNull(asn_time = ASN1_TIME_new());
  25339. #ifndef NO_WOLFSSL_STUB
  25340. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  25341. #endif
  25342. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  25343. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  25344. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  25345. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  25346. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  25347. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  25348. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  25349. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  25350. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  25351. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  25352. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  25353. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  25354. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  25355. ASN1_TIME_free(asn_time);
  25356. ASN1_TIME_free(NULL);
  25357. #endif
  25358. return EXPECT_RESULT();
  25359. }
  25360. static int test_wolfSSL_ASN1_TIME_to_string(void)
  25361. {
  25362. EXPECT_DECLS;
  25363. #ifndef NO_ASN_TIME
  25364. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25365. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  25366. WOLFSSL_ASN1_TIME* t = NULL;
  25367. char buf[ASN_GENERALIZED_TIME_SIZE];
  25368. ExpectNotNull((t = ASN1_TIME_new()));
  25369. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  25370. /* Invalid parameter testing. */
  25371. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  25372. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  25373. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  25374. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  25375. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  25376. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  25377. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  25378. /* Buffer needs to be longer than minimum of 5 characters. */
  25379. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  25380. ASN1_TIME_free(t);
  25381. #endif
  25382. #endif /* NO_ASN_TIME */
  25383. return EXPECT_RESULT();
  25384. }
  25385. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  25386. {
  25387. EXPECT_DECLS;
  25388. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25389. ASN1_TIME* fromTime = NULL;
  25390. ASN1_TIME* closeToTime = NULL;
  25391. ASN1_TIME* toTime = NULL;
  25392. ASN1_TIME* invalidTime = NULL;
  25393. int daysDiff;
  25394. int secsDiff;
  25395. ExpectNotNull((fromTime = ASN1_TIME_new()));
  25396. /* Feb 22, 2003, 21:15:15 */
  25397. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  25398. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  25399. /* Feb 22, 2003, 21:16:15 */
  25400. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  25401. ExpectNotNull((toTime = ASN1_TIME_new()));
  25402. /* Dec 19, 2010, 18:10:11 */
  25403. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  25404. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  25405. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  25406. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  25407. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  25408. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  25409. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25410. /* Error conditions. */
  25411. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  25412. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  25413. /* If both times are NULL, difference is 0. */
  25414. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  25415. ExpectIntEQ(daysDiff, 0);
  25416. ExpectIntEQ(secsDiff, 0);
  25417. /* If one time is NULL, it defaults to the current time. */
  25418. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  25419. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  25420. /* Normal operation. Both times non-NULL. */
  25421. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25422. ExpectIntEQ(daysDiff, 2856);
  25423. ExpectIntEQ(secsDiff, 75296);
  25424. /* Swapping the times should return negative values. */
  25425. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  25426. ExpectIntEQ(daysDiff, -2856);
  25427. ExpectIntEQ(secsDiff, -75296);
  25428. /* Compare with invalid time string. */
  25429. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  25430. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  25431. /* Compare with days difference of 0. */
  25432. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  25433. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  25434. /* Days and seconds differences not 0. */
  25435. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  25436. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  25437. /* Same time. */
  25438. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  25439. /* Compare regression test: No seconds difference, just difference in days.
  25440. */
  25441. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  25442. ASN1_TIME_set_string(toTime, "19800101000000Z");
  25443. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  25444. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  25445. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  25446. /* Edge case with Unix epoch. */
  25447. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  25448. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  25449. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25450. ExpectIntEQ(daysDiff, 3652);
  25451. ExpectIntEQ(secsDiff, 0);
  25452. /* Edge case with year > 2038 (year 2038 problem). */
  25453. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  25454. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  25455. ExpectIntEQ(daysDiff, 2932896);
  25456. ExpectIntEQ(secsDiff, 86399);
  25457. ASN1_TIME_free(fromTime);
  25458. ASN1_TIME_free(closeToTime);
  25459. ASN1_TIME_free(toTime);
  25460. ASN1_TIME_free(invalidTime);
  25461. #endif
  25462. return EXPECT_RESULT();
  25463. }
  25464. static int test_wolfSSL_ASN1_TIME_adj(void)
  25465. {
  25466. EXPECT_DECLS;
  25467. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  25468. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  25469. const int year = 365*24*60*60;
  25470. const int day = 24*60*60;
  25471. const int hour = 60*60;
  25472. const int mini = 60;
  25473. const byte asn_utc_time = ASN_UTC_TIME;
  25474. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  25475. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  25476. #endif
  25477. WOLFSSL_ASN1_TIME* asn_time = NULL;
  25478. WOLFSSL_ASN1_TIME* s = NULL;
  25479. int offset_day;
  25480. long offset_sec;
  25481. char date_str[CTC_DATE_SIZE + 1];
  25482. time_t t;
  25483. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  25484. /* UTC notation test */
  25485. /* 2000/2/15 20:30:00 */
  25486. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  25487. offset_day = 7;
  25488. offset_sec = 45 * mini;
  25489. /* offset_sec = -45 * min;*/
  25490. ExpectNotNull(asn_time =
  25491. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  25492. ExpectTrue(asn_time->type == asn_utc_time);
  25493. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25494. CTC_DATE_SIZE));
  25495. date_str[CTC_DATE_SIZE] = '\0';
  25496. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  25497. /* negative offset */
  25498. offset_sec = -45 * mini;
  25499. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  25500. ExpectNotNull(asn_time);
  25501. ExpectTrue(asn_time->type == asn_utc_time);
  25502. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25503. CTC_DATE_SIZE));
  25504. date_str[CTC_DATE_SIZE] = '\0';
  25505. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  25506. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  25507. s = NULL;
  25508. XMEMSET(date_str, 0, sizeof(date_str));
  25509. /* Generalized time will overflow time_t if not long */
  25510. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  25511. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  25512. DYNAMIC_TYPE_OPENSSL);
  25513. /* GeneralizedTime notation test */
  25514. /* 2055/03/01 09:00:00 */
  25515. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  25516. offset_day = 12;
  25517. offset_sec = 10 * mini;
  25518. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  25519. offset_sec));
  25520. ExpectTrue(asn_time->type == asn_gen_time);
  25521. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25522. CTC_DATE_SIZE));
  25523. date_str[CTC_DATE_SIZE] = '\0';
  25524. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  25525. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  25526. s = NULL;
  25527. XMEMSET(date_str, 0, sizeof(date_str));
  25528. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  25529. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  25530. s = NULL;
  25531. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  25532. offset_day = 7;
  25533. offset_sec = 45 * mini;
  25534. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  25535. offset_sec));
  25536. ExpectTrue(asn_time->type == asn_utc_time);
  25537. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25538. CTC_DATE_SIZE));
  25539. date_str[CTC_DATE_SIZE] = '\0';
  25540. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  25541. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  25542. asn_time = NULL;
  25543. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  25544. offset_sec));
  25545. ExpectTrue(asn_time->type == asn_utc_time);
  25546. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  25547. CTC_DATE_SIZE));
  25548. date_str[CTC_DATE_SIZE] = '\0';
  25549. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  25550. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  25551. #endif
  25552. return EXPECT_RESULT();
  25553. }
  25554. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  25555. {
  25556. EXPECT_DECLS;
  25557. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25558. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  25559. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  25560. ASN1_TIME asnTime;
  25561. struct tm tm;
  25562. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  25563. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  25564. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  25565. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  25566. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25567. ExpectIntEQ(tm.tm_sec, 15);
  25568. ExpectIntEQ(tm.tm_min, 15);
  25569. ExpectIntEQ(tm.tm_hour, 21);
  25570. ExpectIntEQ(tm.tm_mday, 22);
  25571. ExpectIntEQ(tm.tm_mon, 1);
  25572. ExpectIntEQ(tm.tm_year, 100);
  25573. ExpectIntEQ(tm.tm_isdst, 0);
  25574. #ifdef XMKTIME
  25575. ExpectIntEQ(tm.tm_wday, 2);
  25576. ExpectIntEQ(tm.tm_yday, 52);
  25577. #endif
  25578. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  25579. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25580. ExpectIntEQ(tm.tm_year, 50);
  25581. /* Get current time. */
  25582. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  25583. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  25584. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  25585. /* 0 length. */
  25586. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25587. /* No type. */
  25588. asnTime.length = 1;
  25589. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25590. /* Not UTCTIME length. */
  25591. asnTime.type = V_ASN1_UTCTIME;
  25592. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25593. /* Not GENERALIZEDTIME length. */
  25594. asnTime.type = V_ASN1_GENERALIZEDTIME;
  25595. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25596. /* Not Zulu timezone. */
  25597. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  25598. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25599. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  25600. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  25601. #ifdef XMKTIME
  25602. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  25603. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  25604. ExpectIntEQ(tm.tm_sec, 47);
  25605. ExpectIntEQ(tm.tm_min, 22);
  25606. ExpectIntEQ(tm.tm_hour, 21);
  25607. ExpectIntEQ(tm.tm_mday, 12);
  25608. ExpectIntEQ(tm.tm_mon, 4);
  25609. ExpectIntEQ(tm.tm_year, 123);
  25610. ExpectIntEQ(tm.tm_wday, 5);
  25611. ExpectIntEQ(tm.tm_yday, 131);
  25612. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  25613. fields are zeroed out as expected. */
  25614. ExpectIntEQ(tm.tm_isdst, 0);
  25615. #endif
  25616. #endif
  25617. return EXPECT_RESULT();
  25618. }
  25619. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  25620. {
  25621. EXPECT_DECLS;
  25622. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  25623. WOLFSSL_ASN1_TIME *t = NULL;
  25624. WOLFSSL_ASN1_TIME *out = NULL;
  25625. WOLFSSL_ASN1_TIME *gtime = NULL;
  25626. int tlen = 0;
  25627. unsigned char *data = NULL;
  25628. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  25629. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  25630. /* type not set. */
  25631. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25632. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25633. t = NULL;
  25634. /* UTC Time test */
  25635. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  25636. if (t != NULL) {
  25637. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  25638. t->type = ASN_UTC_TIME;
  25639. t->length = ASN_UTC_TIME_SIZE;
  25640. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  25641. }
  25642. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  25643. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  25644. out = NULL;
  25645. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25646. wolfSSL_ASN1_TIME_free(gtime);
  25647. gtime = NULL;
  25648. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  25649. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25650. ExpectPtrEq(gtime, out);
  25651. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25652. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25653. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25654. /* Generalized Time test */
  25655. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25656. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  25657. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  25658. if (t != NULL) {
  25659. t->type = ASN_GENERALIZED_TIME;
  25660. t->length = ASN_GENERALIZED_TIME_SIZE;
  25661. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  25662. }
  25663. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  25664. ASN_GENERALIZED_TIME_SIZE);
  25665. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  25666. "20050727123456Z");
  25667. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25668. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25669. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25670. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25671. /* UTC Time to Generalized Time 1900's test */
  25672. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25673. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  25674. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  25675. if (t != NULL) {
  25676. t->type = ASN_UTC_TIME;
  25677. t->length = ASN_UTC_TIME_SIZE;
  25678. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  25679. }
  25680. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  25681. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25682. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25683. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  25684. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25685. /* Null parameter test */
  25686. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  25687. gtime = NULL;
  25688. out = NULL;
  25689. if (t != NULL) {
  25690. t->type = ASN_UTC_TIME;
  25691. t->length = ASN_UTC_TIME_SIZE;
  25692. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  25693. }
  25694. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  25695. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  25696. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  25697. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  25698. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25699. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25700. #endif
  25701. return EXPECT_RESULT();
  25702. }
  25703. static int test_wolfSSL_ASN1_TIME_print(void)
  25704. {
  25705. EXPECT_DECLS;
  25706. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  25707. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  25708. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  25709. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  25710. !defined(NO_ASN_TIME)
  25711. BIO* bio = NULL;
  25712. BIO* fixed = NULL;
  25713. X509* x509 = NULL;
  25714. const unsigned char* der = client_cert_der_2048;
  25715. ASN1_TIME* notAfter;
  25716. ASN1_TIME* notBefore;
  25717. unsigned char buf[25];
  25718. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25719. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  25720. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  25721. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  25722. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  25723. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  25724. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  25725. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  25726. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  25727. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  25728. ExpectIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
  25729. /* Test BIO_write fails. */
  25730. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25731. /* Ensure there is 0 bytes available to write into. */
  25732. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  25733. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25734. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  25735. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25736. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  25737. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  25738. /* create a bad time and test results */
  25739. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  25740. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  25741. if (EXPECT_SUCCESS()) {
  25742. notAfter->data[8] = 0;
  25743. notAfter->data[3] = 0;
  25744. }
  25745. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  25746. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  25747. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  25748. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  25749. BIO_free(bio);
  25750. BIO_free(fixed);
  25751. X509_free(x509);
  25752. #endif
  25753. return EXPECT_RESULT();
  25754. }
  25755. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  25756. {
  25757. EXPECT_DECLS;
  25758. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  25759. BIO* bio = NULL;
  25760. ASN1_UTCTIME* utc = NULL;
  25761. unsigned char buf[25];
  25762. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  25763. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  25764. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  25765. /* Valid date */
  25766. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25767. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  25768. DYNAMIC_TYPE_ASN1));
  25769. if (utc != NULL) {
  25770. utc->type = ASN_UTC_TIME;
  25771. utc->length = ASN_UTC_TIME_SIZE;
  25772. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  25773. }
  25774. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  25775. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  25776. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  25777. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  25778. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  25779. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  25780. XMEMSET(buf, 0, sizeof(buf));
  25781. BIO_free(bio);
  25782. bio = NULL;
  25783. /* Invalid format */
  25784. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  25785. if (utc != NULL) {
  25786. utc->type = ASN_UTC_TIME;
  25787. utc->length = ASN_UTC_TIME_SIZE;
  25788. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  25789. }
  25790. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  25791. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  25792. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  25793. /* Invalid type */
  25794. if (utc != NULL) {
  25795. utc->type = ASN_GENERALIZED_TIME;
  25796. utc->length = ASN_GENERALIZED_TIME_SIZE;
  25797. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  25798. }
  25799. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  25800. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  25801. BIO_free(bio);
  25802. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  25803. return EXPECT_RESULT();
  25804. }
  25805. static int test_wolfSSL_ASN1_TYPE(void)
  25806. {
  25807. EXPECT_DECLS;
  25808. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  25809. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  25810. WOLFSSL_ASN1_TYPE* t = NULL;
  25811. WOLFSSL_ASN1_OBJECT* obj = NULL;
  25812. #ifndef NO_ASN_TIME
  25813. WOLFSSL_ASN1_TIME* time = NULL;
  25814. #endif
  25815. WOLFSSL_ASN1_STRING* str = NULL;
  25816. unsigned char data[] = { 0x00 };
  25817. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  25818. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25819. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  25820. wolfSSL_ASN1_TYPE_free(t);
  25821. t = NULL;
  25822. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25823. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  25824. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  25825. wolfSSL_ASN1_TYPE_free(t);
  25826. t = NULL;
  25827. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25828. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  25829. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  25830. wolfSSL_ASN1_TYPE_free(t);
  25831. t = NULL;
  25832. #ifndef NO_ASN_TIME
  25833. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25834. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  25835. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  25836. wolfSSL_ASN1_TYPE_free(t);
  25837. t = NULL;
  25838. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25839. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  25840. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  25841. wolfSSL_ASN1_TYPE_free(t);
  25842. t = NULL;
  25843. #endif
  25844. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25845. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25846. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  25847. wolfSSL_ASN1_TYPE_free(t);
  25848. t = NULL;
  25849. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25850. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25851. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  25852. wolfSSL_ASN1_TYPE_free(t);
  25853. t = NULL;
  25854. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25855. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25856. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  25857. wolfSSL_ASN1_TYPE_free(t);
  25858. t = NULL;
  25859. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25860. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25861. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  25862. wolfSSL_ASN1_TYPE_free(t);
  25863. t = NULL;
  25864. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25865. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25866. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  25867. wolfSSL_ASN1_TYPE_free(t);
  25868. t = NULL;
  25869. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  25870. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  25871. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  25872. wolfSSL_ASN1_TYPE_free(t);
  25873. t = NULL;
  25874. #endif
  25875. return EXPECT_RESULT();
  25876. }
  25877. /* Testing code used in dpp.c in hostap */
  25878. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  25879. typedef struct {
  25880. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  25881. * as an OID identifying the curve */
  25882. X509_ALGOR *alg;
  25883. /* Compressed format public key per ANSI X9.63 */
  25884. ASN1_BIT_STRING *pub_key;
  25885. } DPP_BOOTSTRAPPING_KEY;
  25886. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  25887. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  25888. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  25889. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  25890. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  25891. typedef struct {
  25892. ASN1_INTEGER *integer;
  25893. } TEST_ASN1;
  25894. ASN1_SEQUENCE(TEST_ASN1) = {
  25895. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  25896. } ASN1_SEQUENCE_END(TEST_ASN1)
  25897. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  25898. typedef struct {
  25899. ASN1_OCTET_STRING *octet_string;
  25900. } TEST_FAIL_ASN1;
  25901. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  25902. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  25903. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  25904. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  25905. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  25906. #endif
  25907. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  25908. {
  25909. EXPECT_DECLS;
  25910. /* Testing code used in dpp.c in hostap */
  25911. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  25912. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  25913. EC_KEY *eckey = NULL;
  25914. EVP_PKEY *key = NULL;
  25915. size_t len;
  25916. unsigned char *der = NULL;
  25917. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  25918. const unsigned char *in = ecc_clikey_der_256;
  25919. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  25920. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  25921. const EC_GROUP *group = NULL;
  25922. const EC_POINT *point = NULL;
  25923. int nid;
  25924. TEST_ASN1 *test_asn1 = NULL;
  25925. TEST_FAIL_ASN1 test_fail_asn1;
  25926. const unsigned char badObjDer[] = { 0x06, 0x00 };
  25927. const unsigned char goodObjDer[] = {
  25928. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  25929. };
  25930. WOLFSSL_ASN1_ITEM emptyTemplate;
  25931. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  25932. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  25933. der = NULL;
  25934. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  25935. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  25936. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  25937. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  25938. (long)sizeof_ecc_clikey_der_256));
  25939. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  25940. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  25941. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  25942. nid = EC_GROUP_get_curve_name(group);
  25943. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  25944. group_obj = OBJ_nid2obj(nid);
  25945. if ((ec_obj != NULL) && (group_obj != NULL)) {
  25946. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  25947. group_obj), 1);
  25948. if (EXPECT_SUCCESS()) {
  25949. ec_obj = NULL;
  25950. group_obj = NULL;
  25951. }
  25952. }
  25953. wolfSSL_ASN1_OBJECT_free(group_obj);
  25954. wolfSSL_ASN1_OBJECT_free(ec_obj);
  25955. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  25956. #ifdef HAVE_COMP_KEY
  25957. ExpectIntGT((len = EC_POINT_point2oct(
  25958. group, point, POINT_CONVERSION_COMPRESSED,
  25959. NULL, 0, NULL)), 0);
  25960. #else
  25961. ExpectIntGT((len = EC_POINT_point2oct(
  25962. group, point, POINT_CONVERSION_UNCOMPRESSED,
  25963. NULL, 0, NULL)), 0);
  25964. #endif
  25965. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  25966. #ifdef HAVE_COMP_KEY
  25967. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  25968. der, len-1, NULL), 0);
  25969. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  25970. der, len, NULL), len);
  25971. #else
  25972. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  25973. der, len-1, NULL), 0);
  25974. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  25975. der, len, NULL), len);
  25976. #endif
  25977. if (EXPECT_SUCCESS()) {
  25978. bootstrap->pub_key->data = der;
  25979. bootstrap->pub_key->length = (int)len;
  25980. /* Not actually used */
  25981. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  25982. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  25983. }
  25984. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  25985. der = NULL;
  25986. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  25987. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  25988. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  25989. EVP_PKEY_free(key);
  25990. EC_KEY_free(eckey);
  25991. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  25992. bootstrap = NULL;
  25993. DPP_BOOTSTRAPPING_KEY_free(NULL);
  25994. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  25995. * a NULL and an empty BIT_STRING. */
  25996. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  25997. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  25998. if (EXPECT_SUCCESS()) {
  25999. bootstrap->alg->algorithm->obj = badObjDer;
  26000. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  26001. }
  26002. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  26003. if (EXPECT_SUCCESS()) {
  26004. bootstrap->alg->parameter->type = V_ASN1_NULL;
  26005. bootstrap->alg->parameter->value.ptr = NULL;
  26006. bootstrap->pub_key->data = NULL;
  26007. bootstrap->pub_key->length = 0;
  26008. /* Not actually used */
  26009. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  26010. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  26011. }
  26012. /* Encode with bad OBJECT_ID. */
  26013. der = NULL;
  26014. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  26015. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  26016. if (EXPECT_SUCCESS()) {
  26017. bootstrap->alg->algorithm->obj = goodObjDer;
  26018. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  26019. bootstrap->alg->algorithm->grp = 2;
  26020. }
  26021. der = NULL;
  26022. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  26023. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  26024. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  26025. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  26026. /* Test integer */
  26027. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  26028. der = NULL;
  26029. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  26030. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  26031. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  26032. TEST_ASN1_free(test_asn1);
  26033. /* Test integer cases. */
  26034. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  26035. TEST_ASN1_free(NULL);
  26036. /* Test error cases. */
  26037. ExpectNull(TEST_FAIL_ASN1_new());
  26038. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  26039. TEST_FAIL_ASN1_free(NULL);
  26040. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  26041. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  26042. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  26043. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  26044. return EXPECT_RESULT();
  26045. }
  26046. static int test_wolfSSL_lhash(void)
  26047. {
  26048. EXPECT_DECLS;
  26049. #ifdef OPENSSL_ALL
  26050. const char testStr[] = "Like a true nature's child\n"
  26051. "We were born\n"
  26052. "Born to be wild";
  26053. #ifdef NO_SHA
  26054. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  26055. #else
  26056. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26057. #endif
  26058. #endif
  26059. return EXPECT_RESULT();
  26060. }
  26061. static int test_wolfSSL_X509_NAME(void)
  26062. {
  26063. EXPECT_DECLS;
  26064. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26065. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26066. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26067. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26068. defined(OPENSSL_EXTRA))
  26069. X509* x509 = NULL;
  26070. const unsigned char* c;
  26071. unsigned char buf[4096];
  26072. int bytes;
  26073. XFILE f = XBADFILE;
  26074. const X509_NAME* a = NULL;
  26075. const X509_NAME* b = NULL;
  26076. X509_NAME* d2i_name = NULL;
  26077. int sz = 0;
  26078. unsigned char* tmp;
  26079. char file[] = "./certs/ca-cert.der";
  26080. #ifndef OPENSSL_EXTRA_X509_SMALL
  26081. byte empty[] = { /* CN=empty emailAddress= */
  26082. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26083. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26084. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26085. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26086. 0x01, 0x16, 0x00
  26087. };
  26088. #endif
  26089. #ifndef OPENSSL_EXTRA_X509_SMALL
  26090. /* test compile of deprecated function, returns 0 */
  26091. ExpectIntEQ(CRYPTO_thread_id(), 0);
  26092. #endif
  26093. ExpectNotNull(a = X509_NAME_new());
  26094. X509_NAME_free((X509_NAME*)a);
  26095. a = NULL;
  26096. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  26097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  26098. if (f != XBADFILE)
  26099. XFCLOSE(f);
  26100. c = buf;
  26101. ExpectNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26102. /* test cmp function */
  26103. ExpectNotNull(a = X509_get_issuer_name(x509));
  26104. ExpectNotNull(b = X509_get_subject_name(x509));
  26105. #ifndef OPENSSL_EXTRA_X509_SMALL
  26106. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26107. #endif
  26108. tmp = buf;
  26109. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26110. if (sz > 0 && tmp == buf) {
  26111. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  26112. __LINE__);
  26113. fprintf(stderr, " Expected pointer to be incremented\n");
  26114. abort();
  26115. }
  26116. #ifndef OPENSSL_EXTRA_X509_SMALL
  26117. tmp = buf;
  26118. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26119. #endif
  26120. /* if output parameter is NULL, should still return required size. */
  26121. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26122. /* retry but with the function creating a buffer */
  26123. tmp = NULL;
  26124. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26125. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26126. tmp = NULL;
  26127. #ifdef WOLFSSL_CERT_NAME_ALL
  26128. /* test for givenName and name */
  26129. {
  26130. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  26131. const byte gName[] = "test-given-name";
  26132. const byte name[] = "test-name";
  26133. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  26134. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  26135. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  26136. 1);
  26137. wolfSSL_X509_NAME_ENTRY_free(entry);
  26138. entry = NULL;
  26139. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  26140. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  26141. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  26142. 1);
  26143. wolfSSL_X509_NAME_ENTRY_free(entry);
  26144. tmp = NULL;
  26145. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26146. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26147. }
  26148. #endif
  26149. b = NULL;
  26150. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26151. #ifndef OPENSSL_EXTRA_X509_SMALL
  26152. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  26153. #endif
  26154. X509_NAME_free((X509_NAME*)b);
  26155. X509_NAME_free(d2i_name);
  26156. d2i_name = NULL;
  26157. X509_free(x509);
  26158. #ifndef OPENSSL_EXTRA_X509_SMALL
  26159. /* test with an empty domain component */
  26160. tmp = empty;
  26161. sz = sizeof(empty);
  26162. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26163. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26164. /* size of empty emailAddress will be 0 */
  26165. tmp = buf;
  26166. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26167. (char*)tmp, sizeof(buf)), 0);
  26168. /* should contain no organization name */
  26169. tmp = buf;
  26170. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26171. (char*)tmp, sizeof(buf)), -1);
  26172. X509_NAME_free(d2i_name);
  26173. #endif
  26174. #endif
  26175. return EXPECT_RESULT();
  26176. }
  26177. static int test_wolfSSL_X509_NAME_hash(void)
  26178. {
  26179. EXPECT_DECLS;
  26180. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  26181. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26182. BIO* bio = NULL;
  26183. X509* x509 = NULL;
  26184. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  26185. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26186. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26187. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26188. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26189. X509_free(x509);
  26190. BIO_free(bio);
  26191. #endif
  26192. return EXPECT_RESULT();
  26193. }
  26194. static int test_wolfSSL_X509_NAME_print_ex(void)
  26195. {
  26196. EXPECT_DECLS;
  26197. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26198. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26199. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26200. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26201. !defined(NO_BIO) && !defined(NO_RSA)
  26202. int memSz;
  26203. byte* mem = NULL;
  26204. BIO* bio = NULL;
  26205. BIO* membio = NULL;
  26206. X509* x509 = NULL;
  26207. X509_NAME* name = NULL;
  26208. const char* expNormal = "C=US, CN=wolfssl.com";
  26209. const char* expReverse = "CN=wolfssl.com, C=US";
  26210. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26211. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26212. const char* expRFC5523 =
  26213. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26214. /* Test with real cert (svrCertFile) first */
  26215. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  26216. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26217. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26218. ExpectNotNull(name = X509_get_subject_name(x509));
  26219. /* Test without flags */
  26220. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26221. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26222. BIO_free(membio);
  26223. membio = NULL;
  26224. /* Test flag: XN_FLAG_RFC2253 */
  26225. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26226. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26227. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26228. BIO_free(membio);
  26229. membio = NULL;
  26230. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26231. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26232. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26233. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26234. BIO_free(membio);
  26235. membio = NULL;
  26236. X509_free(x509);
  26237. BIO_free(bio);
  26238. name = NULL;
  26239. /* Test normal case without escaped characters */
  26240. {
  26241. /* Create name: "/C=US/CN=wolfssl.com" */
  26242. ExpectNotNull(name = X509_NAME_new());
  26243. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26244. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26245. WOLFSSL_SUCCESS);
  26246. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26247. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26248. WOLFSSL_SUCCESS);
  26249. /* Test without flags */
  26250. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26251. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26252. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26253. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  26254. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26255. BIO_free(membio);
  26256. membio = NULL;
  26257. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26258. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26259. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26260. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26261. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26262. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  26263. BIO_free(membio);
  26264. membio = NULL;
  26265. /* Test flags: XN_FLAG_DN_REV - reversed */
  26266. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26267. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26268. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26269. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26270. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  26271. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26272. BIO_free(membio);
  26273. membio = NULL;
  26274. X509_NAME_free(name);
  26275. name = NULL;
  26276. }
  26277. /* Test RFC2253 characters are escaped with backslashes */
  26278. {
  26279. ExpectNotNull(name = X509_NAME_new());
  26280. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26281. /* space at beginning and end, and: ,+"\ */
  26282. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26283. WOLFSSL_SUCCESS);
  26284. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26285. /* # at beginning, and: <>;*/
  26286. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26287. WOLFSSL_SUCCESS);
  26288. /* Test without flags */
  26289. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26290. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26291. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26292. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  26293. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26294. XSTRLEN(expNotEscaped)), 0);
  26295. BIO_free(membio);
  26296. membio = NULL;
  26297. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26298. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26299. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26300. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26301. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26302. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  26303. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26304. BIO_free(membio);
  26305. membio = NULL;
  26306. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26307. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  26308. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  26309. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26310. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26311. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26312. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26313. XSTRLEN(expNotEscapedRev)), 0);
  26314. BIO_free(membio);
  26315. X509_NAME_free(name);
  26316. }
  26317. #endif
  26318. return EXPECT_RESULT();
  26319. }
  26320. #ifndef NO_BIO
  26321. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26322. {
  26323. EXPECT_DECLS;
  26324. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26325. STACK_OF(X509_INFO) *info_stack = NULL;
  26326. X509_INFO *info = NULL;
  26327. int len;
  26328. int i;
  26329. const char* files[] = {
  26330. cliCertFile,
  26331. cliKeyFile,
  26332. /* This needs to be the order as svrCertFile contains the
  26333. * intermediate cert as well. */
  26334. svrKeyFile,
  26335. svrCertFile,
  26336. NULL,
  26337. };
  26338. const char** curFile;
  26339. BIO *fileBIO = NULL;
  26340. BIO *concatBIO = NULL;
  26341. byte tmp[FOURK_BUF];
  26342. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26343. * to group objects together. */
  26344. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26345. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  26346. int fileLen;
  26347. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26348. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  26349. if (EXPECT_SUCCESS()) {
  26350. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26351. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  26352. fileLen -= len;
  26353. if (EXPECT_FAIL())
  26354. break;
  26355. }
  26356. /* Make sure we read the entire file */
  26357. ExpectIntEQ(fileLen, 0);
  26358. }
  26359. BIO_free(fileBIO);
  26360. fileBIO = NULL;
  26361. }
  26362. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26363. NULL));
  26364. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  26365. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26366. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  26367. ExpectNotNull(info->x509);
  26368. ExpectNull(info->crl);
  26369. if (i != 0) {
  26370. ExpectNotNull(info->x_pkey);
  26371. ExpectIntEQ(X509_check_private_key(info->x509,
  26372. info->x_pkey->dec_pkey), 1);
  26373. }
  26374. else {
  26375. ExpectNull(info->x_pkey);
  26376. }
  26377. }
  26378. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26379. BIO_free(concatBIO);
  26380. #endif
  26381. return EXPECT_RESULT();
  26382. }
  26383. #endif
  26384. #ifndef NO_BIO
  26385. static int test_wolfSSL_X509_INFO(void)
  26386. {
  26387. EXPECT_DECLS;
  26388. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26389. STACK_OF(X509_INFO) *info_stack = NULL;
  26390. X509_INFO *info = NULL;
  26391. BIO *cert = NULL;
  26392. int i;
  26393. /* PEM in hex format to avoid null terminator */
  26394. byte data[] = {
  26395. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  26396. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  26397. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  26398. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  26399. 0x2d, 0x2d, 0x2d
  26400. };
  26401. /* PEM in hex format to avoid null terminator */
  26402. byte data2[] = {
  26403. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  26404. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  26405. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  26406. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  26407. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  26408. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  26409. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  26410. };
  26411. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26412. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26413. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26414. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  26415. ExpectNotNull(info->x509);
  26416. ExpectNull(info->crl);
  26417. ExpectNull(info->x_pkey);
  26418. }
  26419. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26420. info_stack = NULL;
  26421. BIO_free(cert);
  26422. cert = NULL;
  26423. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26424. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26425. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26426. info_stack = NULL;
  26427. BIO_free(cert);
  26428. cert = NULL;
  26429. /* This case should fail due to invalid input. */
  26430. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  26431. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  26432. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26433. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26434. info_stack = NULL;
  26435. BIO_free(cert);
  26436. cert = NULL;
  26437. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  26438. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  26439. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26440. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26441. BIO_free(cert);
  26442. #endif
  26443. return EXPECT_RESULT();
  26444. }
  26445. #endif
  26446. static int test_wolfSSL_X509_subject_name_hash(void)
  26447. {
  26448. EXPECT_DECLS;
  26449. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26450. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26451. X509* x509 = NULL;
  26452. X509_NAME* subjectName = NULL;
  26453. unsigned long ret1 = 0;
  26454. unsigned long ret2 = 0;
  26455. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26456. SSL_FILETYPE_PEM));
  26457. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  26458. /* These two
  26459. * - X509_subject_name_hash(x509)
  26460. * - X509_NAME_hash(X509_get_subject_name(x509))
  26461. * should give the same hash, if !defined(NO_SHA) is true. */
  26462. ret1 = X509_subject_name_hash(x509);
  26463. ExpectIntNE(ret1, 0);
  26464. #if !defined(NO_SHA)
  26465. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  26466. ExpectIntNE(ret2, 0);
  26467. ExpectIntEQ(ret1, ret2);
  26468. #else
  26469. (void) ret2;
  26470. #endif
  26471. X509_free(x509);
  26472. #endif
  26473. return EXPECT_RESULT();
  26474. }
  26475. static int test_wolfSSL_X509_issuer_name_hash(void)
  26476. {
  26477. EXPECT_DECLS;
  26478. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26479. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26480. X509* x509 = NULL;
  26481. X509_NAME* issuertName = NULL;
  26482. unsigned long ret1 = 0;
  26483. unsigned long ret2 = 0;
  26484. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26485. SSL_FILETYPE_PEM));
  26486. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  26487. /* These two
  26488. * - X509_issuer_name_hash(x509)
  26489. * - X509_NAME_hash(X509_get_issuer_name(x509))
  26490. * should give the same hash, if !defined(NO_SHA) is true. */
  26491. ret1 = X509_issuer_name_hash(x509);
  26492. ExpectIntNE(ret1, 0);
  26493. #if !defined(NO_SHA)
  26494. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  26495. ExpectIntNE(ret2, 0);
  26496. ExpectIntEQ(ret1, ret2);
  26497. #else
  26498. (void) ret2;
  26499. #endif
  26500. X509_free(x509);
  26501. #endif
  26502. return EXPECT_RESULT();
  26503. }
  26504. static int test_wolfSSL_X509_check_host(void)
  26505. {
  26506. EXPECT_DECLS;
  26507. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26508. && !defined(NO_SHA) && !defined(NO_RSA)
  26509. X509* x509 = NULL;
  26510. const char altName[] = "example.com";
  26511. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26512. SSL_FILETYPE_PEM));
  26513. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  26514. WOLFSSL_SUCCESS);
  26515. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  26516. WOLFSSL_FAILURE);
  26517. X509_free(x509);
  26518. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  26519. WOLFSSL_FAILURE);
  26520. #endif
  26521. return EXPECT_RESULT();
  26522. }
  26523. static int test_wolfSSL_X509_check_email(void)
  26524. {
  26525. EXPECT_DECLS;
  26526. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  26527. X509* x509 = NULL;
  26528. const char goodEmail[] = "info@wolfssl.com";
  26529. const char badEmail[] = "disinfo@wolfssl.com";
  26530. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26531. SSL_FILETYPE_PEM));
  26532. /* Should fail on non-matching email address */
  26533. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  26534. WOLFSSL_FAILURE);
  26535. /* Should succeed on matching email address */
  26536. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  26537. WOLFSSL_SUCCESS);
  26538. /* Should compute length internally when not provided */
  26539. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  26540. WOLFSSL_SUCCESS);
  26541. /* Should fail when email address is NULL */
  26542. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  26543. WOLFSSL_FAILURE);
  26544. X509_free(x509);
  26545. /* Should fail when x509 is NULL */
  26546. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  26547. WOLFSSL_FAILURE);
  26548. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  26549. return EXPECT_RESULT();
  26550. }
  26551. static int test_wolfSSL_DES(void)
  26552. {
  26553. EXPECT_DECLS;
  26554. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26555. const_DES_cblock myDes;
  26556. DES_cblock iv;
  26557. DES_key_schedule key;
  26558. word32 i;
  26559. DES_LONG dl;
  26560. unsigned char msg[] = "hello wolfssl";
  26561. DES_check_key(1);
  26562. DES_set_key(&myDes, &key);
  26563. /* check, check of odd parity */
  26564. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  26565. myDes[0] = 6; /* set even parity */
  26566. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26567. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  26568. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  26569. /* set odd parity for success case */
  26570. DES_set_odd_parity(&myDes);
  26571. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  26572. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  26573. myDes[3]);
  26574. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  26575. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26576. ExpectIntEQ(key[i], myDes[i]);
  26577. }
  26578. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  26579. /* check weak key */
  26580. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  26581. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26582. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  26583. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  26584. /* now do unchecked copy of a weak key over */
  26585. DES_set_key_unchecked(&myDes, &key);
  26586. /* compare arrays, should be the same */
  26587. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26588. ExpectIntEQ(key[i], myDes[i]);
  26589. }
  26590. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  26591. /* check DES_key_sched API */
  26592. XMEMSET(key, 1, sizeof(DES_key_schedule));
  26593. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  26594. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  26595. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  26596. /* compare arrays, should be the same */
  26597. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26598. ExpectIntEQ(key[i], myDes[i]);
  26599. }
  26600. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  26601. * DES_cbc_encrypt on the input */
  26602. XMEMSET(iv, 0, sizeof(DES_cblock));
  26603. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  26604. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  26605. ExpectIntEQ(dl, 480052723);
  26606. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26607. return EXPECT_RESULT();
  26608. }
  26609. static int test_wc_PemToDer(void)
  26610. {
  26611. EXPECT_DECLS;
  26612. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  26613. int ret;
  26614. DerBuffer* pDer = NULL;
  26615. const char* ca_cert = "./certs/server-cert.pem";
  26616. byte* cert_buf = NULL;
  26617. size_t cert_sz = 0;
  26618. int eccKey = 0;
  26619. EncryptedInfo info;
  26620. XMEMSET(&info, 0, sizeof(info));
  26621. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  26622. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  26623. &info, &eccKey), 0);
  26624. wc_FreeDer(&pDer);
  26625. pDer = NULL;
  26626. if (cert_buf != NULL) {
  26627. free(cert_buf);
  26628. cert_buf = NULL;
  26629. }
  26630. #ifdef HAVE_ECC
  26631. {
  26632. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  26633. byte key_buf[256] = {0};
  26634. /* Test fail of loading a key with cert type */
  26635. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  26636. key_buf[0] = '\n';
  26637. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  26638. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  26639. &pDer, NULL, &info, &eccKey)), 0);
  26640. #ifdef OPENSSL_EXTRA
  26641. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  26642. &pDer, NULL, &info, &eccKey)), 0);
  26643. #endif
  26644. wc_FreeDer(&pDer);
  26645. if (cert_buf != NULL)
  26646. free(cert_buf);
  26647. }
  26648. #endif
  26649. #endif
  26650. return EXPECT_RESULT();
  26651. }
  26652. static int test_wc_AllocDer(void)
  26653. {
  26654. EXPECT_DECLS;
  26655. #if !defined(NO_CERTS)
  26656. DerBuffer* pDer = NULL;
  26657. word32 testSize = 1024;
  26658. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  26659. BAD_FUNC_ARG);
  26660. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  26661. ExpectNotNull(pDer);
  26662. wc_FreeDer(&pDer);
  26663. #endif
  26664. return EXPECT_RESULT();
  26665. }
  26666. static int test_wc_CertPemToDer(void)
  26667. {
  26668. EXPECT_DECLS;
  26669. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  26670. const char* ca_cert = "./certs/ca-cert.pem";
  26671. byte* cert_buf = NULL;
  26672. size_t cert_sz = 0;
  26673. size_t cert_dersz = 0;
  26674. byte* cert_der = NULL;
  26675. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  26676. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26677. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26678. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  26679. (int)cert_dersz, CERT_TYPE), 0);
  26680. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  26681. BAD_FUNC_ARG);
  26682. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  26683. BAD_FUNC_ARG);
  26684. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  26685. BAD_FUNC_ARG);
  26686. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  26687. CERT_TYPE), BAD_FUNC_ARG);
  26688. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  26689. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  26690. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  26691. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  26692. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  26693. CERT_TYPE), BAD_FUNC_ARG);
  26694. if (cert_der != NULL)
  26695. free(cert_der);
  26696. if (cert_buf != NULL)
  26697. free(cert_buf);
  26698. #endif
  26699. return EXPECT_RESULT();
  26700. }
  26701. static int test_wc_KeyPemToDer(void)
  26702. {
  26703. EXPECT_DECLS;
  26704. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26705. int ret;
  26706. const byte cert_buf[] = \
  26707. "-----BEGIN PRIVATE KEY-----\n"
  26708. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  26709. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  26710. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  26711. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  26712. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  26713. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  26714. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  26715. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  26716. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  26717. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  26718. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  26719. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  26720. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  26721. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  26722. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  26723. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  26724. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  26725. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  26726. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  26727. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  26728. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  26729. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  26730. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  26731. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  26732. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  26733. "AQue5o6oVfhgLiJzMdo/77gw\n"
  26734. "-----END PRIVATE KEY-----\n";
  26735. const int cert_sz = sizeof(cert_buf);
  26736. const char cert_pw[] = "password";
  26737. int cert_dersz = 0;
  26738. byte* cert_der = NULL;
  26739. /* Bad arg: Cert buffer is NULL */
  26740. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  26741. BAD_FUNC_ARG);
  26742. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  26743. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  26744. BAD_FUNC_ARG);
  26745. /* Test normal operation */
  26746. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26747. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26748. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  26749. cert_pw), 0);
  26750. ExpectIntLE(ret, cert_sz);
  26751. if (cert_der != NULL) {
  26752. free(cert_der);
  26753. cert_der = NULL;
  26754. }
  26755. /* Test NULL for DER buffer to return needed DER buffer size */
  26756. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  26757. ExpectIntLE(ret, cert_sz);
  26758. if (EXPECT_SUCCESS())
  26759. cert_dersz = ret;
  26760. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26761. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  26762. cert_pw), 0);
  26763. ExpectIntLE(ret, cert_sz);
  26764. if (cert_der != NULL)
  26765. free(cert_der);
  26766. #endif
  26767. return EXPECT_RESULT();
  26768. }
  26769. static int test_wc_PubKeyPemToDer(void)
  26770. {
  26771. EXPECT_DECLS;
  26772. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  26773. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  26774. int ret = 0;
  26775. const char* key = "./certs/ecc-client-keyPub.pem";
  26776. byte* cert_buf = NULL;
  26777. size_t cert_sz = 0, cert_dersz = 0;
  26778. byte* cert_der = NULL;
  26779. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26780. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  26781. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  26782. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26783. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26784. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  26785. (int)cert_dersz), 0);
  26786. if (cert_der != NULL) {
  26787. free(cert_der);
  26788. cert_der = NULL;
  26789. }
  26790. /* Test NULL for DER buffer to return needed DER buffer size */
  26791. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  26792. ExpectIntLE(ret, cert_sz);
  26793. cert_dersz = ret;
  26794. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26795. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  26796. (int)cert_dersz), 0);
  26797. if (cert_der != NULL) {
  26798. free(cert_der);
  26799. }
  26800. if (cert_buf != NULL) {
  26801. free(cert_buf);
  26802. }
  26803. #endif
  26804. return EXPECT_RESULT();
  26805. }
  26806. static int test_wc_PemPubKeyToDer(void)
  26807. {
  26808. EXPECT_DECLS;
  26809. #if !defined(NO_FILESYSTEM) && \
  26810. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  26811. const char* key = "./certs/ecc-client-keyPub.pem";
  26812. size_t cert_dersz = 1024;
  26813. byte* cert_der = NULL;
  26814. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  26815. BAD_FUNC_ARG);
  26816. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26817. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  26818. if (cert_der != NULL) {
  26819. free(cert_der);
  26820. }
  26821. #endif
  26822. return EXPECT_RESULT();
  26823. }
  26824. static int test_wc_GetPubKeyDerFromCert(void)
  26825. {
  26826. EXPECT_DECLS;
  26827. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26828. int ret;
  26829. word32 idx = 0;
  26830. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26831. word32 keyDerSz = (word32)sizeof(keyDer);
  26832. DecodedCert decoded;
  26833. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  26834. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  26835. word32 certBufSz = sizeof(certBuf);
  26836. #endif
  26837. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  26838. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  26839. XFILE fp = XBADFILE;
  26840. #endif
  26841. #ifndef NO_RSA
  26842. RsaKey rsaKey;
  26843. #if defined(USE_CERT_BUFFERS_2048)
  26844. byte* rsaCertDer = (byte*)client_cert_der_2048;
  26845. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  26846. #elif defined(USE_CERT_BUFFERS_1024)
  26847. byte* rsaCertDer = (byte*)client_cert_der_1024;
  26848. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  26849. #else
  26850. unsigned char rsaCertDer[TWOK_BUF];
  26851. word32 rsaCertDerSz;
  26852. #endif
  26853. #endif
  26854. #ifdef HAVE_ECC
  26855. ecc_key eccKey;
  26856. #if defined(USE_CERT_BUFFERS_256)
  26857. byte* eccCert = (byte*)cliecc_cert_der_256;
  26858. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  26859. #else
  26860. unsigned char eccCert[ONEK_BUF];
  26861. word32 eccCertSz;
  26862. XFILE fp2 = XBADFILE;
  26863. #endif
  26864. #endif
  26865. #ifndef NO_RSA
  26866. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  26867. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26868. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  26869. fp), 0);
  26870. if (fp != XBADFILE) {
  26871. XFCLOSE(fp);
  26872. fp = XBADFILE;
  26873. }
  26874. #endif
  26875. /* good test case - RSA DER cert */
  26876. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  26877. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  26878. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  26879. ExpectIntGT(keyDerSz, 0);
  26880. /* sanity check, verify we can import DER public key */
  26881. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26882. ExpectIntEQ(ret, 0);
  26883. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  26884. if (ret == 0) {
  26885. wc_FreeRsaKey(&rsaKey);
  26886. }
  26887. /* test LENGTH_ONLY_E case */
  26888. keyDerSz = 0;
  26889. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  26890. LENGTH_ONLY_E);
  26891. ExpectIntGT(keyDerSz, 0);
  26892. /* bad args: DecodedCert NULL */
  26893. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  26894. /* bad args: output key buff size */
  26895. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  26896. /* bad args: zero size output key buffer */
  26897. keyDerSz = 0;
  26898. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  26899. BAD_FUNC_ARG);
  26900. wc_FreeDecodedCert(&decoded);
  26901. /* Certificate Request Tests */
  26902. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  26903. {
  26904. XMEMSET(certBuf, 0, sizeof(certBuf));
  26905. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  26906. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  26907. if (fp != XBADFILE) {
  26908. XFCLOSE(fp);
  26909. }
  26910. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  26911. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  26912. /* good test case - RSA DER certificate request */
  26913. keyDerSz = sizeof(keyDer);
  26914. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  26915. 0);
  26916. ExpectIntGT(keyDerSz, 0);
  26917. /* sanity check, verify we can import DER public key */
  26918. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26919. ExpectIntEQ(ret, 0);
  26920. idx = 0;
  26921. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  26922. if (ret == 0) {
  26923. wc_FreeRsaKey(&rsaKey);
  26924. }
  26925. wc_FreeDecodedCert(&decoded);
  26926. }
  26927. #endif /* WOLFSSL_CERT_REQ */
  26928. #endif /* NO_RSA */
  26929. #ifdef HAVE_ECC
  26930. #ifndef USE_CERT_BUFFERS_256
  26931. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  26932. XBADFILE);
  26933. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  26934. if (fp2 != XBADFILE) {
  26935. XFCLOSE(fp2);
  26936. }
  26937. #endif
  26938. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  26939. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  26940. /* good test case - ECC */
  26941. XMEMSET(keyDer, 0, sizeof(keyDer));
  26942. keyDerSz = sizeof(keyDer);
  26943. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  26944. ExpectIntGT(keyDerSz, 0);
  26945. /* sanity check, verify we can import DER public key */
  26946. ret = wc_ecc_init(&eccKey);
  26947. ExpectIntEQ(ret, 0);
  26948. idx = 0; /* reset idx to 0, used above in RSA case */
  26949. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  26950. if (ret == 0) {
  26951. wc_ecc_free(&eccKey);
  26952. }
  26953. /* test LENGTH_ONLY_E case */
  26954. keyDerSz = 0;
  26955. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  26956. LENGTH_ONLY_E);
  26957. ExpectIntGT(keyDerSz, 0);
  26958. wc_FreeDecodedCert(&decoded);
  26959. #endif
  26960. #endif /* !NO_RSA || HAVE_ECC */
  26961. return EXPECT_RESULT();
  26962. }
  26963. static int test_wc_CheckCertSigPubKey(void)
  26964. {
  26965. EXPECT_DECLS;
  26966. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26967. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  26968. int ret;
  26969. const char* ca_cert = "./certs/ca-cert.pem";
  26970. byte* cert_buf = NULL;
  26971. size_t cert_sz = 0;
  26972. byte* cert_der = NULL;
  26973. word32 cert_dersz = 0;
  26974. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26975. word32 keyDerSz = (word32)sizeof(keyDer);
  26976. DecodedCert decoded;
  26977. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  26978. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  26979. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  26980. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  26981. (int)cert_dersz, CERT_TYPE), 0);
  26982. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  26983. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  26984. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  26985. ExpectIntGT(keyDerSz, 0);
  26986. /* Good test case. */
  26987. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  26988. keyDerSz, RSAk), 0);
  26989. /* No certificate. */
  26990. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  26991. ECDSAk), BAD_FUNC_ARG);
  26992. /* Bad cert size. */
  26993. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  26994. RSAk), 0);
  26995. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  26996. /* No public key. */
  26997. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  26998. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  26999. /* Bad public key size. */
  27000. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27001. RSAk), BAD_FUNC_ARG);
  27002. /* Wrong aglo. */
  27003. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  27004. keyDerSz, ECDSAk), ASN_PARSE_E);
  27005. wc_FreeDecodedCert(&decoded);
  27006. if (cert_der != NULL)
  27007. free(cert_der);
  27008. if (cert_buf != NULL)
  27009. free(cert_buf);
  27010. #endif
  27011. return EXPECT_RESULT();
  27012. }
  27013. static int test_wolfSSL_certs(void)
  27014. {
  27015. EXPECT_DECLS;
  27016. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27017. !defined(NO_RSA)
  27018. X509* x509ext = NULL;
  27019. #ifdef OPENSSL_ALL
  27020. X509* x509 = NULL;
  27021. WOLFSSL_X509_EXTENSION* ext = NULL;
  27022. ASN1_OBJECT* obj = NULL;
  27023. #endif
  27024. WOLFSSL* ssl = NULL;
  27025. WOLFSSL_CTX* ctx = NULL;
  27026. STACK_OF(ASN1_OBJECT)* sk = NULL;
  27027. ASN1_STRING* asn1_str = NULL;
  27028. AUTHORITY_KEYID* akey = NULL;
  27029. BASIC_CONSTRAINTS* bc = NULL;
  27030. int crit;
  27031. #ifndef NO_WOLFSSL_SERVER
  27032. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27033. #else
  27034. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27035. #endif
  27036. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27037. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27038. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27039. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27040. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27041. #endif
  27042. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27043. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27044. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27045. #endif
  27046. ExpectNotNull(ssl = SSL_new(ctx));
  27047. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27048. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27049. #endif
  27050. #ifdef HAVE_PK_CALLBACKS
  27051. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27052. #endif /* HAVE_PK_CALLBACKS */
  27053. /* create and use x509 */
  27054. #ifdef OPENSSL_ALL
  27055. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27056. WOLFSSL_FILETYPE_PEM));
  27057. #endif
  27058. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  27059. WOLFSSL_FILETYPE_PEM));
  27060. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27061. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27062. /* with loading in a new cert the check on private key should now fail */
  27063. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27064. #endif
  27065. #if defined(USE_CERT_BUFFERS_2048)
  27066. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  27067. (unsigned char*)server_cert_der_2048,
  27068. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27069. #endif
  27070. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27071. /************* Get Digest of Certificate ******************/
  27072. {
  27073. byte digest[64]; /* max digest size */
  27074. word32 digestSz;
  27075. XMEMSET(digest, 0, sizeof(digest));
  27076. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27077. WOLFSSL_SUCCESS);
  27078. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27079. WOLFSSL_SUCCESS);
  27080. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27081. WOLFSSL_FAILURE);
  27082. }
  27083. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27084. /* test and checkout X509 extensions */
  27085. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  27086. NID_basic_constraints, &crit, NULL));
  27087. ExpectIntEQ(crit, 0);
  27088. #ifdef OPENSSL_ALL
  27089. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  27090. X509_EXTENSION_free(ext);
  27091. ext = NULL;
  27092. ExpectNotNull(ext = X509_EXTENSION_new());
  27093. X509_EXTENSION_set_critical(ext, 1);
  27094. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27095. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27096. ASN1_OBJECT_free(obj);
  27097. obj = NULL;
  27098. X509_EXTENSION_free(ext);
  27099. ext = NULL;
  27100. ExpectNotNull(ext = X509_EXTENSION_new());
  27101. X509_EXTENSION_set_critical(ext, 0);
  27102. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27103. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27104. NULL);
  27105. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27106. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27107. * and X509_get_ext_d2i has created new */
  27108. asn1_str = NULL;
  27109. X509_EXTENSION_free(ext);
  27110. ext = NULL;
  27111. #endif
  27112. BASIC_CONSTRAINTS_free(bc);
  27113. bc = NULL;
  27114. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  27115. NID_key_usage, &crit, NULL));
  27116. ExpectIntEQ(crit, 1);
  27117. ExpectIntEQ(asn1_str->type, NID_key_usage);
  27118. #ifdef OPENSSL_ALL
  27119. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  27120. X509_EXTENSION_free(ext);
  27121. ext = NULL;
  27122. #endif
  27123. ASN1_STRING_free(asn1_str);
  27124. asn1_str = NULL;
  27125. #ifdef OPENSSL_ALL
  27126. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  27127. NID_ext_key_usage, &crit, NULL));
  27128. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  27129. X509_EXTENSION_free(ext);
  27130. ext = NULL;
  27131. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27132. sk = NULL;
  27133. #else
  27134. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27135. &crit, NULL);
  27136. ExpectNull(sk);
  27137. #endif
  27138. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27139. NID_authority_key_identifier, &crit, NULL));
  27140. #ifdef OPENSSL_ALL
  27141. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  27142. akey));
  27143. X509_EXTENSION_free(ext);
  27144. ext = NULL;
  27145. #endif
  27146. wolfSSL_AUTHORITY_KEYID_free(akey);
  27147. akey = NULL;
  27148. /* NID not yet supported */
  27149. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27150. NID_private_key_usage_period, &crit, NULL));
  27151. ExpectIntEQ(crit, -1);
  27152. sk_ASN1_OBJECT_free(sk);
  27153. sk = NULL;
  27154. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  27155. NID_subject_alt_name, &crit, NULL));
  27156. {
  27157. int i;
  27158. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27159. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27160. ExpectIntEQ(gen->type, GEN_DNS);
  27161. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27162. }
  27163. }
  27164. sk_GENERAL_NAME_free(sk);
  27165. sk = NULL;
  27166. /* NID not yet supported */
  27167. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27168. NID_issuer_alt_name, &crit, NULL));
  27169. ExpectIntEQ(crit, -1);
  27170. sk_ASN1_OBJECT_free(sk);
  27171. sk = NULL;
  27172. /* NID not yet supported */
  27173. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27174. NID_info_access, &crit, NULL));
  27175. sk_ASN1_OBJECT_free(sk);
  27176. sk = NULL;
  27177. /* NID not yet supported */
  27178. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27179. NID_sinfo_access, &crit, NULL));
  27180. ExpectIntEQ(crit, -1);
  27181. sk_ASN1_OBJECT_free(sk);
  27182. sk = NULL;
  27183. /* NID not yet supported */
  27184. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27185. NID_name_constraints, &crit, NULL));
  27186. ExpectIntEQ(crit, -1);
  27187. sk_ASN1_OBJECT_free(sk);
  27188. sk = NULL;
  27189. /* no cert policy set */
  27190. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27191. NID_certificate_policies, &crit, NULL));
  27192. sk_ASN1_OBJECT_free(sk);
  27193. sk = NULL;
  27194. /* NID not yet supported */
  27195. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27196. NID_policy_mappings, &crit, NULL));
  27197. ExpectIntEQ(crit, -1);
  27198. sk_ASN1_OBJECT_free(sk);
  27199. sk = NULL;
  27200. /* NID not yet supported */
  27201. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27202. NID_policy_constraints, &crit, NULL));
  27203. ExpectIntEQ(crit, -1);
  27204. sk_ASN1_OBJECT_free(sk);
  27205. sk = NULL;
  27206. /* NID not yet supported */
  27207. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27208. NID_inhibit_any_policy, &crit, NULL));
  27209. ExpectIntEQ(crit, -1);
  27210. sk_ASN1_OBJECT_free(sk);
  27211. sk = NULL;
  27212. /* NID not yet supported */
  27213. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27214. NID_tlsfeature, &crit, NULL));
  27215. ExpectIntEQ(crit, -1);
  27216. sk_ASN1_OBJECT_free(sk);
  27217. sk = NULL;
  27218. /* test invalid cases */
  27219. crit = 0;
  27220. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  27221. NULL));
  27222. ExpectIntEQ(crit, -1);
  27223. /* NULL passed for criticality. */
  27224. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  27225. NID_tlsfeature, NULL, NULL));
  27226. ExpectIntEQ(SSL_get_hit(ssl), 0);
  27227. #ifdef OPENSSL_ALL
  27228. X509_free(x509);
  27229. #endif
  27230. X509_free(x509ext);
  27231. SSL_free(ssl);
  27232. SSL_CTX_free(ctx);
  27233. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27234. return EXPECT_RESULT();
  27235. }
  27236. static int test_wolfSSL_X509_check_private_key(void)
  27237. {
  27238. EXPECT_DECLS;
  27239. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27240. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27241. X509* x509 = NULL;
  27242. EVP_PKEY* pkey = NULL;
  27243. const byte* key;
  27244. /* Check with correct key */
  27245. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27246. SSL_FILETYPE_PEM)));
  27247. key = client_key_der_2048;
  27248. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  27249. (long)sizeof_client_key_der_2048));
  27250. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  27251. EVP_PKEY_free(pkey);
  27252. pkey = NULL;
  27253. /* Check with wrong key */
  27254. key = server_key_der_2048;
  27255. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  27256. (long)sizeof_server_key_der_2048));
  27257. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  27258. /* test for incorrect parameter */
  27259. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  27260. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  27261. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  27262. EVP_PKEY_free(pkey);
  27263. X509_free(x509);
  27264. #endif
  27265. return EXPECT_RESULT();
  27266. }
  27267. static int test_wolfSSL_private_keys(void)
  27268. {
  27269. EXPECT_DECLS;
  27270. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27271. !defined(NO_FILESYSTEM)
  27272. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27273. WOLFSSL* ssl = NULL;
  27274. WOLFSSL_CTX* ctx = NULL;
  27275. EVP_PKEY* pkey = NULL;
  27276. OpenSSL_add_all_digests();
  27277. OpenSSL_add_all_algorithms();
  27278. #ifndef NO_RSA
  27279. #ifndef NO_WOLFSSL_SERVER
  27280. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27281. #else
  27282. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27283. #endif
  27284. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27285. /* Have to load a cert before you can check the private key against that
  27286. * certificates public key! */
  27287. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27288. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27289. #endif
  27290. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27291. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27292. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27293. #endif
  27294. ExpectNotNull(ssl = SSL_new(ctx));
  27295. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27296. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27297. #endif
  27298. #ifdef USE_CERT_BUFFERS_2048
  27299. {
  27300. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27301. unsigned char buf[FOURK_BUF];
  27302. word32 bufSz;
  27303. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27304. (unsigned char*)client_key_der_2048,
  27305. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27306. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27307. /* Should mismatch now that a different private key loaded */
  27308. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27309. #endif
  27310. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  27311. (unsigned char*)server_key,
  27312. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27313. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27314. /* After loading back in DER format of original key, should match */
  27315. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27316. #endif
  27317. /* test loading private key to the WOLFSSL_CTX */
  27318. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27319. (unsigned char*)client_key_der_2048,
  27320. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27321. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27322. /* Should mismatch now that a different private key loaded */
  27323. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27324. #endif
  27325. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27326. (unsigned char*)server_key,
  27327. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27328. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27329. /* After loading back in DER format of original key, should match */
  27330. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27331. #endif
  27332. /* pkey not set yet, expecting to fail */
  27333. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  27334. /* set PKEY and test again */
  27335. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27336. &server_key, (long)sizeof_server_key_der_2048));
  27337. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27338. /* reuse PKEY structure and test
  27339. * this should be checked with a memory management sanity checker */
  27340. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  27341. server_key = (const unsigned char*)server_key_der_2048;
  27342. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27343. &server_key, (long)sizeof_server_key_der_2048));
  27344. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27345. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  27346. bufSz = FOURK_BUF;
  27347. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  27348. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  27349. RSAk, NULL, 0)), 0);
  27350. server_key = (const unsigned char*)buf;
  27351. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  27352. (long)bufSz));
  27353. }
  27354. #endif
  27355. EVP_PKEY_free(pkey);
  27356. pkey = NULL;
  27357. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  27358. ssl = NULL;
  27359. SSL_CTX_free(ctx);
  27360. ctx = NULL;
  27361. #endif /* end of RSA private key match tests */
  27362. #ifdef HAVE_ECC
  27363. #ifndef NO_WOLFSSL_SERVER
  27364. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27365. #else
  27366. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27367. #endif
  27368. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  27369. WOLFSSL_FILETYPE_PEM));
  27370. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  27371. WOLFSSL_FILETYPE_PEM));
  27372. ExpectNotNull(ssl = SSL_new(ctx));
  27373. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27374. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27375. #endif
  27376. SSL_free(ssl);
  27377. ssl = NULL;
  27378. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  27379. WOLFSSL_FILETYPE_PEM));
  27380. ExpectNotNull(ssl = SSL_new(ctx));
  27381. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  27382. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27383. #endif
  27384. SSL_free(ssl);
  27385. ssl = NULL;
  27386. SSL_CTX_free(ctx);
  27387. ctx = NULL;
  27388. #endif /* end of ECC private key match tests */
  27389. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  27390. #ifndef NO_WOLFSSL_SERVER
  27391. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27392. #else
  27393. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27394. #endif
  27395. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  27396. WOLFSSL_FILETYPE_PEM));
  27397. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  27398. WOLFSSL_FILETYPE_PEM));
  27399. ExpectNotNull(ssl = SSL_new(ctx));
  27400. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27401. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27402. #endif
  27403. SSL_free(ssl);
  27404. ssl = NULL;
  27405. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  27406. WOLFSSL_FILETYPE_PEM));
  27407. ExpectNotNull(ssl = SSL_new(ctx));
  27408. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27409. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27410. #endif
  27411. SSL_free(ssl);
  27412. ssl = NULL;
  27413. SSL_CTX_free(ctx);
  27414. ctx = NULL;
  27415. #endif /* end of Ed25519 private key match tests */
  27416. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  27417. #ifndef NO_WOLFSSL_SERVER
  27418. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27419. #else
  27420. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27421. #endif
  27422. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  27423. WOLFSSL_FILETYPE_PEM));
  27424. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  27425. WOLFSSL_FILETYPE_PEM));
  27426. ExpectNotNull(ssl = SSL_new(ctx));
  27427. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27428. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27429. #endif
  27430. SSL_free(ssl);
  27431. ssl = NULL;
  27432. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  27433. WOLFSSL_FILETYPE_PEM));
  27434. ExpectNotNull(ssl = SSL_new(ctx));
  27435. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27436. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27437. #endif
  27438. SSL_free(ssl);
  27439. ssl = NULL;
  27440. SSL_CTX_free(ctx);
  27441. ctx = NULL;
  27442. #endif /* end of Ed448 private key match tests */
  27443. EVP_cleanup();
  27444. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  27445. CONF_modules_free();
  27446. ENGINE_cleanup();
  27447. CONF_modules_unload();
  27448. (void)ssl;
  27449. (void)ctx;
  27450. (void)pkey;
  27451. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  27452. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27453. return EXPECT_RESULT();
  27454. }
  27455. static int test_wolfSSL_PEM_read_PrivateKey(void)
  27456. {
  27457. EXPECT_DECLS;
  27458. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27459. && !defined(NO_FILESYSTEM)
  27460. XFILE file = XBADFILE;
  27461. const char* fname = "./certs/server-key.pem";
  27462. EVP_PKEY* pkey = NULL;
  27463. RSA* rsa = NULL;
  27464. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  27465. unsigned char* sig = NULL;
  27466. size_t sigLen;
  27467. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  27468. size_t tbsLen = sizeof(tbs);
  27469. /* Check error case. */
  27470. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  27471. /* Read in an RSA key. */
  27472. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27473. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  27474. if (file != XBADFILE)
  27475. XFCLOSE(file);
  27476. /* Make sure the key is usable by signing some data with it. */
  27477. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  27478. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  27479. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  27480. DYNAMIC_TYPE_TMP_BUFFER));
  27481. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27482. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  27483. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  27484. WOLFSSL_SUCCESS);
  27485. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27486. EVP_PKEY_CTX_free(ctx);
  27487. EVP_PKEY_free(pkey);
  27488. #endif
  27489. return EXPECT_RESULT();
  27490. }
  27491. static int test_wolfSSL_PEM_read_PUBKEY(void)
  27492. {
  27493. EXPECT_DECLS;
  27494. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27495. && !defined(NO_FILESYSTEM)
  27496. XFILE file = XBADFILE;
  27497. const char* fname = "./certs/client-keyPub.pem";
  27498. EVP_PKEY* pkey = NULL;
  27499. /* Check error case. */
  27500. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  27501. /* Read in an RSA key. */
  27502. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27503. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  27504. EVP_PKEY_free(pkey);
  27505. if (file != XBADFILE)
  27506. XFCLOSE(file);
  27507. #endif
  27508. return EXPECT_RESULT();
  27509. }
  27510. static int test_wolfSSL_PEM_PrivateKey(void)
  27511. {
  27512. EXPECT_DECLS;
  27513. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27514. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  27515. #ifndef NO_BIO
  27516. BIO* bio = NULL;
  27517. #endif
  27518. EVP_PKEY* pkey = NULL;
  27519. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27520. #ifndef NO_BIO
  27521. /* test creating new EVP_PKEY with bad arg */
  27522. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  27523. /* test loading RSA key using BIO */
  27524. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  27525. {
  27526. XFILE file = XBADFILE;
  27527. const char* fname = "./certs/server-key.pem";
  27528. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  27529. size_t sz;
  27530. byte* buf = NULL;
  27531. EVP_PKEY* pkey2 = NULL;
  27532. EVP_PKEY* pkey3 = NULL;
  27533. RSA* rsa_key = NULL;
  27534. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27535. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27536. ExpectIntGT(sz = XFTELL(file), 0);
  27537. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27538. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27539. if (buf != NULL) {
  27540. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27541. }
  27542. if (file != XBADFILE) {
  27543. XFCLOSE(file);
  27544. file = XBADFILE;
  27545. }
  27546. /* Test using BIO new mem and loading PEM private key */
  27547. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27548. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27549. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27550. buf = NULL;
  27551. BIO_free(bio);
  27552. bio = NULL;
  27553. ExpectNotNull(pkey2 = EVP_PKEY_new());
  27554. if (pkey2 != NULL) {
  27555. pkey2->type = EVP_PKEY_RSA;
  27556. }
  27557. /* Test parameter copy */
  27558. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  27559. EVP_PKEY_free(pkey2);
  27560. EVP_PKEY_free(pkey);
  27561. pkey = NULL;
  27562. /* Qt unit test case : rsa pkcs8 key */
  27563. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  27564. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27565. ExpectIntGT(sz = XFTELL(file), 0);
  27566. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27567. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27568. if (buf) {
  27569. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27570. }
  27571. if (file != XBADFILE) {
  27572. XFCLOSE(file);
  27573. file = XBADFILE;
  27574. }
  27575. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27576. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27577. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27578. buf = NULL;
  27579. BIO_free(bio);
  27580. bio = NULL;
  27581. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27582. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  27583. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  27584. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27585. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27586. #else
  27587. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27588. #endif
  27589. RSA_free(rsa_key);
  27590. EVP_PKEY_free(pkey3);
  27591. EVP_PKEY_free(pkey);
  27592. pkey = NULL;
  27593. }
  27594. #endif
  27595. /* test loading ECC key using BIO */
  27596. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27597. {
  27598. XFILE file = XBADFILE;
  27599. const char* fname = "./certs/ecc-key.pem";
  27600. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  27601. size_t sz = 0;
  27602. byte* buf = NULL;
  27603. EVP_PKEY* pkey2 = NULL;
  27604. EVP_PKEY* pkey3 = NULL;
  27605. EC_KEY* ec_key = NULL;
  27606. int nid = 0;
  27607. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27608. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27609. ExpectIntGT(sz = XFTELL(file), 0);
  27610. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27611. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27612. if (buf) {
  27613. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27614. }
  27615. if (file != XBADFILE) {
  27616. XFCLOSE(file);
  27617. file = XBADFILE;
  27618. }
  27619. /* Test using BIO new mem and loading PEM private key */
  27620. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27621. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27622. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27623. buf = NULL;
  27624. BIO_free(bio);
  27625. bio = NULL;
  27626. ExpectNotNull(pkey2 = EVP_PKEY_new());
  27627. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27628. if (pkey2 != NULL) {
  27629. pkey2->type = EVP_PKEY_EC;
  27630. }
  27631. /* Test parameter copy */
  27632. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  27633. /* Qt unit test case 1*/
  27634. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27635. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27636. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27637. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27638. #else
  27639. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27640. #endif
  27641. /* Test default digest */
  27642. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  27643. ExpectIntEQ(nid, NID_sha256);
  27644. EC_KEY_free(ec_key);
  27645. ec_key = NULL;
  27646. EVP_PKEY_free(pkey3);
  27647. pkey3 = NULL;
  27648. EVP_PKEY_free(pkey2);
  27649. pkey2 = NULL;
  27650. EVP_PKEY_free(pkey);
  27651. pkey = NULL;
  27652. /* Qt unit test case ec pkcs8 key */
  27653. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  27654. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27655. ExpectIntGT(sz = XFTELL(file), 0);
  27656. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  27657. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27658. if (buf) {
  27659. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  27660. }
  27661. if (file != XBADFILE) {
  27662. XFCLOSE(file);
  27663. file = XBADFILE;
  27664. }
  27665. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27666. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27667. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27668. buf = NULL;
  27669. BIO_free(bio);
  27670. bio = NULL;
  27671. ExpectNotNull(pkey3 = EVP_PKEY_new());
  27672. /* Qt unit test case */
  27673. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27674. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27675. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27676. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27677. #else
  27678. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27679. #endif
  27680. EC_KEY_free(ec_key);
  27681. EVP_PKEY_free(pkey3);
  27682. EVP_PKEY_free(pkey);
  27683. pkey = NULL;
  27684. }
  27685. #endif
  27686. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  27687. defined(WOLFSSL_CERT_GEN))
  27688. {
  27689. #define BIO_PEM_TEST_CHAR 'a'
  27690. EVP_PKEY* pkey2 = NULL;
  27691. unsigned char extra[10];
  27692. int i;
  27693. BIO* pub_bio = NULL;
  27694. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  27695. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27696. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  27697. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27698. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  27699. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  27700. &server_key, (long)sizeof_server_key_der_2048));
  27701. ExpectNull(pkey);
  27702. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27703. &server_key, (long)sizeof_server_key_der_2048));
  27704. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  27705. NULL), WOLFSSL_FAILURE);
  27706. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  27707. NULL), WOLFSSL_FAILURE);
  27708. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  27709. NULL), WOLFSSL_SUCCESS);
  27710. ExpectIntGT(BIO_pending(bio), 0);
  27711. ExpectIntEQ(BIO_pending(bio), 1679);
  27712. /* Check if the pubkey API writes only the public key */
  27713. #ifdef WOLFSSL_KEY_GEN
  27714. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  27715. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  27716. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  27717. ExpectIntGT(BIO_pending(pub_bio), 0);
  27718. /* Previously both the private key and the pubkey calls would write
  27719. * out the private key and the PEM header was the only difference.
  27720. * The public PEM should be significantly shorter than the
  27721. * private key versison. */
  27722. ExpectIntEQ(BIO_pending(pub_bio), 451);
  27723. #endif
  27724. /* test creating new EVP_PKEY with good args */
  27725. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27726. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  27727. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  27728. /* test of reuse of EVP_PKEY */
  27729. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27730. ExpectIntEQ(BIO_pending(bio), 0);
  27731. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27732. SSL_SUCCESS);
  27733. ExpectIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  27734. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27735. ExpectNotNull(pkey);
  27736. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  27737. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  27738. }
  27739. ExpectIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  27740. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  27741. for (i = 0; i < 10; i++) {
  27742. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  27743. }
  27744. BIO_free(pub_bio);
  27745. BIO_free(bio);
  27746. bio = NULL;
  27747. EVP_PKEY_free(pkey);
  27748. pkey = NULL;
  27749. EVP_PKEY_free(pkey2);
  27750. }
  27751. #endif
  27752. /* key is DES encrypted */
  27753. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  27754. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  27755. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  27756. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  27757. {
  27758. XFILE f = XBADFILE;
  27759. wc_pem_password_cb* passwd_cb = NULL;
  27760. void* passwd_cb_userdata;
  27761. SSL_CTX* ctx = NULL;
  27762. char passwd[] = "bad password";
  27763. #ifndef WOLFSSL_NO_TLS12
  27764. #ifndef NO_WOLFSSL_SERVER
  27765. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27766. #else
  27767. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27768. #endif
  27769. #else
  27770. #ifndef NO_WOLFSSL_SERVER
  27771. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27772. #else
  27773. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27774. #endif
  27775. #endif
  27776. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27777. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  27778. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  27779. ExpectNull(passwd_cb_userdata =
  27780. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27781. /* fail case with password call back */
  27782. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  27783. (void*)passwd));
  27784. BIO_free(bio);
  27785. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27786. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27787. (void*)passwd));
  27788. BIO_free(bio);
  27789. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  27790. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  27791. if ((bio == NULL) && (f != XBADFILE)) {
  27792. XFCLOSE(f);
  27793. }
  27794. /* use callback that works */
  27795. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27796. (void*)"yassl123"));
  27797. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27798. EVP_PKEY_free(pkey);
  27799. pkey = NULL;
  27800. BIO_free(bio);
  27801. bio = NULL;
  27802. SSL_CTX_free(ctx);
  27803. }
  27804. #endif /* !defined(NO_DES3) */
  27805. #endif /* !NO_BIO */
  27806. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27807. {
  27808. unsigned char buf[2048];
  27809. size_t bytes = 0;
  27810. XFILE f = XBADFILE;
  27811. SSL_CTX* ctx = NULL;
  27812. #ifndef WOLFSSL_NO_TLS12
  27813. #ifndef NO_WOLFSSL_SERVER
  27814. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27815. #else
  27816. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27817. #endif
  27818. #else
  27819. #ifndef NO_WOLFSSL_SERVER
  27820. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27821. #else
  27822. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27823. #endif
  27824. #endif
  27825. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  27826. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  27827. if (f != XBADFILE)
  27828. XFCLOSE(f);
  27829. server_key = buf;
  27830. pkey = NULL;
  27831. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  27832. ExpectNull(pkey);
  27833. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  27834. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27835. EVP_PKEY_free(pkey);
  27836. pkey = NULL;
  27837. SSL_CTX_free(ctx);
  27838. server_key = NULL;
  27839. }
  27840. #endif
  27841. #ifndef NO_BIO
  27842. (void)bio;
  27843. #endif
  27844. (void)pkey;
  27845. (void)server_key;
  27846. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  27847. return EXPECT_RESULT();
  27848. }
  27849. static int test_wolfSSL_PEM_file_RSAKey(void)
  27850. {
  27851. EXPECT_DECLS;
  27852. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27853. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27854. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27855. RSA* rsa = NULL;
  27856. XFILE fp = XBADFILE;
  27857. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  27858. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  27859. if (fp != XBADFILE)
  27860. XFCLOSE(fp);
  27861. ExpectIntEQ(RSA_size(rsa), 256);
  27862. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  27863. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  27864. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  27865. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  27866. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  27867. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  27868. RSA_free(rsa);
  27869. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27870. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27871. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27872. return EXPECT_RESULT();
  27873. }
  27874. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  27875. {
  27876. EXPECT_DECLS;
  27877. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  27878. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  27879. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  27880. RSA* rsa = NULL;
  27881. XFILE f = NULL;
  27882. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  27883. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  27884. ExpectIntEQ(RSA_size(rsa), 256);
  27885. if (f != XBADFILE) {
  27886. XFCLOSE(f);
  27887. f = XBADFILE;
  27888. }
  27889. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  27890. NULL), WOLFSSL_FAILURE);
  27891. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  27892. NULL), WOLFSSL_FAILURE);
  27893. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  27894. WOLFSSL_SUCCESS);
  27895. RSA_free(rsa);
  27896. #ifdef HAVE_ECC
  27897. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  27898. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  27899. if (f != XBADFILE)
  27900. XFCLOSE(f);
  27901. #endif /* HAVE_ECC */
  27902. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27903. return EXPECT_RESULT();
  27904. }
  27905. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  27906. {
  27907. EXPECT_DECLS;
  27908. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27909. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27910. XFILE file = XBADFILE;
  27911. const char* fname = "./certs/client-keyPub.pem";
  27912. RSA *rsa = NULL;
  27913. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  27914. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  27915. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  27916. ExpectIntEQ(RSA_size(rsa), 256);
  27917. RSA_free(rsa);
  27918. if (file != XBADFILE)
  27919. XFCLOSE(file);
  27920. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27921. return EXPECT_RESULT();
  27922. }
  27923. #ifndef NO_BIO
  27924. static int test_wolfSSL_PEM_bio_RSAKey(void)
  27925. {
  27926. EXPECT_DECLS;
  27927. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27928. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27929. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27930. RSA* rsa = NULL;
  27931. BIO* bio = NULL;
  27932. /* PrivateKey */
  27933. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27934. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  27935. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  27936. ExpectNotNull(rsa);
  27937. ExpectIntEQ(RSA_size(rsa), 256);
  27938. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27939. NULL), WOLFSSL_FAILURE);
  27940. BIO_free(bio);
  27941. bio = NULL;
  27942. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27943. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  27944. NULL), WOLFSSL_SUCCESS);
  27945. BIO_free(bio);
  27946. bio = NULL;
  27947. RSA_free(rsa);
  27948. rsa = NULL;
  27949. /* PUBKEY */
  27950. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  27951. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27952. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27953. ExpectIntEQ(RSA_size(rsa), 256);
  27954. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27955. BIO_free(bio);
  27956. bio = NULL;
  27957. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27958. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  27959. BIO_free(bio);
  27960. bio = NULL;
  27961. RSA_free(rsa);
  27962. rsa = NULL;
  27963. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  27964. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  27965. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27966. BIO_free(bio);
  27967. bio = NULL;
  27968. RSA_free(rsa);
  27969. rsa = NULL;
  27970. #ifdef HAVE_ECC
  27971. /* ensure that non-rsa keys do not work */
  27972. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27973. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27974. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27975. BIO_free(bio);
  27976. bio = NULL;
  27977. RSA_free(rsa);
  27978. rsa = NULL;
  27979. #endif /* HAVE_ECC */
  27980. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27981. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27982. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27983. return EXPECT_RESULT();
  27984. }
  27985. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  27986. {
  27987. EXPECT_DECLS;
  27988. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27989. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27990. RSA* rsa = NULL;
  27991. RSA* rsa_dup = NULL;
  27992. BIO* bio = NULL;
  27993. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27994. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27995. ExpectIntEQ(RSA_size(rsa), 256);
  27996. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  27997. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  27998. /* Test duplicating empty key. */
  27999. ExpectNotNull(rsa_dup = RSA_new());
  28000. ExpectNull(RSAPublicKey_dup(rsa_dup));
  28001. RSA_free(rsa_dup);
  28002. rsa_dup = NULL;
  28003. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28004. ExpectPtrNE(rsa_dup, rsa);
  28005. #endif
  28006. /* test if valgrind complains about unreleased memory */
  28007. RSA_up_ref(rsa);
  28008. RSA_free(rsa);
  28009. BIO_free(bio);
  28010. bio = NULL;
  28011. RSA_free(rsa);
  28012. rsa = NULL;
  28013. RSA_free(rsa_dup);
  28014. rsa_dup = NULL;
  28015. #ifdef HAVE_ECC
  28016. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28017. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28018. BIO_free(bio);
  28019. #endif /* HAVE_ECC */
  28020. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28021. return EXPECT_RESULT();
  28022. }
  28023. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28024. {
  28025. EXPECT_DECLS;
  28026. #ifndef HAVE_SELFTEST
  28027. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28028. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28029. DSA* dsa = NULL;
  28030. BIO* bio = NULL;
  28031. /* PrivateKey */
  28032. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28033. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28034. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28035. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  28036. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  28037. NULL), WOLFSSL_FAILURE);
  28038. BIO_free(bio);
  28039. bio = NULL;
  28040. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28041. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  28042. NULL), WOLFSSL_SUCCESS);
  28043. BIO_free(bio);
  28044. bio = NULL;
  28045. DSA_free(dsa);
  28046. dsa = NULL;
  28047. /* PUBKEY */
  28048. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28049. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28050. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28051. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  28052. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28053. BIO_free(bio);
  28054. bio = NULL;
  28055. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28056. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28057. BIO_free(bio);
  28058. bio = NULL;
  28059. DSA_free(dsa);
  28060. dsa = NULL;
  28061. #ifdef HAVE_ECC
  28062. /* ensure that non-dsa keys do not work */
  28063. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28064. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28065. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28066. BIO_free(bio);
  28067. bio = NULL;
  28068. DSA_free(dsa);
  28069. dsa = NULL;
  28070. #endif /* HAVE_ECC */
  28071. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28072. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28073. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28074. #endif /* HAVE_SELFTEST */
  28075. return EXPECT_RESULT();
  28076. }
  28077. static int test_wolfSSL_PEM_bio_ECKey(void)
  28078. {
  28079. EXPECT_DECLS;
  28080. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28081. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28082. EC_KEY* ec = NULL;
  28083. EC_KEY* ec2;
  28084. BIO* bio = NULL;
  28085. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28086. unsigned char* pem = NULL;
  28087. int pLen;
  28088. #endif
  28089. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  28090. "MAA=\n"
  28091. "-----END PUBLIC KEY-----";
  28092. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  28093. "MAA=\n"
  28094. "-----END EC PRIVATE KEY-----";
  28095. /* PrivateKey */
  28096. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28097. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28098. ec2 = NULL;
  28099. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  28100. ExpectIntEQ(ec == ec2, 1);
  28101. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28102. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  28103. NULL), WOLFSSL_FAILURE);
  28104. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28105. NULL), WOLFSSL_FAILURE);
  28106. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  28107. WOLFSSL_FAILURE);
  28108. BIO_free(bio);
  28109. bio = NULL;
  28110. /* Public key data - fail. */
  28111. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28112. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28113. BIO_free(bio);
  28114. bio = NULL;
  28115. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28116. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28117. NULL), WOLFSSL_SUCCESS);
  28118. BIO_free(bio);
  28119. bio = NULL;
  28120. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  28121. NULL),WOLFSSL_FAILURE);
  28122. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  28123. WOLFSSL_FAILURE);
  28124. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  28125. WOLFSSL_FAILURE);
  28126. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  28127. WOLFSSL_SUCCESS);
  28128. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28129. NULL), 0);
  28130. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  28131. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28132. NULL), 0);
  28133. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28134. NULL), 0);
  28135. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  28136. &pLen), 0);
  28137. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  28138. &pLen), 0);
  28139. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  28140. &pLen), 0);
  28141. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28142. NULL), 0);
  28143. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  28144. &pLen), 1);
  28145. ExpectIntGT(pLen, 0);
  28146. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28147. #endif
  28148. EC_KEY_free(ec);
  28149. ec = NULL;
  28150. /* PUBKEY */
  28151. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28152. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28153. ec2 = NULL;
  28154. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  28155. ExpectIntEQ(ec == ec2, 1);
  28156. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28157. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28158. BIO_free(bio);
  28159. bio = NULL;
  28160. /* Test 0x30, 0x00 fails. */
  28161. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  28162. sizeof(ec_key_bad_1)));
  28163. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28164. BIO_free(bio);
  28165. bio = NULL;
  28166. /* Private key data - fail. */
  28167. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28168. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  28169. BIO_free(bio);
  28170. bio = NULL;
  28171. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28172. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28173. BIO_free(bio);
  28174. bio = NULL;
  28175. /* Same test as above, but with a file pointer rather than a BIO. */
  28176. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28177. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28178. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  28179. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  28180. EC_KEY_free(ec);
  28181. ec = NULL;
  28182. #ifndef NO_RSA
  28183. /* ensure that non-ec keys do not work */
  28184. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28185. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28186. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28187. BIO_free(bio);
  28188. bio = NULL;
  28189. EC_KEY_free(ec);
  28190. ec = NULL;
  28191. #endif /* !NO_RSA */
  28192. /* Test 0x30, 0x00 fails. */
  28193. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  28194. sizeof(ec_priv_key_bad_1)));
  28195. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  28196. BIO_free(bio);
  28197. bio = NULL;
  28198. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28199. return EXPECT_RESULT();
  28200. }
  28201. static int test_wolfSSL_PEM_PUBKEY(void)
  28202. {
  28203. EXPECT_DECLS;
  28204. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28205. BIO* bio = NULL;
  28206. EVP_PKEY* pkey = NULL;
  28207. /* test creating new EVP_PKEY with bad arg */
  28208. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28209. /* test loading ECC key using BIO */
  28210. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28211. {
  28212. XFILE file = XBADFILE;
  28213. const char* fname = "./certs/ecc-client-keyPub.pem";
  28214. size_t sz;
  28215. byte* buf = NULL;
  28216. EVP_PKEY* pkey2 = NULL;
  28217. EC_KEY* ec_key = NULL;
  28218. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  28219. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  28220. ExpectIntGT(sz = XFTELL(file), 0);
  28221. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  28222. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28223. if (buf != NULL) {
  28224. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  28225. }
  28226. if (file != XBADFILE) {
  28227. XFCLOSE(file);
  28228. }
  28229. /* Test using BIO new mem and loading PEM private key */
  28230. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28231. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28232. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28233. BIO_free(bio);
  28234. bio = NULL;
  28235. /* Qt unit test case*/
  28236. ExpectNotNull(pkey2 = EVP_PKEY_new());
  28237. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28238. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28239. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28240. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28241. #else
  28242. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28243. #endif
  28244. EC_KEY_free(ec_key);
  28245. EVP_PKEY_free(pkey2);
  28246. EVP_PKEY_free(pkey);
  28247. pkey = NULL;
  28248. }
  28249. #endif
  28250. (void)bio;
  28251. (void)pkey;
  28252. #endif
  28253. return EXPECT_RESULT();
  28254. }
  28255. #endif /* !NO_BIO */
  28256. static int test_DSA_do_sign_verify(void)
  28257. {
  28258. EXPECT_DECLS;
  28259. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28260. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28261. !defined(NO_DSA)
  28262. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28263. DSA_SIG* sig = NULL;
  28264. DSA* dsa = NULL;
  28265. word32 bytes;
  28266. byte sigBin[DSA_SIG_SIZE];
  28267. int dsacheck;
  28268. #ifdef USE_CERT_BUFFERS_1024
  28269. byte tmp[ONEK_BUF];
  28270. XMEMSET(tmp, 0, sizeof(tmp));
  28271. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28272. bytes = sizeof_dsa_key_der_1024;
  28273. #elif defined(USE_CERT_BUFFERS_2048)
  28274. byte tmp[TWOK_BUF];
  28275. XMEMSET(tmp, 0, sizeof(tmp));
  28276. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28277. bytes = sizeof_dsa_key_der_2048;
  28278. #else
  28279. byte tmp[TWOK_BUF];
  28280. XFILE fp = XBADFILE;
  28281. XMEMSET(tmp, 0, sizeof(tmp));
  28282. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  28283. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  28284. if (fp != XBADFILE)
  28285. XFCLOSE(fp);
  28286. #endif /* END USE_CERT_BUFFERS_1024 */
  28287. XMEMSET(digest, 202, sizeof(digest));
  28288. ExpectNotNull(dsa = DSA_new());
  28289. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28290. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28291. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28292. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28293. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28294. DSA_SIG_free(sig);
  28295. DSA_free(dsa);
  28296. #endif
  28297. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28298. return EXPECT_RESULT();
  28299. }
  28300. static int test_wolfSSL_tmp_dh(void)
  28301. {
  28302. EXPECT_DECLS;
  28303. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28304. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28305. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28306. byte buff[6000];
  28307. char file[] = "./certs/dsaparams.pem";
  28308. XFILE f = XBADFILE;
  28309. int bytes = 0;
  28310. DSA* dsa = NULL;
  28311. DH* dh = NULL;
  28312. #if defined(WOLFSSL_DH_EXTRA) && \
  28313. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28314. DH* dh2 = NULL;
  28315. #endif
  28316. BIO* bio = NULL;
  28317. SSL* ssl = NULL;
  28318. SSL_CTX* ctx = NULL;
  28319. #ifndef NO_WOLFSSL_SERVER
  28320. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28321. #else
  28322. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28323. #endif
  28324. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  28325. WOLFSSL_FILETYPE_PEM));
  28326. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  28327. WOLFSSL_FILETYPE_PEM));
  28328. ExpectNotNull(ssl = SSL_new(ctx));
  28329. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  28330. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  28331. if (f != XBADFILE)
  28332. XFCLOSE(f);
  28333. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  28334. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28335. ExpectNotNull(dsa);
  28336. dh = wolfSSL_DSA_dup_DH(dsa);
  28337. ExpectNotNull(dh);
  28338. #if defined(WOLFSSL_DH_EXTRA) && \
  28339. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28340. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  28341. #endif
  28342. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28343. #ifndef NO_WOLFSSL_SERVER
  28344. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28345. #else
  28346. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28347. #endif
  28348. BIO_free(bio);
  28349. DSA_free(dsa);
  28350. DH_free(dh);
  28351. #if defined(WOLFSSL_DH_EXTRA) && \
  28352. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28353. DH_free(dh2);
  28354. #endif
  28355. SSL_free(ssl);
  28356. SSL_CTX_free(ctx);
  28357. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28358. #endif
  28359. return EXPECT_RESULT();
  28360. }
  28361. static int test_wolfSSL_ctrl(void)
  28362. {
  28363. EXPECT_DECLS;
  28364. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28365. byte buff[6000];
  28366. BIO* bio = NULL;
  28367. int bytes;
  28368. BUF_MEM* ptr = NULL;
  28369. XMEMSET(buff, 0, sizeof(buff));
  28370. bytes = sizeof(buff);
  28371. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  28372. ExpectNotNull(BIO_s_socket());
  28373. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28374. /* needs tested after stubs filled out @TODO
  28375. SSL_ctrl
  28376. SSL_CTX_ctrl
  28377. */
  28378. BIO_free(bio);
  28379. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28380. return EXPECT_RESULT();
  28381. }
  28382. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28383. {
  28384. EXPECT_DECLS;
  28385. #ifdef OPENSSL_EXTRA
  28386. static const unsigned char pw[] = "password";
  28387. static const int pwSz = sizeof(pw) - 1;
  28388. size_t checkPwSz = 0;
  28389. const unsigned char* checkPw = NULL;
  28390. WOLFSSL_EVP_PKEY* key = NULL;
  28391. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28392. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28393. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  28394. pwSz));
  28395. if (key != NULL) {
  28396. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  28397. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  28398. ExpectIntEQ(key->pkey_sz, pwSz);
  28399. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28400. }
  28401. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28402. ExpectIntEQ((int)checkPwSz, pwSz);
  28403. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28404. wolfSSL_EVP_PKEY_free(key);
  28405. key = NULL;
  28406. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  28407. 0));
  28408. ExpectIntEQ(key->pkey_sz, 0);
  28409. if (EXPECT_SUCCESS()) {
  28410. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  28411. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28412. }
  28413. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  28414. ExpectIntEQ((int)checkPwSz, 0);
  28415. wolfSSL_EVP_PKEY_free(key);
  28416. key = NULL;
  28417. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  28418. 0));
  28419. ExpectIntEQ(key->pkey_sz, 0);
  28420. if (EXPECT_SUCCESS()) {
  28421. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  28422. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28423. }
  28424. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  28425. ExpectIntEQ((int)checkPwSz, 0);
  28426. wolfSSL_EVP_PKEY_free(key);
  28427. key = NULL;
  28428. #endif /* OPENSSL_EXTRA */
  28429. return EXPECT_RESULT();
  28430. }
  28431. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  28432. {
  28433. EXPECT_DECLS;
  28434. #ifdef OPENSSL_EXTRA
  28435. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  28436. const char *priv = "ABCDEFGHIJKLMNOP";
  28437. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  28438. WOLFSSL_EVP_PKEY* key = NULL;
  28439. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28440. NULL, NULL, AES_128_KEY_SIZE, cipher));
  28441. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28442. NULL, (const unsigned char *)priv, 0, cipher));
  28443. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28444. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  28445. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28446. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  28447. wolfSSL_EVP_PKEY_free(key);
  28448. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  28449. #endif /* OPENSSL_EXTRA */
  28450. return EXPECT_RESULT();
  28451. }
  28452. static int test_wolfSSL_EVP_Digest(void)
  28453. {
  28454. EXPECT_DECLS;
  28455. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28456. const char* in = "abc";
  28457. int inLen = (int)XSTRLEN(in);
  28458. byte out[WC_SHA256_DIGEST_SIZE];
  28459. unsigned int outLen;
  28460. const char* expOut =
  28461. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  28462. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  28463. "\x15\xAD";
  28464. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  28465. "SHA256", NULL), 1);
  28466. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  28467. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  28468. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  28469. return EXPECT_RESULT();
  28470. }
  28471. static int test_wolfSSL_EVP_Digest_all(void)
  28472. {
  28473. EXPECT_DECLS;
  28474. #ifdef OPENSSL_EXTRA
  28475. const char* digests[] = {
  28476. #ifndef NO_MD5
  28477. "MD5",
  28478. #endif
  28479. #ifndef NO_SHA
  28480. "SHA",
  28481. #endif
  28482. #ifdef WOLFSSL_SHA224
  28483. "SHA224",
  28484. #endif
  28485. #ifndef NO_SHA256
  28486. "SHA256",
  28487. #endif
  28488. #ifdef WOLFSSL_SHA384
  28489. "SHA384",
  28490. #endif
  28491. #ifdef WOLFSSL_SHA512
  28492. "SHA512",
  28493. #endif
  28494. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  28495. "SHA512_224",
  28496. #endif
  28497. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  28498. "SHA512_256",
  28499. #endif
  28500. #ifdef WOLFSSL_SHA3
  28501. #ifndef WOLFSSL_NOSHA3_224
  28502. "SHA3_224",
  28503. #endif
  28504. #ifndef WOLFSSL_NOSHA3_256
  28505. "SHA3_256",
  28506. #endif
  28507. "SHA3_384",
  28508. #ifndef WOLFSSL_NOSHA3_512
  28509. "SHA3_512",
  28510. #endif
  28511. #endif /* WOLFSSL_SHA3 */
  28512. NULL
  28513. };
  28514. const char** d;
  28515. const unsigned char in[] = "abc";
  28516. int inLen = XSTR_SIZEOF(in);
  28517. byte out[WC_MAX_DIGEST_SIZE];
  28518. unsigned int outLen;
  28519. for (d = digests; *d != NULL; d++) {
  28520. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  28521. ExpectIntGT(outLen, 0);
  28522. ExpectIntEQ(EVP_MD_size(*d), outLen);
  28523. }
  28524. #endif
  28525. return EXPECT_RESULT();
  28526. }
  28527. static int test_wolfSSL_EVP_MD_size(void)
  28528. {
  28529. EXPECT_DECLS;
  28530. #ifdef OPENSSL_EXTRA
  28531. WOLFSSL_EVP_MD_CTX mdCtx;
  28532. #ifdef WOLFSSL_SHA3
  28533. #ifndef WOLFSSL_NOSHA3_224
  28534. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28535. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  28536. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  28537. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  28538. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28539. #endif
  28540. #ifndef WOLFSSL_NOSHA3_256
  28541. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28542. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  28543. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  28544. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  28545. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28546. #endif
  28547. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28548. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  28549. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  28550. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  28551. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28552. #ifndef WOLFSSL_NOSHA3_512
  28553. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28554. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  28555. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  28556. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  28557. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28558. #endif
  28559. #endif /* WOLFSSL_SHA3 */
  28560. #ifndef NO_SHA256
  28561. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28562. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  28563. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28564. WC_SHA256_DIGEST_SIZE);
  28565. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28566. WC_SHA256_BLOCK_SIZE);
  28567. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  28568. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  28569. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28570. #endif
  28571. #ifndef NO_MD5
  28572. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28573. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  28574. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28575. WC_MD5_DIGEST_SIZE);
  28576. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28577. WC_MD5_BLOCK_SIZE);
  28578. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  28579. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  28580. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28581. #endif
  28582. #ifdef WOLFSSL_SHA224
  28583. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28584. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  28585. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28586. WC_SHA224_DIGEST_SIZE);
  28587. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28588. WC_SHA224_BLOCK_SIZE);
  28589. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  28590. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  28591. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28592. #endif
  28593. #ifdef WOLFSSL_SHA384
  28594. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28595. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  28596. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28597. WC_SHA384_DIGEST_SIZE);
  28598. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28599. WC_SHA384_BLOCK_SIZE);
  28600. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  28601. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  28602. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28603. #endif
  28604. #ifdef WOLFSSL_SHA512
  28605. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28606. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  28607. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28608. WC_SHA512_DIGEST_SIZE);
  28609. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28610. WC_SHA512_BLOCK_SIZE);
  28611. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  28612. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  28613. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28614. #endif
  28615. #ifndef NO_SHA
  28616. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28617. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  28618. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28619. WC_SHA_DIGEST_SIZE);
  28620. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28621. WC_SHA_BLOCK_SIZE);
  28622. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28623. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28624. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28625. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28626. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  28627. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28628. WC_SHA_DIGEST_SIZE);
  28629. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28630. WC_SHA_BLOCK_SIZE);
  28631. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28632. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28633. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28634. #endif
  28635. /* error case */
  28636. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28637. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  28638. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  28639. BAD_FUNC_ARG);
  28640. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  28641. /* Cleanup is valid on uninit'ed struct */
  28642. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28643. #endif /* OPENSSL_EXTRA */
  28644. return EXPECT_RESULT();
  28645. }
  28646. static int test_wolfSSL_EVP_MD_pkey_type(void)
  28647. {
  28648. EXPECT_DECLS;
  28649. #ifdef OPENSSL_EXTRA
  28650. const WOLFSSL_EVP_MD* md;
  28651. #ifndef NO_MD5
  28652. ExpectNotNull(md = EVP_md5());
  28653. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  28654. #endif
  28655. #ifndef NO_SHA
  28656. ExpectNotNull(md = EVP_sha1());
  28657. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  28658. #endif
  28659. #ifdef WOLFSSL_SHA224
  28660. ExpectNotNull(md = EVP_sha224());
  28661. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  28662. #endif
  28663. ExpectNotNull(md = EVP_sha256());
  28664. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  28665. #ifdef WOLFSSL_SHA384
  28666. ExpectNotNull(md = EVP_sha384());
  28667. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  28668. #endif
  28669. #ifdef WOLFSSL_SHA512
  28670. ExpectNotNull(md = EVP_sha512());
  28671. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  28672. #endif
  28673. #endif
  28674. return EXPECT_RESULT();
  28675. }
  28676. #ifdef OPENSSL_EXTRA
  28677. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  28678. size_t testKeySz, const char* testData, size_t testDataSz,
  28679. const byte* testResult, size_t testResultSz)
  28680. {
  28681. EXPECT_DECLS;
  28682. unsigned char check[WC_MAX_DIGEST_SIZE];
  28683. size_t checkSz = -1;
  28684. WOLFSSL_EVP_PKEY* key = NULL;
  28685. WOLFSSL_EVP_MD_CTX mdCtx;
  28686. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  28687. testKey, (int)testKeySz));
  28688. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28689. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28690. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28691. (unsigned int)testDataSz), 1);
  28692. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28693. ExpectIntEQ((int)checkSz, (int)testResultSz);
  28694. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28695. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28696. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28697. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28698. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28699. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28700. (unsigned int)testDataSz), 1);
  28701. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28702. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28703. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28704. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28705. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28706. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28707. ExpectIntEQ((int)checkSz, (int)testResultSz);
  28708. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28709. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28710. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28711. (unsigned int)testDataSz - 4), 1);
  28712. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28713. ExpectIntEQ((int)checkSz,(int)testResultSz);
  28714. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28715. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28716. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28717. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28718. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28719. (unsigned int)testDataSz - 4), 1);
  28720. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28721. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28722. wolfSSL_EVP_PKEY_free(key);
  28723. return EXPECT_RESULT();
  28724. }
  28725. #endif
  28726. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  28727. {
  28728. EXPECT_DECLS;
  28729. #ifdef OPENSSL_EXTRA
  28730. static const unsigned char testKey[] =
  28731. {
  28732. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28733. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28734. 0x0b, 0x0b, 0x0b, 0x0b
  28735. };
  28736. static const char testData[] = "Hi There";
  28737. #ifdef WOLFSSL_SHA224
  28738. static const unsigned char testResultSha224[] =
  28739. {
  28740. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  28741. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  28742. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  28743. 0x53, 0x68, 0x4b, 0x22
  28744. };
  28745. #endif
  28746. #ifndef NO_SHA256
  28747. static const unsigned char testResultSha256[] =
  28748. {
  28749. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  28750. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  28751. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  28752. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  28753. };
  28754. #endif
  28755. #ifdef WOLFSSL_SHA384
  28756. static const unsigned char testResultSha384[] =
  28757. {
  28758. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  28759. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  28760. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  28761. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  28762. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  28763. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  28764. };
  28765. #endif
  28766. #ifdef WOLFSSL_SHA512
  28767. static const unsigned char testResultSha512[] =
  28768. {
  28769. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  28770. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  28771. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  28772. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  28773. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  28774. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  28775. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  28776. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  28777. };
  28778. #endif
  28779. #ifdef WOLFSSL_SHA3
  28780. #ifndef WOLFSSL_NOSHA3_224
  28781. static const unsigned char testResultSha3_224[] =
  28782. {
  28783. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  28784. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  28785. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  28786. 0xf3, 0xc8, 0x60, 0xf7
  28787. };
  28788. #endif
  28789. #ifndef WOLFSSL_NOSHA3_256
  28790. static const unsigned char testResultSha3_256[] =
  28791. {
  28792. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  28793. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  28794. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  28795. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  28796. };
  28797. #endif
  28798. #ifndef WOLFSSL_NOSHA3_384
  28799. static const unsigned char testResultSha3_384[] =
  28800. {
  28801. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  28802. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  28803. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  28804. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  28805. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  28806. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  28807. };
  28808. #endif
  28809. #ifndef WOLFSSL_NOSHA3_512
  28810. static const unsigned char testResultSha3_512[] =
  28811. {
  28812. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  28813. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  28814. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  28815. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  28816. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  28817. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  28818. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  28819. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  28820. };
  28821. #endif
  28822. #endif
  28823. #ifndef NO_SHA256
  28824. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  28825. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  28826. sizeof(testResultSha256)), TEST_SUCCESS);
  28827. #endif
  28828. #ifdef WOLFSSL_SHA224
  28829. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  28830. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  28831. sizeof(testResultSha224)), TEST_SUCCESS);
  28832. #endif
  28833. #ifdef WOLFSSL_SHA384
  28834. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  28835. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  28836. sizeof(testResultSha384)), TEST_SUCCESS);
  28837. #endif
  28838. #ifdef WOLFSSL_SHA512
  28839. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  28840. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  28841. sizeof(testResultSha512)), TEST_SUCCESS);
  28842. #endif
  28843. #ifdef WOLFSSL_SHA3
  28844. #ifndef WOLFSSL_NOSHA3_224
  28845. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  28846. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  28847. sizeof(testResultSha3_224)), TEST_SUCCESS);
  28848. #endif
  28849. #ifndef WOLFSSL_NOSHA3_256
  28850. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  28851. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  28852. sizeof(testResultSha3_256)), TEST_SUCCESS);
  28853. #endif
  28854. #ifndef WOLFSSL_NOSHA3_384
  28855. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  28856. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  28857. sizeof(testResultSha3_384)), TEST_SUCCESS);
  28858. #endif
  28859. #ifndef WOLFSSL_NOSHA3_512
  28860. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  28861. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  28862. sizeof(testResultSha3_512)), TEST_SUCCESS);
  28863. #endif
  28864. #endif
  28865. #endif /* OPENSSL_EXTRA */
  28866. return EXPECT_RESULT();
  28867. }
  28868. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  28869. {
  28870. EXPECT_DECLS;
  28871. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  28872. defined(USE_CERT_BUFFERS_2048)
  28873. WOLFSSL_EVP_PKEY* privKey = NULL;
  28874. WOLFSSL_EVP_PKEY* pubKey = NULL;
  28875. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  28876. const char testData[] = "Hi There";
  28877. WOLFSSL_EVP_MD_CTX mdCtx;
  28878. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  28879. int ret;
  28880. size_t checkSz = -1;
  28881. int sz = 2048 / 8;
  28882. const unsigned char* cp;
  28883. const unsigned char* p;
  28884. unsigned char check[2048/8];
  28885. size_t i;
  28886. int paddings[] = {
  28887. RSA_PKCS1_PADDING,
  28888. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  28889. RSA_PKCS1_PSS_PADDING,
  28890. #endif
  28891. };
  28892. cp = client_key_der_2048;
  28893. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  28894. sizeof_client_key_der_2048)));
  28895. p = client_keypub_der_2048;
  28896. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28897. sizeof_client_keypub_der_2048)));
  28898. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28899. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  28900. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28901. NULL, privKey), 1);
  28902. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28903. (unsigned int)XSTRLEN(testData)), 1);
  28904. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28905. ExpectIntEQ((int)checkSz, sz);
  28906. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28907. ExpectIntEQ((int)checkSz,sz);
  28908. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28909. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28910. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  28911. ExpectIntEQ(ret, 1);
  28912. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28913. ExpectIntEQ(ret, 1);
  28914. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28915. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28916. NULL, pubKey), 1);
  28917. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28918. (unsigned int)XSTRLEN(testData)),
  28919. 1);
  28920. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28921. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28922. ExpectIntEQ(ret, 1);
  28923. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28924. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28925. NULL, privKey), 1);
  28926. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28927. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28928. ExpectIntEQ((int)checkSz, sz);
  28929. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28930. ExpectIntEQ((int)checkSz, sz);
  28931. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28932. (unsigned int)XSTRLEN(testData) - 4), 1);
  28933. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28934. ExpectIntEQ((int)checkSz, sz);
  28935. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28936. ExpectIntEQ(ret, 1);
  28937. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28938. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28939. NULL, pubKey), 1);
  28940. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28941. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28942. (unsigned int)XSTRLEN(testData) - 4),
  28943. 1);
  28944. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28945. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28946. ExpectIntEQ(ret, 1);
  28947. /* Check all signing padding types */
  28948. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  28949. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28950. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  28951. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  28952. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28953. paddings[i]), 1);
  28954. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28955. (unsigned int)XSTRLEN(testData)), 1);
  28956. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28957. ExpectIntEQ((int)checkSz, sz);
  28958. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28959. ExpectIntEQ((int)checkSz,sz);
  28960. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28961. ExpectIntEQ(ret, 1);
  28962. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28963. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  28964. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  28965. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28966. paddings[i]), 1);
  28967. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28968. (unsigned int)XSTRLEN(testData)), 1);
  28969. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28970. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  28971. ExpectIntEQ(ret, 1);
  28972. }
  28973. wolfSSL_EVP_PKEY_free(pubKey);
  28974. wolfSSL_EVP_PKEY_free(privKey);
  28975. #endif
  28976. return EXPECT_RESULT();
  28977. }
  28978. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  28979. {
  28980. EXPECT_DECLS;
  28981. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28982. WOLFSSL_EVP_PKEY* privKey = NULL;
  28983. WOLFSSL_EVP_PKEY* pubKey = NULL;
  28984. const char testData[] = "Hi There";
  28985. WOLFSSL_EVP_MD_CTX mdCtx;
  28986. int ret;
  28987. size_t checkSz = -1;
  28988. const unsigned char* cp;
  28989. const unsigned char* p;
  28990. unsigned char check[2048/8];
  28991. cp = ecc_clikey_der_256;
  28992. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  28993. sizeof_ecc_clikey_der_256));
  28994. p = ecc_clikeypub_der_256;
  28995. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28996. sizeof_ecc_clikeypub_der_256)));
  28997. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28998. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28999. NULL, privKey), 1);
  29000. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29001. (unsigned int)XSTRLEN(testData)), 1);
  29002. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29003. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29004. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29005. ExpectIntEQ(ret, 1);
  29006. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29007. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29008. NULL, pubKey), 1);
  29009. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29010. (unsigned int)XSTRLEN(testData)),
  29011. 1);
  29012. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29013. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29014. ExpectIntEQ(ret, 1);
  29015. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29016. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29017. NULL, privKey), 1);
  29018. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29019. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29020. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29021. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29022. (unsigned int)XSTRLEN(testData) - 4), 1);
  29023. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29024. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29025. ExpectIntEQ(ret, 1);
  29026. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29027. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29028. NULL, pubKey), 1);
  29029. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29030. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29031. (unsigned int)XSTRLEN(testData) - 4),
  29032. 1);
  29033. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29034. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  29035. ExpectIntEQ(ret, 1);
  29036. wolfSSL_EVP_PKEY_free(pubKey);
  29037. wolfSSL_EVP_PKEY_free(privKey);
  29038. #endif
  29039. return EXPECT_RESULT();
  29040. }
  29041. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29042. {
  29043. EXPECT_DECLS;
  29044. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29045. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29046. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29047. char caFile[] = "./certs/client-ca.pem";
  29048. char clientFile[] = "./certs/client-cert.pem";
  29049. SSL_CTX* ctx = NULL;
  29050. X509* x509 = NULL;
  29051. BIO *bio = NULL;
  29052. X509 *cert = NULL;
  29053. X509 *ca = NULL;
  29054. STACK_OF(X509) *chain = NULL;
  29055. STACK_OF(X509) *chain2 = NULL;
  29056. #ifndef NO_WOLFSSL_SERVER
  29057. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29058. #else
  29059. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29060. #endif
  29061. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  29062. WOLFSSL_FILETYPE_PEM));
  29063. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29064. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  29065. WOLFSSL_FILETYPE_PEM));
  29066. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29067. /* additional test of getting EVP_PKEY key size from X509
  29068. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29069. * allowed with user RSA */
  29070. {
  29071. EVP_PKEY* pkey = NULL;
  29072. #if defined(HAVE_ECC)
  29073. X509* ecX509 = NULL;
  29074. #endif /* HAVE_ECC */
  29075. ExpectNotNull(pkey = X509_get_pubkey(x509));
  29076. /* current RSA key is 2048 bit (256 bytes) */
  29077. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  29078. EVP_PKEY_free(pkey);
  29079. pkey = NULL;
  29080. #if defined(HAVE_ECC)
  29081. #if defined(USE_CERT_BUFFERS_256)
  29082. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29083. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29084. SSL_FILETYPE_ASN1));
  29085. #else
  29086. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  29087. cliEccCertFile, SSL_FILETYPE_PEM));
  29088. #endif
  29089. pkey = X509_get_pubkey(ecX509);
  29090. ExpectNotNull(pkey);
  29091. /* current ECC key is 256 bit (32 bytes) */
  29092. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  29093. X509_free(ecX509);
  29094. ecX509 = NULL;
  29095. EVP_PKEY_free(pkey);
  29096. pkey = NULL;
  29097. #endif /* HAVE_ECC */
  29098. }
  29099. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29100. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29101. if (EXPECT_SUCCESS()) {
  29102. x509 = NULL;
  29103. }
  29104. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29105. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  29106. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29107. #endif
  29108. SSL_CTX_free(ctx);
  29109. ctx = NULL;
  29110. #ifndef NO_WOLFSSL_SERVER
  29111. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29112. #else
  29113. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29114. #endif
  29115. /* Test haproxy use case */
  29116. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29117. /* Read Certificate */
  29118. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29119. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29120. ExpectNotNull(chain = sk_X509_new_null());
  29121. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  29122. if (EXPECT_SUCCESS()) {
  29123. ca = NULL;
  29124. }
  29125. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  29126. ExpectNotNull(ca = sk_X509_shift(chain2));
  29127. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29128. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29129. if (EXPECT_SUCCESS()) {
  29130. ca = NULL;
  29131. }
  29132. BIO_free(bio);
  29133. X509_free(cert);
  29134. X509_free(ca);
  29135. X509_free(x509);
  29136. sk_X509_pop_free(chain, X509_free);
  29137. sk_X509_pop_free(chain2, X509_free);
  29138. SSL_CTX_free(ctx);
  29139. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29140. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29141. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29142. return EXPECT_RESULT();
  29143. }
  29144. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29145. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29146. {
  29147. EXPECT_DECLS;
  29148. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29149. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29150. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29151. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29152. callback_functions client_cb;
  29153. callback_functions server_cb;
  29154. int line = 0;
  29155. int flag = ERR_TXT_STRING;
  29156. const char* file = NULL;
  29157. const char* data = NULL;
  29158. /* create a failed connection and inspect the error */
  29159. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29160. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29161. client_cb.method = wolfTLSv1_1_client_method;
  29162. server_cb.method = wolfTLSv1_2_server_method;
  29163. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  29164. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29165. ExpectNotNull(data);
  29166. /* check clearing error state */
  29167. ERR_remove_state(0);
  29168. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29169. ERR_peek_last_error_line(NULL, &line);
  29170. ExpectIntEQ(line, 0);
  29171. ERR_peek_last_error_line(&file, NULL);
  29172. ExpectNull(file);
  29173. /* retry connection to fill error queue */
  29174. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29175. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29176. client_cb.method = wolfTLSv1_1_client_method;
  29177. server_cb.method = wolfTLSv1_2_server_method;
  29178. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  29179. /* check that error code was stored */
  29180. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29181. ERR_peek_last_error_line(NULL, &line);
  29182. ExpectIntNE(line, 0);
  29183. ERR_peek_last_error_line(&file, NULL);
  29184. ExpectNotNull(file);
  29185. fprintf(stderr, "\nTesting error print out\n");
  29186. ERR_print_errors_fp(stderr);
  29187. fprintf(stderr, "Done testing print out\n\n");
  29188. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  29189. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29190. return EXPECT_RESULT();
  29191. }
  29192. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  29193. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29194. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29195. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29196. {
  29197. (void) ok;
  29198. (void) ctx;
  29199. fprintf(stderr, "ENTER verify_cb\n");
  29200. return SSL_SUCCESS;
  29201. }
  29202. #endif
  29203. static int test_wolfSSL_X509_Name_canon(void)
  29204. {
  29205. EXPECT_DECLS;
  29206. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29207. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29208. defined(WOLFSSL_CERT_GEN) && \
  29209. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29210. const long ex_hash1 = 0x0fdb2da4;
  29211. const long ex_hash2 = 0x9f3e8c9e;
  29212. X509_NAME *name = NULL;
  29213. X509 *x509 = NULL;
  29214. XFILE file = XBADFILE;
  29215. unsigned long hash = 0;
  29216. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29217. byte *pbuf = NULL;
  29218. word32 len = 0;
  29219. (void) ex_hash2;
  29220. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  29221. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29222. ExpectNotNull(name = X509_get_issuer_name(x509));
  29223. /* When output buffer is NULL, should return necessary output buffer
  29224. * length.*/
  29225. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29226. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29227. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29228. hash = (((unsigned long)digest[3] << 24) |
  29229. ((unsigned long)digest[2] << 16) |
  29230. ((unsigned long)digest[1] << 8) |
  29231. ((unsigned long)digest[0]));
  29232. ExpectIntEQ(hash, ex_hash1);
  29233. if (file != XBADFILE) {
  29234. XFCLOSE(file);
  29235. file = XBADFILE;
  29236. }
  29237. X509_free(x509);
  29238. x509 = NULL;
  29239. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29240. pbuf = NULL;
  29241. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  29242. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29243. ExpectNotNull(name = X509_get_issuer_name(x509));
  29244. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29245. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29246. hash = (((unsigned long)digest[3] << 24) |
  29247. ((unsigned long)digest[2] << 16) |
  29248. ((unsigned long)digest[1] << 8) |
  29249. ((unsigned long)digest[0]));
  29250. ExpectIntEQ(hash, ex_hash2);
  29251. if (file != XBADFILE)
  29252. XFCLOSE(file);
  29253. X509_free(x509);
  29254. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29255. #endif
  29256. return EXPECT_RESULT();
  29257. }
  29258. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29259. {
  29260. EXPECT_DECLS;
  29261. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29262. const int MAX_DIR = 4;
  29263. const char paths[][32] = {
  29264. "./certs/ed25519",
  29265. "./certs/ecc",
  29266. "./certs/crl",
  29267. "./certs/",
  29268. };
  29269. char CertCrl_path[MAX_FILENAME_SZ];
  29270. char *p;
  29271. X509_STORE* str = NULL;
  29272. X509_LOOKUP* lookup = NULL;
  29273. WOLFSSL_STACK* sk = NULL;
  29274. int len, total_len, i;
  29275. (void)sk;
  29276. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29277. /* illegal string */
  29278. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29279. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29280. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29281. SSL_FILETYPE_PEM,NULL), 0);
  29282. /* free store */
  29283. X509_STORE_free(str);
  29284. str = NULL;
  29285. /* short folder string */
  29286. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29287. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29288. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29289. SSL_FILETYPE_PEM,NULL), 1);
  29290. #if defined(WOLFSSL_INT_H)
  29291. /* only available when including internal.h */
  29292. ExpectNotNull(sk = lookup->dirs->dir_entry);
  29293. #endif
  29294. /* free store */
  29295. X509_STORE_free(str);
  29296. str = NULL;
  29297. /* typical function check */
  29298. p = &CertCrl_path[0];
  29299. total_len = 0;
  29300. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29301. len = (int)XSTRLEN((const char*)&paths[i]);
  29302. total_len += len;
  29303. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29304. p += len;
  29305. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29306. }
  29307. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29308. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29309. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29310. SSL_FILETYPE_PEM,NULL), 1);
  29311. #if defined(WOLFSSL_INT_H)
  29312. /* only available when including internal.h */
  29313. ExpectNotNull(sk = lookup->dirs->dir_entry);
  29314. #endif
  29315. X509_STORE_free(str);
  29316. #endif
  29317. return EXPECT_RESULT();
  29318. }
  29319. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29320. {
  29321. EXPECT_DECLS;
  29322. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29323. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29324. defined(WOLFSSL_SIGNER_DER_CERT)
  29325. X509_STORE_CTX* ctx = NULL;
  29326. X509_STORE* str = NULL;
  29327. X509_LOOKUP* lookup = NULL;
  29328. X509* cert1 = NULL;
  29329. X509* x509Ca = NULL;
  29330. X509* x509Svr = NULL;
  29331. X509* issuer = NULL;
  29332. WOLFSSL_STACK* sk = NULL;
  29333. X509_NAME* caName = NULL;
  29334. X509_NAME* issuerName = NULL;
  29335. XFILE file1 = XBADFILE;
  29336. int i;
  29337. int cert_count = 0;
  29338. int cmp;
  29339. char der[] = "certs/ca-cert.der";
  29340. #ifdef HAVE_CRL
  29341. char pem[][100] = {
  29342. "./certs/crl/crl.pem",
  29343. "./certs/crl/crl2.pem",
  29344. "./certs/crl/caEccCrl.pem",
  29345. "./certs/crl/eccCliCRL.pem",
  29346. "./certs/crl/eccSrvCRL.pem",
  29347. ""
  29348. };
  29349. #endif
  29350. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  29351. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29352. if (file1 != XBADFILE)
  29353. XFCLOSE(file1);
  29354. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29355. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29356. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29357. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29358. SSL_FILETYPE_PEM,NULL), 1);
  29359. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29360. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  29361. /* check if CA cert is loaded into the store */
  29362. for (i = 0; i < cert_count; i++) {
  29363. x509Ca = sk_X509_value(sk, i);
  29364. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29365. }
  29366. ExpectNotNull((x509Svr =
  29367. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29368. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29369. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29370. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29371. ExpectNotNull(issuer);
  29372. caName = X509_get_subject_name(x509Ca);
  29373. ExpectNotNull(caName);
  29374. issuerName = X509_get_subject_name(issuer);
  29375. ExpectNotNull(issuerName);
  29376. cmp = X509_NAME_cmp(caName, issuerName);
  29377. ExpectIntEQ(cmp, 0);
  29378. /* load der format */
  29379. X509_free(issuer);
  29380. issuer = NULL;
  29381. X509_STORE_CTX_free(ctx);
  29382. ctx = NULL;
  29383. X509_STORE_free(str);
  29384. str = NULL;
  29385. sk_X509_pop_free(sk, NULL);
  29386. sk = NULL;
  29387. X509_free(x509Svr);
  29388. x509Svr = NULL;
  29389. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29390. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29391. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29392. SSL_FILETYPE_ASN1,NULL), 1);
  29393. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29394. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  29395. /* check if CA cert is loaded into the store */
  29396. for (i = 0; i < cert_count; i++) {
  29397. x509Ca = sk_X509_value(sk, i);
  29398. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29399. }
  29400. X509_STORE_free(str);
  29401. str = NULL;
  29402. sk_X509_pop_free(sk, NULL);
  29403. sk = NULL;
  29404. X509_free(cert1);
  29405. cert1 = NULL;
  29406. #ifdef HAVE_CRL
  29407. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  29408. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29409. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29410. SSL_FILETYPE_PEM,NULL), 1);
  29411. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29412. "certs/server-revoked-cert.pem",
  29413. SSL_FILETYPE_PEM,NULL), 1);
  29414. if (str) {
  29415. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29416. WOLFSSL_FILETYPE_PEM), 1);
  29417. /* since store hasn't yet known the revoked cert*/
  29418. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29419. "certs/server-revoked-cert.pem",
  29420. WOLFSSL_FILETYPE_PEM), 1);
  29421. }
  29422. for (i = 0; pem[i][0] != '\0'; i++)
  29423. {
  29424. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29425. SSL_FILETYPE_PEM, NULL), 1);
  29426. }
  29427. if (str) {
  29428. /* since store knows crl list */
  29429. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29430. "certs/server-revoked-cert.pem",
  29431. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29432. }
  29433. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29434. X509_STORE_free(str);
  29435. #endif
  29436. #endif
  29437. return EXPECT_RESULT();
  29438. }
  29439. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29440. {
  29441. int res = TEST_SKIPPED;
  29442. #if defined(OPENSSL_EXTRA)
  29443. X509_STORE_CTX_cleanup(NULL);
  29444. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29445. res = TEST_SUCCESS;
  29446. #endif
  29447. return res;
  29448. }
  29449. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29450. {
  29451. EXPECT_DECLS;
  29452. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29453. X509_STORE_CTX* ctx = NULL;
  29454. X509_STORE* str = NULL;
  29455. X509* x509Ca = NULL;
  29456. X509* x509Svr = NULL;
  29457. X509* issuer = NULL;
  29458. X509_NAME* caName = NULL;
  29459. X509_NAME* issuerName = NULL;
  29460. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29461. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29462. ExpectNotNull((x509Ca =
  29463. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  29464. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  29465. ExpectNotNull((x509Svr =
  29466. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29467. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29468. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29469. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  29470. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  29471. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  29472. #ifdef WOLFSSL_SIGNER_DER_CERT
  29473. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  29474. #endif
  29475. X509_free(issuer);
  29476. X509_STORE_CTX_free(ctx);
  29477. X509_free(x509Svr);
  29478. X509_STORE_free(str);
  29479. X509_free(x509Ca);
  29480. #endif
  29481. return EXPECT_RESULT();
  29482. }
  29483. static int test_wolfSSL_PKCS7_certs(void)
  29484. {
  29485. EXPECT_DECLS;
  29486. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  29487. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  29488. STACK_OF(X509)* sk = NULL;
  29489. STACK_OF(X509_INFO)* info_sk = NULL;
  29490. PKCS7 *p7 = NULL;
  29491. BIO* bio = NULL;
  29492. const byte* p = NULL;
  29493. int buflen = 0;
  29494. int i;
  29495. /* Test twice. Once with d2i and once without to test
  29496. * that everything is free'd correctly. */
  29497. for (i = 0; i < 2; i++) {
  29498. ExpectNotNull(p7 = PKCS7_new());
  29499. if (p7 != NULL) {
  29500. p7->version = 1;
  29501. #ifdef NO_SHA
  29502. p7->hashOID = SHA256h;
  29503. #else
  29504. p7->hashOID = SHAh;
  29505. #endif
  29506. }
  29507. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29508. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29509. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29510. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  29511. ExpectNotNull(sk = sk_X509_new_null());
  29512. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  29513. X509_INFO* info = NULL;
  29514. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  29515. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  29516. if (EXPECT_SUCCESS() && (info != NULL)) {
  29517. info->x509 = NULL;
  29518. }
  29519. X509_INFO_free(info);
  29520. }
  29521. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  29522. info_sk = NULL;
  29523. BIO_free(bio);
  29524. bio = NULL;
  29525. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29526. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  29527. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  29528. sk_X509_pop_free(sk, X509_free);
  29529. }
  29530. sk = NULL;
  29531. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  29532. if (i == 0) {
  29533. PKCS7_free(p7);
  29534. p7 = NULL;
  29535. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  29536. if (p7 != NULL) {
  29537. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  29538. * them */
  29539. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  29540. }
  29541. /* PKCS7_free free's the certs */
  29542. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  29543. }
  29544. BIO_free(bio);
  29545. bio = NULL;
  29546. PKCS7_free(p7);
  29547. p7 = NULL;
  29548. }
  29549. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29550. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  29551. return EXPECT_RESULT();
  29552. }
  29553. static int test_wolfSSL_X509_STORE_CTX(void)
  29554. {
  29555. EXPECT_DECLS;
  29556. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29557. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29558. X509_STORE_CTX* ctx = NULL;
  29559. X509_STORE* str = NULL;
  29560. X509* x509 = NULL;
  29561. #ifdef OPENSSL_ALL
  29562. X509* x5092 = NULL;
  29563. STACK_OF(X509) *sk = NULL;
  29564. STACK_OF(X509) *sk2 = NULL;
  29565. STACK_OF(X509) *sk3 = NULL;
  29566. #endif
  29567. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29568. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  29569. ExpectNotNull((x509 =
  29570. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29571. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  29572. #ifdef OPENSSL_ALL
  29573. /* sk_X509_new only in OPENSSL_ALL */
  29574. sk = sk_X509_new_null();
  29575. ExpectNotNull(sk);
  29576. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  29577. #else
  29578. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  29579. #endif
  29580. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  29581. X509_STORE_CTX_set_error(ctx, -5);
  29582. X509_STORE_CTX_set_error(NULL, -5);
  29583. X509_STORE_CTX_free(ctx);
  29584. ctx = NULL;
  29585. #ifdef OPENSSL_ALL
  29586. sk_X509_pop_free(sk, NULL);
  29587. sk = NULL;
  29588. #endif
  29589. X509_STORE_free(str);
  29590. str = NULL;
  29591. X509_free(x509);
  29592. x509 = NULL;
  29593. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29594. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  29595. X509_STORE_CTX_free(ctx);
  29596. ctx = NULL;
  29597. #ifdef OPENSSL_ALL
  29598. /* test X509_STORE_CTX_get(1)_chain */
  29599. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  29600. SSL_FILETYPE_PEM)));
  29601. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  29602. SSL_FILETYPE_PEM)));
  29603. ExpectNotNull((sk = sk_X509_new_null()));
  29604. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  29605. if (EXPECT_FAIL()) {
  29606. X509_free(x509);
  29607. x509 = NULL;
  29608. }
  29609. ExpectNotNull((str = X509_STORE_new()));
  29610. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  29611. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  29612. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  29613. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  29614. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  29615. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  29616. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  29617. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  29618. X509_STORE_CTX_free(ctx);
  29619. ctx = NULL;
  29620. X509_STORE_free(str);
  29621. str = NULL;
  29622. /* CTX certs not freed yet */
  29623. X509_free(x5092);
  29624. x5092 = NULL;
  29625. sk_X509_pop_free(sk, NULL);
  29626. sk = NULL;
  29627. /* sk3 is dup so free here */
  29628. sk_X509_pop_free(sk3, NULL);
  29629. sk3 = NULL;
  29630. #endif
  29631. /* test X509_STORE_CTX_get/set_ex_data */
  29632. {
  29633. int i = 0, tmpData = 5;
  29634. void* tmpDataRet;
  29635. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29636. #ifdef HAVE_EX_DATA
  29637. for (i = 0; i < MAX_EX_DATA; i++) {
  29638. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29639. WOLFSSL_SUCCESS);
  29640. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29641. ExpectNotNull(tmpDataRet);
  29642. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  29643. }
  29644. #else
  29645. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29646. WOLFSSL_FAILURE);
  29647. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29648. ExpectNull(tmpDataRet);
  29649. #endif
  29650. X509_STORE_CTX_free(ctx);
  29651. ctx = NULL;
  29652. }
  29653. /* test X509_STORE_get/set_ex_data */
  29654. {
  29655. int i = 0, tmpData = 99;
  29656. void* tmpDataRet;
  29657. ExpectNotNull(str = X509_STORE_new());
  29658. #ifdef HAVE_EX_DATA
  29659. for (i = 0; i < MAX_EX_DATA; i++) {
  29660. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29661. WOLFSSL_SUCCESS);
  29662. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29663. ExpectNotNull(tmpDataRet);
  29664. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  29665. }
  29666. #else
  29667. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29668. WOLFSSL_FAILURE);
  29669. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29670. ExpectNull(tmpDataRet);
  29671. #endif
  29672. X509_STORE_free(str);
  29673. str = NULL;
  29674. }
  29675. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29676. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29677. return EXPECT_RESULT();
  29678. }
  29679. static int test_wolfSSL_X509_STORE_set_flags(void)
  29680. {
  29681. EXPECT_DECLS;
  29682. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29683. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29684. X509_STORE* store = NULL;
  29685. X509* x509 = NULL;
  29686. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  29687. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  29688. WOLFSSL_FILETYPE_PEM)));
  29689. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  29690. #ifdef HAVE_CRL
  29691. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29692. WOLFSSL_SUCCESS);
  29693. #else
  29694. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29695. NOT_COMPILED_IN);
  29696. #endif
  29697. wolfSSL_X509_free(x509);
  29698. wolfSSL_X509_STORE_free(store);
  29699. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  29700. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29701. return EXPECT_RESULT();
  29702. }
  29703. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  29704. {
  29705. EXPECT_DECLS;
  29706. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29707. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29708. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  29709. WOLFSSL_X509_STORE* store = NULL;
  29710. WOLFSSL_X509_LOOKUP* lookup = NULL;
  29711. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  29712. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  29713. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  29714. X509_FILETYPE_PEM), 1);
  29715. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  29716. X509_FILETYPE_PEM), 1);
  29717. if (store != NULL) {
  29718. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  29719. WOLFSSL_FILETYPE_PEM), 1);
  29720. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29721. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  29722. }
  29723. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  29724. X509_FILETYPE_PEM), 1);
  29725. if (store != NULL) {
  29726. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29727. WOLFSSL_FILETYPE_PEM), 1);
  29728. }
  29729. wolfSSL_X509_STORE_free(store);
  29730. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  29731. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29732. return EXPECT_RESULT();
  29733. }
  29734. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  29735. {
  29736. EXPECT_DECLS;
  29737. #if defined(OPENSSL_EXTRA)
  29738. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  29739. time_t c_time;
  29740. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  29741. c_time = 365*24*60*60;
  29742. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  29743. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  29744. WOLFSSL_USE_CHECK_TIME);
  29745. ExpectTrue(ctx->param->check_time == c_time);
  29746. wolfSSL_X509_STORE_CTX_free(ctx);
  29747. #endif /* OPENSSL_EXTRA */
  29748. return EXPECT_RESULT();
  29749. }
  29750. static int test_wolfSSL_CTX_get0_set1_param(void)
  29751. {
  29752. EXPECT_DECLS;
  29753. #if defined(OPENSSL_EXTRA)
  29754. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29755. SSL_CTX* ctx = NULL;
  29756. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29757. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  29758. char testIPv4[] = "127.0.0.1";
  29759. char testhostName[] = "foo.hoge.com";
  29760. #ifndef NO_WOLFSSL_SERVER
  29761. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29762. #else
  29763. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29764. #endif
  29765. ExpectNull(SSL_CTX_get0_param(NULL));
  29766. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  29767. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  29768. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  29769. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  29770. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  29771. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  29772. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  29773. WOLFSSL_SUCCESS);
  29774. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  29775. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  29776. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  29777. (int)XSTRLEN(testhostName)));
  29778. ExpectIntEQ(0x01, pParam->hostFlags);
  29779. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29780. /* test for incorrect parameter */
  29781. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  29782. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  29783. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  29784. SSL_CTX_free(ctx);
  29785. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  29786. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29787. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29788. return EXPECT_RESULT();
  29789. }
  29790. static int test_wolfSSL_get0_param(void)
  29791. {
  29792. EXPECT_DECLS;
  29793. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29794. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29795. SSL_CTX* ctx = NULL;
  29796. SSL* ssl = NULL;
  29797. #ifndef NO_WOLFSSL_SERVER
  29798. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29799. #else
  29800. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29801. #endif
  29802. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29803. SSL_FILETYPE_PEM));
  29804. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29805. ExpectNotNull(ssl = SSL_new(ctx));
  29806. ExpectNotNull(SSL_get0_param(ssl));
  29807. SSL_free(ssl);
  29808. SSL_CTX_free(ctx);
  29809. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29810. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29811. return EXPECT_RESULT();
  29812. }
  29813. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  29814. {
  29815. EXPECT_DECLS;
  29816. #if defined(OPENSSL_EXTRA)
  29817. const char host[] = "www.example.com";
  29818. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29819. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  29820. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  29821. if (pParam != NULL) {
  29822. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29823. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  29824. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29825. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29826. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29827. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29828. }
  29829. #endif /* OPENSSL_EXTRA */
  29830. return EXPECT_RESULT();
  29831. }
  29832. static int test_wolfSSL_set1_host(void)
  29833. {
  29834. EXPECT_DECLS;
  29835. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29836. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29837. const char host[] = "www.test_wolfSSL_set1_host.com";
  29838. const char emptyStr[] = "";
  29839. SSL_CTX* ctx = NULL;
  29840. SSL* ssl = NULL;
  29841. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  29842. #ifndef NO_WOLFSSL_SERVER
  29843. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29844. #else
  29845. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29846. #endif
  29847. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29848. SSL_FILETYPE_PEM));
  29849. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29850. ExpectNotNull(ssl = SSL_new(ctx));
  29851. pParam = SSL_get0_param(ssl);
  29852. /* we should get back host string */
  29853. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  29854. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29855. /* we should get back empty string */
  29856. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  29857. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  29858. /* we should get back host string */
  29859. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  29860. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29861. /* we should get back empty string */
  29862. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  29863. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  29864. SSL_free(ssl);
  29865. SSL_CTX_free(ctx);
  29866. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29867. #endif /* OPENSSL_EXTRA */
  29868. return EXPECT_RESULT();
  29869. }
  29870. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  29871. {
  29872. EXPECT_DECLS;
  29873. #if defined(OPENSSL_EXTRA)
  29874. unsigned char buf[16] = {0};
  29875. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  29876. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  29877. /* test 127.0.0.1 */
  29878. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  29879. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  29880. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  29881. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  29882. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29883. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  29884. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  29885. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  29886. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29887. ExpectIntEQ(XSTRNCMP(param->ipasc,
  29888. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  29889. /* test 2001:db8:: */
  29890. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29891. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29892. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29893. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  29894. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29895. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  29896. /* test ::1234:5678 */
  29897. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  29898. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29899. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29900. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29901. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29902. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  29903. /* test 2001:db8::1234:5678 */
  29904. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29905. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29906. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29907. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29908. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29909. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  29910. sizeof(param->ipasc)), 0);
  29911. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  29912. /* 2001:db8:1::ab9:c0a8:102 */
  29913. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29914. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  29915. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  29916. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  29917. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29918. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  29919. sizeof(param->ipasc)), 0);
  29920. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29921. #endif /* OPENSSL_EXTRA */
  29922. return EXPECT_RESULT();
  29923. }
  29924. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  29925. {
  29926. EXPECT_DECLS;
  29927. #if defined(OPENSSL_EXTRA)
  29928. X509_STORE* store = NULL;
  29929. X509_STORE_CTX* ctx = NULL;
  29930. X509_STORE_CTX* ctx_no_init = NULL;
  29931. ExpectNotNull((store = X509_STORE_new()));
  29932. ExpectNotNull(ctx = X509_STORE_CTX_new());
  29933. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  29934. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  29935. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  29936. /* should return NULL if ctx has not bee initialized */
  29937. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  29938. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  29939. wolfSSL_X509_STORE_CTX_free(ctx);
  29940. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  29941. X509_STORE_free(store);
  29942. #endif /* OPENSSL_EXTRA */
  29943. return EXPECT_RESULT();
  29944. }
  29945. static int test_wolfSSL_CTX_set_client_CA_list(void)
  29946. {
  29947. EXPECT_DECLS;
  29948. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29949. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  29950. WOLFSSL_CTX* ctx = NULL;
  29951. WOLFSSL* ssl = NULL;
  29952. X509_NAME* name = NULL;
  29953. STACK_OF(X509_NAME)* names = NULL;
  29954. STACK_OF(X509_NAME)* ca_list = NULL;
  29955. int names_len = 0;
  29956. int i;
  29957. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  29958. /* Send two X501 names in cert request */
  29959. names = SSL_load_client_CA_file(cliCertFile);
  29960. ExpectNotNull(names);
  29961. ca_list = SSL_load_client_CA_file(caCertFile);
  29962. ExpectNotNull(ca_list);
  29963. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  29964. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  29965. if (EXPECT_FAIL()) {
  29966. wolfSSL_X509_NAME_free(name);
  29967. name = NULL;
  29968. }
  29969. SSL_CTX_set_client_CA_list(ctx, names);
  29970. /* This should only free the stack structure */
  29971. sk_X509_NAME_free(ca_list);
  29972. ca_list = NULL;
  29973. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29974. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29975. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29976. for (i = 0; i < names_len; i++) {
  29977. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  29978. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  29979. }
  29980. /* Needed to be able to create ssl object */
  29981. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  29982. SSL_FILETYPE_PEM));
  29983. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29984. ExpectNotNull(ssl = wolfSSL_new(ctx));
  29985. /* load again as old names are responsibility of ctx to free*/
  29986. names = SSL_load_client_CA_file(cliCertFile);
  29987. ExpectNotNull(names);
  29988. SSL_set_client_CA_list(ssl, names);
  29989. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  29990. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29991. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29992. for (i = 0; i < names_len; i++) {
  29993. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  29994. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  29995. }
  29996. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  29997. {
  29998. tcp_ready ready;
  29999. func_args server_args;
  30000. callback_functions server_cb;
  30001. THREAD_TYPE serverThread;
  30002. WOLFSSL* ssl_client = NULL;
  30003. WOLFSSL_CTX* ctx_client = NULL;
  30004. SOCKET_T sockfd = 0;
  30005. /* wolfSSL_get_client_CA_list() with handshake */
  30006. StartTCP();
  30007. InitTcpReady(&ready);
  30008. XMEMSET(&server_args, 0, sizeof(func_args));
  30009. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30010. server_args.signal = &ready;
  30011. server_args.callbacks = &server_cb;
  30012. /* we are responsible for free'ing WOLFSSL_CTX */
  30013. server_cb.ctx = ctx;
  30014. server_cb.isSharedCtx = 1;
  30015. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  30016. cliCertFile, 0));
  30017. start_thread(test_server_nofail, &server_args, &serverThread);
  30018. wait_tcp_ready(&server_args);
  30019. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30020. ExpectNotNull(ctx_client =
  30021. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30022. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  30023. ctx_client, caCertFile, 0));
  30024. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  30025. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30026. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  30027. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30028. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  30029. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30030. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30031. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30032. /* We are expecting two cert names to be sent */
  30033. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  30034. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30035. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30036. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  30037. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  30038. }
  30039. wolfSSL_shutdown(ssl_client);
  30040. wolfSSL_free(ssl_client);
  30041. wolfSSL_CTX_free(ctx_client);
  30042. CloseSocket(sockfd);
  30043. join_thread(serverThread);
  30044. FreeTcpReady(&ready);
  30045. }
  30046. #endif
  30047. wolfSSL_free(ssl);
  30048. wolfSSL_CTX_free(ctx);
  30049. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  30050. * !NO_BIO */
  30051. return EXPECT_RESULT();
  30052. }
  30053. static int test_wolfSSL_CTX_add_client_CA(void)
  30054. {
  30055. EXPECT_DECLS;
  30056. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30057. !defined(NO_WOLFSSL_CLIENT)
  30058. WOLFSSL_CTX* ctx = NULL;
  30059. WOLFSSL_X509* x509 = NULL;
  30060. WOLFSSL_X509* x509_a = NULL;
  30061. STACK_OF(X509_NAME)* ca_list = NULL;
  30062. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30063. /* Add client cert */
  30064. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  30065. SSL_FILETYPE_PEM));
  30066. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  30067. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30068. /* Add another client cert */
  30069. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30070. SSL_FILETYPE_PEM));
  30071. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30072. /* test for incorrect parameter */
  30073. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30074. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30075. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30076. X509_free(x509);
  30077. X509_free(x509_a);
  30078. SSL_CTX_free(ctx);
  30079. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30080. return EXPECT_RESULT();
  30081. }
  30082. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  30083. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  30084. {
  30085. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30086. WOLFSSL_CTX* ctx = callbacks->ctx;
  30087. WOLFSSL* ssl = NULL;
  30088. SOCKET_T sfd = 0;
  30089. SOCKET_T cfd = 0;
  30090. word16 port;
  30091. char input[1024];
  30092. int idx;
  30093. int ret, err = 0;
  30094. const char* privateName = "ech-private-name.com";
  30095. int privateNameLen = (int)XSTRLEN(privateName);
  30096. ((func_args*)args)->return_code = TEST_FAIL;
  30097. port = ((func_args*)args)->signal->port;
  30098. AssertIntEQ(WOLFSSL_SUCCESS,
  30099. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30100. AssertIntEQ(WOLFSSL_SUCCESS,
  30101. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30102. WOLFSSL_FILETYPE_PEM));
  30103. AssertIntEQ(WOLFSSL_SUCCESS,
  30104. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30105. WOLFSSL_FILETYPE_PEM));
  30106. if (callbacks->ctx_ready)
  30107. callbacks->ctx_ready(ctx);
  30108. ssl = wolfSSL_new(ctx);
  30109. /* set the sni for the server */
  30110. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  30111. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30112. CloseSocket(sfd);
  30113. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30114. if (callbacks->ssl_ready)
  30115. callbacks->ssl_ready(ssl);
  30116. do {
  30117. err = 0; /* Reset error */
  30118. ret = wolfSSL_accept(ssl);
  30119. if (ret != WOLFSSL_SUCCESS) {
  30120. err = wolfSSL_get_error(ssl, 0);
  30121. }
  30122. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30123. if (ret != WOLFSSL_SUCCESS) {
  30124. char buff[WOLFSSL_MAX_ERROR_SZ];
  30125. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  30126. }
  30127. else {
  30128. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30129. input[idx] = 0;
  30130. fprintf(stderr, "Client message: %s\n", input);
  30131. }
  30132. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  30133. privateNameLen));
  30134. ((func_args*)args)->return_code = TEST_SUCCESS;
  30135. }
  30136. if (callbacks->on_result)
  30137. callbacks->on_result(ssl);
  30138. wolfSSL_shutdown(ssl);
  30139. wolfSSL_free(ssl);
  30140. wolfSSL_CTX_free(ctx);
  30141. CloseSocket(cfd);
  30142. #ifdef FP_ECC
  30143. wc_ecc_fp_free();
  30144. #endif
  30145. return 0;
  30146. }
  30147. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  30148. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30149. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30150. {
  30151. AssertNotNull(ssl);
  30152. AssertNotNull(line);
  30153. XFILE fp;
  30154. const byte lf = '\n';
  30155. fp = XFOPEN("./MyKeyLog.txt", "a");
  30156. XFWRITE( line, 1, strlen(line),fp);
  30157. XFWRITE( (void*)&lf,1,1,fp);
  30158. XFCLOSE(fp);
  30159. }
  30160. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30161. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30162. {
  30163. EXPECT_DECLS;
  30164. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30165. !defined(NO_WOLFSSL_CLIENT)
  30166. SSL_CTX* ctx = NULL;
  30167. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30168. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30169. SSL_CTX_free(ctx);
  30170. SSL_CTX_set_keylog_callback(NULL, NULL);
  30171. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30172. return EXPECT_RESULT();
  30173. }
  30174. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30175. {
  30176. EXPECT_DECLS;
  30177. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30178. !defined(NO_WOLFSSL_CLIENT)
  30179. SSL_CTX* ctx = NULL;
  30180. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30181. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30182. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30183. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30184. SSL_CTX_set_keylog_callback(ctx, NULL );
  30185. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30186. SSL_CTX_free(ctx);
  30187. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30188. return EXPECT_RESULT();
  30189. }
  30190. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30191. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  30192. {
  30193. /* set keylog callback */
  30194. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  30195. return TEST_SUCCESS;
  30196. }
  30197. #endif
  30198. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30199. {
  30200. EXPECT_DECLS;
  30201. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30202. /* This test is intended for checking whether keylog callback is called
  30203. * in client during TLS handshake between the client and a server.
  30204. */
  30205. test_ssl_cbf server_cbf;
  30206. test_ssl_cbf client_cbf;
  30207. XFILE fp = XBADFILE;
  30208. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  30209. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  30210. server_cbf.method = wolfTLSv1_2_server_method;
  30211. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  30212. /* clean up keylog file */
  30213. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  30214. if (fp != XBADFILE) {
  30215. XFCLOSE(fp);
  30216. fp = XBADFILE;
  30217. }
  30218. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30219. &server_cbf, NULL), TEST_SUCCESS);
  30220. /* check if the keylog file exists */
  30221. char buff[300] = {0};
  30222. int found = 0;
  30223. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  30224. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  30225. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  30226. found = 1;
  30227. break;
  30228. }
  30229. }
  30230. if (fp != XBADFILE) {
  30231. XFCLOSE(fp);
  30232. }
  30233. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30234. ExpectIntEQ(found, 1);
  30235. /* clean up */
  30236. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  30237. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30238. return EXPECT_RESULT();
  30239. }
  30240. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30241. defined(HAVE_SECRET_CALLBACK)
  30242. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  30243. {
  30244. /* set keylog callback */
  30245. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  30246. return TEST_SUCCESS;
  30247. }
  30248. #endif
  30249. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30250. {
  30251. EXPECT_DECLS;
  30252. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30253. defined(HAVE_SECRET_CALLBACK)
  30254. /* This test is intended for checking whether keylog callback is called
  30255. * in client during TLS handshake between the client and a server.
  30256. */
  30257. test_ssl_cbf server_cbf;
  30258. test_ssl_cbf client_cbf;
  30259. XFILE fp = XBADFILE;
  30260. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  30261. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  30262. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30263. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  30264. /* clean up keylog file */
  30265. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  30266. if (fp != XBADFILE) {
  30267. XFCLOSE(fp);
  30268. fp = XBADFILE;
  30269. }
  30270. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30271. &server_cbf, NULL), TEST_SUCCESS);
  30272. /* check if the keylog file exists */
  30273. {
  30274. char buff[300] = {0};
  30275. int found[4] = {0};
  30276. int numfnd = 0;
  30277. int i;
  30278. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  30279. while (EXPECT_SUCCESS() &&
  30280. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  30281. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  30282. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30283. found[0] = 1;
  30284. continue;
  30285. }
  30286. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  30287. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30288. found[1] = 1;
  30289. continue;
  30290. }
  30291. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  30292. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  30293. found[2] = 1;
  30294. continue;
  30295. }
  30296. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  30297. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  30298. found[3] = 1;
  30299. continue;
  30300. }
  30301. }
  30302. if (fp != XBADFILE)
  30303. XFCLOSE(fp);
  30304. for (i = 0; i < 4; i++) {
  30305. if (found[i] != 0)
  30306. numfnd++;
  30307. }
  30308. ExpectIntEQ(numfnd, 4);
  30309. }
  30310. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  30311. return EXPECT_RESULT();
  30312. }
  30313. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  30314. defined(HAVE_IO_TESTS_DEPENDENCIES)
  30315. static int test_wolfSSL_Tls13_ECH_params(void)
  30316. {
  30317. EXPECT_DECLS;
  30318. #if !defined(NO_WOLFSSL_CLIENT)
  30319. word32 outputLen = 0;
  30320. byte testBuf[72];
  30321. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  30322. WOLFSSL *ssl = wolfSSL_new(ctx);
  30323. ExpectNotNull(ctx);
  30324. ExpectNotNull(ssl);
  30325. /* invalid ctx */
  30326. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  30327. "ech-public-name.com", 0, 0, 0));
  30328. /* invalid public name */
  30329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  30330. 0, 0));
  30331. /* invalid algorithms */
  30332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  30333. "ech-public-name.com", 1000, 1000, 1000));
  30334. /* invalid ctx */
  30335. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  30336. &outputLen));
  30337. /* invalid output len */
  30338. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  30339. /* invalid ssl */
  30340. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  30341. (char*)testBuf, sizeof(testBuf)));
  30342. /* invalid configs64 */
  30343. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  30344. sizeof(testBuf)));
  30345. /* invalid size */
  30346. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  30347. (char*)testBuf, 0));
  30348. /* invalid ssl */
  30349. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  30350. sizeof(testBuf)));
  30351. /* invalid configs */
  30352. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  30353. sizeof(testBuf)));
  30354. /* invalid size */
  30355. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  30356. /* invalid ssl */
  30357. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  30358. /* invalid size */
  30359. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  30360. wolfSSL_free(ssl);
  30361. wolfSSL_CTX_free(ctx);
  30362. #endif /* !NO_WOLFSSL_CLIENT */
  30363. return EXPECT_RESULT();
  30364. }
  30365. static int test_wolfSSL_Tls13_ECH(void)
  30366. {
  30367. EXPECT_DECLS;
  30368. tcp_ready ready;
  30369. func_args client_args;
  30370. func_args server_args;
  30371. THREAD_TYPE serverThread;
  30372. callback_functions server_cbf;
  30373. callback_functions client_cbf;
  30374. SOCKET_T sockfd = 0;
  30375. WOLFSSL_CTX* ctx = NULL;
  30376. WOLFSSL* ssl = NULL;
  30377. const char* publicName = "ech-public-name.com";
  30378. const char* privateName = "ech-private-name.com";
  30379. int privateNameLen = 20;
  30380. char reply[1024];
  30381. int replyLen = 0;
  30382. byte rawEchConfig[128];
  30383. word32 rawEchConfigLen = sizeof(rawEchConfig);
  30384. InitTcpReady(&ready);
  30385. ready.port = 22222;
  30386. XMEMSET(&client_args, 0, sizeof(func_args));
  30387. XMEMSET(&server_args, 0, sizeof(func_args));
  30388. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30389. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30390. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30391. /* create the server context here so we can get the ech config */
  30392. ExpectNotNull(server_cbf.ctx =
  30393. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  30394. /* generate ech config */
  30395. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  30396. publicName, 0, 0, 0));
  30397. /* get the config for the client to use */
  30398. ExpectIntEQ(WOLFSSL_SUCCESS,
  30399. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  30400. &rawEchConfigLen));
  30401. server_args.callbacks = &server_cbf;
  30402. server_args.signal = &ready;
  30403. /* start server task */
  30404. start_thread(server_task_ech, &server_args, &serverThread);
  30405. wait_tcp_ready(&server_args);
  30406. /* run as a TLS1.3 client */
  30407. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  30408. ExpectIntEQ(WOLFSSL_SUCCESS,
  30409. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30410. ExpectIntEQ(WOLFSSL_SUCCESS,
  30411. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30412. ExpectIntEQ(WOLFSSL_SUCCESS,
  30413. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30414. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30415. /* get connected the server task */
  30416. ExpectNotNull(ssl = wolfSSL_new(ctx));
  30417. /* set the ech configs for the client */
  30418. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  30419. rawEchConfigLen));
  30420. /* set the sni for the client */
  30421. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  30422. privateName, privateNameLen));
  30423. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30424. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30425. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  30426. privateNameLen);
  30427. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  30428. /* add th null terminator for string compare */
  30429. reply[replyLen] = 0;
  30430. /* check that the server replied with the private name */
  30431. ExpectStrEQ(privateName, reply);
  30432. wolfSSL_free(ssl);
  30433. wolfSSL_CTX_free(ctx);
  30434. CloseSocket(sockfd);
  30435. join_thread(serverThread);
  30436. FreeTcpReady(&ready);
  30437. return EXPECT_RESULT();
  30438. }
  30439. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  30440. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30441. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30442. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30443. static int post_auth_version_cb(WOLFSSL* ssl)
  30444. {
  30445. EXPECT_DECLS;
  30446. /* do handshake and then test version error */
  30447. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30448. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30449. return EXPECT_RESULT();
  30450. }
  30451. static int post_auth_version_client_cb(WOLFSSL* ssl)
  30452. {
  30453. EXPECT_DECLS;
  30454. /* do handshake and then test version error */
  30455. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30456. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30457. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30458. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30459. /* check was added to error queue */
  30460. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30461. /* check the string matches expected string */
  30462. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30463. "WRONG_SSL_VERSION");
  30464. #endif
  30465. return EXPECT_RESULT();
  30466. }
  30467. static int post_auth_cb(WOLFSSL* ssl)
  30468. {
  30469. EXPECT_DECLS;
  30470. WOLFSSL_X509* x509 = NULL;
  30471. /* do handshake and then test version error */
  30472. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30473. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30474. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30475. wolfSSL_X509_free(x509);
  30476. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30477. return EXPECT_RESULT();
  30478. }
  30479. static int set_post_auth_cb(WOLFSSL* ssl)
  30480. {
  30481. if (!wolfSSL_is_server(ssl)) {
  30482. EXPECT_DECLS;
  30483. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30484. return EXPECT_RESULT();
  30485. }
  30486. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30487. return TEST_SUCCESS;
  30488. }
  30489. #endif
  30490. static int test_wolfSSL_Tls13_postauth(void)
  30491. {
  30492. EXPECT_DECLS;
  30493. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30494. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30495. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30496. test_ssl_cbf server_cbf;
  30497. test_ssl_cbf client_cbf;
  30498. /* test version failure doing post auth with TLS 1.2 connection */
  30499. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  30500. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  30501. server_cbf.method = wolfTLSv1_2_server_method;
  30502. server_cbf.ssl_ready = set_post_auth_cb;
  30503. server_cbf.on_result = post_auth_version_cb;
  30504. client_cbf.ssl_ready = set_post_auth_cb;
  30505. client_cbf.on_result = post_auth_version_client_cb;
  30506. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30507. &server_cbf, NULL), TEST_SUCCESS);
  30508. /* tests on post auth with TLS 1.3 */
  30509. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  30510. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  30511. server_cbf.method = wolfTLSv1_3_server_method;
  30512. server_cbf.ssl_ready = set_post_auth_cb;
  30513. client_cbf.ssl_ready = set_post_auth_cb;
  30514. server_cbf.on_result = post_auth_cb;
  30515. client_cbf.on_result = NULL;
  30516. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  30517. &server_cbf, NULL), TEST_SUCCESS);
  30518. #endif
  30519. return EXPECT_RESULT();
  30520. }
  30521. static int test_wolfSSL_X509_NID(void)
  30522. {
  30523. EXPECT_DECLS;
  30524. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30525. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  30526. int sigType;
  30527. int nameSz;
  30528. X509* cert = NULL;
  30529. EVP_PKEY* pubKeyTmp = NULL;
  30530. X509_NAME* name = NULL;
  30531. char commonName[80];
  30532. char countryName[80];
  30533. char localityName[80];
  30534. char stateName[80];
  30535. char orgName[80];
  30536. char orgUnit[80];
  30537. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  30538. /* convert cert from DER to internal WOLFSSL_X509 struct */
  30539. ExpectNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  30540. sizeof_client_cert_der_2048));
  30541. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  30542. /* extract PUBLIC KEY from cert */
  30543. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  30544. /* extract signatureType */
  30545. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  30546. /* extract subjectName info */
  30547. ExpectNotNull(name = X509_get_subject_name(cert));
  30548. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  30549. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30550. NULL, 0)), 0);
  30551. ExpectIntEQ(nameSz, 15);
  30552. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30553. commonName, sizeof(commonName))), 0);
  30554. ExpectIntEQ(nameSz, 15);
  30555. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  30556. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30557. commonName, 9)), 0);
  30558. ExpectIntEQ(nameSz, 8);
  30559. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  30560. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  30561. countryName, sizeof(countryName))), 0);
  30562. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  30563. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  30564. localityName, sizeof(localityName))), 0);
  30565. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  30566. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  30567. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  30568. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  30569. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  30570. orgName, sizeof(orgName))), 0);
  30571. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  30572. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  30573. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  30574. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  30575. EVP_PKEY_free(pubKeyTmp);
  30576. X509_free(cert);
  30577. #endif
  30578. return EXPECT_RESULT();
  30579. }
  30580. static int test_wolfSSL_CTX_set_srp_username(void)
  30581. {
  30582. EXPECT_DECLS;
  30583. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30584. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30585. WOLFSSL_CTX* ctx = NULL;
  30586. WOLFSSL* ssl = NULL;
  30587. const char *username = "TESTUSER";
  30588. const char *password = "TESTPASSWORD";
  30589. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30590. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30591. SSL_SUCCESS);
  30592. wolfSSL_CTX_free(ctx);
  30593. ctx = NULL;
  30594. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30595. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30596. SSL_SUCCESS);
  30597. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30598. SSL_SUCCESS);
  30599. ExpectNotNull(ssl = SSL_new(ctx));
  30600. ExpectNotNull(SSL_get_srp_username(ssl));
  30601. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  30602. wolfSSL_free(ssl);
  30603. wolfSSL_CTX_free(ctx);
  30604. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30605. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30606. return EXPECT_RESULT();
  30607. }
  30608. static int test_wolfSSL_CTX_set_srp_password(void)
  30609. {
  30610. EXPECT_DECLS;
  30611. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  30612. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30613. WOLFSSL_CTX* ctx = NULL;
  30614. const char *username = "TESTUSER";
  30615. const char *password = "TESTPASSWORD";
  30616. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30617. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30618. SSL_SUCCESS);
  30619. wolfSSL_CTX_free(ctx);
  30620. ctx = NULL;
  30621. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30622. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  30623. SSL_SUCCESS);
  30624. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  30625. SSL_SUCCESS);
  30626. wolfSSL_CTX_free(ctx);
  30627. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30628. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30629. return EXPECT_RESULT();
  30630. }
  30631. static int test_wolfSSL_X509_STORE(void)
  30632. {
  30633. EXPECT_DECLS;
  30634. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30635. X509_STORE *store = NULL;
  30636. #ifdef HAVE_CRL
  30637. X509_STORE_CTX *storeCtx = NULL;
  30638. X509_CRL *crl = NULL;
  30639. X509 *ca = NULL;
  30640. X509 *cert = NULL;
  30641. const char crlPem[] = "./certs/crl/crl.revoked";
  30642. const char srvCert[] = "./certs/server-revoked-cert.pem";
  30643. const char caCert[] = "./certs/ca-cert.pem";
  30644. XFILE fp = XBADFILE;
  30645. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30646. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30647. SSL_FILETYPE_PEM)));
  30648. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30649. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30650. SSL_FILETYPE_PEM)));
  30651. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  30652. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30653. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30654. X509_STORE_free(store);
  30655. store = NULL;
  30656. X509_STORE_CTX_free(storeCtx);
  30657. storeCtx = NULL;
  30658. X509_free(cert);
  30659. cert = NULL;
  30660. X509_free(ca);
  30661. ca = NULL;
  30662. /* should fail to verify now after adding in CRL */
  30663. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30664. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30665. SSL_FILETYPE_PEM)));
  30666. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30667. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  30668. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  30669. NULL, NULL));
  30670. if (fp != XBADFILE)
  30671. XFCLOSE(fp);
  30672. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  30673. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  30674. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  30675. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30676. SSL_FILETYPE_PEM)));
  30677. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30678. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30679. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  30680. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  30681. X509_CRL_free(crl);
  30682. crl = NULL;
  30683. X509_STORE_free(store);
  30684. store = NULL;
  30685. X509_STORE_CTX_free(storeCtx);
  30686. storeCtx = NULL;
  30687. X509_free(cert);
  30688. cert = NULL;
  30689. X509_free(ca);
  30690. ca = NULL;
  30691. #endif /* HAVE_CRL */
  30692. #ifndef WOLFCRYPT_ONLY
  30693. {
  30694. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30695. SSL_CTX* ctx = NULL;
  30696. SSL* ssl = NULL;
  30697. int i;
  30698. for (i = 0; i < 2; i++) {
  30699. #ifndef NO_WOLFSSL_SERVER
  30700. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30701. #else
  30702. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30703. #endif
  30704. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30705. SSL_CTX_set_cert_store(ctx, store);
  30706. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30707. SSL_CTX_set_cert_store(ctx, store);
  30708. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  30709. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30710. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30711. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30712. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30713. ExpectNotNull(ssl = SSL_new(ctx));
  30714. if (i == 0) {
  30715. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  30716. SSL_SUCCESS);
  30717. }
  30718. else {
  30719. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  30720. #ifdef OPENSSL_ALL
  30721. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  30722. #endif
  30723. }
  30724. if (EXPECT_FAIL() || (i == 1)) {
  30725. X509_STORE_free(store);
  30726. store = NULL;
  30727. }
  30728. SSL_free(ssl);
  30729. ssl = NULL;
  30730. SSL_CTX_free(ctx);
  30731. ctx = NULL;
  30732. }
  30733. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30734. }
  30735. #endif
  30736. #endif
  30737. return EXPECT_RESULT();
  30738. }
  30739. static int test_wolfSSL_X509_STORE_load_locations(void)
  30740. {
  30741. EXPECT_DECLS;
  30742. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30743. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30744. SSL_CTX *ctx = NULL;
  30745. X509_STORE *store = NULL;
  30746. const char ca_file[] = "./certs/ca-cert.pem";
  30747. const char client_pem_file[] = "./certs/client-cert.pem";
  30748. const char client_der_file[] = "./certs/client-cert.der";
  30749. const char ecc_file[] = "./certs/ecc-key.pem";
  30750. const char certs_path[] = "./certs/";
  30751. const char bad_path[] = "./bad-path/";
  30752. #ifdef HAVE_CRL
  30753. const char crl_path[] = "./certs/crl/";
  30754. const char crl_file[] = "./certs/crl/crl.pem";
  30755. #endif
  30756. #ifndef NO_WOLFSSL_SERVER
  30757. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30758. #else
  30759. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30760. #endif
  30761. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  30762. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  30763. WOLFSSL_SUCCESS);
  30764. /* Test bad arguments */
  30765. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  30766. WOLFSSL_FAILURE);
  30767. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  30768. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  30769. WOLFSSL_FAILURE);
  30770. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  30771. WOLFSSL_FAILURE);
  30772. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  30773. WOLFSSL_FAILURE);
  30774. #ifdef HAVE_CRL
  30775. /* Test with CRL */
  30776. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  30777. WOLFSSL_SUCCESS);
  30778. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  30779. WOLFSSL_SUCCESS);
  30780. #endif
  30781. /* Test with CA */
  30782. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  30783. WOLFSSL_SUCCESS);
  30784. /* Test with client_cert and certs path */
  30785. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  30786. WOLFSSL_SUCCESS);
  30787. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  30788. WOLFSSL_SUCCESS);
  30789. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  30790. /* Clear nodes */
  30791. ERR_clear_error();
  30792. #endif
  30793. SSL_CTX_free(ctx);
  30794. #endif
  30795. return EXPECT_RESULT();
  30796. }
  30797. static int test_X509_STORE_get0_objects(void)
  30798. {
  30799. EXPECT_DECLS;
  30800. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  30801. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30802. X509_STORE *store = NULL;
  30803. X509_STORE *store_cpy = NULL;
  30804. SSL_CTX *ctx = NULL;
  30805. X509_OBJECT *obj = NULL;
  30806. STACK_OF(X509_OBJECT) *objs = NULL;
  30807. int i;
  30808. /* Setup store */
  30809. #ifndef NO_WOLFSSL_SERVER
  30810. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30811. #else
  30812. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30813. #endif
  30814. ExpectNotNull(store_cpy = X509_STORE_new());
  30815. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  30816. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  30817. WOLFSSL_SUCCESS);
  30818. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  30819. WOLFSSL_SUCCESS);
  30820. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  30821. WOLFSSL_SUCCESS);
  30822. #ifdef HAVE_CRL
  30823. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  30824. WOLFSSL_SUCCESS);
  30825. #endif
  30826. /* Store ready */
  30827. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  30828. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  30829. #ifdef HAVE_CRL
  30830. #ifdef WOLFSSL_SIGNER_DER_CERT
  30831. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  30832. #else
  30833. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  30834. #endif
  30835. #else
  30836. #ifdef WOLFSSL_SIGNER_DER_CERT
  30837. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  30838. #else
  30839. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  30840. #endif
  30841. #endif
  30842. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  30843. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  30844. switch (X509_OBJECT_get_type(obj)) {
  30845. case X509_LU_X509:
  30846. {
  30847. WOLFSSL_X509* x509;
  30848. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  30849. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  30850. break;
  30851. }
  30852. case X509_LU_CRL:
  30853. #ifdef HAVE_CRL
  30854. {
  30855. WOLFSSL_CRL* crl = NULL;
  30856. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  30857. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  30858. break;
  30859. }
  30860. #endif
  30861. case X509_LU_NONE:
  30862. default:
  30863. Fail(("X509_OBJECT_get_type should return x509 or crl "
  30864. "(when built with crl support)"),
  30865. ("Unrecognized X509_OBJECT type or none"));
  30866. }
  30867. }
  30868. X509_STORE_free(store_cpy);
  30869. SSL_CTX_free(ctx);
  30870. #endif
  30871. return EXPECT_RESULT();
  30872. }
  30873. static int test_wolfSSL_BN_CTX(void)
  30874. {
  30875. EXPECT_DECLS;
  30876. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  30877. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  30878. WOLFSSL_BN_CTX* bn_ctx = NULL;
  30879. WOLFSSL_BIGNUM* t = NULL;
  30880. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  30881. /* No implementation. */
  30882. BN_CTX_init(NULL);
  30883. ExpectNotNull(t = BN_CTX_get(NULL));
  30884. BN_free(t);
  30885. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  30886. BN_free(t);
  30887. #ifndef NO_WOLFSSL_STUB
  30888. /* No implementation. */
  30889. BN_CTX_start(NULL);
  30890. BN_CTX_start(bn_ctx);
  30891. #endif
  30892. BN_CTX_free(NULL);
  30893. BN_CTX_free(bn_ctx);
  30894. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  30895. return EXPECT_RESULT();
  30896. }
  30897. static int test_wolfSSL_BN(void)
  30898. {
  30899. EXPECT_DECLS;
  30900. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  30901. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  30902. BIGNUM* a = NULL;
  30903. BIGNUM* b = NULL;
  30904. BIGNUM* c = NULL;
  30905. BIGNUM* d = NULL;
  30906. BIGNUM emptyBN;
  30907. /* Setup */
  30908. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  30909. /* internal not set emptyBN. */
  30910. ExpectNotNull(a = BN_new());
  30911. ExpectNotNull(b = BN_new());
  30912. ExpectNotNull(c = BN_dup(b));
  30913. ExpectNotNull(d = BN_new());
  30914. /* Invalid parameter testing. */
  30915. BN_free(NULL);
  30916. ExpectNull(BN_dup(NULL));
  30917. ExpectNull(BN_dup(&emptyBN));
  30918. ExpectNull(BN_copy(NULL, NULL));
  30919. ExpectNull(BN_copy(b, NULL));
  30920. ExpectNull(BN_copy(NULL, c));
  30921. ExpectNull(BN_copy(b, &emptyBN));
  30922. ExpectNull(BN_copy(&emptyBN, c));
  30923. BN_clear(NULL);
  30924. BN_clear(&emptyBN);
  30925. ExpectIntEQ(BN_num_bytes(NULL), 0);
  30926. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  30927. ExpectIntEQ(BN_num_bits(NULL), 0);
  30928. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  30929. ExpectIntEQ(BN_is_negative(NULL), 0);
  30930. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  30931. /* END Invalid Parameters */
  30932. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30933. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30934. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  30935. ExpectIntEQ(BN_num_bits(a), 2);
  30936. ExpectIntEQ(BN_num_bytes(a), 1);
  30937. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  30938. defined(WOLFSSL_SP_INT_NEGATIVE))
  30939. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  30940. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  30941. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  30942. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  30943. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  30944. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30945. {
  30946. /* Do additional tests on negative BN conversions. */
  30947. char* ret = NULL;
  30948. ASN1_INTEGER* asn1 = NULL;
  30949. BIGNUM* tmp = NULL;
  30950. /* Sanity check we have a negative BN. */
  30951. ExpectIntEQ(BN_is_negative(c), 1);
  30952. ExpectNotNull(ret = BN_bn2dec(c));
  30953. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  30954. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  30955. ret = NULL;
  30956. /* Convert to ASN1_INTEGER and back to BN. */
  30957. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  30958. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  30959. /* After converting back BN should be negative and correct. */
  30960. ExpectIntEQ(BN_is_negative(tmp), 1);
  30961. ExpectNotNull(ret = BN_bn2dec(tmp));
  30962. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  30963. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  30964. ASN1_INTEGER_free(asn1);
  30965. BN_free(tmp);
  30966. }
  30967. #endif
  30968. ExpectIntEQ(BN_get_word(c), 4);
  30969. #endif
  30970. ExpectIntEQ(BN_set_word(a, 3), 1);
  30971. ExpectIntEQ(BN_set_word(b, 3), 1);
  30972. ExpectIntEQ(BN_set_word(c, 4), 1);
  30973. /* NULL == NULL, NULL < num, num > NULL */
  30974. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  30975. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  30976. ExpectIntLT(BN_cmp(NULL, b), 0);
  30977. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  30978. ExpectIntGT(BN_cmp(a, NULL), 0);
  30979. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  30980. ExpectIntEQ(BN_cmp(a, b), 0);
  30981. ExpectIntLT(BN_cmp(a, c), 0);
  30982. ExpectIntGT(BN_cmp(c, b), 0);
  30983. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  30984. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  30985. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  30986. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  30987. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  30988. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  30989. BN_clear(a);
  30990. BN_free(a);
  30991. BN_free(b);
  30992. BN_free(c);
  30993. BN_clear_free(d);
  30994. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  30995. return EXPECT_RESULT();
  30996. }
  30997. static int test_wolfSSL_BN_init(void)
  30998. {
  30999. EXPECT_DECLS;
  31000. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31001. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31002. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  31003. BIGNUM* ap = NULL;
  31004. BIGNUM bv;
  31005. BIGNUM cv;
  31006. BIGNUM dv;
  31007. ExpectNotNull(ap = BN_new());
  31008. BN_init(NULL);
  31009. XMEMSET(&bv, 0, sizeof(bv));
  31010. ExpectNull(BN_dup(&bv));
  31011. BN_init(&bv);
  31012. BN_init(&cv);
  31013. BN_init(&dv);
  31014. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31015. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31016. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31017. /* a^b mod c = */
  31018. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31019. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31020. /* check result 3^2 mod 5 */
  31021. ExpectIntEQ(BN_get_word(&dv), 4);
  31022. /* a*b mod c = */
  31023. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31024. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31025. /* check result 3*2 mod 5 */
  31026. ExpectIntEQ(BN_get_word(&dv), 1);
  31027. BN_free(ap);
  31028. #endif
  31029. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31030. return EXPECT_RESULT();
  31031. }
  31032. static int test_wolfSSL_BN_enc_dec(void)
  31033. {
  31034. EXPECT_DECLS;
  31035. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31036. BIGNUM* a = NULL;
  31037. BIGNUM* b = NULL;
  31038. BIGNUM* c = NULL;
  31039. BIGNUM emptyBN;
  31040. char* str = NULL;
  31041. const char* emptyStr = "";
  31042. const char* numberStr = "12345";
  31043. const char* badStr = "g12345";
  31044. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31045. const char* twoStr = "2";
  31046. #endif
  31047. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  31048. unsigned char outNum[5];
  31049. /* Setup */
  31050. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31051. ExpectNotNull(a = BN_new());
  31052. ExpectNotNull(b = BN_new());
  31053. ExpectIntEQ(BN_set_word(a, 2), 1);
  31054. /* Invalid parameters */
  31055. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  31056. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  31057. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  31058. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  31059. ExpectNull(BN_bn2hex(NULL));
  31060. ExpectNull(BN_bn2hex(&emptyBN));
  31061. ExpectNull(BN_bn2dec(NULL));
  31062. ExpectNull(BN_bn2dec(&emptyBN));
  31063. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  31064. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  31065. ExpectNull(BN_bin2bn(binNum, -1, a));
  31066. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  31067. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  31068. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  31069. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  31070. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  31071. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  31072. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  31073. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  31074. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  31075. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  31076. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  31077. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  31078. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  31079. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  31080. ExpectIntEQ(BN_set_word(a, 2), 1);
  31081. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  31082. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  31083. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  31084. ExpectIntEQ(BN_cmp(a, b), 0);
  31085. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  31086. ExpectIntEQ(BN_cmp(a, b), -1);
  31087. ExpectNotNull(str = BN_bn2hex(a));
  31088. ExpectNotNull(BN_hex2bn(&b, str));
  31089. ExpectIntEQ(BN_cmp(a, b), 0);
  31090. ExpectNotNull(BN_hex2bn(&b, numberStr));
  31091. ExpectIntEQ(BN_cmp(a, b), -1);
  31092. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31093. str = NULL;
  31094. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31095. ExpectNotNull(str = BN_bn2dec(a));
  31096. ExpectStrEQ(str, twoStr);
  31097. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31098. str = NULL;
  31099. #ifndef NO_RSA
  31100. ExpectNotNull(str = BN_bn2dec(a));
  31101. ExpectNotNull(BN_dec2bn(&b, str));
  31102. ExpectIntEQ(BN_cmp(a, b), 0);
  31103. ExpectNotNull(BN_dec2bn(&b, numberStr));
  31104. ExpectIntEQ(BN_cmp(a, b), -1);
  31105. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  31106. str = NULL;
  31107. #else
  31108. /* No implementation - fail with good parameters. */
  31109. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  31110. #endif
  31111. #endif
  31112. BN_free(b);
  31113. BN_free(a);
  31114. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31115. return EXPECT_RESULT();
  31116. }
  31117. static int test_wolfSSL_BN_word(void)
  31118. {
  31119. EXPECT_DECLS;
  31120. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31121. BIGNUM* a = NULL;
  31122. BIGNUM* b = NULL;
  31123. BIGNUM* c = NULL;
  31124. BIGNUM av;
  31125. ExpectNotNull(a = BN_new());
  31126. ExpectNotNull(b = BN_new());
  31127. ExpectNotNull(c = BN_new());
  31128. XMEMSET(&av, 0, sizeof(av));
  31129. /* Invalid parameter. */
  31130. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  31131. ExpectIntEQ(BN_add_word(&av, 3), 0);
  31132. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  31133. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  31134. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  31135. ExpectIntEQ(BN_set_word(&av, 3), 0);
  31136. ExpectIntEQ(BN_get_word(NULL), 0);
  31137. ExpectIntEQ(BN_get_word(&av), 0);
  31138. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  31139. ExpectIntEQ(BN_is_word(&av, 3), 0);
  31140. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  31141. !defined(NO_DSA))
  31142. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  31143. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  31144. #endif
  31145. ExpectIntEQ(BN_one(NULL), 0);
  31146. ExpectIntEQ(BN_one(&av), 0);
  31147. BN_zero(NULL);
  31148. BN_zero(&av);
  31149. ExpectIntEQ(BN_is_one(NULL), 0);
  31150. ExpectIntEQ(BN_is_one(&av), 0);
  31151. ExpectIntEQ(BN_is_zero(NULL), 0);
  31152. ExpectIntEQ(BN_is_zero(&av), 0);
  31153. ExpectIntEQ(BN_set_word(a, 3), 1);
  31154. ExpectIntEQ(BN_set_word(b, 2), 1);
  31155. ExpectIntEQ(BN_set_word(c, 5), 1);
  31156. /* a + 3 = */
  31157. ExpectIntEQ(BN_add_word(a, 3), 1);
  31158. /* check result 3 + 3*/
  31159. ExpectIntEQ(BN_get_word(a), 6);
  31160. ExpectIntEQ(BN_is_word(a, 6), 1);
  31161. ExpectIntEQ(BN_is_word(a, 5), 0);
  31162. /* set a back to 3 */
  31163. ExpectIntEQ(BN_set_word(a, 3), 1);
  31164. /* a - 3 = */
  31165. ExpectIntEQ(BN_sub_word(a, 3), 1);
  31166. /* check result 3 - 3*/
  31167. ExpectIntEQ(BN_get_word(a), 0);
  31168. ExpectIntEQ(BN_one(a), 1);
  31169. ExpectIntEQ(BN_is_word(a, 1), 1);
  31170. ExpectIntEQ(BN_is_word(a, 0), 0);
  31171. ExpectIntEQ(BN_is_one(a), 1);
  31172. ExpectIntEQ(BN_is_zero(a), 0);
  31173. BN_zero(a);
  31174. ExpectIntEQ(BN_is_word(a, 0), 1);
  31175. ExpectIntEQ(BN_is_word(a, 1), 0);
  31176. ExpectIntEQ(BN_is_zero(a), 1);
  31177. ExpectIntEQ(BN_is_one(a), 0);
  31178. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  31179. !defined(NO_DSA))
  31180. ExpectIntEQ(BN_set_word(a, 5), 1);
  31181. ExpectIntEQ(BN_mod_word(a, 3), 2);
  31182. ExpectIntEQ(BN_mod_word(a, 0), -1);
  31183. #endif
  31184. BN_free(c);
  31185. BN_free(b);
  31186. BN_free(a);
  31187. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31188. return EXPECT_RESULT();
  31189. }
  31190. static int test_wolfSSL_BN_bits(void)
  31191. {
  31192. EXPECT_DECLS;
  31193. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31194. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31195. BIGNUM* a = NULL;
  31196. BIGNUM emptyBN;
  31197. /* Setup */
  31198. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31199. ExpectNotNull(a = BN_new());
  31200. /* Invalid parameters. */
  31201. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  31202. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  31203. ExpectIntEQ(BN_set_bit(a, -1), 0);
  31204. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  31205. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  31206. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  31207. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  31208. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  31209. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  31210. ExpectIntEQ(BN_is_odd(NULL), 0);
  31211. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  31212. ExpectIntEQ(BN_set_word(a, 0), 1);
  31213. ExpectIntEQ(BN_is_zero(a), 1);
  31214. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  31215. ExpectIntEQ(BN_is_zero(a), 0);
  31216. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  31217. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  31218. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  31219. ExpectIntEQ(BN_is_zero(a), 1);
  31220. ExpectIntEQ(BN_set_bit(a, 0), 1);
  31221. ExpectIntEQ(BN_is_odd(a), 1);
  31222. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  31223. ExpectIntEQ(BN_is_odd(a), 0);
  31224. ExpectIntEQ(BN_set_bit(a, 1), 1);
  31225. ExpectIntEQ(BN_is_odd(a), 0);
  31226. ExpectIntEQ(BN_set_bit(a, 129), 1);
  31227. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  31228. #ifndef NO_WOLFSSL_STUB
  31229. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  31230. #endif
  31231. BN_free(a);
  31232. #endif
  31233. return EXPECT_RESULT();
  31234. }
  31235. static int test_wolfSSL_BN_shift(void)
  31236. {
  31237. EXPECT_DECLS;
  31238. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31239. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31240. BIGNUM* a = NULL;
  31241. BIGNUM* b = NULL;
  31242. BIGNUM emptyBN;
  31243. /* Setup */
  31244. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31245. ExpectNotNull(a = BN_new());
  31246. ExpectNotNull(b = BN_new());
  31247. /* Invalid parameters. */
  31248. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  31249. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  31250. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  31251. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  31252. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  31253. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  31254. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  31255. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  31256. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  31257. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  31258. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  31259. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  31260. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  31261. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  31262. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  31263. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  31264. ExpectIntEQ(BN_set_word(a, 1), 1);
  31265. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  31266. ExpectIntEQ(BN_is_word(b, 2), 1);
  31267. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  31268. ExpectIntEQ(BN_is_word(a, 2), 1);
  31269. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  31270. ExpectIntEQ(BN_is_word(b, 1), 1);
  31271. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  31272. ExpectIntEQ(BN_is_word(a, 1), 1);
  31273. BN_free(b);
  31274. BN_free(a);
  31275. #endif
  31276. return EXPECT_RESULT();
  31277. }
  31278. static int test_wolfSSL_BN_math(void)
  31279. {
  31280. EXPECT_DECLS;
  31281. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31282. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31283. BIGNUM* a = NULL;
  31284. BIGNUM* b = NULL;
  31285. BIGNUM* r = NULL;
  31286. BIGNUM* rem = NULL;
  31287. BIGNUM emptyBN;
  31288. BN_ULONG val1;
  31289. BN_ULONG val2;
  31290. /* Setup */
  31291. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31292. ExpectNotNull(a = BN_new());
  31293. ExpectNotNull(b = BN_new());
  31294. ExpectNotNull(r = BN_new());
  31295. ExpectNotNull(rem = BN_new());
  31296. /* Invalid parameters. */
  31297. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  31298. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  31299. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  31300. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  31301. ExpectIntEQ(BN_add(r, a, NULL), 0);
  31302. ExpectIntEQ(BN_add(r, NULL, b), 0);
  31303. ExpectIntEQ(BN_add(NULL, a, b), 0);
  31304. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  31305. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  31306. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  31307. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  31308. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  31309. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  31310. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  31311. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  31312. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  31313. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  31314. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  31315. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  31316. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  31317. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  31318. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  31319. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  31320. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  31321. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  31322. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  31323. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  31324. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  31325. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  31326. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  31327. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  31328. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  31329. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  31330. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  31331. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  31332. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31333. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  31334. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  31335. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  31336. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  31337. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  31338. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  31339. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  31340. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  31341. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  31342. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  31343. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  31344. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  31345. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  31346. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  31347. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  31348. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31349. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31350. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  31351. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  31352. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  31353. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  31354. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  31355. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  31356. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  31357. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  31358. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  31359. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  31360. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  31361. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  31362. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  31363. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  31364. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31365. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  31366. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  31367. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  31368. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  31369. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  31370. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  31371. /* END Invalid parameters. */
  31372. val1 = 8;
  31373. val2 = 3;
  31374. ExpectIntEQ(BN_set_word(a, val1), 1);
  31375. ExpectIntEQ(BN_set_word(b, val2), 1);
  31376. ExpectIntEQ(BN_add(r, a, b), 1);
  31377. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  31378. ExpectIntEQ(BN_sub(r, a, b), 1);
  31379. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  31380. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  31381. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  31382. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  31383. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  31384. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  31385. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  31386. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  31387. BN_free(rem);
  31388. BN_free(r);
  31389. BN_free(b);
  31390. BN_free(a);
  31391. #endif
  31392. return EXPECT_RESULT();
  31393. }
  31394. static int test_wolfSSL_BN_math_mod(void)
  31395. {
  31396. EXPECT_DECLS;
  31397. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31398. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31399. BIGNUM* a = NULL;
  31400. BIGNUM* b = NULL;
  31401. BIGNUM* m = NULL;
  31402. BIGNUM* r = NULL;
  31403. BIGNUM* t = NULL;
  31404. BIGNUM emptyBN;
  31405. BN_ULONG val1;
  31406. BN_ULONG val2;
  31407. BN_ULONG val3;
  31408. /* Setup */
  31409. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31410. ExpectNotNull(a = BN_new());
  31411. ExpectNotNull(b = BN_new());
  31412. ExpectNotNull(m = BN_new());
  31413. ExpectNotNull(r = BN_new());
  31414. /* Invalid parameters. */
  31415. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  31416. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  31417. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  31418. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  31419. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  31420. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  31421. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  31422. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  31423. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  31424. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31425. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31426. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31427. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31428. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31429. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  31430. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  31431. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  31432. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  31433. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  31434. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  31435. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  31436. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  31437. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  31438. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  31439. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  31440. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  31441. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  31442. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31443. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31444. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31445. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31446. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31447. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  31448. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  31449. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  31450. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  31451. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  31452. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  31453. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  31454. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  31455. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  31456. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  31457. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  31458. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  31459. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  31460. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31461. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31462. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  31463. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  31464. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  31465. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  31466. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  31467. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  31468. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  31469. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  31470. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  31471. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  31472. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  31473. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  31474. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  31475. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  31476. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  31477. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  31478. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  31479. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  31480. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  31481. /* END Invalid parameters. */
  31482. val1 = 9;
  31483. val2 = 13;
  31484. val3 = 5;
  31485. ExpectIntEQ(BN_set_word(a, val1), 1);
  31486. ExpectIntEQ(BN_set_word(b, val2), 1);
  31487. ExpectIntEQ(BN_set_word(m, val3), 1);
  31488. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  31489. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  31490. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  31491. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  31492. ExpectIntEQ(BN_set_word(a, 2), 1);
  31493. ExpectIntEQ(BN_set_word(b, 3), 1);
  31494. ExpectIntEQ(BN_set_word(m, 5), 1);
  31495. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  31496. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  31497. ExpectIntEQ(BN_is_word(r, 3), 1);
  31498. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  31499. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  31500. ExpectIntEQ(BN_is_word(r, 3), 1);
  31501. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  31502. ExpectIntEQ(BN_is_word(t, 3), 1);
  31503. BN_free(t);
  31504. /* No inverse case. No inverse when a divides b. */
  31505. ExpectIntEQ(BN_set_word(a, 3), 1);
  31506. ExpectIntEQ(BN_set_word(m, 9), 1);
  31507. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  31508. BN_free(r);
  31509. BN_free(m);
  31510. BN_free(b);
  31511. BN_free(a);
  31512. #endif
  31513. return EXPECT_RESULT();
  31514. }
  31515. static int test_wolfSSL_BN_math_other(void)
  31516. {
  31517. EXPECT_DECLS;
  31518. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31519. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31520. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31521. BIGNUM* a = NULL;
  31522. BIGNUM* b = NULL;
  31523. BIGNUM* r = NULL;
  31524. BIGNUM emptyBN;
  31525. /* Setup */
  31526. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31527. ExpectNotNull(a = BN_new());
  31528. ExpectNotNull(b = BN_new());
  31529. ExpectNotNull(r = BN_new());
  31530. /* Invalid parameters. */
  31531. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  31532. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  31533. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  31534. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  31535. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  31536. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  31537. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  31538. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  31539. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  31540. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  31541. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  31542. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  31543. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  31544. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  31545. /* END Invalid parameters. */
  31546. /* No common factors between 2 and 3. */
  31547. ExpectIntEQ(BN_set_word(a, 2), 1);
  31548. ExpectIntEQ(BN_set_word(b, 3), 1);
  31549. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  31550. ExpectIntEQ(BN_is_word(r, 1), 1);
  31551. /* 3 is largest value that divides both 6 and 9. */
  31552. ExpectIntEQ(BN_set_word(a, 6), 1);
  31553. ExpectIntEQ(BN_set_word(b, 9), 1);
  31554. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  31555. ExpectIntEQ(BN_is_word(r, 3), 1);
  31556. /* GCD of 0 and 0 is undefined. */
  31557. ExpectIntEQ(BN_set_word(a, 0), 1);
  31558. ExpectIntEQ(BN_set_word(b, 0), 1);
  31559. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  31560. /* Teardown */
  31561. BN_free(r);
  31562. BN_free(b);
  31563. BN_free(a);
  31564. #endif
  31565. #endif
  31566. return EXPECT_RESULT();
  31567. }
  31568. static int test_wolfSSL_BN_rand(void)
  31569. {
  31570. EXPECT_DECLS;
  31571. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  31572. BIGNUM* bn = NULL;
  31573. BIGNUM* range = NULL;
  31574. BIGNUM emptyBN;
  31575. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31576. ExpectNotNull(bn = BN_new());
  31577. ExpectNotNull(range = BN_new());
  31578. /* Invalid parameters. */
  31579. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  31580. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  31581. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  31582. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  31583. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  31584. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  31585. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  31586. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  31587. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  31588. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  31589. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  31590. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  31591. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  31592. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  31593. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  31594. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  31595. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  31596. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  31597. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  31598. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31599. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31600. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31601. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31602. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31603. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31604. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31605. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31606. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31607. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31608. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31609. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31610. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31611. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31612. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31613. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31614. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  31615. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31616. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  31617. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31618. /* 1 bit random value must have no more than one top bit set. */
  31619. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31620. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31621. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31622. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31623. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31624. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  31625. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  31626. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  31627. /* END Invalid parameters. */
  31628. /* 0 bit random: 0. */
  31629. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31630. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31631. ExpectIntEQ(BN_is_zero(bn), 1);
  31632. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  31633. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  31634. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31635. ExpectIntEQ(BN_is_zero(bn), 1);
  31636. /* 1 bit random: 0 or 1. */
  31637. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  31638. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31639. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  31640. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31641. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31642. ExpectIntEQ(BN_get_word(bn), 1);
  31643. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31644. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31645. ExpectIntEQ(BN_get_word(bn), 1);
  31646. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  31647. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31648. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  31649. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31650. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31651. ExpectIntEQ(BN_get_word(bn), 1);
  31652. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  31653. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31654. ExpectIntEQ(BN_get_word(bn), 1);
  31655. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31656. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31657. ExpectIntEQ(BN_num_bits(bn), 8);
  31658. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31659. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31660. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31661. ExpectIntEQ(BN_num_bits(bn), 8);
  31662. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31663. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  31664. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31665. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31666. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  31667. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  31668. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31669. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  31670. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  31671. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31672. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31673. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  31674. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  31675. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  31676. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  31677. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  31678. * requested number of bits up to the nearest multiple of 8. E.g. in this
  31679. * case, requesting a 13-bit random number would actually return a 16-bit
  31680. * random number. */
  31681. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  31682. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31683. ExpectIntEQ(BN_num_bits(bn), 13);
  31684. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  31685. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  31686. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31687. ExpectIntEQ(BN_set_word(range, 0), 1);
  31688. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31689. ExpectIntEQ(BN_set_word(range, 1), 1);
  31690. ExpectIntEQ(BN_rand_range(bn, range), 1);
  31691. BN_free(bn);
  31692. BN_free(range);
  31693. #endif
  31694. return EXPECT_RESULT();
  31695. }
  31696. static int test_wolfSSL_BN_prime(void)
  31697. {
  31698. EXPECT_DECLS;
  31699. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  31700. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  31701. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31702. BIGNUM* a = NULL;
  31703. BIGNUM* add = NULL;
  31704. BIGNUM* rem = NULL;
  31705. BIGNUM emptyBN;
  31706. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  31707. ExpectNotNull(a = BN_new());
  31708. ExpectNotNull(add = BN_new());
  31709. ExpectNotNull(rem = BN_new());
  31710. /* Invalid parameters. */
  31711. /* BN_generate_prime_ex()
  31712. * prime - must have valid BIGNUM
  31713. * bits - Greater then 0
  31714. * safe - not supported, must be 0
  31715. * add - not supported, must be NULL
  31716. * rem - not supported, must be NULL
  31717. * cb - anything
  31718. */
  31719. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  31720. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  31721. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  31722. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  31723. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  31724. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  31725. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  31726. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  31727. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  31728. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  31729. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  31730. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  31731. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  31732. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  31733. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  31734. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  31735. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  31736. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  31737. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  31738. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  31739. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  31740. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  31741. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  31742. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  31743. /* END Invalid parameters. */
  31744. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  31745. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  31746. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  31747. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  31748. BN_free(rem);
  31749. BN_free(add);
  31750. BN_free(a);
  31751. #endif
  31752. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31753. return EXPECT_RESULT();
  31754. }
  31755. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31756. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31757. #define TEST_ARG 0x1234
  31758. static void msg_cb(int write_p, int version, int content_type,
  31759. const void *buf, size_t len, SSL *ssl, void *arg)
  31760. {
  31761. (void)write_p;
  31762. (void)version;
  31763. (void)content_type;
  31764. (void)buf;
  31765. (void)len;
  31766. (void)ssl;
  31767. AssertTrue(arg == (void*)TEST_ARG);
  31768. }
  31769. #endif
  31770. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  31771. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31772. #if defined(SESSION_CERTS)
  31773. #include "wolfssl/internal.h"
  31774. #endif
  31775. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31776. {
  31777. EXPECT_DECLS;
  31778. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31779. STACK_OF(X509)* sk = NULL;
  31780. X509* x509 = NULL;
  31781. int i, num;
  31782. BIO* bio = NULL;
  31783. #endif
  31784. ExpectNotNull(ctx);
  31785. ExpectNotNull(ssl);
  31786. fprintf(stderr, "\n===== msgcb called ====\n");
  31787. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31788. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31789. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31790. ExpectNotNull(SSL_get0_verified_chain(ssl));
  31791. #endif
  31792. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31793. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  31794. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  31795. if (sk == NULL) {
  31796. BIO_free(bio);
  31797. return TEST_FAIL;
  31798. }
  31799. num = sk_X509_num(sk);
  31800. ExpectTrue(num > 0);
  31801. for (i = 0; i < num; i++) {
  31802. ExpectNotNull(x509 = sk_X509_value(sk,i));
  31803. if (x509 == NULL)
  31804. break;
  31805. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  31806. X509_print(bio,x509);
  31807. fprintf(stderr, "\n\n");
  31808. }
  31809. BIO_free(bio);
  31810. #endif
  31811. return EXPECT_RESULT();
  31812. }
  31813. #endif
  31814. static int test_wolfSSL_msgCb(void)
  31815. {
  31816. EXPECT_DECLS;
  31817. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  31818. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31819. test_ssl_cbf client_cb;
  31820. test_ssl_cbf server_cb;
  31821. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31822. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31823. #ifndef WOLFSSL_NO_TLS12
  31824. client_cb.method = wolfTLSv1_2_client_method;
  31825. server_cb.method = wolfTLSv1_2_server_method;
  31826. #else
  31827. client_cb.method = wolfTLSv1_3_client_method;
  31828. server_cb.method = wolfTLSv1_3_server_method;
  31829. #endif
  31830. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31831. &server_cb, msgCb), TEST_SUCCESS);
  31832. #endif
  31833. return EXPECT_RESULT();
  31834. }
  31835. static int test_wolfSSL_either_side(void)
  31836. {
  31837. EXPECT_DECLS;
  31838. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31839. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  31840. test_ssl_cbf client_cb;
  31841. test_ssl_cbf server_cb;
  31842. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31843. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31844. /* Use different CTX for client and server */
  31845. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31846. ExpectNotNull(client_cb.ctx);
  31847. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31848. ExpectNotNull(server_cb.ctx);
  31849. /* we are responsible for free'ing WOLFSSL_CTX */
  31850. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31851. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31852. &server_cb, NULL), TEST_SUCCESS);
  31853. wolfSSL_CTX_free(client_cb.ctx);
  31854. wolfSSL_CTX_free(server_cb.ctx);
  31855. #endif
  31856. return EXPECT_RESULT();
  31857. }
  31858. static int test_wolfSSL_DTLS_either_side(void)
  31859. {
  31860. EXPECT_DECLS;
  31861. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31862. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  31863. test_ssl_cbf client_cb;
  31864. test_ssl_cbf server_cb;
  31865. XMEMSET(&client_cb, 0, sizeof(client_cb));
  31866. XMEMSET(&server_cb, 0, sizeof(server_cb));
  31867. /* Use different CTX for client and server */
  31868. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31869. ExpectNotNull(client_cb.ctx);
  31870. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31871. ExpectNotNull(server_cb.ctx);
  31872. /* we are responsible for free'ing WOLFSSL_CTX */
  31873. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31874. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  31875. &server_cb, NULL), TEST_SUCCESS);
  31876. wolfSSL_CTX_free(client_cb.ctx);
  31877. wolfSSL_CTX_free(server_cb.ctx);
  31878. #endif
  31879. return EXPECT_RESULT();
  31880. }
  31881. static int test_generate_cookie(void)
  31882. {
  31883. EXPECT_DECLS;
  31884. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  31885. SSL_CTX* ctx = NULL;
  31886. SSL* ssl = NULL;
  31887. byte buf[FOURK_BUF] = {0};
  31888. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  31889. ExpectNotNull(ssl = SSL_new(ctx));
  31890. /* Test unconnected */
  31891. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  31892. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  31893. wolfSSL_SetCookieCtx(ssl, ctx);
  31894. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  31895. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  31896. SSL_free(ssl);
  31897. SSL_CTX_free(ctx);
  31898. #endif
  31899. return EXPECT_RESULT();
  31900. }
  31901. static int test_wolfSSL_set_options(void)
  31902. {
  31903. EXPECT_DECLS;
  31904. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31905. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31906. WOLFSSL* ssl = NULL;
  31907. WOLFSSL_CTX* ctx = NULL;
  31908. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31909. char appData[] = "extra msg";
  31910. #endif
  31911. #ifdef OPENSSL_EXTRA
  31912. unsigned char protos[] = {
  31913. 7, 't', 'l', 's', '/', '1', '.', '2',
  31914. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  31915. };
  31916. unsigned int len = sizeof(protos);
  31917. void *arg = (void *)TEST_ARG;
  31918. #endif
  31919. #ifndef NO_WOLFSSL_SERVER
  31920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31921. #else
  31922. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31923. #endif
  31924. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31925. WOLFSSL_FILETYPE_PEM));
  31926. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31927. WOLFSSL_FILETYPE_PEM));
  31928. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  31929. == WOLFSSL_OP_NO_TLSv1);
  31930. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  31931. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31932. WOLFSSL_OP_NO_SSLv2)), 0);
  31933. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31934. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31935. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  31936. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31937. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31938. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31939. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31940. WOLFSSL_OP_NO_COMPRESSION));
  31941. wolfSSL_CTX_free(ctx);
  31942. ctx = NULL;
  31943. #ifndef NO_WOLFSSL_SERVER
  31944. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31945. #else
  31946. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31947. #endif
  31948. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31949. WOLFSSL_FILETYPE_PEM));
  31950. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31951. WOLFSSL_FILETYPE_PEM));
  31952. #ifdef OPENSSL_EXTRA
  31953. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  31954. #endif
  31955. ExpectNotNull(ssl = wolfSSL_new(ctx));
  31956. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31957. #ifdef HAVE_EX_DATA
  31958. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  31959. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31960. if (ssl != NULL) {
  31961. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  31962. appData, sizeof(appData)), 0);
  31963. }
  31964. #else
  31965. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  31966. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31967. #endif
  31968. #endif
  31969. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  31970. WOLFSSL_OP_NO_TLSv1);
  31971. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  31972. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31973. WOLFSSL_OP_NO_SSLv2)), 0);
  31974. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31975. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31976. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  31977. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31978. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31979. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31980. #ifdef OPENSSL_EXTRA
  31981. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31982. WOLFSSL_OP_NO_COMPRESSION));
  31983. #endif
  31984. #ifdef OPENSSL_EXTRA
  31985. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  31986. wolfSSL_set_msg_callback_arg(ssl, arg);
  31987. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31988. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  31989. #else
  31990. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  31991. #endif
  31992. #endif
  31993. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  31994. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  31995. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  31996. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  31997. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31998. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  31999. #else
  32000. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  32001. #endif
  32002. #endif /* HAVE_ALPN && !NO_BIO */
  32003. #endif
  32004. wolfSSL_free(ssl);
  32005. wolfSSL_CTX_free(ctx);
  32006. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32007. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32008. return EXPECT_RESULT();
  32009. }
  32010. static int test_wolfSSL_sk_SSL_CIPHER(void)
  32011. {
  32012. EXPECT_DECLS;
  32013. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32014. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32015. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32016. SSL* ssl = NULL;
  32017. SSL_CTX* ctx = NULL;
  32018. STACK_OF(SSL_CIPHER) *sk = NULL;
  32019. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  32020. #ifndef NO_WOLFSSL_SERVER
  32021. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32022. #else
  32023. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32024. #endif
  32025. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  32026. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32027. ExpectNotNull(ssl = SSL_new(ctx));
  32028. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  32029. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  32030. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  32031. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  32032. /* error case because connection has not been established yet */
  32033. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  32034. sk_SSL_CIPHER_free(dupSk);
  32035. /* sk is pointer to internal struct that should be free'd in SSL_free */
  32036. SSL_free(ssl);
  32037. SSL_CTX_free(ctx);
  32038. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32039. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32040. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32041. return EXPECT_RESULT();
  32042. }
  32043. static int test_wolfSSL_set1_curves_list(void)
  32044. {
  32045. EXPECT_DECLS;
  32046. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  32047. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32048. SSL* ssl = NULL;
  32049. SSL_CTX* ctx = NULL;
  32050. #ifndef NO_WOLFSSL_SERVER
  32051. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32052. #else
  32053. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32054. #endif
  32055. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  32056. SSL_FILETYPE_PEM));
  32057. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  32058. ExpectNotNull(ssl = SSL_new(ctx));
  32059. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  32060. #ifdef HAVE_ECC
  32061. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  32062. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  32063. #endif
  32064. #ifdef HAVE_CURVE25519
  32065. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  32066. #else
  32067. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  32068. #endif
  32069. #ifdef HAVE_CURVE448
  32070. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  32071. #else
  32072. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  32073. #endif
  32074. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  32075. #ifdef HAVE_ECC
  32076. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  32077. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  32078. #endif
  32079. #ifdef HAVE_CURVE25519
  32080. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  32081. #else
  32082. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  32083. #endif
  32084. #ifdef HAVE_CURVE448
  32085. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  32086. #else
  32087. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  32088. #endif
  32089. SSL_free(ssl);
  32090. SSL_CTX_free(ctx);
  32091. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32092. #endif
  32093. return EXPECT_RESULT();
  32094. }
  32095. static int test_wolfSSL_set1_sigalgs_list(void)
  32096. {
  32097. EXPECT_DECLS;
  32098. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  32099. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32100. SSL* ssl = NULL;
  32101. SSL_CTX* ctx = NULL;
  32102. #ifndef NO_WOLFSSL_SERVER
  32103. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32104. #else
  32105. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32106. #endif
  32107. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32108. SSL_FILETYPE_PEM));
  32109. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32110. ExpectNotNull(ssl = SSL_new(ctx));
  32111. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32112. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  32113. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  32114. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  32115. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  32116. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  32117. #ifndef NO_RSA
  32118. #ifndef NO_SHA256
  32119. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32120. WOLFSSL_FAILURE);
  32121. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  32122. WOLFSSL_FAILURE);
  32123. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  32124. WOLFSSL_SUCCESS);
  32125. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  32126. WOLFSSL_SUCCESS);
  32127. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  32128. WOLFSSL_FAILURE);
  32129. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  32130. WOLFSSL_FAILURE);
  32131. #ifdef WC_RSA_PSS
  32132. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  32133. WOLFSSL_SUCCESS);
  32134. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  32135. WOLFSSL_SUCCESS);
  32136. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  32137. WOLFSSL_SUCCESS);
  32138. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  32139. WOLFSSL_SUCCESS);
  32140. #endif
  32141. #ifdef WOLFSSL_SHA512
  32142. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32143. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32144. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32145. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  32146. #elif defined(WOLFSSL_SHA384)
  32147. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32148. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32149. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32150. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  32151. #endif
  32152. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  32153. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  32154. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  32155. WOLFSSL_FAILURE);
  32156. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  32157. WOLFSSL_FAILURE);
  32158. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  32159. WOLFSSL_FAILURE);
  32160. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  32161. WOLFSSL_FAILURE);
  32162. #endif
  32163. #endif
  32164. #ifdef HAVE_ECC
  32165. #ifndef NO_SHA256
  32166. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  32167. WOLFSSL_SUCCESS);
  32168. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  32169. WOLFSSL_SUCCESS);
  32170. #ifdef WOLFSSL_SHA512
  32171. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32172. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32173. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32174. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  32175. #elif defined(WOLFSSL_SHA384)
  32176. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  32177. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32178. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  32179. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  32180. #endif
  32181. #endif
  32182. #endif
  32183. #ifdef HAVE_ED25519
  32184. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  32185. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  32186. #endif
  32187. #ifdef HAVE_ED448
  32188. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  32189. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  32190. #endif
  32191. #ifndef NO_DSA
  32192. #ifndef NO_SHA256
  32193. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  32194. WOLFSSL_SUCCESS);
  32195. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  32196. WOLFSSL_SUCCESS);
  32197. #endif
  32198. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  32199. defined(WOLFSSL_ALLOW_TLS_SHA1))
  32200. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  32201. WOLFSSL_SUCCESS);
  32202. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  32203. WOLFSSL_SUCCESS);
  32204. #endif
  32205. #endif
  32206. SSL_free(ssl);
  32207. SSL_CTX_free(ctx);
  32208. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32209. #endif
  32210. return EXPECT_RESULT();
  32211. }
  32212. /* Testing wolfSSL_set_tlsext_status_type function.
  32213. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  32214. */
  32215. static int test_wolfSSL_set_tlsext_status_type(void)
  32216. {
  32217. EXPECT_DECLS;
  32218. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  32219. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  32220. SSL* ssl = NULL;
  32221. SSL_CTX* ctx = NULL;
  32222. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32223. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  32224. SSL_FILETYPE_PEM));
  32225. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  32226. ExpectNotNull(ssl = SSL_new(ctx));
  32227. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  32228. SSL_SUCCESS);
  32229. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  32230. SSL_free(ssl);
  32231. SSL_CTX_free(ctx);
  32232. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32233. return EXPECT_RESULT();
  32234. }
  32235. #ifndef NO_BIO
  32236. static int test_wolfSSL_PEM_read_bio(void)
  32237. {
  32238. EXPECT_DECLS;
  32239. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32240. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32241. byte buff[6000];
  32242. XFILE f = XBADFILE;
  32243. int bytes;
  32244. X509* x509 = NULL;
  32245. BIO* bio = NULL;
  32246. BUF_MEM* buf = NULL;
  32247. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32248. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  32249. if (f != XBADFILE)
  32250. XFCLOSE(f);
  32251. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32252. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32253. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32254. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32255. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32256. /* BIO should return the set EOF value */
  32257. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32258. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32259. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32260. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32261. BIO_free(bio);
  32262. BUF_MEM_free(buf);
  32263. X509_free(x509);
  32264. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32265. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32266. return EXPECT_RESULT();
  32267. }
  32268. #if defined(OPENSSL_EXTRA)
  32269. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32270. long argl, long ret)
  32271. {
  32272. (void)bio;
  32273. (void)cmd;
  32274. (void)argp;
  32275. (void)argi;
  32276. (void)argl;
  32277. return ret;
  32278. }
  32279. #endif
  32280. static int test_wolfSSL_BIO(void)
  32281. {
  32282. EXPECT_DECLS;
  32283. #if defined(OPENSSL_EXTRA)
  32284. const unsigned char* p = NULL;
  32285. byte buff[20];
  32286. BIO* bio1 = NULL;
  32287. BIO* bio2 = NULL;
  32288. BIO* bio3 = NULL;
  32289. char* bufPt = NULL;
  32290. int i;
  32291. for (i = 0; i < 20; i++) {
  32292. buff[i] = i;
  32293. }
  32294. /* test BIO_free with NULL */
  32295. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32296. /* Creating and testing type BIO_s_bio */
  32297. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  32298. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  32299. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  32300. /* read/write before set up */
  32301. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32302. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32303. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  32304. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32305. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32306. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32307. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32308. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  32309. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32310. /* write buffer full */
  32311. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32312. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32313. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32314. /* write the other direction with pair */
  32315. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32316. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  32317. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32318. /* try read */
  32319. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32320. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32321. /* try read using ctrl function */
  32322. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32323. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32324. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32325. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32326. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32327. for (i = 0; i < 20; i++) {
  32328. ExpectIntEQ((int)bufPt[i], i);
  32329. }
  32330. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32331. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32332. for (i = 0; i < 8; i++) {
  32333. ExpectIntEQ((int)bufPt[i], i);
  32334. }
  32335. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32336. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32337. /* new pair */
  32338. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32339. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32340. bio2 = NULL;
  32341. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32342. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32343. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32344. /* test wrap around... */
  32345. ExpectIntEQ(BIO_reset(bio1), 0);
  32346. ExpectIntEQ(BIO_reset(bio3), 0);
  32347. /* fill write buffer, read only small amount then write again */
  32348. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32349. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32350. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32351. for (i = 0; i < 4; i++) {
  32352. ExpectIntEQ(bufPt[i], i);
  32353. }
  32354. /* try writing over read index */
  32355. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32356. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  32357. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32358. /* read and write 0 bytes */
  32359. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32360. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32361. /* should read only to end of write buffer then need to read again */
  32362. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32363. for (i = 0; i < 16; i++) {
  32364. ExpectIntEQ(bufPt[i], buff[4 + i]);
  32365. }
  32366. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32367. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32368. for (i = 0; i < 4; i++) {
  32369. ExpectIntEQ(bufPt[i], 0);
  32370. }
  32371. /* read index should not have advanced with nread0 */
  32372. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32373. for (i = 0; i < 4; i++) {
  32374. ExpectIntEQ(bufPt[i], 0);
  32375. }
  32376. /* write and fill up buffer checking reset of index state */
  32377. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32378. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32379. /* test reset on data in bio1 write buffer */
  32380. ExpectIntEQ(BIO_reset(bio1), 0);
  32381. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32382. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32383. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32384. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32385. ExpectNotNull(p);
  32386. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  32387. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32388. for (i = 0; i < 6; i++) {
  32389. ExpectIntEQ(bufPt[i], i);
  32390. }
  32391. /* test case of writing twice with offset read index */
  32392. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32393. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32394. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32395. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32396. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32397. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32398. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32399. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32400. BIO_free(bio1);
  32401. bio1 = NULL;
  32402. BIO_free(bio3);
  32403. bio3 = NULL;
  32404. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32405. {
  32406. BIO* bioA = NULL;
  32407. BIO* bioB = NULL;
  32408. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32409. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32410. BIO_free(bioA);
  32411. bioA = NULL;
  32412. BIO_free(bioB);
  32413. bioB = NULL;
  32414. }
  32415. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32416. /* BIOs with file pointers */
  32417. #if !defined(NO_FILESYSTEM)
  32418. {
  32419. XFILE f1 = XBADFILE;
  32420. XFILE f2 = XBADFILE;
  32421. BIO* f_bio1 = NULL;
  32422. BIO* f_bio2 = NULL;
  32423. unsigned char cert[300];
  32424. char testFile[] = "tests/bio_write_test.txt";
  32425. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32426. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32427. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32428. /* Failure due to wrong BIO type */
  32429. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32430. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32431. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  32432. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32433. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  32434. WOLFSSL_SUCCESS);
  32435. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32436. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32437. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32438. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32439. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32440. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32441. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32442. ExpectIntEQ(BIO_reset(f_bio2), 0);
  32443. ExpectIntEQ(BIO_tell(NULL),-1);
  32444. ExpectIntEQ(BIO_tell(f_bio2),0);
  32445. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  32446. ExpectIntEQ(BIO_tell(f_bio2),4);
  32447. BIO_free(f_bio1);
  32448. f_bio1 = NULL;
  32449. BIO_free(f_bio2);
  32450. f_bio2 = NULL;
  32451. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32452. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32453. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32454. BIO_free(f_bio1);
  32455. f_bio1 = NULL;
  32456. }
  32457. #endif /* !defined(NO_FILESYSTEM) */
  32458. /* BIO info callback */
  32459. {
  32460. const char* testArg = "test";
  32461. BIO* cb_bio = NULL;
  32462. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32463. BIO_set_callback(cb_bio, bioCallback);
  32464. ExpectNotNull(BIO_get_callback(cb_bio));
  32465. BIO_set_callback(cb_bio, NULL);
  32466. ExpectNull(BIO_get_callback(cb_bio));
  32467. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32468. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32469. ExpectNull(BIO_get_callback_arg(NULL));
  32470. BIO_free(cb_bio);
  32471. cb_bio = NULL;
  32472. }
  32473. /* BIO_vfree */
  32474. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  32475. BIO_vfree(NULL);
  32476. BIO_vfree(bio1);
  32477. #endif
  32478. return EXPECT_RESULT();
  32479. }
  32480. #endif /* !NO_BIO */
  32481. static int test_wolfSSL_a2i_IPADDRESS(void)
  32482. {
  32483. EXPECT_DECLS;
  32484. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32485. const unsigned char* data = NULL;
  32486. int dataSz = 0;
  32487. ASN1_OCTET_STRING *st = NULL;
  32488. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32489. const unsigned char ipv6_exp[] = {
  32490. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32491. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32492. };
  32493. const unsigned char ipv6_home[] = {
  32494. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32495. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32496. };
  32497. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32498. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32499. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32500. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  32501. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32502. ASN1_STRING_free(st);
  32503. st = NULL;
  32504. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  32505. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32506. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  32507. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32508. ASN1_STRING_free(st);
  32509. st = NULL;
  32510. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32511. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  32512. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  32513. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32514. ASN1_STRING_free(st);
  32515. #endif
  32516. return EXPECT_RESULT();
  32517. }
  32518. static int test_wolfSSL_DES_ecb_encrypt(void)
  32519. {
  32520. EXPECT_DECLS;
  32521. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32522. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  32523. WOLFSSL_DES_key_schedule key;
  32524. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  32525. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  32526. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  32527. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32528. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32529. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32530. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32531. /* Encrypt messages */
  32532. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  32533. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  32534. {
  32535. /* Decrypt messages */
  32536. int ret1 = 0;
  32537. int ret2 = 0;
  32538. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  32539. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  32540. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  32541. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  32542. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  32543. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  32544. }
  32545. #endif
  32546. return EXPECT_RESULT();
  32547. }
  32548. static int test_wolfSSL_X509_cmp_time(void)
  32549. {
  32550. EXPECT_DECLS;
  32551. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32552. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32553. WOLFSSL_ASN1_TIME asn_time;
  32554. time_t t;
  32555. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32556. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32557. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32558. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32559. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32560. #endif
  32561. return EXPECT_RESULT();
  32562. }
  32563. static int test_wolfSSL_X509_time_adj(void)
  32564. {
  32565. EXPECT_DECLS;
  32566. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32567. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32568. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32569. !defined(NO_ASN_TIME)
  32570. X509* x509 = NULL;
  32571. time_t t;
  32572. time_t not_before;
  32573. time_t not_after;
  32574. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32575. client_cert_der_2048, sizeof_client_cert_der_2048,
  32576. WOLFSSL_FILETYPE_ASN1));
  32577. t = 0;
  32578. not_before = wc_Time(0);
  32579. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32580. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32581. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32582. /* Check X509_gmtime_adj, too. */
  32583. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32584. X509_free(x509);
  32585. #endif
  32586. return EXPECT_RESULT();
  32587. }
  32588. static int test_wolfSSL_X509(void)
  32589. {
  32590. EXPECT_DECLS;
  32591. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32592. !defined(NO_RSA)
  32593. X509* x509 = NULL;
  32594. #ifndef NO_BIO
  32595. BIO* bio = NULL;
  32596. X509_STORE_CTX* ctx = NULL;
  32597. X509_STORE* store = NULL;
  32598. #endif
  32599. char der[] = "certs/ca-cert.der";
  32600. XFILE fp = XBADFILE;
  32601. ExpectNotNull(x509 = X509_new());
  32602. X509_free(x509);
  32603. x509 = NULL;
  32604. #ifndef NO_BIO
  32605. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  32606. SSL_FILETYPE_PEM));
  32607. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32608. #ifdef WOLFSSL_CERT_GEN
  32609. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32610. #endif
  32611. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32612. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32613. ExpectNotNull(store = X509_STORE_new());
  32614. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32615. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32616. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32617. X509_STORE_CTX_free(ctx);
  32618. X509_STORE_free(store);
  32619. X509_free(x509);
  32620. x509 = NULL;
  32621. BIO_free(bio);
  32622. #endif
  32623. /** d2i_X509_fp test **/
  32624. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  32625. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32626. ExpectNotNull(x509);
  32627. X509_free(x509);
  32628. x509 = NULL;
  32629. if (fp != XBADFILE) {
  32630. XFCLOSE(fp);
  32631. fp = XBADFILE;
  32632. }
  32633. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  32634. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32635. ExpectNotNull(x509);
  32636. X509_free(x509);
  32637. if (fp != XBADFILE)
  32638. XFCLOSE(fp);
  32639. /* X509_up_ref test */
  32640. ExpectIntEQ(X509_up_ref(NULL), 0);
  32641. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  32642. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32643. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32644. X509_free(x509); /* refCount = 2 */
  32645. X509_free(x509); /* refCount = 1 */
  32646. X509_free(x509); /* refCount = 0, free */
  32647. #endif
  32648. return EXPECT_RESULT();
  32649. }
  32650. static int test_wolfSSL_X509_get_ext_count(void)
  32651. {
  32652. EXPECT_DECLS;
  32653. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32654. !defined(NO_RSA)
  32655. int ret = 0;
  32656. WOLFSSL_X509* x509 = NULL;
  32657. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  32658. XFILE f = XBADFILE;
  32659. /* NULL parameter check */
  32660. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  32661. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32662. SSL_FILETYPE_PEM));
  32663. ExpectIntEQ(X509_get_ext_count(x509), 5);
  32664. wolfSSL_X509_free(x509);
  32665. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  32666. SSL_FILETYPE_PEM));
  32667. ExpectIntEQ(X509_get_ext_count(x509), 5);
  32668. wolfSSL_X509_free(x509);
  32669. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  32670. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  32671. if (f != XBADFILE)
  32672. XFCLOSE(f);
  32673. /* wolfSSL_X509_get_ext_count() valid input */
  32674. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  32675. /* wolfSSL_X509_get_ext_count() NULL argument */
  32676. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  32677. wolfSSL_X509_free(x509);
  32678. #endif
  32679. return EXPECT_RESULT();
  32680. }
  32681. static int test_wolfSSL_X509_sign2(void)
  32682. {
  32683. EXPECT_DECLS;
  32684. /* test requires WOLFSSL_AKID_NAME to match expected output */
  32685. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32686. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  32687. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  32688. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32689. defined(WOLFSSL_IP_ALT_NAME))
  32690. WOLFSSL_X509 *x509 = NULL;
  32691. WOLFSSL_X509 *ca = NULL;
  32692. const unsigned char *der = NULL;
  32693. const unsigned char *pt = NULL;
  32694. WOLFSSL_EVP_PKEY *priv = NULL;
  32695. WOLFSSL_X509_NAME *name = NULL;
  32696. int derSz;
  32697. #ifndef NO_ASN_TIME
  32698. WOLFSSL_ASN1_TIME *notBefore = NULL;
  32699. WOLFSSL_ASN1_TIME *notAfter = NULL;
  32700. const int year = 365*24*60*60;
  32701. const int day = 24*60*60;
  32702. const int hour = 60*60;
  32703. const int mini = 60;
  32704. time_t t;
  32705. #endif
  32706. const unsigned char expected[] = {
  32707. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  32708. 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
  32709. 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
  32710. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  32711. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  32712. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  32713. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  32714. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  32715. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  32716. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  32717. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  32718. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  32719. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  32720. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  32721. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32722. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  32723. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  32724. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  32725. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  32726. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  32727. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  32728. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  32729. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  32730. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  32731. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  32732. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  32733. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  32734. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  32735. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  32736. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  32737. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  32738. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  32739. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  32740. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32741. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  32742. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  32743. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  32744. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  32745. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  32746. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  32747. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  32748. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  32749. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  32750. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  32751. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  32752. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  32753. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  32754. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  32755. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  32756. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  32757. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  32758. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  32759. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  32760. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  32761. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  32762. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  32763. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  32764. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  32765. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  32766. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  32767. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  32768. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  32769. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32770. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32771. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  32772. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  32773. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  32774. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  32775. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  32776. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  32777. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  32778. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  32779. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  32780. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  32781. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  32782. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  32783. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  32784. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  32785. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  32786. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  32787. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  32788. 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
  32789. 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
  32790. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  32791. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  32792. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  32793. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  32794. 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
  32795. 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
  32796. 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
  32797. 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
  32798. 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
  32799. 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
  32800. 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
  32801. 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
  32802. 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
  32803. 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
  32804. 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
  32805. 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
  32806. 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
  32807. 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
  32808. 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
  32809. 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
  32810. 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
  32811. 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
  32812. 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
  32813. 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
  32814. 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
  32815. 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
  32816. };
  32817. pt = ca_key_der_2048;
  32818. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  32819. sizeof_ca_key_der_2048));
  32820. pt = client_cert_der_2048;
  32821. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  32822. sizeof_client_cert_der_2048));
  32823. pt = ca_cert_der_2048;
  32824. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  32825. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  32826. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32827. #ifndef NO_ASN_TIME
  32828. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32829. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  32830. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  32831. ExpectIntEQ(notAfter->length, 13);
  32832. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  32833. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  32834. #endif
  32835. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  32836. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  32837. ExpectIntEQ(derSz, sizeof(expected));
  32838. #ifndef NO_ASN_TIME
  32839. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  32840. #endif
  32841. wolfSSL_X509_free(ca);
  32842. wolfSSL_X509_free(x509);
  32843. wolfSSL_EVP_PKEY_free(priv);
  32844. #ifndef NO_ASN_TIME
  32845. wolfSSL_ASN1_TIME_free(notBefore);
  32846. wolfSSL_ASN1_TIME_free(notAfter);
  32847. #endif
  32848. #endif
  32849. return EXPECT_RESULT();
  32850. }
  32851. static int test_wolfSSL_X509_sign(void)
  32852. {
  32853. EXPECT_DECLS;
  32854. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  32855. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  32856. int ret;
  32857. char *cn = NULL;
  32858. word32 cnSz;
  32859. X509_NAME *name = NULL;
  32860. X509 *x509 = NULL;
  32861. X509 *ca = NULL;
  32862. DecodedCert dCert;
  32863. EVP_PKEY *pub = NULL;
  32864. EVP_PKEY *priv = NULL;
  32865. EVP_MD_CTX *mctx = NULL;
  32866. #if defined(USE_CERT_BUFFERS_1024)
  32867. const unsigned char* rsaPriv = client_key_der_1024;
  32868. const unsigned char* rsaPub = client_keypub_der_1024;
  32869. const unsigned char* certIssuer = client_cert_der_1024;
  32870. long clientKeySz = (long)sizeof_client_key_der_1024;
  32871. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  32872. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  32873. #elif defined(USE_CERT_BUFFERS_2048)
  32874. const unsigned char* rsaPriv = client_key_der_2048;
  32875. const unsigned char* rsaPub = client_keypub_der_2048;
  32876. const unsigned char* certIssuer = client_cert_der_2048;
  32877. long clientKeySz = (long)sizeof_client_key_der_2048;
  32878. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  32879. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  32880. #endif
  32881. byte sn[16];
  32882. int snSz = sizeof(sn);
  32883. /* Set X509_NAME fields */
  32884. ExpectNotNull(name = X509_NAME_new());
  32885. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  32886. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  32887. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32888. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  32889. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32890. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  32891. /* Get private and public keys */
  32892. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  32893. clientKeySz));
  32894. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  32895. ExpectNotNull(x509 = X509_new());
  32896. /* Set version 3 */
  32897. ExpectIntNE(X509_set_version(x509, 2L), 0);
  32898. /* Set subject name, add pubkey, and sign certificate */
  32899. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  32900. X509_NAME_free(name);
  32901. name = NULL;
  32902. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  32903. #ifdef WOLFSSL_ALT_NAMES
  32904. /* Add some subject alt names */
  32905. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  32906. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  32907. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32908. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  32909. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32910. "sphygmomanometer",
  32911. ASN_DNS_TYPE), SSL_SUCCESS);
  32912. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32913. "supercalifragilisticexpialidocious",
  32914. ASN_DNS_TYPE), SSL_SUCCESS);
  32915. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  32916. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32917. ASN_DNS_TYPE), SSL_SUCCESS);
  32918. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32919. {
  32920. unsigned char ip4_type[] = {127,128,0,255};
  32921. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32922. 0xff, 0xee, 0x99, 0x88,
  32923. 0x77, 0x66, 0x55, 0x44,
  32924. 0x00, 0x33, 0x22, 0x11};
  32925. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32926. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32927. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32928. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32929. }
  32930. #endif
  32931. #endif /* WOLFSSL_ALT_NAMES */
  32932. /* test valid sign case */
  32933. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  32934. /* test valid X509_sign_ctx case */
  32935. ExpectNotNull(mctx = EVP_MD_CTX_new());
  32936. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32937. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  32938. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32939. ExpectIntEQ(X509_get_ext_count(x509), 1);
  32940. #endif
  32941. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32942. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32943. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32944. #endif
  32945. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32946. WOLFSSL_SUCCESS);
  32947. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32948. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32949. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  32950. * with the MSB set. See GenerateInteger in asn.c */
  32951. #ifndef USE_CERT_BUFFERS_1024
  32952. #ifndef WOLFSSL_ALT_NAMES
  32953. /* Valid case - size should be 781-786 with 16 byte serial number */
  32954. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  32955. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32956. /* Valid case - size should be 955-960 with 16 byte serial number */
  32957. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  32958. #else
  32959. /* Valid case - size should be 926-931 with 16 byte serial number */
  32960. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  32961. #endif
  32962. #else
  32963. #ifndef WOLFSSL_ALT_NAMES
  32964. /* Valid case - size should be 537-542 with 16 byte serial number */
  32965. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  32966. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32967. /* Valid case - size should be 695-670 with 16 byte serial number */
  32968. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  32969. #else
  32970. /* Valid case - size should be 666-671 with 16 byte serial number */
  32971. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  32972. #endif
  32973. #endif
  32974. /* check that issuer name is as expected after signature */
  32975. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  32976. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  32977. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  32978. ExpectNotNull(name = X509_get_subject_name(ca));
  32979. cnSz = X509_NAME_get_sz(name);
  32980. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32981. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32982. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  32983. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32984. cn = NULL;
  32985. #ifdef WOLFSSL_MULTI_ATTRIB
  32986. /* test adding multiple OU's to the signer */
  32987. ExpectNotNull(name = X509_get_subject_name(ca));
  32988. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32989. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  32990. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32991. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  32992. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  32993. #endif
  32994. ExpectNotNull(name = X509_get_subject_name(ca));
  32995. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  32996. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32997. ExpectNotNull(name = X509_get_issuer_name(x509));
  32998. cnSz = X509_NAME_get_sz(name);
  32999. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33000. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  33001. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  33002. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  33003. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33004. cn = NULL;
  33005. FreeDecodedCert(&dCert);
  33006. /* Test invalid parameters */
  33007. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  33008. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  33009. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  33010. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  33011. EVP_MD_CTX_free(mctx);
  33012. mctx = NULL;
  33013. ExpectNotNull(mctx = EVP_MD_CTX_new());
  33014. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  33015. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  33016. /* test invalid version number */
  33017. #if defined(OPENSSL_ALL)
  33018. ExpectIntNE(X509_set_version(x509, 6L), 0);
  33019. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  33020. /* uses ParseCert which fails on bad version number */
  33021. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  33022. #endif
  33023. EVP_MD_CTX_free(mctx);
  33024. EVP_PKEY_free(priv);
  33025. EVP_PKEY_free(pub);
  33026. X509_free(x509);
  33027. X509_free(ca);
  33028. #endif
  33029. return EXPECT_RESULT();
  33030. }
  33031. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  33032. {
  33033. EXPECT_DECLS;
  33034. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33035. X509* x509 = NULL;
  33036. const X509_ALGOR* alg;
  33037. ExpectNotNull(x509 = X509_new());
  33038. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  33039. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  33040. X509_free(x509);
  33041. #endif
  33042. return EXPECT_RESULT();
  33043. }
  33044. static int test_wolfSSL_X509_ALGOR_get0(void)
  33045. {
  33046. EXPECT_DECLS;
  33047. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33048. !defined(NO_SHA256) && !defined(NO_RSA)
  33049. X509* x509 = NULL;
  33050. const ASN1_OBJECT* obj = NULL;
  33051. const X509_ALGOR* alg = NULL;
  33052. int pptype = 0;
  33053. const void *ppval = NULL;
  33054. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  33055. SSL_FILETYPE_PEM));
  33056. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  33057. /* Invalid case */
  33058. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  33059. ExpectNull(obj);
  33060. /* Valid case */
  33061. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  33062. ExpectNotNull(obj);
  33063. ExpectNull(ppval);
  33064. ExpectIntNE(pptype, 0);
  33065. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  33066. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  33067. X509_free(x509);
  33068. #endif
  33069. return EXPECT_RESULT();
  33070. }
  33071. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  33072. {
  33073. EXPECT_DECLS;
  33074. #if defined(OPENSSL_EXTRA)
  33075. X509_VERIFY_PARAM *paramTo = NULL;
  33076. X509_VERIFY_PARAM *paramFrom = NULL;
  33077. char testIPv4[] = "127.0.0.1";
  33078. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33079. char testhostName1[] = "foo.hoge.com";
  33080. char testhostName2[] = "foobar.hoge.com";
  33081. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  33082. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  33083. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  33084. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  33085. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33086. (int)XSTRLEN(testhostName1)), 1);
  33087. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33088. (int)XSTRLEN(testhostName1)));
  33089. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33090. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33091. ExpectIntEQ(0x01, paramFrom->hostFlags);
  33092. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  33093. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  33094. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33095. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  33096. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  33097. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33098. /* null pointer */
  33099. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  33100. /* in the case of "from" null, returns success */
  33101. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  33102. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  33103. /* inherit flags test : VPARAM_DEFAULT */
  33104. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33105. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33106. (int)XSTRLEN(testhostName1)));
  33107. ExpectIntEQ(0x01, paramTo->hostFlags);
  33108. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33109. /* inherit flags test : VPARAM OVERWRITE */
  33110. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33111. (int)XSTRLEN(testhostName2)), 1);
  33112. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33113. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33114. if (paramTo != NULL) {
  33115. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33116. }
  33117. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33118. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33119. (int)XSTRLEN(testhostName1)));
  33120. ExpectIntEQ(0x01, paramTo->hostFlags);
  33121. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33122. /* inherit flags test : VPARAM_RESET_FLAGS */
  33123. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33124. (int)XSTRLEN(testhostName2)), 1);
  33125. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33126. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33127. if (paramTo != NULL) {
  33128. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33129. }
  33130. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33131. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33132. (int)XSTRLEN(testhostName1)));
  33133. ExpectIntEQ(0x01, paramTo->hostFlags);
  33134. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33135. /* inherit flags test : VPARAM_LOCKED */
  33136. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33137. (int)XSTRLEN(testhostName2)), 1);
  33138. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  33139. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33140. if (paramTo != NULL) {
  33141. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33142. }
  33143. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  33144. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33145. (int)XSTRLEN(testhostName2)));
  33146. ExpectIntEQ(0x00, paramTo->hostFlags);
  33147. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33148. /* test for incorrect parameters */
  33149. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  33150. 0);
  33151. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  33152. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33153. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  33154. 1);
  33155. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  33156. X509_V_FLAG_CRL_CHECK_ALL);
  33157. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  33158. X509_V_FLAG_CRL_CHECK_ALL), 1);
  33159. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  33160. X509_VERIFY_PARAM_free(paramTo);
  33161. X509_VERIFY_PARAM_free(paramFrom);
  33162. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33163. #endif
  33164. return EXPECT_RESULT();
  33165. }
  33166. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  33167. static int test_wolfSSL_check_domain_verify_count = 0;
  33168. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33169. WOLFSSL_X509_STORE_CTX* store)
  33170. {
  33171. EXPECT_DECLS;
  33172. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  33173. ExpectIntEQ(preverify, 1);
  33174. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  33175. return EXPECT_SUCCESS();
  33176. }
  33177. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33178. {
  33179. EXPECT_DECLS;
  33180. X509_VERIFY_PARAM *param = NULL;
  33181. ExpectNotNull(param = SSL_get0_param(ssl));
  33182. /* Domain check should only be done on the leaf cert */
  33183. X509_VERIFY_PARAM_set_hostflags(param,
  33184. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33185. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33186. "wolfSSL Server Chain", 0), 1);
  33187. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33188. test_wolfSSL_check_domain_verify_cb);
  33189. return EXPECT_RESULT();
  33190. }
  33191. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33192. {
  33193. EXPECT_DECLS;
  33194. /* Use a cert with different domains in chain */
  33195. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33196. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33197. return EXPECT_RESULT();
  33198. }
  33199. static int test_wolfSSL_check_domain(void)
  33200. {
  33201. EXPECT_DECLS;
  33202. test_ssl_cbf func_cb_client;
  33203. test_ssl_cbf func_cb_server;
  33204. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  33205. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  33206. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33207. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33208. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  33209. &func_cb_server, NULL), TEST_SUCCESS);
  33210. /* Should have been called once for each cert in sent chain */
  33211. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33212. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33213. #else
  33214. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33215. #endif
  33216. return EXPECT_RESULT();
  33217. }
  33218. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  33219. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33220. {
  33221. EXPECT_DECLS;
  33222. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33223. X509* x509 = NULL;
  33224. X509_PUBKEY* pubKey;
  33225. ExpectNotNull(x509 = X509_new());
  33226. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33227. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33228. X509_free(x509);
  33229. #endif
  33230. return EXPECT_RESULT();
  33231. }
  33232. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33233. {
  33234. EXPECT_DECLS;
  33235. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33236. !defined(NO_SHA256) && !defined(NO_RSA)
  33237. X509* x509 = NULL;
  33238. ASN1_OBJECT* obj = NULL;
  33239. const ASN1_OBJECT* pa_oid = NULL;
  33240. X509_PUBKEY* pubKey = NULL;
  33241. X509_PUBKEY* pubKey2 = NULL;
  33242. EVP_PKEY* evpKey = NULL;
  33243. const unsigned char *pk = NULL;
  33244. int ppklen;
  33245. int pptype;
  33246. X509_ALGOR *pa = NULL;
  33247. const void *pval;
  33248. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33249. SSL_FILETYPE_PEM));
  33250. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33251. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33252. ExpectNotNull(pk);
  33253. ExpectNotNull(pa);
  33254. ExpectNotNull(pubKey);
  33255. ExpectIntGT(ppklen, 0);
  33256. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33257. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33258. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  33259. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33260. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33261. ExpectNotNull(pk);
  33262. ExpectNotNull(pa);
  33263. ExpectIntGT(ppklen, 0);
  33264. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33265. ExpectNotNull(pa_oid);
  33266. ExpectNull(pval);
  33267. ExpectIntEQ(pptype, V_ASN1_NULL);
  33268. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33269. X509_PUBKEY_free(pubKey2);
  33270. X509_free(x509);
  33271. EVP_PKEY_free(evpKey);
  33272. #endif
  33273. return EXPECT_RESULT();
  33274. }
  33275. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33276. {
  33277. EXPECT_DECLS;
  33278. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33279. X509* x509 = NULL;
  33280. ASN1_OBJECT* obj = NULL;
  33281. ASN1_OBJECT* poid = NULL;
  33282. const ASN1_OBJECT* pa_oid = NULL;
  33283. X509_PUBKEY* pubKey = NULL;
  33284. X509_PUBKEY* pubKey2 = NULL;
  33285. EVP_PKEY* evpKey = NULL;
  33286. const unsigned char *pk = NULL;
  33287. int ppklen;
  33288. int pptype;
  33289. X509_ALGOR *pa = NULL;
  33290. const void *pval;
  33291. char buf[50];
  33292. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33293. SSL_FILETYPE_PEM));
  33294. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33295. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33296. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  33297. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33298. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33299. ExpectNotNull(pk);
  33300. ExpectNotNull(pa);
  33301. ExpectIntGT(ppklen, 0);
  33302. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33303. ExpectNotNull(pa_oid);
  33304. ExpectNotNull(pval);
  33305. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  33306. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33307. poid = (ASN1_OBJECT *)pval;
  33308. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33309. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33310. X509_PUBKEY_free(pubKey2);
  33311. X509_free(x509);
  33312. EVP_PKEY_free(evpKey);
  33313. #endif
  33314. return EXPECT_RESULT();
  33315. }
  33316. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33317. {
  33318. EXPECT_DECLS;
  33319. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33320. word32 bytes;
  33321. #ifdef USE_CERT_BUFFERS_1024
  33322. byte tmp[ONEK_BUF];
  33323. #elif defined(USE_CERT_BUFFERS_2048)
  33324. byte tmp[TWOK_BUF];
  33325. #else
  33326. byte tmp[TWOK_BUF];
  33327. #endif /* END USE_CERT_BUFFERS_1024 */
  33328. const unsigned char* dsaKeyDer = tmp;
  33329. ASN1_OBJECT* obj = NULL;
  33330. ASN1_STRING* str;
  33331. const ASN1_OBJECT* pa_oid = NULL;
  33332. X509_PUBKEY* pubKey = NULL;
  33333. EVP_PKEY* evpKey = NULL;
  33334. const unsigned char *pk = NULL;
  33335. int ppklen, pptype;
  33336. X509_ALGOR *pa = NULL;
  33337. const void *pval;
  33338. #ifdef USE_CERT_BUFFERS_1024
  33339. XMEMSET(tmp, 0, sizeof(tmp));
  33340. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33341. bytes = sizeof_dsa_key_der_1024;
  33342. #elif defined(USE_CERT_BUFFERS_2048)
  33343. XMEMSET(tmp, 0, sizeof(tmp));
  33344. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33345. bytes = sizeof_dsa_key_der_2048;
  33346. #else
  33347. {
  33348. XFILE fp = XBADFILE;
  33349. XMEMSET(tmp, 0, sizeof(tmp));
  33350. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  33351. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  33352. if (fp != XBADFILE)
  33353. XFCLOSE(fp);
  33354. }
  33355. #endif
  33356. /* Initialize pkey with der format dsa key */
  33357. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33358. ExpectNotNull(pubKey = X509_PUBKEY_new());
  33359. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33360. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33361. ExpectNotNull(pk);
  33362. ExpectNotNull(pa);
  33363. ExpectIntGT(ppklen, 0);
  33364. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33365. ExpectNotNull(pa_oid);
  33366. ExpectNotNull(pval);
  33367. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  33368. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33369. str = (ASN1_STRING *)pval;
  33370. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33371. #ifdef USE_CERT_BUFFERS_1024
  33372. ExpectIntEQ(ASN1_STRING_length(str), 291);
  33373. #else
  33374. ExpectIntEQ(ASN1_STRING_length(str), 549);
  33375. #endif /* END USE_CERT_BUFFERS_1024 */
  33376. X509_PUBKEY_free(pubKey);
  33377. EVP_PKEY_free(evpKey);
  33378. #endif
  33379. return EXPECT_RESULT();
  33380. }
  33381. static int test_wolfSSL_BUF(void)
  33382. {
  33383. EXPECT_DECLS;
  33384. #if defined(OPENSSL_EXTRA)
  33385. BUF_MEM* buf = NULL;
  33386. ExpectNotNull(buf = BUF_MEM_new());
  33387. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  33388. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  33389. BUF_MEM_free(buf);
  33390. #endif
  33391. return EXPECT_RESULT();
  33392. }
  33393. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33394. static int stub_rand_seed(const void *buf, int num)
  33395. {
  33396. (void)buf;
  33397. (void)num;
  33398. return 123;
  33399. }
  33400. static int stub_rand_bytes(unsigned char *buf, int num)
  33401. {
  33402. (void)buf;
  33403. (void)num;
  33404. return 456;
  33405. }
  33406. static byte* was_stub_rand_cleanup_called(void)
  33407. {
  33408. static byte was_called = 0;
  33409. return &was_called;
  33410. }
  33411. static void stub_rand_cleanup(void)
  33412. {
  33413. byte* was_called = was_stub_rand_cleanup_called();
  33414. *was_called = 1;
  33415. return;
  33416. }
  33417. static byte* was_stub_rand_add_called(void)
  33418. {
  33419. static byte was_called = 0;
  33420. return &was_called;
  33421. }
  33422. static int stub_rand_add(const void *buf, int num, double entropy)
  33423. {
  33424. byte* was_called = was_stub_rand_add_called();
  33425. (void)buf;
  33426. (void)num;
  33427. (void)entropy;
  33428. *was_called = 1;
  33429. return 0;
  33430. }
  33431. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33432. {
  33433. (void)buf;
  33434. (void)num;
  33435. return 9876;
  33436. }
  33437. static int stub_rand_status(void)
  33438. {
  33439. return 5432;
  33440. }
  33441. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33442. static int test_wolfSSL_RAND_set_rand_method(void)
  33443. {
  33444. EXPECT_DECLS;
  33445. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33446. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33447. unsigned char* buf = NULL;
  33448. int num = 0;
  33449. double entropy = 0;
  33450. int ret;
  33451. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33452. byte* was_add_called = was_stub_rand_add_called();
  33453. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33454. DYNAMIC_TYPE_TMP_BUFFER));
  33455. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  33456. ExpectIntEQ(*was_cleanup_called, 0);
  33457. RAND_cleanup();
  33458. ExpectIntEQ(*was_cleanup_called, 0);
  33459. rand_methods.seed = &stub_rand_seed;
  33460. rand_methods.bytes = &stub_rand_bytes;
  33461. rand_methods.cleanup = &stub_rand_cleanup;
  33462. rand_methods.add = &stub_rand_add;
  33463. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33464. rand_methods.status = &stub_rand_status;
  33465. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33466. ExpectIntEQ(RAND_seed(buf, num), 123);
  33467. ExpectIntEQ(RAND_bytes(buf, num), 456);
  33468. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33469. ExpectIntEQ(RAND_status(), 5432);
  33470. ExpectIntEQ(*was_add_called, 0);
  33471. /* The function pointer for RAND_add returns int, but RAND_add itself
  33472. * returns void. */
  33473. RAND_add(buf, num, entropy);
  33474. ExpectIntEQ(*was_add_called, 1);
  33475. was_add_called = 0;
  33476. ExpectIntEQ(*was_cleanup_called, 0);
  33477. RAND_cleanup();
  33478. ExpectIntEQ(*was_cleanup_called, 1);
  33479. *was_cleanup_called = 0;
  33480. ret = RAND_set_rand_method(NULL);
  33481. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  33482. ExpectIntNE(RAND_status(), 5432);
  33483. ExpectIntEQ(*was_cleanup_called, 0);
  33484. RAND_cleanup();
  33485. ExpectIntEQ(*was_cleanup_called, 0);
  33486. RAND_set_rand_method(NULL);
  33487. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33488. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33489. return EXPECT_RESULT();
  33490. }
  33491. static int test_wolfSSL_RAND_bytes(void)
  33492. {
  33493. EXPECT_DECLS;
  33494. #if defined(OPENSSL_EXTRA)
  33495. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33496. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33497. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33498. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33499. int max_bufsize;
  33500. byte *my_buf = NULL;
  33501. /* sanity check */
  33502. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  33503. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  33504. max_bufsize = size4;
  33505. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33506. DYNAMIC_TYPE_TMP_BUFFER));
  33507. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  33508. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  33509. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  33510. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  33511. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  33512. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  33513. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  33514. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33515. #endif
  33516. return EXPECT_RESULT();
  33517. }
  33518. static int test_wolfSSL_RAND(void)
  33519. {
  33520. EXPECT_DECLS;
  33521. #if defined(OPENSSL_EXTRA)
  33522. byte seed[16];
  33523. XMEMSET(seed, 0, sizeof(seed));
  33524. /* No global methods set. */
  33525. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  33526. ExpectIntEQ(RAND_poll(), 1);
  33527. RAND_cleanup();
  33528. ExpectIntEQ(RAND_egd(NULL), -1);
  33529. #ifndef NO_FILESYSTEM
  33530. {
  33531. char fname[100];
  33532. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33533. ExpectIntEQ(RAND_write_file(NULL), 0);
  33534. }
  33535. #endif
  33536. #endif
  33537. return EXPECT_RESULT();
  33538. }
  33539. static int test_wolfSSL_PKCS8_Compat(void)
  33540. {
  33541. EXPECT_DECLS;
  33542. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  33543. !defined(NO_BIO)
  33544. PKCS8_PRIV_KEY_INFO* pt = NULL;
  33545. BIO* bio = NULL;
  33546. XFILE f = XBADFILE;
  33547. int bytes;
  33548. char pkcs8_buffer[512];
  33549. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33550. EVP_PKEY *pkey = NULL;
  33551. #endif
  33552. /* file from wolfssl/certs/ directory */
  33553. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  33554. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  33555. 0);
  33556. if (f != XBADFILE)
  33557. XFCLOSE(f);
  33558. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33559. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33560. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33561. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  33562. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  33563. /* gets PKCS8 pointer to pkey */
  33564. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  33565. EVP_PKEY_free(pkey);
  33566. #endif
  33567. BIO_free(bio);
  33568. PKCS8_PRIV_KEY_INFO_free(pt);
  33569. #endif
  33570. return EXPECT_RESULT();
  33571. }
  33572. static int test_wolfSSL_PKCS8_d2i(void)
  33573. {
  33574. EXPECT_DECLS;
  33575. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  33576. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  33577. * requires a 12 byte password in FIPS mode. This test ends up
  33578. * trying to use an 8 byte password. */
  33579. #ifndef NO_FILESYSTEM
  33580. unsigned char pkcs8_buffer[2048];
  33581. const unsigned char* p;
  33582. int bytes;
  33583. XFILE file = XBADFILE;
  33584. WOLFSSL_EVP_PKEY* pkey = NULL;
  33585. #ifndef NO_BIO
  33586. BIO* bio = NULL;
  33587. #if defined(OPENSSL_ALL) && \
  33588. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  33589. defined(HAVE_ECC)) && \
  33590. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33591. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  33592. #endif
  33593. #endif
  33594. #ifndef NO_RSA
  33595. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  33596. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  33597. #ifndef NO_DES3
  33598. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  33599. #endif
  33600. #endif /* NO_RSA */
  33601. #ifdef HAVE_ECC
  33602. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  33603. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  33604. #ifndef NO_DES3
  33605. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  33606. #endif
  33607. #endif /* HAVE_ECC */
  33608. #endif /* !NO_FILESYSTEM */
  33609. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  33610. #ifndef NO_RSA
  33611. #ifdef USE_CERT_BUFFERS_1024
  33612. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  33613. int rsaSz = sizeof_server_key_der_1024;
  33614. #else
  33615. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  33616. int rsaSz = sizeof_server_key_der_2048;
  33617. #endif
  33618. #endif
  33619. #ifdef HAVE_ECC
  33620. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  33621. int ecSz = sizeof_ecc_key_der_256;
  33622. #endif
  33623. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  33624. #ifndef NO_FILESYSTEM
  33625. (void)pkcs8_buffer;
  33626. (void)p;
  33627. (void)bytes;
  33628. (void)file;
  33629. #ifndef NO_BIO
  33630. (void)bio;
  33631. #endif
  33632. #endif
  33633. #ifdef OPENSSL_ALL
  33634. #ifndef NO_RSA
  33635. /* Try to auto-detect normal RSA private key */
  33636. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  33637. EVP_PKEY_free(pkey);
  33638. pkey = NULL;
  33639. #endif
  33640. #ifdef HAVE_ECC
  33641. /* Try to auto-detect normal EC private key */
  33642. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  33643. EVP_PKEY_free(pkey);
  33644. pkey = NULL;
  33645. #endif
  33646. #endif /* OPENSSL_ALL */
  33647. #ifndef NO_FILESYSTEM
  33648. #ifndef NO_RSA
  33649. /* Get DER encoded RSA PKCS#8 data. */
  33650. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  33651. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33652. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33653. file)), 0);
  33654. if (file != XBADFILE) {
  33655. XFCLOSE(file);
  33656. file = XBADFILE;
  33657. }
  33658. p = pkcs8_buffer;
  33659. #ifdef OPENSSL_ALL
  33660. /* Try to decode - auto-detect key type. */
  33661. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33662. #else
  33663. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  33664. #endif
  33665. /* Get PEM encoded RSA PKCS#8 data. */
  33666. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  33667. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33668. file)), 0);
  33669. if (file != XBADFILE) {
  33670. XFCLOSE(file);
  33671. file = XBADFILE;
  33672. }
  33673. #if defined(OPENSSL_ALL) && \
  33674. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33675. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33676. /* Write PKCS#8 PEM to BIO. */
  33677. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33678. NULL), bytes);
  33679. /* Compare file and written data */
  33680. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33681. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33682. BIO_free(bio);
  33683. bio = NULL;
  33684. #if !defined(NO_DES3) && !defined(NO_SHA)
  33685. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33686. /* Write Encrypted PKCS#8 PEM to BIO. */
  33687. bytes = 1834;
  33688. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  33689. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33690. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33691. (void*)"yassl123"));
  33692. EVP_PKEY_free(evpPkey);
  33693. evpPkey = NULL;
  33694. BIO_free(bio);
  33695. bio = NULL;
  33696. #endif /* !NO_DES3 && !NO_SHA */
  33697. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33698. EVP_PKEY_free(pkey);
  33699. pkey = NULL;
  33700. /* PKCS#8 encrypted RSA key */
  33701. #ifndef NO_DES3
  33702. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  33703. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33704. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33705. file)), 0);
  33706. if (file != XBADFILE) {
  33707. XFCLOSE(file);
  33708. file = XBADFILE;
  33709. }
  33710. #if defined(OPENSSL_ALL) && \
  33711. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33712. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33713. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33714. (void*)"yassl123"));
  33715. EVP_PKEY_free(pkey);
  33716. pkey = NULL;
  33717. BIO_free(bio);
  33718. bio = NULL;
  33719. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33720. #endif /* !NO_DES3 */
  33721. #endif /* NO_RSA */
  33722. #ifdef HAVE_ECC
  33723. /* PKCS#8 encode EC key */
  33724. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  33725. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33726. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33727. file)), 0);
  33728. if (file != XBADFILE) {
  33729. XFCLOSE(file);
  33730. file = XBADFILE;
  33731. }
  33732. p = pkcs8_buffer;
  33733. #ifdef OPENSSL_ALL
  33734. /* Try to decode - auto-detect key type. */
  33735. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33736. #else
  33737. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  33738. #endif
  33739. /* Get PEM encoded RSA PKCS#8 data. */
  33740. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  33741. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33742. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33743. file)), 0);
  33744. if (file != XBADFILE) {
  33745. XFCLOSE(file);
  33746. file = XBADFILE;
  33747. }
  33748. #if defined(OPENSSL_ALL) && \
  33749. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  33750. defined(HAVE_AES_CBC)
  33751. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33752. /* Write PKCS#8 PEM to BIO. */
  33753. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33754. NULL), bytes);
  33755. /* Compare file and written data */
  33756. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33757. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33758. BIO_free(bio);
  33759. bio = NULL;
  33760. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33761. /* Write Encrypted PKCS#8 PEM to BIO. */
  33762. bytes = 379;
  33763. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  33764. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33765. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33766. (void*)"yassl123"));
  33767. EVP_PKEY_free(evpPkey);
  33768. evpPkey = NULL;
  33769. BIO_free(bio);
  33770. bio = NULL;
  33771. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  33772. EVP_PKEY_free(pkey);
  33773. pkey = NULL;
  33774. /* PKCS#8 encrypted EC key */
  33775. #ifndef NO_DES3
  33776. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  33777. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  33778. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33779. file)), 0);
  33780. if (file != XBADFILE) {
  33781. XFCLOSE(file);
  33782. file = XBADFILE;
  33783. }
  33784. #if defined(OPENSSL_ALL) && \
  33785. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33786. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33787. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33788. (void*)"yassl123"));
  33789. EVP_PKEY_free(pkey);
  33790. pkey = NULL;
  33791. BIO_free(bio);
  33792. bio = NULL;
  33793. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33794. #endif /* !NO_DES3 */
  33795. #endif /* HAVE_ECC */
  33796. #endif /* !NO_FILESYSTEM */
  33797. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  33798. return EXPECT_RESULT();
  33799. }
  33800. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33801. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33802. #define LOGGING_THREADS 5
  33803. #define ERROR_COUNT 10
  33804. /* copied from logging.c since this is not exposed otherwise */
  33805. #ifndef ERROR_QUEUE_MAX
  33806. #ifdef ERROR_QUEUE_PER_THREAD
  33807. #define ERROR_QUEUE_MAX 16
  33808. #else
  33809. /* this breaks from compat of unlimited error queue size */
  33810. #define ERROR_QUEUE_MAX 100
  33811. #endif
  33812. #endif
  33813. static volatile int loggingThreadsReady;
  33814. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  33815. {
  33816. const char* file;
  33817. int line;
  33818. unsigned long err;
  33819. int errorCount = 0;
  33820. int i;
  33821. (void)args;
  33822. while (!loggingThreadsReady);
  33823. for (i = 0; i < ERROR_COUNT; i++)
  33824. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33825. while ((err = ERR_get_error_line(&file, &line))) {
  33826. AssertIntEQ(err, 990 + errorCount);
  33827. errorCount++;
  33828. }
  33829. AssertIntEQ(errorCount, ERROR_COUNT);
  33830. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  33831. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  33832. errorCount = 0;
  33833. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  33834. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33835. while ((err = ERR_get_error_line(&file, &line))) {
  33836. AssertIntEQ(err, 990 + errorCount);
  33837. errorCount++;
  33838. }
  33839. /* test that the 3 errors over the max were dropped */
  33840. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  33841. return 0;
  33842. }
  33843. #endif
  33844. static int test_error_queue_per_thread(void)
  33845. {
  33846. int res = TEST_SKIPPED;
  33847. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33848. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33849. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33850. int i;
  33851. ERR_clear_error(); /* clear out any error nodes */
  33852. loggingThreadsReady = 0;
  33853. for (i = 0; i < LOGGING_THREADS; i++)
  33854. start_thread(test_logging, NULL, &loggingThreads[i]);
  33855. loggingThreadsReady = 1;
  33856. for (i = 0; i < LOGGING_THREADS; i++)
  33857. join_thread(loggingThreads[i]);
  33858. res = TEST_SUCCESS;
  33859. #endif
  33860. return res;
  33861. }
  33862. static int test_wolfSSL_ERR_put_error(void)
  33863. {
  33864. EXPECT_DECLS;
  33865. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33866. defined(DEBUG_WOLFSSL)
  33867. const char* file;
  33868. int line;
  33869. ERR_clear_error(); /* clear out any error nodes */
  33870. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33871. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33872. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33873. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  33874. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33875. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  33876. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33877. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  33878. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  33879. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  33880. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  33881. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  33882. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  33883. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  33884. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  33885. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  33886. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  33887. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  33888. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  33889. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  33890. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  33891. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  33892. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  33893. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  33894. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  33895. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  33896. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  33897. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  33898. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  33899. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  33900. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  33901. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  33902. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  33903. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  33904. "this file", 100);
  33905. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  33906. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33907. ExpectIntEQ(line, 100);
  33908. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  33909. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33910. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  33911. #endif
  33912. /* try reading past end of error queue */
  33913. file = NULL;
  33914. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33915. ExpectNull(file);
  33916. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  33917. PEMerr(4,4);
  33918. ExpectIntEQ(ERR_get_error(), 4);
  33919. /* Empty and free up all error nodes */
  33920. ERR_clear_error();
  33921. /* Verify all nodes are cleared */
  33922. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33923. ERR_clear_error();
  33924. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  33925. #endif
  33926. return EXPECT_RESULT();
  33927. }
  33928. /*
  33929. * This is a regression test for a bug where the peek/get error functions were
  33930. * drawing from the end of the queue rather than the front.
  33931. */
  33932. static int test_wolfSSL_ERR_get_error_order(void)
  33933. {
  33934. EXPECT_DECLS;
  33935. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  33936. /* Empty the queue. */
  33937. wolfSSL_ERR_clear_error();
  33938. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  33939. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  33940. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  33941. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  33942. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  33943. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  33944. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  33945. return EXPECT_RESULT();
  33946. }
  33947. #ifndef NO_BIO
  33948. static int test_wolfSSL_ERR_print_errors(void)
  33949. {
  33950. EXPECT_DECLS;
  33951. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33952. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  33953. BIO* bio = NULL;
  33954. char buf[1024];
  33955. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33956. ERR_clear_error(); /* clear out any error nodes */
  33957. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33958. /* Choosing -600 as an unused errno. */
  33959. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  33960. ERR_print_errors(bio);
  33961. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  33962. ExpectIntEQ(XSTRNCMP(
  33963. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  33964. buf, 55), 0);
  33965. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  33966. ExpectIntEQ(XSTRNCMP(
  33967. "error:600:wolfSSL library:unknown error number:asn.c:100",
  33968. buf, 56), 0);
  33969. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  33970. ExpectIntEQ(buf[0], '\0');
  33971. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  33972. BIO_free(bio);
  33973. #endif
  33974. return EXPECT_RESULT();
  33975. }
  33976. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33977. defined(DEBUG_WOLFSSL)
  33978. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  33979. {
  33980. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  33981. return 0;
  33982. }
  33983. #endif
  33984. static int test_wolfSSL_ERR_print_errors_cb(void)
  33985. {
  33986. EXPECT_DECLS;
  33987. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33988. defined(DEBUG_WOLFSSL)
  33989. BIO* bio = NULL;
  33990. char buf[1024];
  33991. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  33992. ERR_clear_error(); /* clear out any error nodes */
  33993. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33994. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  33995. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  33996. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  33997. ExpectIntEQ(XSTRNCMP(
  33998. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  33999. buf, 53), 0);
  34000. ExpectIntEQ(XSTRNCMP(
  34001. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  34002. buf + 53, 55), 0);
  34003. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  34004. BIO_free(bio);
  34005. #endif
  34006. return EXPECT_RESULT();
  34007. }
  34008. /*
  34009. * Testing WOLFSSL_ERROR_MSG
  34010. */
  34011. static int test_WOLFSSL_ERROR_MSG(void)
  34012. {
  34013. int res = TEST_SKIPPED;
  34014. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34015. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34016. const char* msg = TEST_STRING;
  34017. WOLFSSL_ERROR_MSG(msg);
  34018. res = TEST_SUCCESS;
  34019. #endif
  34020. return res;
  34021. } /* End test_WOLFSSL_ERROR_MSG */
  34022. /*
  34023. * Testing wc_ERR_remove_state
  34024. */
  34025. static int test_wc_ERR_remove_state(void)
  34026. {
  34027. int res = TEST_SKIPPED;
  34028. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34029. wc_ERR_remove_state();
  34030. res = TEST_SUCCESS;
  34031. #endif
  34032. return res;
  34033. } /* End test_wc_ERR_remove_state */
  34034. /*
  34035. * Testing wc_ERR_print_errors_fp
  34036. */
  34037. static int test_wc_ERR_print_errors_fp(void)
  34038. {
  34039. EXPECT_DECLS;
  34040. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34041. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34042. long sz;
  34043. XFILE fp = XBADFILE;
  34044. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34045. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  34046. XBADFILE);
  34047. wc_ERR_print_errors_fp(fp);
  34048. #if defined(DEBUG_WOLFSSL)
  34049. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34050. #ifdef NO_ERROR_QUEUE
  34051. ExpectIntEQ(sz = XFTELL(fp), 0);
  34052. #else
  34053. ExpectIntNE(sz = XFTELL(fp), 0);
  34054. #endif
  34055. #endif
  34056. if (fp != XBADFILE)
  34057. XFCLOSE(fp);
  34058. (void)sz;
  34059. #endif
  34060. return EXPECT_RESULT();
  34061. } /* End test_wc_ERR_print_errors_fp */
  34062. #ifdef DEBUG_WOLFSSL
  34063. static void Logging_cb(const int logLevel, const char *const logMessage)
  34064. {
  34065. (void)logLevel;
  34066. (void)logMessage;
  34067. }
  34068. #endif
  34069. /*
  34070. * Testing wolfSSL_GetLoggingCb
  34071. */
  34072. static int test_wolfSSL_GetLoggingCb(void)
  34073. {
  34074. EXPECT_DECLS;
  34075. #ifdef DEBUG_WOLFSSL
  34076. /* Testing without wolfSSL_SetLoggingCb() */
  34077. ExpectNull(wolfSSL_GetLoggingCb());
  34078. /* Testing with wolfSSL_SetLoggingCb() */
  34079. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  34080. ExpectNotNull(wolfSSL_GetLoggingCb());
  34081. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  34082. #endif
  34083. ExpectNull(wolfSSL_GetLoggingCb());
  34084. return EXPECT_RESULT();
  34085. } /* End test_wolfSSL_GetLoggingCb */
  34086. #endif /* !NO_BIO */
  34087. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34088. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34089. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34090. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34091. {
  34092. EXPECT_DECLS;
  34093. static const unsigned char key[] = "simple test key";
  34094. HMAC_CTX* hmac = NULL;
  34095. ENGINE* e = NULL;
  34096. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34097. unsigned int len;
  34098. ExpectNotNull(hmac = HMAC_CTX_new());
  34099. HMAC_CTX_init(hmac);
  34100. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34101. SSL_SUCCESS);
  34102. /* re-using test key as data to hash */
  34103. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34104. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34105. ExpectIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34106. ExpectIntEQ(len, md_len);
  34107. ExpectIntEQ(HMAC_size(hmac), md_len);
  34108. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  34109. HMAC_cleanup(hmac);
  34110. HMAC_CTX_free(hmac);
  34111. len = 0;
  34112. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34113. ExpectIntEQ(len, md_len);
  34114. return EXPECT_RESULT();
  34115. }
  34116. #endif
  34117. static int test_wolfSSL_HMAC(void)
  34118. {
  34119. EXPECT_DECLS;
  34120. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34121. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34122. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34123. #ifndef NO_SHA256
  34124. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  34125. TEST_SUCCESS);
  34126. #endif
  34127. #ifdef WOLFSSL_SHA224
  34128. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  34129. TEST_SUCCESS);
  34130. #endif
  34131. #ifdef WOLFSSL_SHA384
  34132. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  34133. TEST_SUCCESS);
  34134. #endif
  34135. #ifdef WOLFSSL_SHA512
  34136. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  34137. TEST_SUCCESS);
  34138. #endif
  34139. #ifdef WOLFSSL_SHA3
  34140. #ifndef WOLFSSL_NOSHA3_224
  34141. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  34142. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  34143. #endif
  34144. #ifndef WOLFSSL_NOSHA3_256
  34145. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  34146. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  34147. #endif
  34148. #ifndef WOLFSSL_NOSHA3_384
  34149. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  34150. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  34151. #endif
  34152. #ifndef WOLFSSL_NOSHA3_512
  34153. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  34154. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  34155. #endif
  34156. #endif
  34157. #ifndef NO_SHA
  34158. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  34159. TEST_SUCCESS);
  34160. #endif
  34161. #endif
  34162. return EXPECT_RESULT();
  34163. }
  34164. static int test_wolfSSL_CMAC(void)
  34165. {
  34166. EXPECT_DECLS;
  34167. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34168. defined(WOLFSSL_AES_DIRECT)
  34169. int i;
  34170. byte key[AES_128_KEY_SIZE];
  34171. CMAC_CTX* cmacCtx = NULL;
  34172. byte out[AES_BLOCK_SIZE];
  34173. size_t outLen = AES_BLOCK_SIZE;
  34174. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34175. key[i] = i;
  34176. }
  34177. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  34178. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34179. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34180. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34181. NULL), SSL_SUCCESS);
  34182. /* re-using test key as data to hash */
  34183. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34184. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34185. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34186. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  34187. CMAC_CTX_free(cmacCtx);
  34188. /* give a key too small for the cipher, verify we get failure */
  34189. cmacCtx = NULL;
  34190. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  34191. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34192. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  34193. NULL), SSL_FAILURE);
  34194. CMAC_CTX_free(cmacCtx);
  34195. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34196. return EXPECT_RESULT();
  34197. }
  34198. static int test_wolfSSL_OBJ(void)
  34199. {
  34200. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34201. * mode
  34202. */
  34203. EXPECT_DECLS;
  34204. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34205. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34206. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34207. ASN1_OBJECT *obj = NULL;
  34208. ASN1_OBJECT *obj2 = NULL;
  34209. char buf[50];
  34210. XFILE fp = XBADFILE;
  34211. X509 *x509 = NULL;
  34212. X509_NAME *x509Name = NULL;
  34213. X509_NAME_ENTRY *x509NameEntry = NULL;
  34214. ASN1_OBJECT *asn1Name = NULL;
  34215. int numNames = 0;
  34216. BIO *bio = NULL;
  34217. int nid;
  34218. int i, j;
  34219. const char *f[] = {
  34220. #ifndef NO_RSA
  34221. "./certs/ca-cert.der",
  34222. #endif
  34223. #ifdef HAVE_ECC
  34224. "./certs/ca-ecc-cert.der",
  34225. "./certs/ca-ecc384-cert.der",
  34226. #endif
  34227. NULL};
  34228. ASN1_OBJECT *field_name_obj = NULL;
  34229. int lastpos = -1;
  34230. int tmp = -1;
  34231. ASN1_STRING *asn1 = NULL;
  34232. unsigned char *buf_dyn = NULL;
  34233. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34234. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34235. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34236. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34237. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34238. ASN1_OBJECT_free(obj);
  34239. obj = NULL;
  34240. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  34241. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34242. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34243. #ifdef WOLFSSL_CERT_EXT
  34244. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34245. #endif
  34246. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34247. ExpectNotNull(obj2 = OBJ_dup(obj));
  34248. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  34249. ASN1_OBJECT_free(obj);
  34250. obj = NULL;
  34251. ASN1_OBJECT_free(obj2);
  34252. obj2 = NULL;
  34253. for (i = 0; f[i] != NULL; i++)
  34254. {
  34255. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34256. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  34257. if (fp != XBADFILE) {
  34258. XFCLOSE(fp);
  34259. fp = XBADFILE;
  34260. }
  34261. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  34262. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34263. /* Get the Common Name by using OBJ_txt2obj */
  34264. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34265. do
  34266. {
  34267. lastpos = tmp;
  34268. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34269. } while (tmp > -1);
  34270. ExpectIntNE(lastpos, -1);
  34271. ASN1_OBJECT_free(field_name_obj);
  34272. field_name_obj = NULL;
  34273. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34274. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34275. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34276. /*
  34277. * All Common Names should be www.wolfssl.com
  34278. * This makes testing easier as we can test for the expected value.
  34279. */
  34280. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34281. OPENSSL_free(buf_dyn);
  34282. buf_dyn = NULL;
  34283. bio = BIO_new(BIO_s_mem());
  34284. ExpectTrue(bio != NULL);
  34285. for (j = 0; j < numNames; j++)
  34286. {
  34287. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34288. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34289. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34290. }
  34291. BIO_free(bio);
  34292. bio = NULL;
  34293. X509_free(x509);
  34294. x509 = NULL;
  34295. }
  34296. #ifdef HAVE_PKCS12
  34297. {
  34298. PKCS12 *p12 = NULL;
  34299. int boolRet;
  34300. EVP_PKEY *pkey = NULL;
  34301. const char *p12_f[] = {
  34302. #if !defined(NO_DES3) && !defined(NO_RSA)
  34303. "./certs/test-servercert.p12",
  34304. #endif
  34305. NULL};
  34306. for (i = 0; p12_f[i] != NULL; i++)
  34307. {
  34308. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34309. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34310. if (fp != XBADFILE) {
  34311. XFCLOSE(fp);
  34312. fp = XBADFILE;
  34313. }
  34314. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34315. &pkey, &x509, NULL)) > 0);
  34316. wc_PKCS12_free(p12);
  34317. p12 = NULL;
  34318. EVP_PKEY_free(pkey);
  34319. x509Name = X509_get_issuer_name(x509);
  34320. ExpectNotNull(x509Name);
  34321. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34322. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34323. for (j = 0; j < numNames; j++)
  34324. {
  34325. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34326. ExpectNotNull(asn1Name =
  34327. X509_NAME_ENTRY_get_object(x509NameEntry));
  34328. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34329. }
  34330. BIO_free(bio);
  34331. bio = NULL;
  34332. X509_free(x509);
  34333. x509 = NULL;
  34334. }
  34335. }
  34336. #endif /* HAVE_PKCS12 */
  34337. #endif
  34338. return EXPECT_RESULT();
  34339. }
  34340. static int test_wolfSSL_OBJ_cmp(void)
  34341. {
  34342. EXPECT_DECLS;
  34343. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34344. ASN1_OBJECT *obj = NULL;
  34345. ASN1_OBJECT *obj2 = NULL;
  34346. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34347. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34348. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34349. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34350. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34351. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34352. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  34353. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  34354. ASN1_OBJECT_free(obj);
  34355. ASN1_OBJECT_free(obj2);
  34356. #endif
  34357. return EXPECT_RESULT();
  34358. }
  34359. static int test_wolfSSL_OBJ_txt2nid(void)
  34360. {
  34361. EXPECT_DECLS;
  34362. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  34363. defined(WOLFSSL_APACHE_HTTPD)
  34364. int i;
  34365. static const struct {
  34366. const char* sn;
  34367. const char* ln;
  34368. const char* oid;
  34369. int nid;
  34370. } testVals[] = {
  34371. #ifdef WOLFSSL_APACHE_HTTPD
  34372. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34373. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34374. NID_id_on_dnsSRV },
  34375. { "msUPN", "Microsoft User Principal Name",
  34376. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34377. #endif
  34378. { NULL, NULL, NULL, NID_undef }
  34379. };
  34380. /* Invalid cases */
  34381. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34382. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34383. /* Valid cases */
  34384. for (i = 0; testVals[i].sn != NULL; i++) {
  34385. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34386. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34387. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34388. }
  34389. #endif
  34390. return EXPECT_RESULT();
  34391. }
  34392. static int test_wolfSSL_OBJ_txt2obj(void)
  34393. {
  34394. EXPECT_DECLS;
  34395. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34396. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34397. int i;
  34398. char buf[50];
  34399. ASN1_OBJECT* obj = NULL;
  34400. static const struct {
  34401. const char* oidStr;
  34402. const char* sn;
  34403. const char* ln;
  34404. } objs_list[] = {
  34405. #if defined(WOLFSSL_APACHE_HTTPD)
  34406. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34407. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34408. #endif
  34409. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34410. { NULL, NULL, NULL }
  34411. };
  34412. static const struct {
  34413. const char* numeric;
  34414. const char* name;
  34415. } objs_named[] = {
  34416. /* In dictionary but not in normal list. */
  34417. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34418. /* Made up OID. */
  34419. { "1.3.5.7", "1.3.5.7" },
  34420. { NULL, NULL }
  34421. };
  34422. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  34423. ASN1_OBJECT_free(obj);
  34424. obj = NULL;
  34425. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  34426. ASN1_OBJECT_free(obj);
  34427. obj = NULL;
  34428. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34429. /* Test numerical value of oid (oidStr) */
  34430. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34431. /* Convert object back to text to confirm oid is correct */
  34432. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34433. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34434. ASN1_OBJECT_free(obj);
  34435. obj = NULL;
  34436. XMEMSET(buf, 0, sizeof(buf));
  34437. /* Test short name (sn) */
  34438. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34439. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34440. /* Convert object back to text to confirm oid is correct */
  34441. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34442. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34443. ASN1_OBJECT_free(obj);
  34444. obj = NULL;
  34445. XMEMSET(buf, 0, sizeof(buf));
  34446. /* Test long name (ln) - should fail when no_name = 1 */
  34447. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34448. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34449. /* Convert object back to text to confirm oid is correct */
  34450. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34451. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34452. ASN1_OBJECT_free(obj);
  34453. obj = NULL;
  34454. XMEMSET(buf, 0, sizeof(buf));
  34455. }
  34456. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34457. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34458. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34459. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34460. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34461. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34462. ASN1_OBJECT_free(obj);
  34463. obj = NULL;
  34464. }
  34465. #endif
  34466. return EXPECT_RESULT();
  34467. }
  34468. static int test_wolfSSL_PEM_write_bio_X509(void)
  34469. {
  34470. EXPECT_DECLS;
  34471. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  34472. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  34473. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  34474. !defined(NO_FILESYSTEM)
  34475. /* This test contains the hard coded expected
  34476. * lengths. Update if necessary */
  34477. XFILE fp = XBADFILE;
  34478. WOLFSSL_EVP_PKEY *priv = NULL;
  34479. BIO* input = NULL;
  34480. BIO* output = NULL;
  34481. X509* x509a = NULL;
  34482. X509* x509b = NULL;
  34483. ASN1_TIME* notBeforeA = NULL;
  34484. ASN1_TIME* notAfterA = NULL;
  34485. #ifndef NO_ASN_TIME
  34486. ASN1_TIME* notBeforeB = NULL;
  34487. ASN1_TIME* notAfterB = NULL;
  34488. #endif
  34489. int expectedLen;
  34490. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  34491. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  34492. if (fp != XBADFILE) {
  34493. XFCLOSE(fp);
  34494. fp = XBADFILE;
  34495. }
  34496. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  34497. "rb"));
  34498. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34499. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34500. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34501. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  34502. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  34503. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  34504. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34505. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34506. /* compare length against expected */
  34507. expectedLen = 2000;
  34508. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34509. #ifndef NO_ASN_TIME
  34510. /* read exported X509 PEM back into struct, sanity check on export,
  34511. * make sure notBefore/notAfter are the same and certs are identical. */
  34512. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34513. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  34514. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  34515. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34516. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34517. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34518. X509_free(x509b);
  34519. x509b = NULL;
  34520. #endif
  34521. /* Reset output buffer */
  34522. BIO_free(output);
  34523. output = NULL;
  34524. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34525. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34526. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  34527. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34528. x509a->authKeyId = x509a->authKeyIdSrc;
  34529. x509a->authKeyIdSrc = NULL;
  34530. x509a->authKeyIdSrcSz = 0;
  34531. }
  34532. /* Resign to re-generate the der */
  34533. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  34534. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34535. /* Check that we generate a smaller output since the AKID will
  34536. * only contain the KeyIdentifier without any additional
  34537. * information */
  34538. /* Here we copy the validity struct from the original */
  34539. expectedLen = 1688;
  34540. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34541. /* Reset buffers and x509 */
  34542. BIO_free(input);
  34543. input = NULL;
  34544. BIO_free(output);
  34545. output = NULL;
  34546. X509_free(x509a);
  34547. x509a = NULL;
  34548. /* test CA and basicConstSet values are encoded when
  34549. * the cert is a CA */
  34550. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  34551. /* read PEM into X509 struct */
  34552. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  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. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  34557. * values are maintained and certs are identical.*/
  34558. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34559. ExpectIntEQ(x509b->isCa, 1);
  34560. ExpectIntEQ(x509b->basicConstSet, 1);
  34561. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34562. X509_free(x509a);
  34563. x509a = NULL;
  34564. X509_free(x509b);
  34565. x509b = NULL;
  34566. BIO_free(input);
  34567. input = NULL;
  34568. BIO_free(output);
  34569. output = NULL;
  34570. /* test CA and basicConstSet values are encoded when
  34571. * the cert is not CA */
  34572. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  34573. /* read PEM into X509 struct */
  34574. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34575. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  34576. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34577. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34578. /* read exported X509 PEM back into struct, ensure isCa and
  34579. * basicConstSet values are maintained and certs are identical */
  34580. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34581. ExpectIntEQ(x509b->isCa, 0);
  34582. ExpectIntEQ(x509b->basicConstSet, 1);
  34583. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  34584. wolfSSL_EVP_PKEY_free(priv);
  34585. X509_free(x509a);
  34586. X509_free(x509b);
  34587. BIO_free(input);
  34588. BIO_free(output);
  34589. #endif
  34590. return EXPECT_RESULT();
  34591. }
  34592. static int test_wolfSSL_X509_NAME_ENTRY(void)
  34593. {
  34594. EXPECT_DECLS;
  34595. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34596. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  34597. X509* x509 = NULL;
  34598. #ifndef NO_BIO
  34599. BIO* bio = NULL;
  34600. #endif
  34601. X509_NAME* nm = NULL;
  34602. X509_NAME_ENTRY* entry = NULL;
  34603. unsigned char cn[] = "another name to add";
  34604. #ifdef OPENSSL_ALL
  34605. int i;
  34606. int names_len = 0;
  34607. #endif
  34608. ExpectNotNull(x509 =
  34609. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34610. #ifndef NO_BIO
  34611. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34612. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  34613. #endif
  34614. #ifdef WOLFSSL_CERT_REQ
  34615. {
  34616. X509_REQ* req = NULL;
  34617. #ifndef NO_BIO
  34618. BIO* bReq = NULL;
  34619. #endif
  34620. ExpectNotNull(req =
  34621. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34622. #ifndef NO_BIO
  34623. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  34624. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  34625. BIO_free(bReq);
  34626. #endif
  34627. X509_free(req);
  34628. }
  34629. #endif
  34630. ExpectNotNull(nm = X509_get_subject_name(x509));
  34631. /* Test add entry */
  34632. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  34633. 0x0c, cn, (int)sizeof(cn)));
  34634. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34635. #ifdef WOLFSSL_CERT_EXT
  34636. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  34637. (byte*)"support@wolfssl.com", 19, -1,
  34638. 1), WOLFSSL_SUCCESS);
  34639. #endif
  34640. X509_NAME_ENTRY_free(entry);
  34641. entry = NULL;
  34642. #ifdef WOLFSSL_CERT_REQ
  34643. {
  34644. unsigned char srv_pkcs9p[] = "Server";
  34645. unsigned char fvrtDrnk[] = "tequila";
  34646. unsigned char* der = NULL;
  34647. char* subject = NULL;
  34648. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  34649. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  34650. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  34651. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  34652. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  34653. ExpectNotNull(der);
  34654. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  34655. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  34656. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  34657. #ifdef DEBUG_WOLFSSL
  34658. if (subject != NULL) {
  34659. fprintf(stderr, "\n\t%s\n", subject);
  34660. }
  34661. #endif
  34662. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  34663. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  34664. }
  34665. #endif
  34666. /* Test add entry by text */
  34667. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  34668. 0x0c, cn, (int)sizeof(cn)));
  34669. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  34670. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  34671. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  34672. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  34673. #endif
  34674. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34675. X509_NAME_ENTRY_free(entry);
  34676. entry = NULL;
  34677. /* Test add entry by NID */
  34678. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  34679. cn, -1, -1, 0), SSL_SUCCESS);
  34680. #ifdef OPENSSL_ALL
  34681. /* stack of name entry */
  34682. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  34683. for (i = 0; i < names_len; i++) {
  34684. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  34685. }
  34686. #endif
  34687. #ifndef NO_BIO
  34688. BIO_free(bio);
  34689. #endif
  34690. X509_free(x509); /* free's nm */
  34691. #endif
  34692. return EXPECT_RESULT();
  34693. }
  34694. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  34695. static int test_GENERAL_NAME_set0_othername(void) {
  34696. EXPECT_DECLS;
  34697. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34698. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  34699. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  34700. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  34701. defined(WOLFSSL_FPKI)
  34702. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  34703. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  34704. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  34705. const char * cert_fname = "./certs/server-cert.der";
  34706. const char * key_fname = "./certs/server-key.der";
  34707. X509* x509 = NULL;
  34708. GENERAL_NAME* gn = NULL;
  34709. GENERAL_NAMES* gns = NULL;
  34710. ASN1_OBJECT* upn_oid = NULL;
  34711. ASN1_UTF8STRING *utf8str = NULL;
  34712. ASN1_TYPE *value = NULL;
  34713. X509_EXTENSION * ext = NULL;
  34714. byte* pt = NULL;
  34715. byte der[4096];
  34716. int derSz = 0;
  34717. EVP_PKEY* priv = NULL;
  34718. XFILE f = XBADFILE;
  34719. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  34720. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  34721. if (f != XBADFILE) {
  34722. XFCLOSE(f);
  34723. f = XBADFILE;
  34724. }
  34725. ExpectNotNull(gn = GENERAL_NAME_new());
  34726. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  34727. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  34728. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  34729. ExpectNotNull(value = ASN1_TYPE_new());
  34730. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  34731. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  34732. wolfSSL_ASN1_STRING_free(utf8str);
  34733. }
  34734. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  34735. if (EXPECT_FAIL()) {
  34736. ASN1_TYPE_free(value);
  34737. }
  34738. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  34739. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  34740. if (EXPECT_FAIL()) {
  34741. GENERAL_NAME_free(gn);
  34742. gn = NULL;
  34743. }
  34744. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  34745. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  34746. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  34747. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  34748. if (f != XBADFILE) {
  34749. XFCLOSE(f);
  34750. f = XBADFILE;
  34751. }
  34752. pt = der;
  34753. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  34754. (const unsigned char**)&pt, derSz));
  34755. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  34756. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34757. gns = NULL;
  34758. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  34759. NULL));
  34760. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  34761. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  34762. ExpectIntEQ(gn->type, 0);
  34763. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34764. ASN1_OBJECT_free(upn_oid);
  34765. X509_EXTENSION_free(ext);
  34766. X509_free(x509);
  34767. EVP_PKEY_free(priv);
  34768. #endif
  34769. return EXPECT_RESULT();
  34770. }
  34771. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  34772. static int test_othername_and_SID_ext(void) {
  34773. EXPECT_DECLS;
  34774. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34775. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  34776. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  34777. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  34778. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  34779. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  34780. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  34781. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  34782. const char* csr_fname = "./certs/csr.signed.der";
  34783. const char* key_fname = "./certs/server-key.der";
  34784. byte der[4096];
  34785. int derSz = 0;
  34786. X509_REQ* x509 = NULL;
  34787. STACK_OF(X509_EXTENSION) *exts = NULL;
  34788. X509_EXTENSION * san_ext = NULL;
  34789. X509_EXTENSION * ext = NULL;
  34790. GENERAL_NAME* gn = NULL;
  34791. GENERAL_NAMES* gns = NULL;
  34792. ASN1_OBJECT* upn_oid = NULL;
  34793. ASN1_UTF8STRING *utf8str = NULL;
  34794. ASN1_TYPE *value = NULL;
  34795. ASN1_STRING *extval = NULL;
  34796. /* SID extension. SID data format explained here:
  34797. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  34798. */
  34799. uint8_t SidExtension[] = {
  34800. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  34801. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  34802. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  34803. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  34804. uint8_t expectedAltName[] = {
  34805. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  34806. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  34807. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  34808. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  34809. X509_EXTENSION *sid_ext = NULL;
  34810. ASN1_OBJECT* sid_oid = NULL;
  34811. ASN1_OCTET_STRING *sid_data = NULL;
  34812. EVP_PKEY* priv = NULL;
  34813. XFILE f = XBADFILE;
  34814. byte* pt = NULL;
  34815. BIO* bio = NULL;
  34816. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  34817. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  34818. if (f != XBADFILE) {
  34819. XFCLOSE(f);
  34820. f = XBADFILE;
  34821. }
  34822. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  34823. ExpectNotNull(gn = GENERAL_NAME_new());
  34824. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  34825. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  34826. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  34827. ExpectNotNull(value = ASN1_TYPE_new());
  34828. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  34829. if (EXPECT_FAIL()) {
  34830. ASN1_UTF8STRING_free(utf8str);
  34831. }
  34832. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  34833. if (EXPECT_FAIL()) {
  34834. ASN1_TYPE_free(value);
  34835. GENERAL_NAME_free(gn);
  34836. gn = NULL;
  34837. }
  34838. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  34839. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  34840. if (EXPECT_FAIL()) {
  34841. GENERAL_NAME_free(gn);
  34842. }
  34843. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  34844. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  34845. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  34846. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  34847. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  34848. sid_data));
  34849. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  34850. /* Ensure an empty stack doesn't raise an error. */
  34851. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  34852. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  34853. if (EXPECT_FAIL()) {
  34854. X509_EXTENSION_free(san_ext);
  34855. }
  34856. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  34857. if (EXPECT_FAIL()) {
  34858. X509_EXTENSION_free(sid_ext);
  34859. }
  34860. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  34861. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  34862. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  34863. if (f != XBADFILE)
  34864. XFCLOSE(f);
  34865. pt = der;
  34866. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  34867. (const unsigned char**)&pt, derSz));
  34868. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  34869. pt = der;
  34870. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  34871. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34872. gns = NULL;
  34873. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  34874. exts = NULL;
  34875. ASN1_OBJECT_free(upn_oid);
  34876. ASN1_OBJECT_free(sid_oid);
  34877. ASN1_OCTET_STRING_free(sid_data);
  34878. X509_REQ_free(x509);
  34879. x509 = NULL;
  34880. EVP_PKEY_free(priv);
  34881. /* At this point everything used to generate what is in der is cleaned up.
  34882. * We now read back from der to confirm the extensions were inserted
  34883. * correctly. */
  34884. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  34885. ExpectNotNull(bio);
  34886. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  34887. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  34888. ExpectNotNull(x509);
  34889. BIO_free(bio);
  34890. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  34891. x509));
  34892. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  34893. /* Check the SID extension. */
  34894. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  34895. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  34896. ExpectIntEQ(extval->length, sizeof(SidExtension));
  34897. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  34898. /* Check the AltNames extension. */
  34899. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  34900. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  34901. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  34902. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  34903. 0);
  34904. /* Cleanup */
  34905. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  34906. NULL));
  34907. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  34908. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  34909. ExpectIntEQ(gn->type, 0);
  34910. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  34911. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  34912. X509_REQ_free(x509);
  34913. #endif
  34914. return EXPECT_RESULT();
  34915. }
  34916. static int test_wolfSSL_X509_set_name(void)
  34917. {
  34918. EXPECT_DECLS;
  34919. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34920. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34921. X509* x509 = NULL;
  34922. X509_NAME* name = NULL;
  34923. ExpectNotNull(name = X509_NAME_new());
  34924. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34925. (byte*)"wolfssl.com", 11, 0, 1),
  34926. WOLFSSL_SUCCESS);
  34927. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34928. (byte*)"support@wolfssl.com", 19, -1,
  34929. 1), WOLFSSL_SUCCESS);
  34930. ExpectNotNull(x509 = X509_new());
  34931. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  34932. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  34933. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  34934. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  34935. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  34936. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  34937. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  34938. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34939. X509_free(x509);
  34940. X509_NAME_free(name);
  34941. #endif /* OPENSSL_ALL && !NO_CERTS */
  34942. return EXPECT_RESULT();
  34943. }
  34944. static int test_wolfSSL_X509_set_notAfter(void)
  34945. {
  34946. EXPECT_DECLS;
  34947. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34948. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34949. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34950. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  34951. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  34952. /* Generalized time will overflow time_t if not long */
  34953. X509* x = NULL;
  34954. BIO* bio = NULL;
  34955. ASN1_TIME *asn_time = NULL;
  34956. ASN1_TIME *time_check = NULL;
  34957. const int year = 365*24*60*60;
  34958. const int day = 24*60*60;
  34959. const int hour = 60*60;
  34960. const int mini = 60;
  34961. int offset_day;
  34962. unsigned char buf[25];
  34963. time_t t;
  34964. /*
  34965. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34966. */
  34967. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  34968. offset_day = 7;
  34969. /*
  34970. * Free these.
  34971. */
  34972. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34973. ExpectNotNull(asn_time);
  34974. ExpectNotNull(x = X509_new());
  34975. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34976. /*
  34977. * Tests
  34978. */
  34979. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  34980. /* time_check is simply (ANS1_TIME*)x->notAfter */
  34981. ExpectNotNull(time_check = X509_get_notAfter(x));
  34982. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34983. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34984. /* Convert to human readable format and compare to intended date */
  34985. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34986. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34987. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  34988. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  34989. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  34990. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  34991. /*
  34992. * Cleanup
  34993. */
  34994. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  34995. X509_free(x);
  34996. BIO_free(bio);
  34997. #endif
  34998. return EXPECT_RESULT();
  34999. }
  35000. static int test_wolfSSL_X509_set_notBefore(void)
  35001. {
  35002. EXPECT_DECLS;
  35003. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  35004. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  35005. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  35006. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  35007. X509* x = NULL;
  35008. BIO* bio = NULL;
  35009. ASN1_TIME *asn_time = NULL;
  35010. ASN1_TIME *time_check = NULL;
  35011. const int year = 365*24*60*60;
  35012. const int day = 24*60*60;
  35013. const int hour = 60*60;
  35014. const int mini = 60;
  35015. int offset_day;
  35016. unsigned char buf[25];
  35017. time_t t;
  35018. /*
  35019. * Setup asn_time. APACHE HTTPD uses time(NULL)
  35020. */
  35021. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  35022. offset_day = 7;
  35023. /*
  35024. * Free these.
  35025. */
  35026. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  35027. ExpectNotNull(asn_time);
  35028. ExpectNotNull(x = X509_new());
  35029. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35030. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  35031. /*
  35032. * Main Tests
  35033. */
  35034. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  35035. /* time_check == (ANS1_TIME*)x->notBefore */
  35036. ExpectNotNull(time_check = X509_get_notBefore(x));
  35037. /* ANS1_TIME_check validates by checking if argument can be parsed */
  35038. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  35039. /* Convert to human readable format and compare to intended date */
  35040. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  35041. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  35042. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  35043. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  35044. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  35045. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  35046. /*
  35047. * Cleanup
  35048. */
  35049. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  35050. X509_free(x);
  35051. BIO_free(bio);
  35052. #endif
  35053. return EXPECT_RESULT();
  35054. }
  35055. static int test_wolfSSL_X509_set_version(void)
  35056. {
  35057. EXPECT_DECLS;
  35058. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35059. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  35060. X509* x509 = NULL;
  35061. long v = 2L;
  35062. long maxInt = INT_MAX;
  35063. ExpectNotNull(x509 = X509_new());
  35064. /* These should pass. */
  35065. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  35066. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  35067. /* Fail Case: When v(long) is greater than x509->version(int). */
  35068. v = maxInt+1;
  35069. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  35070. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  35071. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  35072. /* Cleanup */
  35073. X509_free(x509);
  35074. #endif
  35075. return EXPECT_RESULT();
  35076. }
  35077. #ifndef NO_BIO
  35078. static int test_wolfSSL_BIO_gets(void)
  35079. {
  35080. EXPECT_DECLS;
  35081. #if defined(OPENSSL_EXTRA)
  35082. BIO* bio = NULL;
  35083. BIO* bio2 = NULL;
  35084. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  35085. char emp[] = "";
  35086. char bio_buffer[20];
  35087. int bufferSz = 20;
  35088. #ifdef OPENSSL_ALL
  35089. BUF_MEM* emp_bm = NULL;
  35090. BUF_MEM* msg_bm = NULL;
  35091. #endif
  35092. /* try with bad args */
  35093. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  35094. #ifdef OPENSSL_ALL
  35095. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  35096. #endif
  35097. /* try with real msg */
  35098. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  35099. XMEMSET(bio_buffer, 0, bufferSz);
  35100. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  35101. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  35102. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  35103. ExpectFalse(bio2 != BIO_next(bio));
  35104. /* make buffer filled with no terminating characters */
  35105. XMEMSET(bio_buffer, 1, bufferSz);
  35106. /* BIO_gets reads a line of data */
  35107. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35108. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35109. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35110. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35111. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35112. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35113. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35114. #ifdef OPENSSL_ALL
  35115. /* test setting the mem_buf manually */
  35116. BIO_free(bio);
  35117. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  35118. ExpectNotNull(emp_bm = BUF_MEM_new());
  35119. ExpectNotNull(msg_bm = BUF_MEM_new());
  35120. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  35121. if (EXPECT_SUCCESS())
  35122. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  35123. /* emp size is 1 for terminator */
  35124. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  35125. if (EXPECT_SUCCESS()) {
  35126. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  35127. emp_bm->data = emp;
  35128. msg_bm->data = msg;
  35129. }
  35130. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  35131. /* check reading an empty string */
  35132. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35133. ExpectStrEQ(emp, bio_buffer);
  35134. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35135. /* BIO_gets reads a line of data */
  35136. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  35137. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35138. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35139. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35140. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35141. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35142. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35143. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35144. if (EXPECT_SUCCESS())
  35145. emp_bm->data = NULL;
  35146. BUF_MEM_free(emp_bm);
  35147. if (EXPECT_SUCCESS())
  35148. msg_bm->data = NULL;
  35149. BUF_MEM_free(msg_bm);
  35150. #endif
  35151. /* check not null terminated string */
  35152. BIO_free(bio);
  35153. bio = NULL;
  35154. msg[0] = 0x33;
  35155. msg[1] = 0x33;
  35156. msg[2] = 0x33;
  35157. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35158. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  35159. ExpectIntEQ(bio_buffer[0], msg[0]);
  35160. ExpectIntEQ(bio_buffer[1], msg[1]);
  35161. ExpectIntNE(bio_buffer[2], msg[2]);
  35162. BIO_free(bio);
  35163. bio = NULL;
  35164. msg[3] = 0x33;
  35165. bio_buffer[3] = 0x33;
  35166. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  35167. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  35168. ExpectIntEQ(bio_buffer[0], msg[0]);
  35169. ExpectIntEQ(bio_buffer[1], msg[1]);
  35170. ExpectIntEQ(bio_buffer[2], msg[2]);
  35171. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  35172. /* check reading an empty string */
  35173. BIO_free(bio);
  35174. bio = NULL;
  35175. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  35176. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  35177. ExpectStrEQ(emp, bio_buffer);
  35178. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35179. /* check error cases */
  35180. BIO_free(bio);
  35181. bio = NULL;
  35182. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  35183. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35184. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35185. #if !defined(NO_FILESYSTEM)
  35186. {
  35187. BIO* f_bio = NULL;
  35188. XFILE f = XBADFILE;
  35189. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  35190. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35191. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  35192. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  35193. if (EXPECT_FAIL() && (f != XBADFILE)) {
  35194. XFCLOSE(f);
  35195. }
  35196. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  35197. BIO_free(f_bio);
  35198. f_bio = NULL;
  35199. }
  35200. #endif /* NO_FILESYSTEM */
  35201. BIO_free(bio);
  35202. bio = NULL;
  35203. BIO_free(bio2);
  35204. bio2 = NULL;
  35205. /* try with type BIO */
  35206. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  35207. sizeof(msg));
  35208. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  35209. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  35210. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35211. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  35212. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  35213. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  35214. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  35215. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  35216. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  35217. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  35218. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  35219. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  35220. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  35221. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  35222. BIO_free(bio);
  35223. bio = NULL;
  35224. BIO_free(bio2);
  35225. bio2 = NULL;
  35226. /* check reading an empty string */
  35227. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  35228. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  35229. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  35230. ExpectStrEQ(emp, bio_buffer);
  35231. BIO_free(bio);
  35232. bio = NULL;
  35233. #endif
  35234. return EXPECT_RESULT();
  35235. }
  35236. static int test_wolfSSL_BIO_puts(void)
  35237. {
  35238. EXPECT_DECLS;
  35239. #if defined(OPENSSL_EXTRA)
  35240. BIO* bio = NULL;
  35241. char input[] = "hello\0world\n.....ok\n\0";
  35242. char output[128];
  35243. XMEMSET(output, 0, sizeof(output));
  35244. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35245. ExpectIntEQ(BIO_puts(bio, input), 5);
  35246. ExpectIntEQ(BIO_pending(bio), 5);
  35247. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  35248. ExpectIntEQ(BIO_pending(bio), 19);
  35249. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  35250. ExpectStrEQ(output, "helloworld\n");
  35251. ExpectIntEQ(BIO_pending(bio), 8);
  35252. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  35253. ExpectStrEQ(output, ".....ok\n");
  35254. ExpectIntEQ(BIO_pending(bio), 0);
  35255. ExpectIntEQ(BIO_puts(bio, ""), -1);
  35256. BIO_free(bio);
  35257. #endif
  35258. return EXPECT_RESULT();
  35259. }
  35260. static int test_wolfSSL_BIO_dump(void)
  35261. {
  35262. EXPECT_DECLS;
  35263. #if defined(OPENSSL_EXTRA)
  35264. BIO* bio;
  35265. static const unsigned char data[] = {
  35266. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  35267. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  35268. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  35269. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  35270. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  35271. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  35272. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  35273. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  35274. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  35275. 0xB4
  35276. };
  35277. /* Generated with OpenSSL. */
  35278. static const char expected[] =
  35279. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  35280. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  35281. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  35282. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  35283. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  35284. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  35285. static const char expectedAll[] =
  35286. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  35287. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  35288. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  35289. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35290. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35291. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35292. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35293. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35294. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35295. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35296. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35297. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35298. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35299. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35300. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35301. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35302. char output[16 * 80];
  35303. int i;
  35304. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35305. /* Example key dumped. */
  35306. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35307. sizeof(expected) - 1);
  35308. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35309. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35310. /* Try every possible value for a character. */
  35311. for (i = 0; i < 256; i++)
  35312. output[i] = i;
  35313. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35314. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35315. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35316. BIO_free(bio);
  35317. #endif
  35318. return EXPECT_RESULT();
  35319. }
  35320. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35321. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35322. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35323. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35324. {
  35325. (void)ssl;
  35326. (void)buf;
  35327. (void)sz;
  35328. (void)ctx;
  35329. return WOLFSSL_CBIO_ERR_WANT_READ;
  35330. }
  35331. #endif
  35332. static int test_wolfSSL_BIO_should_retry(void)
  35333. {
  35334. EXPECT_DECLS;
  35335. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35336. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35337. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35338. tcp_ready ready;
  35339. func_args server_args;
  35340. THREAD_TYPE serverThread;
  35341. SOCKET_T sockfd = 0;
  35342. WOLFSSL_CTX* ctx = NULL;
  35343. WOLFSSL* ssl = NULL;
  35344. char msg[64] = "hello wolfssl!";
  35345. char reply[1024];
  35346. int msgSz = (int)XSTRLEN(msg);
  35347. int ret;
  35348. BIO* bio = NULL;
  35349. XMEMSET(&server_args, 0, sizeof(func_args));
  35350. #ifdef WOLFSSL_TIRTOS
  35351. fdOpenSession(Task_self());
  35352. #endif
  35353. StartTCP();
  35354. InitTcpReady(&ready);
  35355. #if defined(USE_WINDOWS_API)
  35356. /* use RNG to get random port if using windows */
  35357. ready.port = GetRandomPort();
  35358. #endif
  35359. server_args.signal = &ready;
  35360. start_thread(test_server_nofail, &server_args, &serverThread);
  35361. wait_tcp_ready(&server_args);
  35362. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35363. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35364. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35365. #endif
  35366. ExpectIntEQ(WOLFSSL_SUCCESS,
  35367. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35368. ExpectIntEQ(WOLFSSL_SUCCESS,
  35369. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35370. ExpectIntEQ(WOLFSSL_SUCCESS,
  35371. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35372. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35373. /* force retry */
  35374. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  35375. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  35376. ExpectNotNull(ssl);
  35377. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35378. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35379. if (EXPECT_FAIL()) {
  35380. wolfSSL_free(ssl);
  35381. ssl = NULL;
  35382. }
  35383. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  35384. ExpectIntNE(BIO_should_retry(bio), 0);
  35385. ExpectIntEQ(BIO_should_read(bio), 0);
  35386. ExpectIntEQ(BIO_should_write(bio), 0);
  35387. /* now perform successful connection */
  35388. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35389. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35390. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  35391. ret = wolfSSL_get_error(ssl, -1);
  35392. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35393. ExpectIntNE(BIO_should_retry(bio), 0);
  35394. if (ret == WOLFSSL_ERROR_WANT_READ)
  35395. ExpectIntEQ(BIO_should_read(bio), 1);
  35396. else
  35397. ExpectIntEQ(BIO_should_read(bio), 0);
  35398. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  35399. ExpectIntEQ(BIO_should_write(bio), 1);
  35400. else
  35401. ExpectIntEQ(BIO_should_write(bio), 0);
  35402. }
  35403. else {
  35404. ExpectIntEQ(BIO_should_retry(bio), 0);
  35405. ExpectIntEQ(BIO_should_read(bio), 0);
  35406. ExpectIntEQ(BIO_should_write(bio), 0);
  35407. }
  35408. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35409. XSTRLEN("I hear you fa shizzle!")), 0);
  35410. BIO_free(bio);
  35411. wolfSSL_CTX_free(ctx);
  35412. CloseSocket(sockfd);
  35413. join_thread(serverThread);
  35414. FreeTcpReady(&ready);
  35415. #ifdef WOLFSSL_TIRTOS
  35416. fdOpenSession(Task_self());
  35417. #endif
  35418. #endif
  35419. return EXPECT_RESULT();
  35420. }
  35421. static int test_wolfSSL_BIO_connect(void)
  35422. {
  35423. EXPECT_DECLS;
  35424. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35425. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35426. tcp_ready ready;
  35427. func_args server_args;
  35428. THREAD_TYPE serverThread;
  35429. BIO *tcpBio = NULL;
  35430. BIO *sslBio = NULL;
  35431. SSL_CTX* ctx = NULL;
  35432. SSL *ssl = NULL;
  35433. SSL *sslPtr;
  35434. char msg[] = "hello wolfssl!";
  35435. char reply[30];
  35436. char buff[10] = {0};
  35437. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35438. ExpectIntEQ(WOLFSSL_SUCCESS,
  35439. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35440. ExpectIntEQ(WOLFSSL_SUCCESS,
  35441. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35442. ExpectIntEQ(WOLFSSL_SUCCESS,
  35443. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35444. /* Setup server */
  35445. XMEMSET(&server_args, 0, sizeof(func_args));
  35446. StartTCP();
  35447. InitTcpReady(&ready);
  35448. #if defined(USE_WINDOWS_API)
  35449. /* use RNG to get random port if using windows */
  35450. ready.port = GetRandomPort();
  35451. #endif
  35452. server_args.signal = &ready;
  35453. start_thread(test_server_nofail, &server_args, &serverThread);
  35454. wait_tcp_ready(&server_args);
  35455. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35456. /* Start the test proper */
  35457. /* Setup the TCP BIO */
  35458. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35459. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35460. /* Setup the SSL object */
  35461. ExpectNotNull(ssl = SSL_new(ctx));
  35462. SSL_set_connect_state(ssl);
  35463. /* Setup the SSL BIO */
  35464. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35465. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35466. if (EXPECT_FAIL()) {
  35467. wolfSSL_free(ssl);
  35468. }
  35469. /* Verify that BIO_get_ssl works. */
  35470. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35471. ExpectPtrEq(ssl, sslPtr);
  35472. /* Link BIO's so that sslBio uses tcpBio for IO */
  35473. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35474. /* Do TCP connect */
  35475. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  35476. /* Do TLS handshake */
  35477. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  35478. /* Test writing */
  35479. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35480. /* Expect length of default wolfSSL reply */
  35481. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35482. /* Clean it all up */
  35483. BIO_free_all(sslBio);
  35484. /* Server clean up */
  35485. join_thread(serverThread);
  35486. FreeTcpReady(&ready);
  35487. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35488. * after. */
  35489. XMEMSET(&server_args, 0, sizeof(func_args));
  35490. StartTCP();
  35491. InitTcpReady(&ready);
  35492. #if defined(USE_WINDOWS_API)
  35493. /* use RNG to get random port if using windows */
  35494. ready.port = GetRandomPort();
  35495. #endif
  35496. server_args.signal = &ready;
  35497. start_thread(test_server_nofail, &server_args, &serverThread);
  35498. wait_tcp_ready(&server_args);
  35499. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35500. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35501. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35502. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35503. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  35504. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  35505. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35506. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35507. /* Attempt to close the TLS connection gracefully. */
  35508. BIO_ssl_shutdown(sslBio);
  35509. BIO_free_all(sslBio);
  35510. join_thread(serverThread);
  35511. FreeTcpReady(&ready);
  35512. SSL_CTX_free(ctx);
  35513. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35514. wc_ecc_fp_free(); /* free per thread cache */
  35515. #endif
  35516. #endif
  35517. return EXPECT_RESULT();
  35518. }
  35519. static int test_wolfSSL_BIO_tls(void)
  35520. {
  35521. EXPECT_DECLS;
  35522. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35523. SSL_CTX* ctx = NULL;
  35524. SSL *ssl = NULL;
  35525. BIO *readBio = NULL;
  35526. BIO *writeBio = NULL;
  35527. int ret;
  35528. int err = 0;
  35529. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35530. ExpectNotNull(ssl = SSL_new(ctx));
  35531. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  35532. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  35533. /* Qt reads data from write-bio,
  35534. * then writes the read data into plain packet.
  35535. * Qt reads data from plain packet,
  35536. * then writes the read data into read-bio.
  35537. */
  35538. SSL_set_bio(ssl, readBio, writeBio);
  35539. do {
  35540. #ifdef WOLFSSL_ASYNC_CRYPT
  35541. if (err == WC_PENDING_E) {
  35542. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35543. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35544. }
  35545. #endif
  35546. ret = SSL_connect(ssl);
  35547. err = SSL_get_error(ssl, 0);
  35548. } while (err == WC_PENDING_E);
  35549. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35550. /* in this use case, should return WANT READ
  35551. * so that Qt will read the data from plain packet for next state.
  35552. */
  35553. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  35554. SSL_free(ssl);
  35555. SSL_CTX_free(ctx);
  35556. #endif
  35557. return EXPECT_RESULT();
  35558. }
  35559. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35560. defined(HAVE_HTTP_CLIENT)
  35561. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35562. {
  35563. BIO* clientBio;
  35564. SSL* sslClient;
  35565. SSL_CTX* ctx;
  35566. char connectAddr[20]; /* IP + port */;
  35567. (void)args;
  35568. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35569. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35570. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35571. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35572. AssertNotNull(sslClient = SSL_new(ctx));
  35573. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35574. SSL_set_bio(sslClient, clientBio, clientBio);
  35575. AssertIntEQ(SSL_connect(sslClient), 1);
  35576. SSL_free(sslClient);
  35577. SSL_CTX_free(ctx);
  35578. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35579. wc_ecc_fp_free(); /* free per thread cache */
  35580. #endif
  35581. return 0;
  35582. }
  35583. #endif
  35584. static int test_wolfSSL_BIO_accept(void)
  35585. {
  35586. EXPECT_DECLS;
  35587. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35588. defined(HAVE_HTTP_CLIENT)
  35589. BIO* serverBindBio = NULL;
  35590. BIO* serverAcceptBio = NULL;
  35591. SSL* sslServer = NULL;
  35592. SSL_CTX* ctx = NULL;
  35593. func_args args;
  35594. THREAD_TYPE thread;
  35595. char port[10]; /* 10 bytes should be enough to store the string
  35596. * representation of the port */
  35597. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35598. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  35599. /* First BIO_do_accept binds the port */
  35600. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  35601. XMEMSET(&args, 0, sizeof(func_args));
  35602. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35603. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  35604. /* Let's plug it into SSL to test */
  35605. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35606. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35607. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35608. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35609. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35610. ExpectNotNull(sslServer = SSL_new(ctx));
  35611. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35612. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35613. ExpectIntEQ(SSL_accept(sslServer), 1);
  35614. join_thread(thread);
  35615. BIO_free(serverBindBio);
  35616. SSL_free(sslServer);
  35617. SSL_CTX_free(ctx);
  35618. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35619. wc_ecc_fp_free(); /* free per thread cache */
  35620. #endif
  35621. #endif
  35622. return EXPECT_RESULT();
  35623. }
  35624. static int test_wolfSSL_BIO_write(void)
  35625. {
  35626. EXPECT_DECLS;
  35627. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35628. BIO* bio = NULL;
  35629. BIO* bio64 = NULL;
  35630. BIO* bio_mem = NULL;
  35631. BIO* ptr = NULL;
  35632. int sz;
  35633. char msg[] = "conversion test";
  35634. char out[40];
  35635. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35636. void* bufPtr = NULL;
  35637. BUF_MEM* buf = NULL;
  35638. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  35639. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35640. if (EXPECT_FAIL()) {
  35641. BIO_free(bio64);
  35642. }
  35643. /* now should convert to base64 then write to memory */
  35644. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35645. BIO_flush(bio);
  35646. /* test BIO chain */
  35647. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35648. ExpectNotNull(buf);
  35649. ExpectIntEQ(buf->length, 25);
  35650. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35651. ExpectPtrEq(buf->data, bufPtr);
  35652. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35653. sz = sizeof(out);
  35654. XMEMSET(out, 0, sz);
  35655. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35656. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  35657. /* write then read should return the same message */
  35658. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35659. sz = sizeof(out);
  35660. XMEMSET(out, 0, sz);
  35661. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  35662. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35663. /* now try encoding with no line ending */
  35664. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35665. #ifdef HAVE_EX_DATA
  35666. BIO_set_ex_data(bio64, 0, (void*) "data");
  35667. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35668. #endif
  35669. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35670. BIO_flush(bio);
  35671. sz = sizeof(out);
  35672. XMEMSET(out, 0, sz);
  35673. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35674. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  35675. BIO_free_all(bio); /* frees bio64 also */
  35676. bio = NULL;
  35677. /* test with more than one bio64 in list */
  35678. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  35679. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35680. if (EXPECT_FAIL()) {
  35681. BIO_free(bio64);
  35682. bio64 = NULL;
  35683. }
  35684. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  35685. ExpectNotNull(BIO_push(bio64, bio_mem));
  35686. if (EXPECT_FAIL()) {
  35687. BIO_free(bio_mem);
  35688. }
  35689. /* now should convert to base64 when stored and then decode with read */
  35690. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35691. BIO_flush(bio);
  35692. sz = sizeof(out);
  35693. XMEMSET(out, 0, sz);
  35694. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35695. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  35696. BIO_clear_flags(bio64, ~0);
  35697. BIO_set_retry_read(bio);
  35698. BIO_free_all(bio); /* frees bio64s also */
  35699. bio = NULL;
  35700. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  35701. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35702. BIO_free(bio);
  35703. #endif
  35704. return EXPECT_RESULT();
  35705. }
  35706. static int test_wolfSSL_BIO_printf(void)
  35707. {
  35708. EXPECT_DECLS;
  35709. #if defined(OPENSSL_ALL)
  35710. BIO* bio = NULL;
  35711. int sz = 7;
  35712. char msg[] = "TLS 1.3 for the world";
  35713. char out[60];
  35714. char expected[] = "TLS 1.3 for the world : sz = 7";
  35715. XMEMSET(out, 0, sizeof(out));
  35716. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35717. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35718. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35719. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35720. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35721. BIO_free(bio);
  35722. #endif
  35723. return EXPECT_RESULT();
  35724. }
  35725. static int test_wolfSSL_BIO_f_md(void)
  35726. {
  35727. EXPECT_DECLS;
  35728. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35729. BIO* bio = NULL;
  35730. BIO* mem = NULL;
  35731. char msg[] = "message to hash";
  35732. char out[60];
  35733. EVP_MD_CTX* ctx = NULL;
  35734. const unsigned char testKey[] =
  35735. {
  35736. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35737. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35738. 0x0b, 0x0b, 0x0b, 0x0b
  35739. };
  35740. const char testData[] = "Hi There";
  35741. const unsigned char testResult[] =
  35742. {
  35743. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35744. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35745. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35746. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35747. };
  35748. const unsigned char expectedHash[] =
  35749. {
  35750. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35751. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35752. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35753. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35754. };
  35755. const unsigned char emptyHash[] =
  35756. {
  35757. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35758. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35759. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35760. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35761. };
  35762. unsigned char check[sizeof(testResult) + 1];
  35763. size_t checkSz = -1;
  35764. EVP_PKEY* key = NULL;
  35765. XMEMSET(out, 0, sizeof(out));
  35766. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35767. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  35768. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35769. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35770. /* should not be able to write/read yet since just digest wrapper and no
  35771. * data is passing through the bio */
  35772. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  35773. ExpectIntEQ(BIO_pending(bio), 0);
  35774. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35775. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  35776. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35777. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35778. BIO_reset(bio);
  35779. /* append BIO mem to bio in order to read/write */
  35780. ExpectNotNull(bio = BIO_push(bio, mem));
  35781. XMEMSET(out, 0, sizeof(out));
  35782. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35783. ExpectIntEQ(BIO_pending(bio), 16);
  35784. /* this just reads the message and does not hash it (gets calls final) */
  35785. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35786. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35787. /* create a message digest using BIO */
  35788. XMEMSET(out, 0, sizeof(out));
  35789. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35790. ExpectIntEQ(BIO_pending(mem), 16);
  35791. ExpectIntEQ(BIO_pending(bio), 16);
  35792. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35793. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35794. BIO_free(bio);
  35795. bio = NULL;
  35796. BIO_free(mem);
  35797. mem = NULL;
  35798. /* test with HMAC */
  35799. XMEMSET(out, 0, sizeof(out));
  35800. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35801. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  35802. BIO_get_md_ctx(bio, &ctx);
  35803. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  35804. (int)sizeof(testKey)));
  35805. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35806. ExpectNotNull(bio = BIO_push(bio, mem));
  35807. BIO_write(bio, testData, (int)strlen(testData));
  35808. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  35809. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  35810. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35811. EVP_PKEY_free(key);
  35812. BIO_free(bio);
  35813. BIO_free(mem);
  35814. #endif
  35815. return EXPECT_RESULT();
  35816. }
  35817. static int test_wolfSSL_BIO_up_ref(void)
  35818. {
  35819. EXPECT_DECLS;
  35820. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35821. BIO* bio = NULL;
  35822. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  35823. ExpectIntEQ(BIO_up_ref(NULL), 0);
  35824. ExpectIntEQ(BIO_up_ref(bio), 1);
  35825. BIO_free(bio);
  35826. ExpectIntEQ(BIO_up_ref(bio), 1);
  35827. BIO_free(bio);
  35828. BIO_free(bio);
  35829. #endif
  35830. return EXPECT_RESULT();
  35831. }
  35832. static int test_wolfSSL_BIO_reset(void)
  35833. {
  35834. EXPECT_DECLS;
  35835. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35836. BIO* bio = NULL;
  35837. byte buf[16];
  35838. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  35839. (word32)XSTRLEN("secure your data")));
  35840. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  35841. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  35842. XMEMSET(buf, 0, 16);
  35843. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  35844. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  35845. /* You cannot write to MEM BIO with read-only mode. */
  35846. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  35847. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  35848. XMEMSET(buf, 0, 16);
  35849. ExpectIntEQ(BIO_reset(bio), 0);
  35850. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  35851. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  35852. BIO_free(bio);
  35853. #endif
  35854. return EXPECT_RESULT();
  35855. }
  35856. #endif /* !NO_BIO */
  35857. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35858. /* test that the callback arg is correct */
  35859. static int certCbArg = 0;
  35860. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35861. {
  35862. if (ssl == NULL || arg != &certCbArg)
  35863. return 0;
  35864. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35865. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35866. return 0;
  35867. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35868. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35869. return 0;
  35870. return 1;
  35871. }
  35872. static int clientCertSetupCb(WOLFSSL_CTX* ctx)
  35873. {
  35874. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35875. return TEST_SUCCESS;
  35876. }
  35877. /**
  35878. * This is only done because test_client_nofail has no way to stop
  35879. * certificate and key loading
  35880. */
  35881. static int clientCertClearCb(WOLFSSL* ssl)
  35882. {
  35883. /* Clear the loaded certs to force the callbacks to set them up */
  35884. SSL_certs_clear(ssl);
  35885. return TEST_SUCCESS;
  35886. }
  35887. static int serverCertCb(WOLFSSL* ssl, void* arg)
  35888. {
  35889. if (ssl == NULL || arg != &certCbArg)
  35890. return 0;
  35891. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  35892. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35893. return 0;
  35894. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  35895. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35896. return 0;
  35897. return 1;
  35898. }
  35899. static int serverCertSetupCb(WOLFSSL_CTX* ctx)
  35900. {
  35901. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  35902. return TEST_SUCCESS;
  35903. }
  35904. /**
  35905. * This is only done because test_server_nofail has no way to stop
  35906. * certificate and key loading
  35907. */
  35908. static int serverCertClearCb(WOLFSSL* ssl)
  35909. {
  35910. /* Clear the loaded certs to force the callbacks to set them up */
  35911. SSL_certs_clear(ssl);
  35912. return TEST_SUCCESS;
  35913. }
  35914. #endif
  35915. static int test_wolfSSL_cert_cb(void)
  35916. {
  35917. EXPECT_DECLS;
  35918. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35919. test_ssl_cbf func_cb_client;
  35920. test_ssl_cbf func_cb_server;
  35921. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35922. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35923. func_cb_client.ctx_ready = clientCertSetupCb;
  35924. func_cb_client.ssl_ready = clientCertClearCb;
  35925. func_cb_server.ctx_ready = serverCertSetupCb;
  35926. func_cb_server.ssl_ready = serverCertClearCb;
  35927. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35928. &func_cb_server, NULL), TEST_SUCCESS);
  35929. #endif
  35930. return EXPECT_RESULT();
  35931. }
  35932. static int test_wolfSSL_SESSION(void)
  35933. {
  35934. EXPECT_DECLS;
  35935. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35936. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35937. !defined(NO_SESSION_CACHE)
  35938. WOLFSSL* ssl = NULL;
  35939. WOLFSSL_CTX* ctx = NULL;
  35940. WOLFSSL_SESSION* sess = NULL;
  35941. WOLFSSL_SESSION* sess_copy = NULL;
  35942. #ifdef OPENSSL_EXTRA
  35943. #ifdef HAVE_EXT_CACHE
  35944. unsigned char* sessDer = NULL;
  35945. unsigned char* ptr = NULL;
  35946. int sz;
  35947. #endif
  35948. const unsigned char context[] = "user app context";
  35949. unsigned int contextSz = (unsigned int)sizeof(context);
  35950. #endif
  35951. int ret, err;
  35952. SOCKET_T sockfd;
  35953. tcp_ready ready;
  35954. func_args server_args;
  35955. THREAD_TYPE serverThread;
  35956. char msg[80];
  35957. const char* sendGET = "GET";
  35958. /* TLS v1.3 requires session tickets */
  35959. /* CHACHA and POLY1305 required for myTicketEncCb */
  35960. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  35961. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  35962. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  35963. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35964. #else
  35965. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35966. #endif
  35967. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35968. WOLFSSL_FILETYPE_PEM));
  35969. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35970. WOLFSSL_FILETYPE_PEM));
  35971. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35972. WOLFSSL_SUCCESS);
  35973. #ifdef WOLFSSL_ENCRYPTED_KEYS
  35974. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  35975. #endif
  35976. #ifdef HAVE_SESSION_TICKET
  35977. /* Use session tickets, for ticket tests below */
  35978. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  35979. #endif
  35980. XMEMSET(&server_args, 0, sizeof(func_args));
  35981. #ifdef WOLFSSL_TIRTOS
  35982. fdOpenSession(Task_self());
  35983. #endif
  35984. StartTCP();
  35985. InitTcpReady(&ready);
  35986. #if defined(USE_WINDOWS_API)
  35987. /* use RNG to get random port if using windows */
  35988. ready.port = GetRandomPort();
  35989. #endif
  35990. server_args.signal = &ready;
  35991. start_thread(test_server_nofail, &server_args, &serverThread);
  35992. wait_tcp_ready(&server_args);
  35993. /* client connection */
  35994. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35995. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  35996. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35997. #ifdef WOLFSSL_ASYNC_CRYPT
  35998. err = 0; /* Reset error */
  35999. #endif
  36000. do {
  36001. #ifdef WOLFSSL_ASYNC_CRYPT
  36002. if (err == WC_PENDING_E) {
  36003. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36004. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36005. }
  36006. #endif
  36007. ret = wolfSSL_connect(ssl);
  36008. err = wolfSSL_get_error(ssl, 0);
  36009. } while (err == WC_PENDING_E);
  36010. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36011. #ifdef WOLFSSL_ASYNC_CRYPT
  36012. err = 0; /* Reset error */
  36013. #endif
  36014. do {
  36015. #ifdef WOLFSSL_ASYNC_CRYPT
  36016. if (err == WC_PENDING_E) {
  36017. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36018. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36019. }
  36020. #endif
  36021. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  36022. err = wolfSSL_get_error(ssl, 0);
  36023. } while (err == WC_PENDING_E);
  36024. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  36025. #ifdef WOLFSSL_ASYNC_CRYPT
  36026. err = 0; /* Reset error */
  36027. #endif
  36028. do {
  36029. #ifdef WOLFSSL_ASYNC_CRYPT
  36030. if (err == WC_PENDING_E) {
  36031. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  36032. if (ret < 0) { break; } else if (ret == 0) { continue; }
  36033. }
  36034. #endif
  36035. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  36036. err = wolfSSL_get_error(ssl, 0);
  36037. } while (err == WC_PENDING_E);
  36038. ExpectIntEQ(ret, 23);
  36039. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  36040. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  36041. #ifdef HAVE_EXT_CACHE
  36042. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  36043. * HAVE_EXT_CACHE we get new objects each time */
  36044. #endif
  36045. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  36046. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  36047. sess = wolfSSL_get_session(ssl);
  36048. #ifdef OPENSSL_EXTRA
  36049. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  36050. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  36051. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  36052. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  36053. #ifdef HAVE_SESSION_TICKET
  36054. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  36055. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  36056. SESSION_TICKET_HINT_DEFAULT);
  36057. #else
  36058. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  36059. #endif
  36060. #else
  36061. (void)sess;
  36062. #endif /* OPENSSL_EXTRA */
  36063. /* Retain copy of the session for later testing */
  36064. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  36065. wolfSSL_shutdown(ssl);
  36066. wolfSSL_free(ssl); ssl = NULL;
  36067. join_thread(serverThread);
  36068. FreeTcpReady(&ready);
  36069. #ifdef WOLFSSL_TIRTOS
  36070. fdOpenSession(Task_self());
  36071. #endif
  36072. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  36073. {
  36074. X509 *x509 = NULL;
  36075. char buf[30];
  36076. int bufSz = 0;
  36077. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  36078. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  36079. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  36080. sizeof(buf))), 0);
  36081. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  36082. if (bufSz == 7) {
  36083. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  36084. }
  36085. if (bufSz == 16) {
  36086. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  36087. }
  36088. }
  36089. #endif
  36090. #ifdef HAVE_EXT_CACHE
  36091. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  36092. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  36093. sess_copy = NULL;
  36094. #endif
  36095. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  36096. /* get session from DER and update the timeout */
  36097. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  36098. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  36099. wolfSSL_SESSION_free(sess); sess = NULL;
  36100. sess = NULL;
  36101. ptr = sessDer;
  36102. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  36103. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  36104. (const unsigned char**)&ptr, sz));
  36105. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  36106. sessDer = NULL;
  36107. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  36108. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36109. #endif
  36110. /* successful set session test */
  36111. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36112. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  36113. #ifdef HAVE_SESSION_TICKET
  36114. /* Test set/get session ticket */
  36115. {
  36116. const char* ticket = "This is a session ticket";
  36117. char buf[64] = {0};
  36118. word32 bufSz = (word32)sizeof(buf);
  36119. ExpectIntEQ(SSL_SUCCESS,
  36120. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  36121. (word32)XSTRLEN(ticket)));
  36122. ExpectIntEQ(SSL_SUCCESS,
  36123. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  36124. ExpectStrEQ(ticket, buf);
  36125. }
  36126. #endif
  36127. #ifdef OPENSSL_EXTRA
  36128. /* session timeout case */
  36129. /* make the session to be expired */
  36130. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  36131. XSLEEP_MS(1200);
  36132. /* SSL_set_session should reject specified session but return success
  36133. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  36134. */
  36135. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  36136. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  36137. #else
  36138. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  36139. #endif
  36140. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  36141. /* fail case with miss match session context IDs (use compatibility API) */
  36142. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  36143. SSL_SUCCESS);
  36144. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36145. wolfSSL_free(ssl); ssl = NULL;
  36146. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  36147. SSL_FAILURE);
  36148. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  36149. SSL_SUCCESS);
  36150. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36151. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  36152. #endif /* OPENSSL_EXTRA */
  36153. wolfSSL_free(ssl);
  36154. wolfSSL_SESSION_free(sess);
  36155. wolfSSL_CTX_free(ctx);
  36156. #endif
  36157. return EXPECT_RESULT();
  36158. }
  36159. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  36160. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  36161. static int clientSessRemCountMalloc = 0;
  36162. static int serverSessRemCountMalloc = 0;
  36163. static int clientSessRemCountFree = 0;
  36164. static int serverSessRemCountFree = 0;
  36165. static WOLFSSL_CTX* serverSessCtx = NULL;
  36166. static WOLFSSL_SESSION* serverSess = NULL;
  36167. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36168. !defined(NO_SESSION_CACHE_REF)
  36169. static WOLFSSL_CTX* clientSessCtx = NULL;
  36170. static WOLFSSL_SESSION* clientSess = NULL;
  36171. #endif
  36172. static int serverSessRemIdx = 3;
  36173. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  36174. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  36175. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  36176. {
  36177. int* side;
  36178. (void)ctx;
  36179. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  36180. if (side != NULL) {
  36181. if (*side == WOLFSSL_CLIENT_END)
  36182. clientSessRemCountFree++;
  36183. else
  36184. serverSessRemCountFree++;
  36185. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  36186. }
  36187. }
  36188. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  36189. {
  36190. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  36191. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  36192. !defined(NO_SESSION_CACHE_REF)
  36193. {
  36194. EXPECT_DECLS;
  36195. /* Allow downgrade, set min version, and disable TLS 1.3.
  36196. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  36197. * reference to the session cache. But with WOLFSSL_TLS13 and without
  36198. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  36199. * the session in the cache. In this case we need to downgrade to
  36200. * previous versions to just use the legacy session ID field. */
  36201. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  36202. SSL_SUCCESS);
  36203. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  36204. SSL_SUCCESS);
  36205. return EXPECT_RESULT();
  36206. }
  36207. #else
  36208. return TEST_SUCCESS;
  36209. #endif
  36210. }
  36211. static int SessRemSslSetupCb(WOLFSSL* ssl)
  36212. {
  36213. EXPECT_DECLS;
  36214. int* side;
  36215. if (SSL_is_server(ssl)) {
  36216. side = &sessRemCtx_Server;
  36217. serverSessRemCountMalloc++;
  36218. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  36219. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  36220. SSL_SUCCESS);
  36221. }
  36222. else {
  36223. side = &sessRemCtx_Client;
  36224. clientSessRemCountMalloc++;
  36225. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36226. !defined(NO_SESSION_CACHE_REF)
  36227. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  36228. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  36229. SSL_SUCCESS);
  36230. #endif
  36231. }
  36232. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  36233. serverSessRemIdx, side), SSL_SUCCESS);
  36234. return EXPECT_RESULT();
  36235. }
  36236. #endif
  36237. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  36238. {
  36239. EXPECT_DECLS;
  36240. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  36241. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  36242. /* Check that the remove callback gets called for external data in a
  36243. * session object */
  36244. test_ssl_cbf func_cb;
  36245. XMEMSET(&func_cb, 0, sizeof(func_cb));
  36246. func_cb.ctx_ready = SessRemCtxSetupCb;
  36247. func_cb.on_result = SessRemSslSetupCb;
  36248. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  36249. NULL), TEST_SUCCESS);
  36250. /* Both should have been allocated */
  36251. ExpectIntEQ(clientSessRemCountMalloc, 1);
  36252. ExpectIntEQ(serverSessRemCountMalloc, 1);
  36253. /* This should not be called yet. Session wasn't evicted from cache yet. */
  36254. ExpectIntEQ(clientSessRemCountFree, 0);
  36255. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36256. !defined(NO_SESSION_CACHE_REF)
  36257. /* Force a cache lookup */
  36258. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36259. /* Force a cache update */
  36260. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  36261. /* This should set the timeout to 0 and call the remove callback from within
  36262. * the session cache. */
  36263. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  36264. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  36265. ExpectIntEQ(clientSessRemCountFree, 1);
  36266. #endif
  36267. /* Server session is in the cache so ex_data isn't free'd with the SSL
  36268. * object */
  36269. ExpectIntEQ(serverSessRemCountFree, 0);
  36270. /* Force a cache lookup */
  36271. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36272. /* Force a cache update */
  36273. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  36274. /* This should set the timeout to 0 and call the remove callback from within
  36275. * the session cache. */
  36276. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  36277. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  36278. ExpectIntEQ(serverSessRemCountFree, 1);
  36279. /* Need to free the references that we kept */
  36280. SSL_CTX_free(serverSessCtx);
  36281. SSL_SESSION_free(serverSess);
  36282. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  36283. !defined(NO_SESSION_CACHE_REF)
  36284. SSL_CTX_free(clientSessCtx);
  36285. SSL_SESSION_free(clientSess);
  36286. #endif
  36287. #endif
  36288. return EXPECT_RESULT();
  36289. }
  36290. static int test_wolfSSL_ticket_keys(void)
  36291. {
  36292. EXPECT_DECLS;
  36293. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  36294. !defined(NO_WOLFSSL_SERVER)
  36295. WOLFSSL_CTX* ctx = NULL;
  36296. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  36297. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36298. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  36299. WOLFSSL_FAILURE);
  36300. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  36301. WOLFSSL_FAILURE);
  36302. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  36303. WOLFSSL_FAILURE);
  36304. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  36305. WOLFSSL_FAILURE);
  36306. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36307. WOLFSSL_FAILURE);
  36308. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36309. WOLFSSL_FAILURE);
  36310. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36311. WOLFSSL_FAILURE);
  36312. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36313. WOLFSSL_FAILURE);
  36314. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36315. WOLFSSL_FAILURE);
  36316. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36317. WOLFSSL_FAILURE);
  36318. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36319. WOLFSSL_FAILURE);
  36320. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36321. WOLFSSL_FAILURE);
  36322. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36323. WOLFSSL_FAILURE);
  36324. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36325. WOLFSSL_FAILURE);
  36326. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36327. WOLFSSL_SUCCESS);
  36328. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36329. WOLFSSL_SUCCESS);
  36330. wolfSSL_CTX_free(ctx);
  36331. #endif
  36332. return EXPECT_RESULT();
  36333. }
  36334. #ifndef NO_BIO
  36335. static int test_wolfSSL_d2i_PUBKEY(void)
  36336. {
  36337. EXPECT_DECLS;
  36338. #if defined(OPENSSL_EXTRA)
  36339. BIO* bio = NULL;
  36340. EVP_PKEY* pkey = NULL;
  36341. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36342. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  36343. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36344. /* RSA PUBKEY test */
  36345. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  36346. sizeof_client_keypub_der_2048), 0);
  36347. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36348. EVP_PKEY_free(pkey);
  36349. pkey = NULL;
  36350. #endif
  36351. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36352. /* ECC PUBKEY test */
  36353. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36354. sizeof_ecc_clikeypub_der_256), 0);
  36355. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36356. EVP_PKEY_free(pkey);
  36357. pkey = NULL;
  36358. #endif
  36359. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36360. /* DSA PUBKEY test */
  36361. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36362. sizeof_dsa_pub_key_der_2048), 0);
  36363. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36364. EVP_PKEY_free(pkey);
  36365. pkey = NULL;
  36366. #endif
  36367. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36368. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36369. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36370. (HAVE_FIPS_VERSION > 2))
  36371. /* DH PUBKEY test */
  36372. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36373. sizeof_dh_pub_key_der_2048), 0);
  36374. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36375. EVP_PKEY_free(pkey);
  36376. pkey = NULL;
  36377. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36378. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36379. BIO_free(bio);
  36380. (void)pkey;
  36381. #endif
  36382. return EXPECT_RESULT();
  36383. }
  36384. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36385. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36386. {
  36387. EXPECT_DECLS;
  36388. BIO* bio = NULL;
  36389. EVP_PKEY* pkey = NULL;
  36390. WOLFSSL_CTX* ctx = NULL;
  36391. #if defined(WOLFSSL_KEY_GEN)
  36392. unsigned char buff[4096];
  36393. unsigned char* bufPtr = buff;
  36394. #endif
  36395. /* test creating new EVP_PKEY with bad arg */
  36396. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36397. /* test loading RSA key using BIO */
  36398. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36399. {
  36400. XFILE file = XBADFILE;
  36401. const char* fname = "./certs/server-key.der";
  36402. size_t sz;
  36403. byte* buf = NULL;
  36404. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  36405. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36406. ExpectTrue((sz = XFTELL(file)) != 0);
  36407. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  36408. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36409. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  36410. if (file != XBADFILE) {
  36411. XFCLOSE(file);
  36412. }
  36413. /* Test using BIO new mem and loading DER private key */
  36414. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36415. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36416. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36417. BIO_free(bio);
  36418. bio = NULL;
  36419. EVP_PKEY_free(pkey);
  36420. pkey = NULL;
  36421. }
  36422. #endif
  36423. /* test loading ECC key using BIO */
  36424. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36425. {
  36426. XFILE file = XBADFILE;
  36427. const char* fname = "./certs/ecc-key.der";
  36428. size_t sz;
  36429. byte* buf = NULL;
  36430. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  36431. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36432. ExpectTrue((sz = XFTELL(file)) != 0);
  36433. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  36434. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36435. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  36436. if (file != XBADFILE)
  36437. XFCLOSE(file);
  36438. /* Test using BIO new mem and loading DER private key */
  36439. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36440. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36441. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36442. BIO_free(bio);
  36443. bio = NULL;
  36444. EVP_PKEY_free(pkey);
  36445. pkey = NULL;
  36446. }
  36447. #endif
  36448. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36449. #ifndef NO_WOLFSSL_SERVER
  36450. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36451. #else
  36452. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36453. #endif
  36454. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36455. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36456. {
  36457. RSA* rsa = NULL;
  36458. /* Tests bad parameters */
  36459. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36460. /* RSA not set yet, expecting to fail*/
  36461. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36462. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36463. /* set RSA using bio*/
  36464. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  36465. sizeof_client_key_der_2048), 0);
  36466. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36467. ExpectNotNull(rsa);
  36468. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36469. /* i2d RSAprivate key tests */
  36470. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36471. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36472. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36473. sizeof_client_key_der_2048);
  36474. bufPtr -= sizeof_client_key_der_2048;
  36475. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36476. sizeof_client_key_der_2048), 0);
  36477. bufPtr = NULL;
  36478. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36479. sizeof_client_key_der_2048);
  36480. ExpectNotNull(bufPtr);
  36481. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36482. sizeof_client_key_der_2048), 0);
  36483. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36484. RSA_free(rsa);
  36485. rsa = RSA_new();
  36486. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36487. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36488. RSA_free(rsa);
  36489. }
  36490. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36491. SSL_CTX_free(ctx);
  36492. ctx = NULL;
  36493. BIO_free(bio);
  36494. bio = NULL;
  36495. return EXPECT_RESULT();
  36496. }
  36497. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  36498. #endif /* !NO_BIO */
  36499. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36500. {
  36501. EXPECT_DECLS;
  36502. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36503. !defined(NO_RSA)
  36504. X509* x509 = NULL;
  36505. GENERAL_NAME* gn = NULL;
  36506. unsigned char buf[4096];
  36507. const unsigned char* bufPt = NULL;
  36508. int bytes = 0;
  36509. int i;
  36510. int j;
  36511. XFILE f = XBADFILE;
  36512. STACK_OF(GENERAL_NAME)* sk = NULL;
  36513. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  36514. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36515. if (f != XBADFILE)
  36516. XFCLOSE(f);
  36517. for (j = 0; j < 2; ++j) {
  36518. bufPt = buf;
  36519. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36520. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36521. NID_subject_alt_name, NULL, NULL));
  36522. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36523. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36524. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36525. if (gn != NULL) {
  36526. switch (gn->type) {
  36527. case GEN_DNS:
  36528. fprintf(stderr, "found type GEN_DNS\n");
  36529. break;
  36530. case GEN_EMAIL:
  36531. fprintf(stderr, "found type GEN_EMAIL\n");
  36532. break;
  36533. case GEN_URI:
  36534. fprintf(stderr, "found type GEN_URI\n");
  36535. break;
  36536. }
  36537. }
  36538. }
  36539. X509_free(x509);
  36540. x509 = NULL;
  36541. if (j == 0) {
  36542. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36543. }
  36544. else {
  36545. /*
  36546. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36547. * it was supposed to. This is a regression test for that bug.
  36548. */
  36549. GENERAL_NAMES_free(sk);
  36550. }
  36551. sk = NULL;
  36552. }
  36553. #endif
  36554. return EXPECT_RESULT();
  36555. }
  36556. static int test_wolfSSL_GENERAL_NAME_print(void)
  36557. {
  36558. EXPECT_DECLS;
  36559. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36560. X509* x509 = NULL;
  36561. GENERAL_NAME* gn = NULL;
  36562. unsigned char buf[4096];
  36563. const unsigned char* bufPt = NULL;
  36564. int bytes;
  36565. XFILE f = XBADFILE;
  36566. STACK_OF(GENERAL_NAME)* sk = NULL;
  36567. BIO* out = NULL;
  36568. unsigned char outbuf[128];
  36569. X509_EXTENSION* ext = NULL;
  36570. AUTHORITY_INFO_ACCESS* aia = NULL;
  36571. ACCESS_DESCRIPTION* ad = NULL;
  36572. ASN1_IA5STRING *dnsname = NULL;
  36573. const unsigned char v4Addr[] = {192,168,53,1};
  36574. const unsigned char v6Addr[] =
  36575. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36576. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36577. const unsigned char email[] =
  36578. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36579. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36580. const char* dnsStr = "DNS:example.com";
  36581. const char* uriStr = "URI:http://127.0.0.1:22220";
  36582. const char* v4addStr = "IP Address:192.168.53.1";
  36583. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36584. const char* emailStr = "email:info@wolfssl.com";
  36585. const char* othrStr = "othername:<unsupported>";
  36586. const char* x400Str = "X400Name:<unsupported>";
  36587. const char* ediStr = "EdiPartyName:<unsupported>";
  36588. /* BIO to output */
  36589. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  36590. /* test for NULL param */
  36591. gn = NULL;
  36592. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36593. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36594. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36595. /* test for GEN_DNS */
  36596. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  36597. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36598. if (f != XBADFILE) {
  36599. XFCLOSE(f);
  36600. f = XBADFILE;
  36601. }
  36602. bufPt = buf;
  36603. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36604. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36605. NID_subject_alt_name, NULL, NULL));
  36606. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36607. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36608. XMEMSET(outbuf, 0, sizeof(outbuf));
  36609. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36610. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36611. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36612. sk = NULL;
  36613. X509_free(x509);
  36614. x509 = NULL;
  36615. /* Lets test for setting as well. */
  36616. ExpectNotNull(gn = GENERAL_NAME_new());
  36617. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  36618. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  36619. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  36620. dnsname = NULL;
  36621. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36622. XMEMSET(outbuf, 0, sizeof(outbuf));
  36623. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36624. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36625. GENERAL_NAME_free(gn);
  36626. /* test for GEN_URI */
  36627. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  36628. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36629. if (f != XBADFILE) {
  36630. XFCLOSE(f);
  36631. f = XBADFILE;
  36632. }
  36633. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36634. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  36635. ext));
  36636. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  36637. if (ad != NULL) {
  36638. gn = ad->location;
  36639. }
  36640. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36641. gn = NULL;
  36642. XMEMSET(outbuf,0,sizeof(outbuf));
  36643. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36644. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36645. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36646. aia = NULL;
  36647. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36648. ExpectNotNull(aia);
  36649. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36650. aia = NULL;
  36651. X509_free(x509);
  36652. x509 = NULL;
  36653. /* test for GEN_IPADD */
  36654. /* ip v4 address */
  36655. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36656. if (gn != NULL) {
  36657. gn->type = GEN_IPADD;
  36658. if (gn->d.iPAddress != NULL) {
  36659. gn->d.iPAddress->length = sizeof(v4Addr);
  36660. }
  36661. }
  36662. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36663. sizeof(v4Addr)), 1);
  36664. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36665. XMEMSET(outbuf,0,sizeof(outbuf));
  36666. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36667. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36668. GENERAL_NAME_free(gn);
  36669. gn = NULL;
  36670. /* ip v6 address */
  36671. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36672. if (gn != NULL) {
  36673. gn->type = GEN_IPADD;
  36674. if (gn->d.iPAddress != NULL) {
  36675. gn->d.iPAddress->length = sizeof(v6Addr);
  36676. }
  36677. }
  36678. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36679. sizeof(v6Addr)), 1);
  36680. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36681. XMEMSET(outbuf,0,sizeof(outbuf));
  36682. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36683. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36684. GENERAL_NAME_free(gn);
  36685. gn = NULL;
  36686. /* test for GEN_EMAIL */
  36687. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36688. if (gn != NULL) {
  36689. gn->type = GEN_EMAIL;
  36690. if (gn->d.rfc822Name != NULL) {
  36691. gn->d.rfc822Name->length = sizeof(email);
  36692. }
  36693. }
  36694. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  36695. 1);
  36696. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36697. XMEMSET(outbuf,0,sizeof(outbuf));
  36698. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36699. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36700. GENERAL_NAME_free(gn);
  36701. gn = NULL;
  36702. /* test for GEN_OTHERNAME */
  36703. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36704. if (gn != NULL) {
  36705. gn->type = GEN_OTHERNAME;
  36706. }
  36707. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36708. XMEMSET(outbuf,0,sizeof(outbuf));
  36709. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36710. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36711. GENERAL_NAME_free(gn);
  36712. gn = NULL;
  36713. /* test for GEN_X400 */
  36714. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36715. if (gn != NULL) {
  36716. gn->type = GEN_X400;
  36717. }
  36718. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36719. XMEMSET(outbuf,0,sizeof(outbuf));
  36720. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36721. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36722. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  36723. if (gn != NULL) {
  36724. gn->type = GEN_IA5;
  36725. }
  36726. GENERAL_NAME_free(gn);
  36727. gn = NULL;
  36728. /* test for GEN_EDIPARTY */
  36729. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36730. if (gn != NULL) {
  36731. gn->type = GEN_EDIPARTY;
  36732. }
  36733. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  36734. XMEMSET(outbuf,0,sizeof(outbuf));
  36735. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36736. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36737. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  36738. if (gn != NULL) {
  36739. gn->type = GEN_IA5;
  36740. }
  36741. GENERAL_NAME_free(gn);
  36742. gn = NULL;
  36743. BIO_free(out);
  36744. #endif /* OPENSSL_ALL */
  36745. return EXPECT_RESULT();
  36746. }
  36747. static int test_wolfSSL_sk_DIST_POINT(void)
  36748. {
  36749. EXPECT_DECLS;
  36750. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36751. !defined(NO_RSA)
  36752. X509* x509 = NULL;
  36753. unsigned char buf[4096];
  36754. const unsigned char* bufPt;
  36755. int bytes;
  36756. int i;
  36757. int j;
  36758. XFILE f = XBADFILE;
  36759. DIST_POINT* dp;
  36760. DIST_POINT_NAME* dpn;
  36761. GENERAL_NAME* gn;
  36762. ASN1_IA5STRING* uri;
  36763. STACK_OF(DIST_POINT)* dps = NULL;
  36764. STACK_OF(GENERAL_NAME)* gns = NULL;
  36765. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36766. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  36767. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36768. if (f != XBADFILE)
  36769. XFCLOSE(f);
  36770. bufPt = buf;
  36771. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36772. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36773. NID_crl_distribution_points, NULL, NULL));
  36774. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  36775. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36776. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  36777. ExpectNotNull(dpn = dp->distpoint);
  36778. /* this should be type 0, fullname */
  36779. ExpectIntEQ(dpn->type, 0);
  36780. ExpectNotNull(gns = dp->distpoint->name.fullname);
  36781. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36782. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36783. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  36784. ExpectIntEQ(gn->type, GEN_URI);
  36785. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  36786. ExpectNotNull(uri->data);
  36787. ExpectIntGT(uri->length, 0);
  36788. }
  36789. }
  36790. X509_free(x509);
  36791. CRL_DIST_POINTS_free(dps);
  36792. #endif
  36793. return EXPECT_RESULT();
  36794. }
  36795. static int test_wolfSSL_MD4(void)
  36796. {
  36797. EXPECT_DECLS;
  36798. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36799. MD4_CTX md4;
  36800. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36801. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36802. "789012345678901234567890";
  36803. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36804. "\xcc\x05\x36";
  36805. int msgSz = (int)XSTRLEN(msg);
  36806. XMEMSET(out, 0, sizeof(out));
  36807. MD4_Init(&md4);
  36808. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36809. MD4_Final(out, &md4);
  36810. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36811. #endif
  36812. return EXPECT_RESULT();
  36813. }
  36814. static int test_wolfSSL_verify_mode(void)
  36815. {
  36816. EXPECT_DECLS;
  36817. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36818. WOLFSSL* ssl = NULL;
  36819. WOLFSSL_CTX* ctx = NULL;
  36820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36821. ExpectNotNull(ssl = SSL_new(ctx));
  36822. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36823. SSL_free(ssl);
  36824. ssl = NULL;
  36825. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36826. ExpectNotNull(ssl = SSL_new(ctx));
  36827. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36828. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36829. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36830. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36831. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36832. SSL_free(ssl);
  36833. ssl = NULL;
  36834. wolfSSL_CTX_set_verify(ctx,
  36835. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36836. ExpectNotNull(ssl = SSL_new(ctx));
  36837. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36838. ExpectIntEQ(SSL_get_verify_mode(ssl),
  36839. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36840. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36841. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  36842. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36843. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36844. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36845. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36846. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36847. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36848. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36849. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  36850. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  36851. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  36852. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  36853. #endif
  36854. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  36855. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36856. SSL_free(ssl);
  36857. SSL_CTX_free(ctx);
  36858. #endif
  36859. return EXPECT_RESULT();
  36860. }
  36861. static int test_wolfSSL_verify_depth(void)
  36862. {
  36863. EXPECT_DECLS;
  36864. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36865. WOLFSSL* ssl = NULL;
  36866. WOLFSSL_CTX* ctx = NULL;
  36867. long depth;
  36868. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36869. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  36870. ExpectNotNull(ssl = SSL_new(ctx));
  36871. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  36872. SSL_free(ssl);
  36873. ssl = NULL;
  36874. SSL_CTX_set_verify_depth(ctx, -1);
  36875. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  36876. SSL_CTX_set_verify_depth(ctx, 2);
  36877. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  36878. ExpectNotNull(ssl = SSL_new(ctx));
  36879. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  36880. SSL_free(ssl);
  36881. SSL_CTX_free(ctx);
  36882. #endif
  36883. return EXPECT_RESULT();
  36884. }
  36885. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36886. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36887. * buffer of 64 bytes.
  36888. *
  36889. * returns the size of the digest buffer on success and a negative value on
  36890. * failure.
  36891. */
  36892. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  36893. int* sz)
  36894. {
  36895. EXPECT_DECLS;
  36896. HMAC_CTX ctx1;
  36897. HMAC_CTX ctx2;
  36898. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36899. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36900. unsigned char long_key[] =
  36901. "0123456789012345678901234567890123456789"
  36902. "0123456789012345678901234567890123456789"
  36903. "0123456789012345678901234567890123456789"
  36904. "0123456789012345678901234567890123456789";
  36905. unsigned char msg[] = "message to hash";
  36906. unsigned int digestSz = 64;
  36907. int keySz = sizeof(key);
  36908. int long_keySz = sizeof(long_key);
  36909. int msgSz = sizeof(msg);
  36910. unsigned char digest2[64];
  36911. unsigned int digestSz2 = 64;
  36912. HMAC_CTX_init(&ctx1);
  36913. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36914. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36915. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36916. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36917. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36918. HMAC_CTX_cleanup(&ctx1);
  36919. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36920. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36921. HMAC_CTX_cleanup(&ctx2);
  36922. ExpectIntEQ(digestSz, digestSz2);
  36923. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36924. /* test HMAC_Init with NULL key */
  36925. /* init after copy */
  36926. HMAC_CTX_init(&ctx1);
  36927. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36928. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36929. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36930. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36931. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36932. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36933. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36934. HMAC_CTX_cleanup(&ctx1);
  36935. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36936. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36937. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36938. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36939. HMAC_CTX_cleanup(&ctx2);
  36940. ExpectIntEQ(digestSz, digestSz2);
  36941. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36942. /* long key */
  36943. HMAC_CTX_init(&ctx1);
  36944. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  36945. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36946. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36947. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36948. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36949. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36950. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36951. HMAC_CTX_cleanup(&ctx1);
  36952. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36953. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36954. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36955. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36956. HMAC_CTX_cleanup(&ctx2);
  36957. ExpectIntEQ(digestSz, digestSz2);
  36958. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36959. /* init before copy */
  36960. HMAC_CTX_init(&ctx1);
  36961. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36962. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36963. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36964. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36965. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36966. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36967. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36968. HMAC_CTX_cleanup(&ctx1);
  36969. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36970. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36971. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36972. HMAC_CTX_cleanup(&ctx2);
  36973. ExpectIntEQ(digestSz, digestSz2);
  36974. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36975. *sz = digestSz;
  36976. return EXPECT_RESULT();
  36977. }
  36978. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36979. static int test_wolfSSL_HMAC_CTX(void)
  36980. {
  36981. EXPECT_DECLS;
  36982. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36983. unsigned char digest[64];
  36984. int digestSz;
  36985. #ifndef NO_SHA
  36986. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  36987. TEST_SUCCESS);
  36988. ExpectIntEQ(digestSz, 20);
  36989. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36990. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36991. #endif /* !NO_SHA */
  36992. #ifdef WOLFSSL_SHA224
  36993. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  36994. TEST_SUCCESS);
  36995. ExpectIntEQ(digestSz, 28);
  36996. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36997. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36998. "\x02\x0E", digest, digestSz), 0);
  36999. #endif /* WOLFSSL_SHA224 */
  37000. #ifndef NO_SHA256
  37001. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  37002. TEST_SUCCESS);
  37003. ExpectIntEQ(digestSz, 32);
  37004. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  37005. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  37006. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  37007. #endif /* !NO_SHA256 */
  37008. #ifdef WOLFSSL_SHA384
  37009. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  37010. TEST_SUCCESS);
  37011. ExpectIntEQ(digestSz, 48);
  37012. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  37013. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  37014. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  37015. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  37016. digest, digestSz), 0);
  37017. #endif /* WOLFSSL_SHA384 */
  37018. #ifdef WOLFSSL_SHA512
  37019. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  37020. TEST_SUCCESS);
  37021. ExpectIntEQ(digestSz, 64);
  37022. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  37023. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  37024. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  37025. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  37026. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  37027. digest, digestSz), 0);
  37028. #endif /* WOLFSSL_SHA512 */
  37029. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  37030. HAVE_FIPS_VERSION <= 2)
  37031. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  37032. TEST_SUCCESS);
  37033. ExpectIntEQ(digestSz, 16);
  37034. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  37035. "\xE4\x98\xDD", digest, digestSz), 0);
  37036. #endif /* !NO_MD5 */
  37037. #endif
  37038. return EXPECT_RESULT();
  37039. }
  37040. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37041. static void sslMsgCb(int w, int version, int type, const void* buf,
  37042. size_t sz, SSL* ssl, void* arg)
  37043. {
  37044. int i;
  37045. unsigned char* pt = (unsigned char*)buf;
  37046. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  37047. (w)?"Writing":"Reading", (int)sz, version, type);
  37048. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  37049. fprintf(stderr, "\n");
  37050. (void)ssl;
  37051. (void)arg;
  37052. }
  37053. #endif /* OPENSSL_EXTRA */
  37054. static int test_wolfSSL_msg_callback(void)
  37055. {
  37056. EXPECT_DECLS;
  37057. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  37058. WOLFSSL* ssl = NULL;
  37059. WOLFSSL_CTX* ctx = NULL;
  37060. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  37061. ExpectNotNull(ssl = SSL_new(ctx));
  37062. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  37063. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  37064. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  37065. SSL_free(ssl);
  37066. SSL_CTX_free(ctx);
  37067. #endif
  37068. return EXPECT_RESULT();
  37069. }
  37070. static int test_wolfSSL_SHA(void)
  37071. {
  37072. EXPECT_DECLS;
  37073. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37074. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37075. (!defined(HAVE_FIPS) || \
  37076. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37077. {
  37078. const unsigned char in[] = "abc";
  37079. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37080. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37081. unsigned char out[WC_SHA_DIGEST_SIZE];
  37082. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37083. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37084. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37085. /* SHA interface test */
  37086. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37087. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37088. ExpectNotNull(SHA(in, 0, out));
  37089. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37090. ExpectNotNull(SHA(NULL, 0, out));
  37091. ExpectNotNull(SHA(NULL, 0, NULL));
  37092. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37093. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37094. }
  37095. #endif
  37096. #if !defined(NO_SHA256)
  37097. {
  37098. const unsigned char in[] = "abc";
  37099. unsigned char expected[] =
  37100. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37101. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37102. "\x15\xAD";
  37103. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37104. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37105. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37106. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37107. #else
  37108. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37109. #endif
  37110. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37111. }
  37112. #endif
  37113. #if defined(WOLFSSL_SHA384)
  37114. {
  37115. const unsigned char in[] = "abc";
  37116. unsigned char expected[] =
  37117. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37118. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37119. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37120. "\xc8\x25\xa7";
  37121. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37122. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37123. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37124. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37125. #else
  37126. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37127. #endif
  37128. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37129. }
  37130. #endif
  37131. #if defined(WOLFSSL_SHA512)
  37132. {
  37133. const unsigned char in[] = "abc";
  37134. unsigned char expected[] =
  37135. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37136. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37137. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37138. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37139. "\xa5\x4c\xa4\x9f";
  37140. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37141. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37142. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37143. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37144. #else
  37145. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37146. #endif
  37147. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37148. }
  37149. #endif
  37150. #endif
  37151. return EXPECT_RESULT();
  37152. }
  37153. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  37154. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37155. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37156. static void binary_dump(void *ptr, int size)
  37157. {
  37158. #ifdef WOLFSSL_EVP_PRINT
  37159. int i = 0;
  37160. unsigned char *p = (unsigned char *) ptr;
  37161. fprintf(stderr, "{");
  37162. while ((p != NULL) && (i < size)) {
  37163. if ((i % 8) == 0) {
  37164. fprintf(stderr, "\n");
  37165. fprintf(stderr, " ");
  37166. }
  37167. fprintf(stderr, "0x%02x, ", p[i]);
  37168. i++;
  37169. }
  37170. fprintf(stderr, "\n};\n");
  37171. #else
  37172. (void) ptr;
  37173. (void) size;
  37174. #endif
  37175. }
  37176. static int last_val = 0x0f;
  37177. static int check_result(unsigned char *data, int len)
  37178. {
  37179. int i;
  37180. for ( ; len; ) {
  37181. last_val = (last_val + 1) % 16;
  37182. for (i = 0; i < 16; len--, i++, data++)
  37183. if (*data != last_val) {
  37184. return -1;
  37185. }
  37186. }
  37187. return 0;
  37188. }
  37189. static int r_offset;
  37190. static int w_offset;
  37191. static void init_offset(void)
  37192. {
  37193. r_offset = 0;
  37194. w_offset = 0;
  37195. }
  37196. static void get_record(unsigned char *data, unsigned char *buf, int len)
  37197. {
  37198. XMEMCPY(buf, data+r_offset, len);
  37199. r_offset += len;
  37200. }
  37201. static void set_record(unsigned char *data, unsigned char *buf, int len)
  37202. {
  37203. XMEMCPY(data+w_offset, buf, len);
  37204. w_offset += len;
  37205. }
  37206. static void set_plain(unsigned char *plain, int rec)
  37207. {
  37208. int i, j;
  37209. unsigned char *p = plain;
  37210. #define BLOCKSZ 16
  37211. for (i=0; i<(rec/BLOCKSZ); i++) {
  37212. for (j=0; j<BLOCKSZ; j++)
  37213. *p++ = (i % 16);
  37214. }
  37215. }
  37216. #endif
  37217. static int test_wolfSSL_EVP_Cipher_extra(void)
  37218. {
  37219. EXPECT_DECLS;
  37220. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  37221. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  37222. /* aes128-cbc, keylen=16, ivlen=16 */
  37223. byte aes128_cbc_key[] = {
  37224. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37225. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  37226. };
  37227. byte aes128_cbc_iv[] = {
  37228. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  37229. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  37230. };
  37231. /* teset data size table */
  37232. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  37233. int test_drive2[] = {8, 3, 8, 512, 0};
  37234. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  37235. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  37236. int test_drive_len[100];
  37237. int ret = 0;
  37238. EVP_CIPHER_CTX *evp = NULL;
  37239. int ilen = 0;
  37240. int klen = 0;
  37241. int i, j;
  37242. const EVP_CIPHER *type;
  37243. byte *iv;
  37244. byte *key;
  37245. int ivlen;
  37246. int keylen;
  37247. #define RECORDS 16
  37248. #define BUFFSZ 512
  37249. byte plain [BUFFSZ * RECORDS];
  37250. byte cipher[BUFFSZ * RECORDS];
  37251. byte inb[BUFFSZ];
  37252. byte outb[BUFFSZ+16];
  37253. int outl = 0;
  37254. int inl;
  37255. iv = aes128_cbc_iv;
  37256. ivlen = sizeof(aes128_cbc_iv);
  37257. key = aes128_cbc_key;
  37258. keylen = sizeof(aes128_cbc_key);
  37259. type = EVP_aes_128_cbc();
  37260. set_plain(plain, BUFFSZ * RECORDS);
  37261. SSL_library_init();
  37262. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  37263. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37264. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37265. klen = EVP_CIPHER_CTX_key_length(evp);
  37266. if (klen > 0 && keylen != klen) {
  37267. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37268. }
  37269. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37270. if (ilen > 0 && ivlen != ilen) {
  37271. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37272. }
  37273. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37274. for (j = 0; j<RECORDS; j++)
  37275. {
  37276. inl = BUFFSZ;
  37277. get_record(plain, inb, inl);
  37278. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  37279. set_record(cipher, outb, outl);
  37280. }
  37281. for (i = 0; test_drive[i]; i++) {
  37282. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37283. init_offset();
  37284. test_drive_len[i] = 0;
  37285. for (j = 0; test_drive[i][j]; j++)
  37286. {
  37287. inl = test_drive[i][j];
  37288. test_drive_len[i] += inl;
  37289. get_record(plain, inb, inl);
  37290. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  37291. 0);
  37292. /* output to cipher buffer, so that following Dec test can detect
  37293. if any error */
  37294. set_record(cipher, outb, outl);
  37295. }
  37296. EVP_CipherFinal(evp, outb, &outl);
  37297. if (outl > 0)
  37298. set_record(cipher, outb, outl);
  37299. }
  37300. for (i = 0; test_drive[i]; i++) {
  37301. last_val = 0x0f;
  37302. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  37303. init_offset();
  37304. for (j = 0; test_drive[i][j]; j++) {
  37305. inl = test_drive[i][j];
  37306. get_record(cipher, inb, inl);
  37307. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  37308. 0);
  37309. binary_dump(outb, outl);
  37310. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  37311. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  37312. }
  37313. ret = EVP_CipherFinal(evp, outb, &outl);
  37314. binary_dump(outb, outl);
  37315. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  37316. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  37317. ExpectTrue(ret);
  37318. }
  37319. EVP_CIPHER_CTX_free(evp);
  37320. evp = NULL;
  37321. /* Do an extra test to verify correct behavior with empty input. */
  37322. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  37323. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  37324. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  37325. klen = EVP_CIPHER_CTX_key_length(evp);
  37326. if (klen > 0 && keylen != klen) {
  37327. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  37328. }
  37329. ilen = EVP_CIPHER_CTX_iv_length(evp);
  37330. if (ilen > 0 && ivlen != ilen) {
  37331. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  37332. }
  37333. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  37334. /* outl should be set to 0 after passing NULL, 0 for input args. */
  37335. outl = -1;
  37336. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  37337. ExpectIntEQ(outl, 0);
  37338. EVP_CIPHER_CTX_free(evp);
  37339. #endif /* test_EVP_Cipher */
  37340. return EXPECT_RESULT();
  37341. }
  37342. static int test_wolfSSL_PEM_read_DHparams(void)
  37343. {
  37344. EXPECT_DECLS;
  37345. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  37346. !defined(NO_FILESYSTEM)
  37347. DH* dh = NULL;
  37348. XFILE fp = XBADFILE;
  37349. unsigned char derOut[300];
  37350. unsigned char* derOutBuf = derOut;
  37351. int derOutSz = 0;
  37352. unsigned char derExpected[300];
  37353. int derExpectedSz = 0;
  37354. XMEMSET(derOut, 0, sizeof(derOut));
  37355. XMEMSET(derExpected, 0, sizeof(derExpected));
  37356. /* open DH param file, read into DH struct */
  37357. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  37358. /* bad args */
  37359. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  37360. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  37361. /* good args */
  37362. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37363. if (fp != XBADFILE) {
  37364. XFCLOSE(fp);
  37365. fp = XBADFILE;
  37366. }
  37367. /* read in certs/dh2048.der for comparison against exported params */
  37368. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  37369. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  37370. fp), 0);
  37371. if (fp != XBADFILE) {
  37372. XFCLOSE(fp);
  37373. fp = XBADFILE;
  37374. }
  37375. /* export DH back to DER and compare */
  37376. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  37377. ExpectIntEQ(derOutSz, derExpectedSz);
  37378. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  37379. DH_free(dh);
  37380. dh = NULL;
  37381. /* Test parsing with X9.42 header */
  37382. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  37383. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  37384. if (fp != XBADFILE)
  37385. XFCLOSE(fp);
  37386. DH_free(dh);
  37387. #endif
  37388. return EXPECT_RESULT();
  37389. }
  37390. static int test_wolfSSL_AES_ecb_encrypt(void)
  37391. {
  37392. EXPECT_DECLS;
  37393. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  37394. AES_KEY aes;
  37395. const byte msg[] =
  37396. {
  37397. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37398. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  37399. };
  37400. const byte verify[] =
  37401. {
  37402. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37403. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37404. };
  37405. const byte key[] =
  37406. {
  37407. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37408. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37409. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37410. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37411. };
  37412. byte out[AES_BLOCK_SIZE];
  37413. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37414. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37415. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37416. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37417. #ifdef HAVE_AES_DECRYPT
  37418. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37419. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37420. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37421. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37422. #endif
  37423. /* test bad arguments */
  37424. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37425. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37426. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37427. #endif
  37428. return EXPECT_RESULT();
  37429. }
  37430. static int test_wolfSSL_MD5(void)
  37431. {
  37432. EXPECT_DECLS;
  37433. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37434. byte input1[] = "";
  37435. byte input2[] = "message digest";
  37436. byte hash[WC_MD5_DIGEST_SIZE];
  37437. unsigned char output1[] =
  37438. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37439. unsigned char output2[] =
  37440. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37441. WOLFSSL_MD5_CTX md5;
  37442. XMEMSET(&md5, 0, sizeof(md5));
  37443. /* Test cases for illegal parameters */
  37444. ExpectIntEQ(MD5_Init(NULL), 0);
  37445. ExpectIntEQ(MD5_Init(&md5), 1);
  37446. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37447. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37448. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37449. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37450. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37451. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37452. /* Init MD5 CTX */
  37453. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37454. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37455. 1);
  37456. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37457. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37458. /* Init MD5 CTX */
  37459. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37460. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37461. (int)XSTRLEN((const char*)input2)), 1);
  37462. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37463. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37464. #if !defined(NO_OLD_NAMES) && \
  37465. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37466. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37467. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37468. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37469. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37470. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37471. &hash);
  37472. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37473. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37474. &hash);
  37475. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37476. {
  37477. byte data[] = "Data to be hashed.";
  37478. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37479. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37480. ExpectNotNull(MD5(data, sizeof(data), hash));
  37481. ExpectNotNull(MD5(NULL, 0, hash));
  37482. ExpectNull(MD5(NULL, sizeof(data), hash));
  37483. }
  37484. #endif
  37485. #endif
  37486. return EXPECT_RESULT();
  37487. }
  37488. static int test_wolfSSL_MD5_Transform(void)
  37489. {
  37490. EXPECT_DECLS;
  37491. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37492. byte input1[] = "";
  37493. byte input2[] = "abc";
  37494. byte local[WC_MD5_BLOCK_SIZE];
  37495. word32 sLen = 0;
  37496. #ifdef BIG_ENDIAN_ORDER
  37497. unsigned char output1[] =
  37498. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37499. unsigned char output2[] =
  37500. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37501. #else
  37502. unsigned char output1[] =
  37503. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37504. unsigned char output2[] =
  37505. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37506. #endif
  37507. union {
  37508. wc_Md5 native;
  37509. MD5_CTX compat;
  37510. } md5;
  37511. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37512. XMEMSET(&local, 0, sizeof(local));
  37513. /* sanity check */
  37514. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37515. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37516. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37517. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37518. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37519. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37520. /* Init MD5 CTX */
  37521. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37522. /* Do Transform*/
  37523. sLen = (word32)XSTRLEN((char*)input1);
  37524. XMEMCPY(local, input1, sLen);
  37525. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37526. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37527. /* Init MD5 CTX */
  37528. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37529. sLen = (word32)XSTRLEN((char*)input2);
  37530. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37531. XMEMCPY(local, input2, sLen);
  37532. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37533. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37534. #endif
  37535. return EXPECT_RESULT();
  37536. }
  37537. static int test_wolfSSL_SHA224(void)
  37538. {
  37539. EXPECT_DECLS;
  37540. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37541. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37542. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37543. unsigned char input[] =
  37544. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37545. unsigned char output[] =
  37546. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37547. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37548. size_t inLen;
  37549. byte hash[WC_SHA224_DIGEST_SIZE];
  37550. inLen = XSTRLEN((char*)input);
  37551. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37552. ExpectNull(SHA224(NULL, inLen, hash));
  37553. ExpectNotNull(SHA224(input, 0, hash));
  37554. ExpectNotNull(SHA224(input, inLen, NULL));
  37555. ExpectNotNull(SHA224(NULL, 0, hash));
  37556. ExpectNotNull(SHA224(NULL, 0, NULL));
  37557. ExpectNotNull(SHA224(input, inLen, hash));
  37558. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37559. #endif
  37560. return EXPECT_RESULT();
  37561. }
  37562. static int test_wolfSSL_SHA_Transform(void)
  37563. {
  37564. EXPECT_DECLS;
  37565. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37566. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37567. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37568. byte input1[] = "";
  37569. byte input2[] = "abc";
  37570. byte local[WC_SHA_BLOCK_SIZE];
  37571. word32 sLen = 0;
  37572. #ifdef BIG_ENDIAN_ORDER
  37573. unsigned char output1[] =
  37574. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37575. "\x09\xf7\x3a\x93";
  37576. unsigned char output2[] =
  37577. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37578. "\x7c\x6e\x08\xb8";
  37579. #else
  37580. unsigned char output1[] =
  37581. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37582. "\x93\x3a\xf7\x09";
  37583. unsigned char output2[] =
  37584. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37585. "\xb8\x08\x6e\x7c";
  37586. #endif
  37587. union {
  37588. wc_Sha native;
  37589. SHA_CTX compat;
  37590. } sha;
  37591. union {
  37592. wc_Sha native;
  37593. SHA_CTX compat;
  37594. } sha1;
  37595. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37596. XMEMSET(&local, 0, sizeof(local));
  37597. /* sanity check */
  37598. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37599. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37600. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37601. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37602. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37603. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37604. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37605. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37606. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37607. /* Init SHA CTX */
  37608. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37609. /* Do Transform*/
  37610. sLen = (word32)XSTRLEN((char*)input1);
  37611. XMEMCPY(local, input1, sLen);
  37612. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37613. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37614. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37615. /* Init SHA CTX */
  37616. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37617. sLen = (word32)XSTRLEN((char*)input2);
  37618. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37619. XMEMCPY(local, input2, sLen);
  37620. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37621. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37622. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37623. /* SHA1 */
  37624. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37625. /* Init SHA CTX */
  37626. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37627. /* Do Transform*/
  37628. sLen = (word32)XSTRLEN((char*)input1);
  37629. XMEMCPY(local, input1, sLen);
  37630. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37631. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37632. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37633. /* Init SHA CTX */
  37634. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37635. sLen = (word32)XSTRLEN((char*)input2);
  37636. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37637. XMEMCPY(local, input2, sLen);
  37638. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37639. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37640. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37641. #endif
  37642. #endif
  37643. return EXPECT_RESULT();
  37644. }
  37645. static int test_wolfSSL_SHA256_Transform(void)
  37646. {
  37647. EXPECT_DECLS;
  37648. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37649. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37650. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37651. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37652. !defined(WOLFSSL_KCAPI_HASH)
  37653. byte input1[] = "";
  37654. byte input2[] = "abc";
  37655. byte local[WC_SHA256_BLOCK_SIZE];
  37656. word32 sLen = 0;
  37657. #ifdef BIG_ENDIAN_ORDER
  37658. unsigned char output1[] =
  37659. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37660. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37661. unsigned char output2[] =
  37662. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37663. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37664. #else
  37665. unsigned char output1[] =
  37666. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37667. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37668. unsigned char output2[] =
  37669. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37670. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37671. #endif
  37672. union {
  37673. wc_Sha256 native;
  37674. SHA256_CTX compat;
  37675. } sha256;
  37676. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37677. XMEMSET(&local, 0, sizeof(local));
  37678. /* sanity check */
  37679. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37680. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37681. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37682. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37683. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37684. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37685. /* Init SHA256 CTX */
  37686. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37687. /* Do Transform*/
  37688. sLen = (word32)XSTRLEN((char*)input1);
  37689. XMEMCPY(local, input1, sLen);
  37690. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37691. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37692. 0);
  37693. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37694. /* Init SHA256 CTX */
  37695. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37696. sLen = (word32)XSTRLEN((char*)input2);
  37697. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37698. XMEMCPY(local, input2, sLen);
  37699. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37700. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37701. 0);
  37702. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37703. #endif
  37704. #endif
  37705. return EXPECT_RESULT();
  37706. }
  37707. static int test_wolfSSL_SHA256(void)
  37708. {
  37709. EXPECT_DECLS;
  37710. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37711. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37712. unsigned char input[] =
  37713. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37714. unsigned char output[] =
  37715. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37716. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37717. "\x06\xC1";
  37718. size_t inLen;
  37719. byte hash[WC_SHA256_DIGEST_SIZE];
  37720. inLen = XSTRLEN((char*)input);
  37721. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37722. ExpectNotNull(SHA256(input, inLen, hash));
  37723. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37724. #endif
  37725. return EXPECT_RESULT();
  37726. }
  37727. static int test_wolfSSL_SHA512_Transform(void)
  37728. {
  37729. EXPECT_DECLS;
  37730. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37731. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37732. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37733. !defined(WOLFSSL_KCAPI_HASH)
  37734. byte input1[] = "";
  37735. byte input2[] = "abc";
  37736. byte local[WC_SHA512_BLOCK_SIZE];
  37737. word32 sLen = 0;
  37738. #ifdef BIG_ENDIAN_ORDER
  37739. unsigned char output1[] =
  37740. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37741. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37742. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37743. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37744. unsigned char output2[] =
  37745. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37746. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37747. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37748. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37749. #else
  37750. unsigned char output1[] =
  37751. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37752. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37753. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37754. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37755. unsigned char output2[] =
  37756. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37757. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37758. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37759. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37760. #endif
  37761. union {
  37762. wc_Sha512 native;
  37763. SHA512_CTX compat;
  37764. } sha512;
  37765. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37766. XMEMSET(&local, 0, sizeof(local));
  37767. /* sanity check */
  37768. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37769. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37770. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37771. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37772. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37773. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37774. /* Init SHA512 CTX */
  37775. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37776. /* Do Transform*/
  37777. sLen = (word32)XSTRLEN((char*)input1);
  37778. XMEMCPY(local, input1, sLen);
  37779. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37780. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37781. WC_SHA512_DIGEST_SIZE), 0);
  37782. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37783. /* Init SHA512 CTX */
  37784. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37785. sLen = (word32)XSTRLEN((char*)input2);
  37786. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37787. XMEMCPY(local, input2, sLen);
  37788. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37789. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37790. WC_SHA512_DIGEST_SIZE), 0);
  37791. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37792. (void)input1;
  37793. #endif
  37794. #endif
  37795. return EXPECT_RESULT();
  37796. }
  37797. static int test_wolfSSL_X509_get_serialNumber(void)
  37798. {
  37799. EXPECT_DECLS;
  37800. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37801. ASN1_INTEGER* a = NULL;
  37802. BIGNUM* bn = NULL;
  37803. X509* x509 = NULL;
  37804. char *serialHex = NULL;
  37805. byte serial[3];
  37806. int serialSz;
  37807. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37808. SSL_FILETYPE_PEM));
  37809. ExpectNotNull(a = X509_get_serialNumber(x509));
  37810. /* check on value of ASN1 Integer */
  37811. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37812. a = NULL;
  37813. /* test setting serial number and then retrieving it */
  37814. ExpectNotNull(a = ASN1_INTEGER_new());
  37815. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  37816. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37817. serialSz = sizeof(serial);
  37818. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37819. WOLFSSL_SUCCESS);
  37820. ExpectIntEQ(serialSz, 1);
  37821. ExpectIntEQ(serial[0], 3);
  37822. ASN1_INTEGER_free(a);
  37823. a = NULL;
  37824. /* test setting serial number with 0's in it */
  37825. serial[0] = 0x01;
  37826. serial[1] = 0x00;
  37827. serial[2] = 0x02;
  37828. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37829. if (a != NULL) {
  37830. a->data[0] = ASN_INTEGER;
  37831. a->data[1] = sizeof(serial);
  37832. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37833. a->length = sizeof(serial) + 2;
  37834. }
  37835. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37836. XMEMSET(serial, 0, sizeof(serial));
  37837. serialSz = sizeof(serial);
  37838. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37839. WOLFSSL_SUCCESS);
  37840. ExpectIntEQ(serialSz, 3);
  37841. ExpectIntEQ(serial[0], 0x01);
  37842. ExpectIntEQ(serial[1], 0x00);
  37843. ExpectIntEQ(serial[2], 0x02);
  37844. ASN1_INTEGER_free(a);
  37845. a = NULL;
  37846. X509_free(x509); /* free's a */
  37847. ExpectNotNull(serialHex = BN_bn2hex(bn));
  37848. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37849. ExpectStrEQ(serialHex, "01");
  37850. #else
  37851. ExpectStrEQ(serialHex, "1");
  37852. #endif
  37853. OPENSSL_free(serialHex);
  37854. ExpectIntEQ(BN_get_word(bn), 1);
  37855. BN_free(bn);
  37856. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  37857. ExpectNotNull(a = ASN1_INTEGER_new());
  37858. if (a != NULL) {
  37859. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  37860. DYNAMIC_TYPE_OPENSSL));
  37861. a->isDynamic = 1;
  37862. ASN1_INTEGER_free(a);
  37863. }
  37864. #endif
  37865. return EXPECT_RESULT();
  37866. }
  37867. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  37868. {
  37869. EXPECT_DECLS;
  37870. #if defined(OPENSSL_EXTRA)
  37871. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  37872. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  37873. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  37874. #endif
  37875. return EXPECT_RESULT();
  37876. }
  37877. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  37878. {
  37879. EXPECT_DECLS;
  37880. #if defined(OPENSSL_EXTRA)
  37881. #define MAX_HEXSTR_BUFSZ 9
  37882. #define NUM_CASES 5
  37883. struct Output {
  37884. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  37885. long ret;
  37886. };
  37887. int i;
  37888. int j;
  37889. const char* inputs[NUM_CASES] = {
  37890. "aabcd1357e",
  37891. "01:12:23:34:a5:b6:c7:d8:e9",
  37892. ":01:02",
  37893. "012",
  37894. ":ab:ac:d"
  37895. };
  37896. struct Output expectedOutputs[NUM_CASES] = {
  37897. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  37898. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  37899. {{0x01, 0x02}, 2},
  37900. {{0x00}, 0},
  37901. {{0x00}, 0}
  37902. };
  37903. long len = 0;
  37904. unsigned char* returnedBuf = NULL;
  37905. for (i = 0; i < NUM_CASES && EXPECT_SUCCESS(); ++i) {
  37906. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  37907. if (returnedBuf == NULL) {
  37908. ExpectIntEQ(expectedOutputs[i].ret, 0);
  37909. continue;
  37910. }
  37911. ExpectIntEQ(expectedOutputs[i].ret, len);
  37912. for (j = 0; j < len; ++j) {
  37913. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  37914. }
  37915. OPENSSL_free(returnedBuf);
  37916. returnedBuf = NULL;
  37917. }
  37918. #endif
  37919. return EXPECT_RESULT();
  37920. }
  37921. static int test_wolfSSL_X509_CA_num(void)
  37922. {
  37923. EXPECT_DECLS;
  37924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  37925. defined(HAVE_ECC) && !defined(NO_RSA)
  37926. WOLFSSL_X509_STORE *store = NULL;
  37927. WOLFSSL_X509 *x509_1 = NULL;
  37928. WOLFSSL_X509 *x509_2 = NULL;
  37929. int ca_num = 0;
  37930. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  37931. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37932. WOLFSSL_FILETYPE_PEM));
  37933. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  37934. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  37935. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  37936. WOLFSSL_FILETYPE_PEM));
  37937. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  37938. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  37939. wolfSSL_X509_free(x509_1);
  37940. wolfSSL_X509_free(x509_2);
  37941. wolfSSL_X509_STORE_free(store);
  37942. #endif
  37943. return EXPECT_RESULT();
  37944. }
  37945. static int test_wolfSSL_X509_check_ca(void)
  37946. {
  37947. EXPECT_DECLS;
  37948. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37949. WOLFSSL_X509 *x509 = NULL;
  37950. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37951. WOLFSSL_FILETYPE_PEM));
  37952. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  37953. wolfSSL_X509_free(x509);
  37954. #endif
  37955. return EXPECT_RESULT();
  37956. }
  37957. static int test_wolfSSL_X509_check_ip_asc(void)
  37958. {
  37959. EXPECT_DECLS;
  37960. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37961. WOLFSSL_X509 *x509 = NULL;
  37962. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  37963. WOLFSSL_FILETYPE_PEM));
  37964. #if 0
  37965. /* TODO: add cert gen for testing positive case */
  37966. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  37967. #endif
  37968. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  37969. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  37970. wolfSSL_X509_free(x509);
  37971. #endif
  37972. return EXPECT_RESULT();
  37973. }
  37974. static int test_wolfSSL_make_cert(void)
  37975. {
  37976. EXPECT_DECLS;
  37977. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  37978. defined(WOLFSSL_CERT_EXT)
  37979. int ret;
  37980. Cert cert;
  37981. CertName name;
  37982. RsaKey key;
  37983. WC_RNG rng;
  37984. byte der[FOURK_BUF];
  37985. word32 idx;
  37986. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  37987. #ifdef OPENSSL_EXTRA
  37988. const unsigned char* pt;
  37989. int certSz;
  37990. X509* x509 = NULL;
  37991. X509_NAME* x509name;
  37992. X509_NAME_ENTRY* entry;
  37993. ASN1_STRING* entryValue;
  37994. #endif
  37995. XMEMSET(&name, 0, sizeof(CertName));
  37996. /* set up cert name */
  37997. XMEMCPY(name.country, "US", sizeof("US"));
  37998. name.countryEnc = CTC_PRINTABLE;
  37999. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  38000. name.stateEnc = CTC_UTF8;
  38001. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  38002. name.localityEnc = CTC_UTF8;
  38003. XMEMCPY(name.sur, "Test", sizeof("Test"));
  38004. name.surEnc = CTC_UTF8;
  38005. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  38006. name.orgEnc = CTC_UTF8;
  38007. XMEMCPY(name.unit, "Development", sizeof("Development"));
  38008. name.unitEnc = CTC_UTF8;
  38009. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  38010. name.commonNameEnc = CTC_UTF8;
  38011. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  38012. name.serialDevEnc = CTC_PRINTABLE;
  38013. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  38014. name.userIdEnc = CTC_PRINTABLE;
  38015. #ifdef WOLFSSL_MULTI_ATTRIB
  38016. #if CTC_MAX_ATTRIB > 2
  38017. {
  38018. NameAttrib* n;
  38019. n = &name.name[0];
  38020. n->id = ASN_DOMAIN_COMPONENT;
  38021. n->type = CTC_UTF8;
  38022. n->sz = sizeof("com");
  38023. XMEMCPY(n->value, "com", sizeof("com"));
  38024. n = &name.name[1];
  38025. n->id = ASN_DOMAIN_COMPONENT;
  38026. n->type = CTC_UTF8;
  38027. n->sz = sizeof("wolfssl");
  38028. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  38029. }
  38030. #endif
  38031. #endif /* WOLFSSL_MULTI_ATTRIB */
  38032. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  38033. #ifndef HAVE_FIPS
  38034. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  38035. #else
  38036. ExpectIntEQ(wc_InitRng(&rng), 0);
  38037. #endif
  38038. /* load test RSA key */
  38039. idx = 0;
  38040. #if defined(USE_CERT_BUFFERS_1024)
  38041. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  38042. sizeof_server_key_der_1024), 0);
  38043. #elif defined(USE_CERT_BUFFERS_2048)
  38044. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  38045. sizeof_server_key_der_2048), 0);
  38046. #else
  38047. /* error case, no RSA key loaded, happens later */
  38048. (void)idx;
  38049. #endif
  38050. XMEMSET(&cert, 0 , sizeof(Cert));
  38051. ExpectIntEQ(wc_InitCert(&cert), 0);
  38052. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  38053. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  38054. cert.serialSz = (int)sizeof(mySerial);
  38055. cert.isCA = 1;
  38056. #ifndef NO_SHA256
  38057. cert.sigType = CTC_SHA256wRSA;
  38058. #else
  38059. cert.sigType = CTC_SHAwRSA;
  38060. #endif
  38061. /* add SKID from the Public Key */
  38062. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38063. /* add AKID from the Public Key */
  38064. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  38065. ret = 0;
  38066. do {
  38067. #if defined(WOLFSSL_ASYNC_CRYPT)
  38068. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  38069. #endif
  38070. if (ret >= 0) {
  38071. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  38072. }
  38073. } while (ret == WC_PENDING_E);
  38074. ExpectIntGT(ret, 0);
  38075. #ifdef OPENSSL_EXTRA
  38076. /* der holds a certificate with DC's now check X509 parsing of it */
  38077. certSz = ret;
  38078. pt = der;
  38079. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  38080. ExpectNotNull(x509name = X509_get_subject_name(x509));
  38081. #ifdef WOLFSSL_MULTI_ATTRIB
  38082. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38083. -1)), 5);
  38084. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38085. idx)), 6);
  38086. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38087. idx)), -1);
  38088. #endif /* WOLFSSL_MULTI_ATTRIB */
  38089. /* compare DN at index 0 */
  38090. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  38091. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38092. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  38093. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  38094. #ifndef WOLFSSL_MULTI_ATTRIB
  38095. /* compare Serial Number */
  38096. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  38097. -1)), 7);
  38098. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38099. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38100. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  38101. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  38102. #endif
  38103. #ifdef WOLFSSL_MULTI_ATTRIB
  38104. /* get first and second DC and compare result */
  38105. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38106. -1)), 5);
  38107. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38108. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38109. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  38110. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  38111. idx)), 6);
  38112. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  38113. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  38114. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  38115. #endif /* WOLFSSL_MULTI_ATTRIB */
  38116. /* try invalid index locations for regression test and sanity check */
  38117. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  38118. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  38119. X509_free(x509);
  38120. #endif /* OPENSSL_EXTRA */
  38121. wc_FreeRsaKey(&key);
  38122. wc_FreeRng(&rng);
  38123. #endif
  38124. return EXPECT_RESULT();
  38125. }
  38126. static int test_wolfSSL_X509_get_version(void)
  38127. {
  38128. EXPECT_DECLS;
  38129. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38130. WOLFSSL_X509 *x509 = NULL;
  38131. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38132. WOLFSSL_FILETYPE_PEM));
  38133. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  38134. wolfSSL_X509_free(x509);
  38135. #endif
  38136. return EXPECT_RESULT();
  38137. }
  38138. static int test_wolfSSL_DES_ncbc(void)
  38139. {
  38140. EXPECT_DECLS;
  38141. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38142. const_DES_cblock myDes;
  38143. DES_cblock iv = {1};
  38144. DES_key_schedule key = {0};
  38145. unsigned char msg[] = "hello wolfssl";
  38146. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38147. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38148. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38149. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38150. /* partial block test */
  38151. DES_set_key(&key, &myDes);
  38152. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38153. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38154. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38155. DES_set_key(&key, &myDes);
  38156. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38157. *((byte*)&iv) = 1;
  38158. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38159. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38160. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38161. /* full block test */
  38162. DES_set_key(&key, &myDes);
  38163. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38164. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38165. *((byte*)&iv) = 1;
  38166. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38167. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38168. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38169. DES_set_key(&key, &myDes);
  38170. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38171. *((byte*)&iv) = 1;
  38172. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38173. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38174. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38175. #endif
  38176. return EXPECT_RESULT();
  38177. }
  38178. static int test_wolfSSL_AES_cbc_encrypt(void)
  38179. {
  38180. EXPECT_DECLS;
  38181. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  38182. AES_KEY aes;
  38183. AES_KEY* aesN = NULL;
  38184. size_t len = 0;
  38185. size_t lenB = 0;
  38186. int keySz0 = 0;
  38187. int keySzN = -1;
  38188. byte out[AES_BLOCK_SIZE] = {0};
  38189. byte* outN = NULL;
  38190. /* Test vectors retrieved from:
  38191. * <begin URL>
  38192. * https://csrc.nist.gov/
  38193. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38194. * documents/aes/KAT_AES.zip
  38195. * </end URL>
  38196. */
  38197. const byte* pt128N = NULL;
  38198. byte* key128N = NULL;
  38199. byte* iv128N = NULL;
  38200. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38201. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38202. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38203. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38204. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38205. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38206. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38207. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38208. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38209. len = sizeof(pt128);
  38210. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38211. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38212. ExpectIntNE(XMEMCMP(b, g, h), i)
  38213. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38214. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38215. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38216. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38217. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38218. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38219. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38220. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38221. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38222. /* Stressing wolfSSL_AES_set_encrypt_key */
  38223. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38224. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38225. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38226. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38227. /* Stressing wolfSSL_AES_set_decrypt_key */
  38228. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38229. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38230. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38231. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38232. #ifdef WOLFSSL_AES_128
  38233. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38234. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38235. RESET_IV(iv128tmp, iv128);
  38236. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38237. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38238. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38239. wc_AesFree((Aes*)&aes);
  38240. #ifdef HAVE_AES_DECRYPT
  38241. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38242. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38243. RESET_IV(iv128tmp, iv128);
  38244. len = sizeof(ct128);
  38245. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38246. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38247. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38248. wc_AesFree((Aes*)&aes);
  38249. #endif
  38250. #endif /* WOLFSSL_AES_128 */
  38251. #ifdef WOLFSSL_AES_192
  38252. {
  38253. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38254. * Appendix F.2.3 */
  38255. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38256. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38257. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38258. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38259. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38260. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38261. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38262. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38263. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38264. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38265. len = sizeof(pt192);
  38266. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38267. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38268. RESET_IV(iv192tmp, iv192);
  38269. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38270. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38271. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38272. wc_AesFree((Aes*)&aes);
  38273. #ifdef HAVE_AES_DECRYPT
  38274. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38275. len = sizeof(ct192);
  38276. RESET_IV(iv192tmp, iv192);
  38277. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38278. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38279. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38280. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38281. wc_AesFree((Aes*)&aes);
  38282. #endif
  38283. }
  38284. #endif /* WOLFSSL_AES_192 */
  38285. #ifdef WOLFSSL_AES_256
  38286. {
  38287. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38288. * Appendix F.2.5 */
  38289. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38290. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38291. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38292. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38293. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38294. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38295. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38296. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38297. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38298. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38299. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38300. len = sizeof(pt256);
  38301. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38302. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38303. RESET_IV(iv256tmp, iv256);
  38304. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38305. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38306. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38307. wc_AesFree((Aes*)&aes);
  38308. #ifdef HAVE_AES_DECRYPT
  38309. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38310. len = sizeof(ct256);
  38311. RESET_IV(iv256tmp, iv256);
  38312. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38313. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38314. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38315. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38316. wc_AesFree((Aes*)&aes);
  38317. #endif
  38318. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38319. {
  38320. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38321. byte wrapPlain[sizeof(key256)] = { 0 };
  38322. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38323. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  38324. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38325. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38326. 15), WOLFSSL_FAILURE);
  38327. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38328. sizeof(key256)), sizeof(wrapCipher));
  38329. wc_AesFree((Aes*)&aes);
  38330. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  38331. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38332. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38333. 23), WOLFSSL_FAILURE);
  38334. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38335. sizeof(wrapCipher)), sizeof(wrapPlain));
  38336. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38337. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38338. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38339. wc_AesFree((Aes*)&aes);
  38340. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  38341. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38342. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38343. sizeof(key256)), sizeof(wrapCipher));
  38344. wc_AesFree((Aes*)&aes);
  38345. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  38346. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38347. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38348. sizeof(wrapCipher)), sizeof(wrapPlain));
  38349. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38350. wc_AesFree((Aes*)&aes);
  38351. }
  38352. #endif /* HAVE_AES_KEYWRAP */
  38353. }
  38354. #endif /* WOLFSSL_AES_256 */
  38355. #endif
  38356. return EXPECT_RESULT();
  38357. }
  38358. static int test_wolfSSL_CRYPTO_cts128(void)
  38359. {
  38360. EXPECT_DECLS;
  38361. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38362. defined(HAVE_CTS)
  38363. byte tmp[64]; /* Largest vector size */
  38364. /* Test vectors taken form RFC3962 Appendix B */
  38365. const testVector vects[] = {
  38366. {
  38367. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38368. "\x20",
  38369. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38370. "\x97",
  38371. 17, 17
  38372. },
  38373. {
  38374. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38375. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38376. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38377. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38378. 31, 31
  38379. },
  38380. {
  38381. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38382. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38383. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38384. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38385. 32, 32
  38386. },
  38387. {
  38388. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38389. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38390. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38391. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38392. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38393. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38394. 47, 47
  38395. },
  38396. {
  38397. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38398. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38399. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38400. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38401. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38402. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38403. 48, 48
  38404. },
  38405. {
  38406. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38407. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38408. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38409. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38410. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38411. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38412. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38413. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38414. 64, 64
  38415. }
  38416. };
  38417. byte keyBytes[AES_128_KEY_SIZE] = {
  38418. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38419. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38420. };
  38421. size_t i;
  38422. XMEMSET(tmp, 0, sizeof(tmp));
  38423. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38424. AES_KEY encKey;
  38425. AES_KEY decKey;
  38426. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38427. XMEMSET(iv, 0, sizeof(iv));
  38428. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38429. &encKey), 0);
  38430. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  38431. &decKey), 0);
  38432. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38433. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38434. vects[i].outLen);
  38435. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38436. XMEMSET(iv, 0, sizeof(iv));
  38437. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38438. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38439. vects[i].inLen);
  38440. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38441. }
  38442. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38443. return EXPECT_RESULT();
  38444. }
  38445. #if defined(OPENSSL_ALL)
  38446. static int test_wolfSSL_sk_CIPHER_description(void)
  38447. {
  38448. EXPECT_DECLS;
  38449. #if !defined(NO_RSA)
  38450. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38451. int i;
  38452. int numCiphers = 0;
  38453. const SSL_METHOD *method = NULL;
  38454. const SSL_CIPHER *cipher = NULL;
  38455. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38456. SSL_CTX *ctx = NULL;
  38457. SSL *ssl = NULL;
  38458. char buf[256];
  38459. char test_str[9] = "0000000";
  38460. const char badStr[] = "unknown";
  38461. const char certPath[] = "./certs/client-cert.pem";
  38462. XMEMSET(buf, 0, sizeof(buf));
  38463. ExpectNotNull(method = TLSv1_2_client_method());
  38464. ExpectNotNull(ctx = SSL_CTX_new(method));
  38465. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38466. SSL_CTX_set_verify_depth(ctx, 4);
  38467. SSL_CTX_set_options(ctx, flags);
  38468. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38469. WOLFSSL_SUCCESS);
  38470. ExpectNotNull(ssl = SSL_new(ctx));
  38471. /* SSL_get_ciphers returns a stack of all configured ciphers
  38472. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38473. */
  38474. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38475. /* loop through the amount of supportedCiphers */
  38476. numCiphers = sk_num(supportedCiphers);
  38477. for (i = 0; i < numCiphers; ++i) {
  38478. int j;
  38479. /* sk_value increments "sk->data.cipher->cipherOffset".
  38480. * wolfSSL_sk_CIPHER_description sets the description for
  38481. * the cipher based on the provided offset.
  38482. */
  38483. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38484. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38485. }
  38486. /* Search cipher description string for "unknown" descriptor */
  38487. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38488. int k = 0;
  38489. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38490. test_str[k] = badStr[k];
  38491. j++;
  38492. k++;
  38493. }
  38494. }
  38495. /* Fail if test_str == badStr == "unknown" */
  38496. ExpectStrNE(test_str,badStr);
  38497. }
  38498. SSL_free(ssl);
  38499. SSL_CTX_free(ctx);
  38500. #endif
  38501. return EXPECT_RESULT();
  38502. }
  38503. static int test_wolfSSL_get_ciphers_compat(void)
  38504. {
  38505. EXPECT_DECLS;
  38506. #if !defined(NO_RSA)
  38507. const SSL_METHOD *method = NULL;
  38508. const char certPath[] = "./certs/client-cert.pem";
  38509. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38510. SSL_CTX *ctx = NULL;
  38511. WOLFSSL *ssl = NULL;
  38512. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38513. ExpectNotNull(method = SSLv23_client_method());
  38514. ExpectNotNull(ctx = SSL_CTX_new(method));
  38515. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38516. SSL_CTX_set_verify_depth(ctx, 4);
  38517. SSL_CTX_set_options(ctx, flags);
  38518. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38519. WOLFSSL_SUCCESS);
  38520. ExpectNotNull(ssl = SSL_new(ctx));
  38521. /* Test Bad NULL input */
  38522. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  38523. /* Test for Good input */
  38524. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38525. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38526. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38527. SSL_free(ssl);
  38528. SSL_CTX_free(ctx);
  38529. #endif
  38530. return EXPECT_RESULT();
  38531. }
  38532. static int test_wolfSSL_X509_PUBKEY_get(void)
  38533. {
  38534. EXPECT_DECLS;
  38535. WOLFSSL_X509_PUBKEY pubkey;
  38536. WOLFSSL_X509_PUBKEY* key;
  38537. WOLFSSL_EVP_PKEY evpkey ;
  38538. WOLFSSL_EVP_PKEY* evpPkey;
  38539. WOLFSSL_EVP_PKEY* retEvpPkey;
  38540. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38541. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38542. key = &pubkey;
  38543. evpPkey = &evpkey;
  38544. evpPkey->type = WOLFSSL_SUCCESS;
  38545. key->pkey = evpPkey;
  38546. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38547. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38548. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38549. key->pkey = NULL;
  38550. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38551. return EXPECT_RESULT();
  38552. }
  38553. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38554. {
  38555. EXPECT_DECLS;
  38556. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38557. DSA *dsa = NULL;
  38558. DSA *setDsa = NULL;
  38559. EVP_PKEY *pkey = NULL;
  38560. EVP_PKEY *set1Pkey = NULL;
  38561. SHA_CTX sha;
  38562. byte signature[DSA_SIG_SIZE];
  38563. byte hash[WC_SHA_DIGEST_SIZE];
  38564. word32 bytes;
  38565. int answer;
  38566. #ifdef USE_CERT_BUFFERS_1024
  38567. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38568. int dsaKeySz = sizeof_dsa_key_der_1024;
  38569. byte tmp[ONEK_BUF];
  38570. XMEMSET(tmp, 0, sizeof(tmp));
  38571. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38572. bytes = dsaKeySz;
  38573. #elif defined(USE_CERT_BUFFERS_2048)
  38574. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38575. int dsaKeySz = sizeof_dsa_key_der_2048;
  38576. byte tmp[TWOK_BUF];
  38577. XMEMSET(tmp, 0, sizeof(tmp));
  38578. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38579. bytes = dsaKeySz;
  38580. #else
  38581. byte tmp[TWOK_BUF];
  38582. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38583. int dsaKeySz;
  38584. XFILE fp = XBADFILE;
  38585. XMEMSET(tmp, 0, sizeof(tmp));
  38586. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  38587. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  38588. if (fp != XBADFILE)
  38589. XFCLOSE(fp);
  38590. #endif /* END USE_CERT_BUFFERS_1024 */
  38591. /* Create hash to later Sign and Verify */
  38592. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38593. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38594. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38595. /* Initialize pkey with der format dsa key */
  38596. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  38597. (long)dsaKeySz));
  38598. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38599. /* Should Fail: NULL argument */
  38600. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38601. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38602. /* Should Pass: Initialized pkey argument */
  38603. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38604. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38605. #ifdef USE_CERT_BUFFERS_1024
  38606. ExpectIntEQ(DSA_bits(dsa), 1024);
  38607. #else
  38608. ExpectIntEQ(DSA_bits(dsa), 2048);
  38609. #endif
  38610. /* Sign */
  38611. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38612. /* Verify. */
  38613. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38614. WOLFSSL_SUCCESS);
  38615. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38616. /* Should Fail: set1Pkey not initialized */
  38617. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38618. /* Initialize set1Pkey */
  38619. set1Pkey = EVP_PKEY_new();
  38620. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38621. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38622. WOLFSSL_SUCCESS);
  38623. /* Should Pass: set dsa into set1Pkey */
  38624. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38625. DSA_free(dsa);
  38626. DSA_free(setDsa);
  38627. EVP_PKEY_free(pkey);
  38628. EVP_PKEY_free(set1Pkey);
  38629. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38630. return EXPECT_RESULT();
  38631. } /* END test_EVP_PKEY_set1_get1_DSA */
  38632. static int test_wolfSSL_DSA_generate_parameters(void)
  38633. {
  38634. EXPECT_DECLS;
  38635. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38636. !defined(HAVE_FIPS)
  38637. DSA *dsa = NULL;
  38638. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  38639. NULL));
  38640. DSA_free(dsa);
  38641. #endif
  38642. return EXPECT_RESULT();
  38643. }
  38644. static int test_wolfSSL_DSA_SIG(void)
  38645. {
  38646. EXPECT_DECLS;
  38647. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38648. !defined(HAVE_FIPS)
  38649. DSA *dsa = NULL;
  38650. DSA *dsa2 = NULL;
  38651. DSA_SIG *sig = NULL;
  38652. const BIGNUM *p = NULL;
  38653. const BIGNUM *q = NULL;
  38654. const BIGNUM *g = NULL;
  38655. const BIGNUM *pub = NULL;
  38656. const BIGNUM *priv = NULL;
  38657. BIGNUM *dup_p = NULL;
  38658. BIGNUM *dup_q = NULL;
  38659. BIGNUM *dup_g = NULL;
  38660. BIGNUM *dup_pub = NULL;
  38661. BIGNUM *dup_priv = NULL;
  38662. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38663. ExpectNotNull(dsa = DSA_new());
  38664. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  38665. NULL), 1);
  38666. ExpectIntEQ(DSA_generate_key(dsa), 1);
  38667. DSA_get0_pqg(dsa, &p, &q, &g);
  38668. DSA_get0_key(dsa, &pub, &priv);
  38669. ExpectNotNull(dup_p = BN_dup(p));
  38670. ExpectNotNull(dup_q = BN_dup(q));
  38671. ExpectNotNull(dup_g = BN_dup(g));
  38672. ExpectNotNull(dup_pub = BN_dup(pub));
  38673. ExpectNotNull(dup_priv = BN_dup(priv));
  38674. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38675. ExpectNotNull(dsa2 = DSA_new());
  38676. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  38677. if (EXPECT_FAIL()) {
  38678. BN_free(dup_p);
  38679. BN_free(dup_q);
  38680. BN_free(dup_g);
  38681. }
  38682. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  38683. if (EXPECT_FAIL()) {
  38684. BN_free(dup_pub);
  38685. BN_free(dup_priv);
  38686. }
  38687. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38688. DSA_free(dsa);
  38689. DSA_free(dsa2);
  38690. DSA_SIG_free(sig);
  38691. #endif
  38692. return EXPECT_RESULT();
  38693. }
  38694. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38695. {
  38696. EXPECT_DECLS;
  38697. #ifdef HAVE_ECC
  38698. WOLFSSL_EC_KEY* ecKey = NULL;
  38699. WOLFSSL_EC_KEY* ecGet1 = NULL;
  38700. EVP_PKEY* pkey = NULL;
  38701. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  38702. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38703. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38704. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38705. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38706. /* Should fail since ecKey is empty */
  38707. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38708. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38709. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38710. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38711. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38712. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38713. wolfSSL_EC_KEY_free(ecKey);
  38714. wolfSSL_EC_KEY_free(ecGet1);
  38715. EVP_PKEY_free(pkey);
  38716. #endif /* HAVE_ECC */
  38717. return EXPECT_RESULT();
  38718. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38719. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38720. {
  38721. EXPECT_DECLS;
  38722. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38723. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38724. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38725. DH *dh = NULL;
  38726. DH *setDh = NULL;
  38727. EVP_PKEY *pkey = NULL;
  38728. XFILE f = XBADFILE;
  38729. unsigned char buf[4096];
  38730. const unsigned char* pt = buf;
  38731. const char* dh2048 = "./certs/dh2048.der";
  38732. long len = 0;
  38733. int code = -1;
  38734. XMEMSET(buf, 0, sizeof(buf));
  38735. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  38736. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  38737. if (f != XBADFILE)
  38738. XFCLOSE(f);
  38739. /* Load dh2048.der into DH with internal format */
  38740. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38741. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38742. ExpectIntEQ(code, 0);
  38743. code = -1;
  38744. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38745. /* Set DH into PKEY */
  38746. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38747. /* Get DH from PKEY */
  38748. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38749. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38750. ExpectIntEQ(code, 0);
  38751. EVP_PKEY_free(pkey);
  38752. DH_free(setDh);
  38753. DH_free(dh);
  38754. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38755. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38756. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38757. return EXPECT_RESULT();
  38758. } /* END test_EVP_PKEY_set1_get1_DH */
  38759. static int test_wolfSSL_CTX_ctrl(void)
  38760. {
  38761. EXPECT_DECLS;
  38762. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38763. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38764. char caFile[] = "./certs/client-ca.pem";
  38765. char clientFile[] = "./certs/client-cert.pem";
  38766. SSL_CTX* ctx = NULL;
  38767. X509* x509 = NULL;
  38768. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38769. byte buf[6000];
  38770. char file[] = "./certs/dsaparams.pem";
  38771. XFILE f = XBADFILE;
  38772. int bytes;
  38773. BIO* bio = NULL;
  38774. DSA* dsa = NULL;
  38775. DH* dh = NULL;
  38776. #endif
  38777. #ifdef HAVE_ECC
  38778. WOLFSSL_EC_KEY* ecKey = NULL;
  38779. #endif
  38780. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38781. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  38782. WOLFSSL_FILETYPE_PEM));
  38783. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38784. if (EXPECT_FAIL()) {
  38785. wolfSSL_X509_free(x509);
  38786. }
  38787. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  38788. WOLFSSL_FILETYPE_PEM));
  38789. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38790. /* Initialize DH */
  38791. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  38792. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  38793. if (f != XBADFILE)
  38794. XFCLOSE(f);
  38795. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  38796. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  38797. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  38798. #endif
  38799. #ifdef HAVE_ECC
  38800. /* Initialize WOLFSSL_EC_KEY */
  38801. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  38802. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38803. #endif
  38804. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38805. /* additional test of getting EVP_PKEY key size from X509
  38806. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38807. * allowed with user RSA */
  38808. {
  38809. EVP_PKEY* pkey = NULL;
  38810. #if defined(HAVE_ECC)
  38811. X509* ecX509 = NULL;
  38812. #endif /* HAVE_ECC */
  38813. ExpectNotNull(pkey = X509_get_pubkey(x509));
  38814. /* current RSA key is 2048 bit (256 bytes) */
  38815. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  38816. EVP_PKEY_free(pkey);
  38817. pkey = NULL;
  38818. #if defined(HAVE_ECC)
  38819. #if defined(USE_CERT_BUFFERS_256)
  38820. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38821. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38822. SSL_FILETYPE_ASN1));
  38823. #else
  38824. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38825. cliEccCertFile, SSL_FILETYPE_PEM));
  38826. #endif
  38827. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  38828. /* current ECC key is 256 bit (32 bytes) */
  38829. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  38830. X509_free(ecX509);
  38831. EVP_PKEY_free(pkey);
  38832. #endif /* HAVE_ECC */
  38833. }
  38834. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38835. /* Tests should fail with passed in NULL pointer */
  38836. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  38837. SSL_FAILURE);
  38838. #if !defined(NO_DH) && !defined(NO_DSA)
  38839. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  38840. SSL_FAILURE);
  38841. #endif
  38842. #ifdef HAVE_ECC
  38843. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  38844. SSL_FAILURE);
  38845. #endif
  38846. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38847. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38848. */
  38849. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  38850. SSL_SUCCESS);
  38851. if (EXPECT_FAIL()) {
  38852. wolfSSL_X509_free(x509);
  38853. }
  38854. /* Test with SSL_CTRL_OPTIONS
  38855. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38856. */
  38857. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  38858. NULL) == SSL_OP_NO_TLSv1);
  38859. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38860. /* Test with SSL_CTRL_SET_TMP_DH
  38861. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38862. */
  38863. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38864. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  38865. SSL_SUCCESS);
  38866. #endif
  38867. /* Test with SSL_CTRL_SET_TMP_ECDH
  38868. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38869. */
  38870. #ifdef HAVE_ECC
  38871. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  38872. SSL_SUCCESS);
  38873. #endif
  38874. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38875. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  38876. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38877. #endif
  38878. /* Test for min/max proto */
  38879. #ifndef WOLFSSL_NO_TLS12
  38880. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38881. 0, NULL), SSL_SUCCESS);
  38882. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38883. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38884. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38885. #endif
  38886. #ifdef WOLFSSL_TLS13
  38887. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38888. 0, NULL), SSL_SUCCESS);
  38889. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38890. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38891. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38892. #ifndef WOLFSSL_NO_TLS12
  38893. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38894. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38895. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38896. #endif
  38897. #endif
  38898. /* Cleanup and Pass */
  38899. #if !defined(NO_DH) && !defined(NO_DSA)
  38900. #ifndef NO_BIO
  38901. BIO_free(bio);
  38902. DSA_free(dsa);
  38903. DH_free(dh);
  38904. #endif
  38905. #endif
  38906. #ifdef HAVE_ECC
  38907. wolfSSL_EC_KEY_free(ecKey);
  38908. #endif
  38909. SSL_CTX_free(ctx);
  38910. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38911. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38912. return EXPECT_RESULT();
  38913. }
  38914. static int test_wolfSSL_EVP_PKEY_assign(void)
  38915. {
  38916. EXPECT_DECLS;
  38917. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  38918. int type;
  38919. WOLFSSL_EVP_PKEY* pkey = NULL;
  38920. #ifndef NO_RSA
  38921. WOLFSSL_RSA* rsa = NULL;
  38922. #endif
  38923. #ifndef NO_DSA
  38924. WOLFSSL_DSA* dsa = NULL;
  38925. #endif
  38926. #ifdef HAVE_ECC
  38927. WOLFSSL_EC_KEY* ecKey = NULL;
  38928. #endif
  38929. #ifndef NO_RSA
  38930. type = EVP_PKEY_RSA;
  38931. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38932. ExpectNotNull(rsa = wolfSSL_RSA_new());
  38933. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  38934. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  38935. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  38936. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  38937. if (EXPECT_FAIL()) {
  38938. wolfSSL_RSA_free(rsa);
  38939. }
  38940. wolfSSL_EVP_PKEY_free(pkey);
  38941. pkey = NULL;
  38942. #endif /* NO_RSA */
  38943. #ifndef NO_DSA
  38944. type = EVP_PKEY_DSA;
  38945. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38946. ExpectNotNull(dsa = wolfSSL_DSA_new());
  38947. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  38948. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  38949. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  38950. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  38951. if (EXPECT_FAIL()) {
  38952. wolfSSL_DSA_free(dsa);
  38953. }
  38954. wolfSSL_EVP_PKEY_free(pkey);
  38955. pkey = NULL;
  38956. #endif /* NO_DSA */
  38957. #ifdef HAVE_ECC
  38958. type = EVP_PKEY_EC;
  38959. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38960. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  38961. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  38962. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  38963. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  38964. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  38965. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38966. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  38967. if (EXPECT_FAIL()) {
  38968. wolfSSL_EC_KEY_free(ecKey);
  38969. }
  38970. wolfSSL_EVP_PKEY_free(pkey);
  38971. pkey = NULL;
  38972. #endif /* HAVE_ECC */
  38973. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  38974. return EXPECT_RESULT();
  38975. }
  38976. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  38977. {
  38978. EXPECT_DECLS;
  38979. #if !defined(NO_DH) && \
  38980. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38981. XFILE f = XBADFILE;
  38982. unsigned char buf[4096];
  38983. const unsigned char* pt = buf;
  38984. const char* params1 = "./certs/dh2048.der";
  38985. long len = 0;
  38986. WOLFSSL_DH* dh = NULL;
  38987. WOLFSSL_EVP_PKEY* pkey = NULL;
  38988. XMEMSET(buf, 0, sizeof(buf));
  38989. /* Load DH parameters DER. */
  38990. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  38991. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  38992. if (f != XBADFILE)
  38993. XFCLOSE(f);
  38994. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38995. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  38996. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38997. /* Bad cases */
  38998. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  38999. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39000. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39001. /* Good case */
  39002. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39003. if (EXPECT_FAIL()) {
  39004. wolfSSL_DH_free(dh);
  39005. }
  39006. EVP_PKEY_free(pkey);
  39007. #endif
  39008. return EXPECT_RESULT();
  39009. }
  39010. static int test_wolfSSL_EVP_PKEY_base_id(void)
  39011. {
  39012. EXPECT_DECLS;
  39013. WOLFSSL_EVP_PKEY* pkey = NULL;
  39014. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39015. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  39016. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  39017. EVP_PKEY_free(pkey);
  39018. return EXPECT_RESULT();
  39019. }
  39020. static int test_wolfSSL_EVP_PKEY_id(void)
  39021. {
  39022. EXPECT_DECLS;
  39023. WOLFSSL_EVP_PKEY* pkey = NULL;
  39024. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39025. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  39026. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  39027. EVP_PKEY_free(pkey);
  39028. return EXPECT_RESULT();
  39029. }
  39030. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  39031. {
  39032. EXPECT_DECLS;
  39033. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  39034. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  39035. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  39036. ECC_MIN_KEY_SZ <= 256)
  39037. EVP_PKEY_CTX* ctx = NULL;
  39038. EVP_PKEY* pkey = NULL;
  39039. /* Test error conditions. */
  39040. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  39041. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39042. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  39043. #ifndef NO_RSA
  39044. EVP_PKEY_CTX_free(ctx);
  39045. /* Parameter generation for RSA not supported yet. */
  39046. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  39047. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  39048. #endif
  39049. #ifdef HAVE_ECC
  39050. EVP_PKEY_CTX_free(ctx);
  39051. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  39052. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  39053. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  39054. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  39055. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  39056. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  39057. WOLFSSL_SUCCESS);
  39058. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39059. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39060. #endif
  39061. EVP_PKEY_CTX_free(ctx);
  39062. EVP_PKEY_free(pkey);
  39063. #endif
  39064. return EXPECT_RESULT();
  39065. }
  39066. static int test_wolfSSL_EVP_PKEY_keygen(void)
  39067. {
  39068. EXPECT_DECLS;
  39069. WOLFSSL_EVP_PKEY* pkey = NULL;
  39070. EVP_PKEY_CTX* ctx = NULL;
  39071. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39072. WOLFSSL_EVP_PKEY* params = NULL;
  39073. DH* dh = NULL;
  39074. const BIGNUM* pubkey = NULL;
  39075. const BIGNUM* privkey = NULL;
  39076. ASN1_INTEGER* asn1int = NULL;
  39077. unsigned int length = 0;
  39078. byte* derBuffer = NULL;
  39079. #endif
  39080. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39081. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39082. /* Bad cases */
  39083. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  39084. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  39085. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  39086. /* Good case */
  39087. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  39088. EVP_PKEY_CTX_free(ctx);
  39089. ctx = NULL;
  39090. EVP_PKEY_free(pkey);
  39091. pkey = NULL;
  39092. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  39093. /* Test DH keygen */
  39094. {
  39095. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  39096. ExpectNotNull(dh = DH_get_2048_256());
  39097. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39098. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  39099. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39100. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  39101. DH_free(dh);
  39102. dh = NULL;
  39103. EVP_PKEY_CTX_free(ctx);
  39104. EVP_PKEY_free(params);
  39105. /* try exporting generated key to DER, to verify */
  39106. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  39107. DH_get0_key(dh, &pubkey, &privkey);
  39108. ExpectNotNull(pubkey);
  39109. ExpectNotNull(privkey);
  39110. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  39111. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  39112. ASN1_INTEGER_free(asn1int);
  39113. DH_free(dh);
  39114. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39115. EVP_PKEY_free(pkey);
  39116. }
  39117. #endif
  39118. return EXPECT_RESULT();
  39119. }
  39120. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  39121. {
  39122. EXPECT_DECLS;
  39123. WOLFSSL_EVP_PKEY* pkey = NULL;
  39124. EVP_PKEY_CTX *ctx = NULL;
  39125. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39126. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39127. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  39128. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  39129. EVP_PKEY_CTX_free(ctx);
  39130. EVP_PKEY_free(pkey);
  39131. return EXPECT_RESULT();
  39132. }
  39133. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  39134. {
  39135. EXPECT_DECLS;
  39136. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  39137. WOLFSSL_EVP_PKEY* pkey = NULL;
  39138. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39139. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  39140. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  39141. EVP_PKEY_free(pkey);
  39142. #endif
  39143. return EXPECT_RESULT();
  39144. }
  39145. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  39146. {
  39147. EXPECT_DECLS;
  39148. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  39149. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  39150. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  39151. !defined(NO_FILESYSTEM)
  39152. WOLFSSL_EVP_PKEY* params = NULL;
  39153. WOLFSSL_EVP_PKEY* copy = NULL;
  39154. DH* dh = NULL;
  39155. BIGNUM* p1;
  39156. BIGNUM* g1;
  39157. BIGNUM* q1;
  39158. BIGNUM* p2;
  39159. BIGNUM* g2;
  39160. BIGNUM* q2;
  39161. /* create DH with DH_get_2048_256 params */
  39162. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  39163. ExpectNotNull(dh = DH_get_2048_256());
  39164. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  39165. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  39166. (const BIGNUM**)&q1,
  39167. (const BIGNUM**)&g1);
  39168. DH_free(dh);
  39169. dh = NULL;
  39170. /* create DH with random generated DH params */
  39171. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  39172. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  39173. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  39174. DH_free(dh);
  39175. dh = NULL;
  39176. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  39177. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  39178. ExpectNotNull(dh->p);
  39179. ExpectNotNull(dh->g);
  39180. ExpectNotNull(dh->q);
  39181. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  39182. (const BIGNUM**)&q2,
  39183. (const BIGNUM**)&g2);
  39184. ExpectIntEQ(BN_cmp(p1, p2), 0);
  39185. ExpectIntEQ(BN_cmp(q1, q2), 0);
  39186. ExpectIntEQ(BN_cmp(g1, g2), 0);
  39187. DH_free(dh);
  39188. EVP_PKEY_free(copy);
  39189. EVP_PKEY_free(params);
  39190. #endif
  39191. return EXPECT_RESULT();
  39192. }
  39193. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39194. {
  39195. EXPECT_DECLS;
  39196. WOLFSSL_EVP_PKEY* pkey = NULL;
  39197. EVP_PKEY_CTX* ctx = NULL;
  39198. int bits = 2048;
  39199. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39200. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39201. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39202. WOLFSSL_SUCCESS);
  39203. EVP_PKEY_CTX_free(ctx);
  39204. EVP_PKEY_free(pkey);
  39205. return EXPECT_RESULT();
  39206. }
  39207. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39208. {
  39209. EXPECT_DECLS;
  39210. /* This is large enough to be used for all key sizes */
  39211. byte key[AES_256_KEY_SIZE] = {0};
  39212. byte iv[AES_BLOCK_SIZE] = {0};
  39213. int i;
  39214. int nids[] = {
  39215. #ifdef HAVE_AES_CBC
  39216. NID_aes_128_cbc,
  39217. #endif
  39218. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39219. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39220. #ifdef HAVE_AESGCM
  39221. NID_aes_128_gcm,
  39222. #endif
  39223. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39224. #ifdef WOLFSSL_AES_COUNTER
  39225. NID_aes_128_ctr,
  39226. #endif
  39227. #ifndef NO_DES3
  39228. NID_des_cbc,
  39229. NID_des_ede3_cbc,
  39230. #endif
  39231. };
  39232. int iv_lengths[] = {
  39233. #ifdef HAVE_AES_CBC
  39234. AES_BLOCK_SIZE,
  39235. #endif
  39236. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39237. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39238. #ifdef HAVE_AESGCM
  39239. GCM_NONCE_MID_SZ,
  39240. #endif
  39241. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39242. #ifdef WOLFSSL_AES_COUNTER
  39243. AES_BLOCK_SIZE,
  39244. #endif
  39245. #ifndef NO_DES3
  39246. DES_BLOCK_SIZE,
  39247. DES_BLOCK_SIZE,
  39248. #endif
  39249. };
  39250. int nidsLen = (sizeof(nids)/sizeof(int));
  39251. for (i = 0; i < nidsLen; i++) {
  39252. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  39253. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  39254. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39255. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39256. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39257. EVP_CIPHER_CTX_free(ctx);
  39258. }
  39259. return EXPECT_RESULT();
  39260. }
  39261. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39262. {
  39263. EXPECT_DECLS;
  39264. byte key[AES_256_KEY_SIZE] = {0};
  39265. byte iv[AES_BLOCK_SIZE] = {0};
  39266. int i;
  39267. int nids[] = {
  39268. #ifdef HAVE_AES_CBC
  39269. NID_aes_128_cbc,
  39270. NID_aes_256_cbc,
  39271. #endif
  39272. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39273. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39274. #ifdef HAVE_AESGCM
  39275. NID_aes_128_gcm,
  39276. NID_aes_256_gcm,
  39277. #endif
  39278. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39279. #ifdef WOLFSSL_AES_COUNTER
  39280. NID_aes_128_ctr,
  39281. NID_aes_256_ctr,
  39282. #endif
  39283. #ifndef NO_DES3
  39284. NID_des_cbc,
  39285. NID_des_ede3_cbc,
  39286. #endif
  39287. };
  39288. int key_lengths[] = {
  39289. #ifdef HAVE_AES_CBC
  39290. AES_128_KEY_SIZE,
  39291. AES_256_KEY_SIZE,
  39292. #endif
  39293. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39294. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39295. #ifdef HAVE_AESGCM
  39296. AES_128_KEY_SIZE,
  39297. AES_256_KEY_SIZE,
  39298. #endif
  39299. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39300. #ifdef WOLFSSL_AES_COUNTER
  39301. AES_128_KEY_SIZE,
  39302. AES_256_KEY_SIZE,
  39303. #endif
  39304. #ifndef NO_DES3
  39305. DES_KEY_SIZE,
  39306. DES3_KEY_SIZE,
  39307. #endif
  39308. };
  39309. int nidsLen = (sizeof(nids)/sizeof(int));
  39310. for (i = 0; i < nidsLen; i++) {
  39311. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  39312. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  39313. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39314. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39315. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  39316. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  39317. WOLFSSL_SUCCESS);
  39318. EVP_CIPHER_CTX_free(ctx);
  39319. }
  39320. return EXPECT_RESULT();
  39321. }
  39322. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39323. {
  39324. EXPECT_DECLS;
  39325. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39326. int ivLen, keyLen;
  39327. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39328. #ifdef HAVE_AESGCM
  39329. byte key[AES_128_KEY_SIZE] = {0};
  39330. byte iv[AES_BLOCK_SIZE] = {0};
  39331. const EVP_CIPHER *init = EVP_aes_128_gcm();
  39332. #else
  39333. byte key[DES3_KEY_SIZE] = {0};
  39334. byte iv[DES_BLOCK_SIZE] = {0};
  39335. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39336. #endif
  39337. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39338. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39339. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39340. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39341. /* Bad cases */
  39342. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  39343. WOLFSSL_FAILURE);
  39344. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  39345. WOLFSSL_FAILURE);
  39346. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39347. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39348. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  39349. WOLFSSL_FAILURE);
  39350. /* Good case */
  39351. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39352. EVP_CIPHER_CTX_free(ctx);
  39353. #endif
  39354. return EXPECT_RESULT();
  39355. }
  39356. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39357. {
  39358. EXPECT_DECLS;
  39359. WOLFSSL_ENGINE* e = NULL;
  39360. int id = 0;
  39361. EVP_PKEY_CTX *ctx = NULL;
  39362. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39363. EVP_PKEY_CTX_free(ctx);
  39364. return EXPECT_RESULT();
  39365. }
  39366. static int test_wolfSSL_EVP_rc4(void)
  39367. {
  39368. EXPECT_DECLS;
  39369. #if !defined(NO_RC4)
  39370. ExpectNotNull(wolfSSL_EVP_rc4());
  39371. #endif
  39372. return EXPECT_RESULT();
  39373. }
  39374. static int test_wolfSSL_EVP_enc_null(void)
  39375. {
  39376. EXPECT_DECLS;
  39377. ExpectNotNull(wolfSSL_EVP_enc_null());
  39378. return EXPECT_RESULT();
  39379. }
  39380. static int test_wolfSSL_EVP_rc2_cbc(void)
  39381. {
  39382. EXPECT_DECLS;
  39383. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39384. ExpectNull(wolfSSL_EVP_rc2_cbc());
  39385. #endif
  39386. return EXPECT_RESULT();
  39387. }
  39388. static int test_wolfSSL_EVP_mdc2(void)
  39389. {
  39390. EXPECT_DECLS;
  39391. #if !defined(NO_WOLFSSL_STUB)
  39392. ExpectNull(wolfSSL_EVP_mdc2());
  39393. #endif
  39394. return EXPECT_RESULT();
  39395. }
  39396. static int test_wolfSSL_EVP_md4(void)
  39397. {
  39398. EXPECT_DECLS;
  39399. #if !defined(NO_MD4)
  39400. ExpectNotNull(wolfSSL_EVP_md4());
  39401. #endif
  39402. return EXPECT_RESULT();
  39403. }
  39404. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39405. {
  39406. EXPECT_DECLS;
  39407. #ifdef HAVE_AESGCM
  39408. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  39409. #endif
  39410. return EXPECT_RESULT();
  39411. }
  39412. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39413. {
  39414. EXPECT_DECLS;
  39415. #ifdef HAVE_AESGCM
  39416. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  39417. #endif
  39418. return EXPECT_RESULT();
  39419. }
  39420. static int test_wolfSSL_EVP_aes_256_ccm(void)
  39421. {
  39422. EXPECT_DECLS;
  39423. #ifdef HAVE_AESCCM
  39424. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  39425. #endif
  39426. return EXPECT_RESULT();
  39427. }
  39428. static int test_wolfSSL_EVP_aes_192_ccm(void)
  39429. {
  39430. EXPECT_DECLS;
  39431. #ifdef HAVE_AESCCM
  39432. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  39433. #endif
  39434. return EXPECT_RESULT();
  39435. }
  39436. static int test_wolfSSL_EVP_aes_128_ccm(void)
  39437. {
  39438. EXPECT_DECLS;
  39439. #ifdef HAVE_AESCCM
  39440. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  39441. #endif
  39442. return EXPECT_RESULT();
  39443. }
  39444. static int test_wolfSSL_EVP_ripemd160(void)
  39445. {
  39446. EXPECT_DECLS;
  39447. #if !defined(NO_WOLFSSL_STUB)
  39448. ExpectNull(wolfSSL_EVP_ripemd160());
  39449. #endif
  39450. return EXPECT_RESULT();
  39451. }
  39452. static int test_wolfSSL_EVP_get_digestbynid(void)
  39453. {
  39454. EXPECT_DECLS;
  39455. #ifndef NO_MD5
  39456. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39457. #endif
  39458. #ifndef NO_SHA
  39459. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39460. #endif
  39461. #ifndef NO_SHA256
  39462. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  39463. #endif
  39464. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  39465. return EXPECT_RESULT();
  39466. }
  39467. static int test_wolfSSL_EVP_MD_nid(void)
  39468. {
  39469. EXPECT_DECLS;
  39470. #ifndef NO_MD5
  39471. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39472. #endif
  39473. #ifndef NO_SHA
  39474. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39475. #endif
  39476. #ifndef NO_SHA256
  39477. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39478. #endif
  39479. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  39480. return EXPECT_RESULT();
  39481. }
  39482. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39483. {
  39484. EXPECT_DECLS;
  39485. #if defined(HAVE_ECC)
  39486. WOLFSSL_EVP_PKEY* pkey = NULL;
  39487. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  39488. ExpectNotNull(pkey = EVP_PKEY_new());
  39489. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  39490. EVP_PKEY_free(pkey);
  39491. #endif
  39492. return EXPECT_RESULT();
  39493. }
  39494. static int test_wolfSSL_EVP_X_STATE(void)
  39495. {
  39496. EXPECT_DECLS;
  39497. #if !defined(NO_DES3) && !defined(NO_RC4)
  39498. byte key[DES3_KEY_SIZE] = {0};
  39499. byte iv[DES_IV_SIZE] = {0};
  39500. EVP_CIPHER_CTX *ctx = NULL;
  39501. const EVP_CIPHER *init = NULL;
  39502. /* Bad test cases */
  39503. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39504. ExpectNotNull(init = EVP_des_ede3_cbc());
  39505. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39506. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39507. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  39508. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  39509. EVP_CIPHER_CTX_free(ctx);
  39510. ctx = NULL;
  39511. /* Good test case */
  39512. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39513. ExpectNotNull(init = wolfSSL_EVP_rc4());
  39514. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39515. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39516. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  39517. EVP_CIPHER_CTX_free(ctx);
  39518. #endif
  39519. return EXPECT_RESULT();
  39520. }
  39521. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39522. {
  39523. EXPECT_DECLS;
  39524. #if !defined(NO_DES3) && !defined(NO_RC4)
  39525. byte key[DES3_KEY_SIZE] = {0};
  39526. byte iv[DES_IV_SIZE] = {0};
  39527. EVP_CIPHER_CTX *ctx = NULL;
  39528. const EVP_CIPHER *init = NULL;
  39529. /* Bad test cases */
  39530. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39531. ExpectNotNull(init = EVP_des_ede3_cbc());
  39532. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39533. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39534. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39535. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39536. EVP_CIPHER_CTX_free(ctx);
  39537. ctx = NULL;
  39538. /* Good test case */
  39539. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  39540. ExpectNotNull(init = wolfSSL_EVP_rc4());
  39541. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39542. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39543. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39544. EVP_CIPHER_CTX_free(ctx);
  39545. #endif
  39546. return EXPECT_RESULT();
  39547. }
  39548. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39549. {
  39550. EXPECT_DECLS;
  39551. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  39552. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  39553. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  39554. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  39555. #ifdef HAVE_AES_CBC
  39556. #ifdef WOLFSSL_AES_128
  39557. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39558. #endif
  39559. #ifdef WOLFSSL_AES_192
  39560. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39561. #endif
  39562. #ifdef WOLFSSL_AES_256
  39563. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39564. #endif
  39565. #endif
  39566. #ifdef HAVE_AESGCM
  39567. #ifdef WOLFSSL_AES_128
  39568. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39569. #endif
  39570. #ifdef WOLFSSL_AES_192
  39571. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39572. #endif
  39573. #ifdef WOLFSSL_AES_256
  39574. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39575. #endif
  39576. #endif
  39577. #ifdef HAVE_AESCCM
  39578. #ifdef WOLFSSL_AES_128
  39579. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  39580. #endif
  39581. #ifdef WOLFSSL_AES_192
  39582. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  39583. #endif
  39584. #ifdef WOLFSSL_AES_256
  39585. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  39586. #endif
  39587. #endif
  39588. #ifdef WOLFSSL_AES_COUNTER
  39589. #ifdef WOLFSSL_AES_128
  39590. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39591. #endif
  39592. #ifdef WOLFSSL_AES_192
  39593. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39594. #endif
  39595. #ifdef WOLFSSL_AES_256
  39596. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39597. #endif
  39598. #endif
  39599. #ifdef HAVE_AES_ECB
  39600. #ifdef WOLFSSL_AES_128
  39601. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39602. #endif
  39603. #ifdef WOLFSSL_AES_192
  39604. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39605. #endif
  39606. #ifdef WOLFSSL_AES_256
  39607. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39608. #endif
  39609. #endif
  39610. #ifdef WOLFSSL_AES_OFB
  39611. #ifdef WOLFSSL_AES_128
  39612. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39613. #endif
  39614. #ifdef WOLFSSL_AES_192
  39615. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39616. #endif
  39617. #ifdef WOLFSSL_AES_256
  39618. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39619. #endif
  39620. #endif
  39621. #ifndef NO_RC4
  39622. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39623. #endif
  39624. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39625. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39626. #endif
  39627. #endif
  39628. #ifdef WOLFSSL_SM4_ECB
  39629. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  39630. #endif
  39631. #ifdef WOLFSSL_SM4_CBC
  39632. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  39633. #endif
  39634. #ifdef WOLFSSL_SM4_CTR
  39635. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  39636. #endif
  39637. #ifdef WOLFSSL_SM4_GCM
  39638. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  39639. #endif
  39640. #ifdef WOLFSSL_SM4_CCM
  39641. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  39642. #endif
  39643. return EXPECT_RESULT();
  39644. }
  39645. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39646. {
  39647. EXPECT_DECLS;
  39648. int nids[] = {
  39649. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39650. #ifdef WOLFSSL_AES_128
  39651. NID_aes_128_cbc,
  39652. #endif
  39653. #ifdef WOLFSSL_AES_192
  39654. NID_aes_192_cbc,
  39655. #endif
  39656. #ifdef WOLFSSL_AES_256
  39657. NID_aes_256_cbc,
  39658. #endif
  39659. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39660. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39661. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39662. #ifdef HAVE_AESGCM
  39663. #ifdef WOLFSSL_AES_128
  39664. NID_aes_128_gcm,
  39665. #endif
  39666. #ifdef WOLFSSL_AES_192
  39667. NID_aes_192_gcm,
  39668. #endif
  39669. #ifdef WOLFSSL_AES_256
  39670. NID_aes_256_gcm,
  39671. #endif
  39672. #endif /* HAVE_AESGCM */
  39673. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39674. #ifdef WOLFSSL_AES_COUNTER
  39675. #ifdef WOLFSSL_AES_128
  39676. NID_aes_128_ctr,
  39677. #endif
  39678. #ifdef WOLFSSL_AES_192
  39679. NID_aes_192_ctr,
  39680. #endif
  39681. #ifdef WOLFSSL_AES_256
  39682. NID_aes_256_ctr,
  39683. #endif
  39684. #endif
  39685. #ifndef NO_DES3
  39686. NID_des_cbc,
  39687. NID_des_ede3_cbc,
  39688. #endif
  39689. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39690. NID_chacha20_poly1305,
  39691. #endif
  39692. };
  39693. int iv_lengths[] = {
  39694. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39695. #ifdef WOLFSSL_AES_128
  39696. AES_BLOCK_SIZE,
  39697. #endif
  39698. #ifdef WOLFSSL_AES_192
  39699. AES_BLOCK_SIZE,
  39700. #endif
  39701. #ifdef WOLFSSL_AES_256
  39702. AES_BLOCK_SIZE,
  39703. #endif
  39704. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39705. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39706. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39707. #ifdef HAVE_AESGCM
  39708. #ifdef WOLFSSL_AES_128
  39709. GCM_NONCE_MID_SZ,
  39710. #endif
  39711. #ifdef WOLFSSL_AES_192
  39712. GCM_NONCE_MID_SZ,
  39713. #endif
  39714. #ifdef WOLFSSL_AES_256
  39715. GCM_NONCE_MID_SZ,
  39716. #endif
  39717. #endif /* HAVE_AESGCM */
  39718. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39719. #ifdef WOLFSSL_AES_COUNTER
  39720. #ifdef WOLFSSL_AES_128
  39721. AES_BLOCK_SIZE,
  39722. #endif
  39723. #ifdef WOLFSSL_AES_192
  39724. AES_BLOCK_SIZE,
  39725. #endif
  39726. #ifdef WOLFSSL_AES_256
  39727. AES_BLOCK_SIZE,
  39728. #endif
  39729. #endif
  39730. #ifndef NO_DES3
  39731. DES_BLOCK_SIZE,
  39732. DES_BLOCK_SIZE,
  39733. #endif
  39734. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39735. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39736. #endif
  39737. };
  39738. int i;
  39739. int nidsLen = (sizeof(nids)/sizeof(int));
  39740. for (i = 0; i < nidsLen; i++) {
  39741. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  39742. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39743. }
  39744. return EXPECT_RESULT();
  39745. }
  39746. static int test_wolfSSL_EVP_SignInit_ex(void)
  39747. {
  39748. EXPECT_DECLS;
  39749. WOLFSSL_EVP_MD_CTX mdCtx;
  39750. WOLFSSL_ENGINE* e = 0;
  39751. const EVP_MD* md = EVP_sha256();
  39752. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39753. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39754. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39755. return EXPECT_RESULT();
  39756. }
  39757. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39758. {
  39759. EXPECT_DECLS;
  39760. #if !defined(NO_SHA256)
  39761. WOLFSSL_EVP_MD_CTX mdCtx;
  39762. unsigned int s = 0;
  39763. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39764. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39765. /* Bad Case */
  39766. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  39767. (HAVE_FIPS_VERSION > 2))
  39768. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39769. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39770. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39771. #else
  39772. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39773. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39774. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39775. #endif
  39776. /* Good Case */
  39777. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39778. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  39779. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39780. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39781. #endif
  39782. return EXPECT_RESULT();
  39783. }
  39784. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39785. {
  39786. EXPECT_DECLS;
  39787. #if defined(OPENSSL_EXTRA)
  39788. EVP_PKEY* pkey = NULL;
  39789. EVP_PKEY_CTX* ctx = NULL;
  39790. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39791. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39792. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39793. /* void */
  39794. EVP_PKEY_CTX_free(ctx);
  39795. #else
  39796. /* int */
  39797. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39798. #endif
  39799. EVP_PKEY_free(pkey);
  39800. #endif
  39801. return EXPECT_RESULT();
  39802. }
  39803. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39804. {
  39805. EXPECT_DECLS;
  39806. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39807. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39808. DH *dh = NULL;
  39809. DH *setDh = NULL;
  39810. EVP_PKEY *pkey = NULL;
  39811. EVP_PKEY_CTX* ctx = NULL;
  39812. FILE* f = NULL;
  39813. unsigned char buf[512];
  39814. const unsigned char* pt = buf;
  39815. const char* dh2048 = "./certs/dh2048.der";
  39816. long len = 0;
  39817. int code = -1;
  39818. XMEMSET(buf, 0, sizeof(buf));
  39819. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  39820. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  39821. if (f != XBADFILE)
  39822. XFCLOSE(f);
  39823. /* Load dh2048.der into DH with internal format */
  39824. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39825. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39826. ExpectIntEQ(code, 0);
  39827. code = -1;
  39828. pkey = wolfSSL_EVP_PKEY_new();
  39829. /* Set DH into PKEY */
  39830. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39831. /* create ctx from pkey */
  39832. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39833. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39834. /* TODO: more invalid cases */
  39835. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  39836. EVP_PKEY_CTX_free(ctx);
  39837. EVP_PKEY_free(pkey);
  39838. DH_free(setDh);
  39839. DH_free(dh);
  39840. #endif
  39841. #endif
  39842. return EXPECT_RESULT();
  39843. }
  39844. static int test_wolfSSL_EVP_BytesToKey(void)
  39845. {
  39846. EXPECT_DECLS;
  39847. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39848. byte key[AES_BLOCK_SIZE] = {0};
  39849. byte iv[AES_BLOCK_SIZE] = {0};
  39850. int count = 0;
  39851. const EVP_MD* md = EVP_sha256();
  39852. const EVP_CIPHER *type;
  39853. const unsigned char *salt = (unsigned char *)"salt1234";
  39854. int sz = 5;
  39855. const byte data[] = {
  39856. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39857. 0x72,0x6c,0x64
  39858. };
  39859. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39860. /* Bad cases */
  39861. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39862. 0);
  39863. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39864. 16);
  39865. md = "2";
  39866. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39867. WOLFSSL_FAILURE);
  39868. /* Good case */
  39869. md = EVP_sha256();
  39870. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39871. 16);
  39872. #endif
  39873. return EXPECT_RESULT();
  39874. }
  39875. static int test_evp_cipher_aes_gcm(void)
  39876. {
  39877. EXPECT_DECLS;
  39878. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39879. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39880. (HAVE_FIPS_VERSION >= 2)))
  39881. /*
  39882. * This test checks data at various points in the encrypt/decrypt process
  39883. * against known values produced using the same test with OpenSSL. This
  39884. * interop testing is critical for verifying the correctness of our
  39885. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39886. * a flow supported by OpenSSL that uses the control command
  39887. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39888. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39889. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39890. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39891. * happened to "cancel out" if both sides of the connection had the bug.
  39892. */
  39893. enum {
  39894. NUM_ENCRYPTIONS = 3,
  39895. AAD_SIZE = 4
  39896. };
  39897. byte plainText1[] = {
  39898. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39899. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39900. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39901. };
  39902. byte plainText2[] = {
  39903. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39904. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39905. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39906. };
  39907. byte plainText3[] = {
  39908. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39909. 0x5b, 0x57, 0x10
  39910. };
  39911. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39912. plainText1,
  39913. plainText2,
  39914. plainText3
  39915. };
  39916. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39917. sizeof(plainText1),
  39918. sizeof(plainText2),
  39919. sizeof(plainText3)
  39920. };
  39921. byte aad1[AAD_SIZE] = {
  39922. 0x00, 0x00, 0x00, 0x01
  39923. };
  39924. byte aad2[AAD_SIZE] = {
  39925. 0x00, 0x00, 0x00, 0x10
  39926. };
  39927. byte aad3[AAD_SIZE] = {
  39928. 0x00, 0x00, 0x01, 0x00
  39929. };
  39930. byte* aads[NUM_ENCRYPTIONS] = {
  39931. aad1,
  39932. aad2,
  39933. aad3
  39934. };
  39935. const byte iv[GCM_NONCE_MID_SZ] = {
  39936. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39937. };
  39938. byte currentIv[GCM_NONCE_MID_SZ];
  39939. const byte key[] = {
  39940. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39941. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39942. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39943. };
  39944. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39945. {
  39946. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39947. 0xEF
  39948. },
  39949. {
  39950. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39951. 0xF0
  39952. },
  39953. {
  39954. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39955. 0xF1
  39956. }
  39957. };
  39958. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39959. {
  39960. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39961. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39962. },
  39963. {
  39964. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39965. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39966. },
  39967. {
  39968. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39969. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39970. }
  39971. };
  39972. const byte expCipherText1[] = {
  39973. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39974. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39975. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39976. };
  39977. const byte expCipherText2[] = {
  39978. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39979. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39980. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39981. };
  39982. const byte expCipherText3[] = {
  39983. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39984. 0x80, 0x5E, 0x6B,
  39985. };
  39986. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39987. expCipherText1,
  39988. expCipherText2,
  39989. expCipherText3
  39990. };
  39991. byte* cipherText = NULL;
  39992. byte* calcPlainText = NULL;
  39993. byte tag[AES_BLOCK_SIZE];
  39994. EVP_CIPHER_CTX* encCtx = NULL;
  39995. EVP_CIPHER_CTX* decCtx = NULL;
  39996. int i, j, outl;
  39997. /****************************************************/
  39998. for (i = 0; i < 3; ++i) {
  39999. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  40000. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  40001. /* First iteration, set key before IV. */
  40002. if (i == 0) {
  40003. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  40004. SSL_SUCCESS);
  40005. /*
  40006. * The call to EVP_CipherInit below (with NULL key) should clear the
  40007. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  40008. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  40009. * behavior.
  40010. */
  40011. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40012. (void*)iv), SSL_SUCCESS);
  40013. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  40014. SSL_SUCCESS);
  40015. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40016. currentIv), SSL_FAILURE);
  40017. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  40018. SSL_SUCCESS);
  40019. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  40020. SSL_SUCCESS);
  40021. }
  40022. /* Second iteration, IV before key. */
  40023. else {
  40024. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  40025. SSL_SUCCESS);
  40026. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  40027. SSL_SUCCESS);
  40028. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  40029. SSL_SUCCESS);
  40030. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  40031. SSL_SUCCESS);
  40032. }
  40033. /*
  40034. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  40035. * been issued first.
  40036. */
  40037. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40038. currentIv), SSL_FAILURE);
  40039. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40040. (void*)iv), SSL_SUCCESS);
  40041. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  40042. (void*)iv), SSL_SUCCESS);
  40043. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  40044. /*************** Encrypt ***************/
  40045. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40046. currentIv), SSL_SUCCESS);
  40047. /* Check current IV against expected. */
  40048. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40049. /* Add AAD. */
  40050. if (i == 2) {
  40051. /* Test streaming API. */
  40052. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  40053. AAD_SIZE), SSL_SUCCESS);
  40054. }
  40055. else {
  40056. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  40057. AAD_SIZE);
  40058. }
  40059. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40060. DYNAMIC_TYPE_TMP_BUFFER));
  40061. /* Encrypt plaintext. */
  40062. if (i == 2) {
  40063. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  40064. plainTexts[j], plainTextSzs[j]),
  40065. SSL_SUCCESS);
  40066. }
  40067. else {
  40068. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  40069. plainTextSzs[j]), plainTextSzs[j]);
  40070. }
  40071. if (i == 2) {
  40072. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  40073. SSL_SUCCESS);
  40074. }
  40075. else {
  40076. /*
  40077. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  40078. * akin to calling EVP_CipherFinal.
  40079. */
  40080. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  40081. }
  40082. /* Check ciphertext against expected. */
  40083. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  40084. 0);
  40085. /* Get and check tag against expected. */
  40086. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  40087. sizeof(tag), tag), SSL_SUCCESS);
  40088. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  40089. /*************** Decrypt ***************/
  40090. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  40091. currentIv), SSL_SUCCESS);
  40092. /* Check current IV against expected. */
  40093. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  40094. /* Add AAD. */
  40095. if (i == 2) {
  40096. /* Test streaming API. */
  40097. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  40098. AAD_SIZE), SSL_SUCCESS);
  40099. }
  40100. else {
  40101. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  40102. AAD_SIZE);
  40103. }
  40104. /* Set expected tag. */
  40105. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  40106. sizeof(tag), tag), SSL_SUCCESS);
  40107. /* Decrypt ciphertext. */
  40108. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  40109. DYNAMIC_TYPE_TMP_BUFFER));
  40110. if (i == 2) {
  40111. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  40112. cipherText, plainTextSzs[j]),
  40113. SSL_SUCCESS);
  40114. }
  40115. else {
  40116. /* This first EVP_Cipher call will check the tag, too. */
  40117. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  40118. plainTextSzs[j]), plainTextSzs[j]);
  40119. }
  40120. if (i == 2) {
  40121. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  40122. SSL_SUCCESS);
  40123. }
  40124. else {
  40125. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  40126. }
  40127. /* Check plaintext against expected. */
  40128. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  40129. 0);
  40130. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40131. cipherText = NULL;
  40132. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40133. calcPlainText = NULL;
  40134. }
  40135. EVP_CIPHER_CTX_free(encCtx);
  40136. encCtx = NULL;
  40137. EVP_CIPHER_CTX_free(decCtx);
  40138. decCtx = NULL;
  40139. }
  40140. #endif
  40141. return EXPECT_RESULT();
  40142. }
  40143. static int test_wolfSSL_OBJ_ln(void)
  40144. {
  40145. EXPECT_DECLS;
  40146. const int nid_set[] = {
  40147. NID_commonName,
  40148. NID_serialNumber,
  40149. NID_countryName,
  40150. NID_localityName,
  40151. NID_stateOrProvinceName,
  40152. NID_organizationName,
  40153. NID_organizationalUnitName,
  40154. NID_domainComponent,
  40155. NID_businessCategory,
  40156. NID_jurisdictionCountryName,
  40157. NID_jurisdictionStateOrProvinceName,
  40158. NID_emailAddress
  40159. };
  40160. const char* ln_set[] = {
  40161. "commonName",
  40162. "serialNumber",
  40163. "countryName",
  40164. "localityName",
  40165. "stateOrProvinceName",
  40166. "organizationName",
  40167. "organizationalUnitName",
  40168. "domainComponent",
  40169. "businessCategory",
  40170. "jurisdictionCountryName",
  40171. "jurisdictionStateOrProvinceName",
  40172. "emailAddress",
  40173. };
  40174. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  40175. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  40176. #ifdef HAVE_ECC
  40177. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40178. {
  40179. EC_builtin_curve r[27];
  40180. size_t nCurves = sizeof(r) / sizeof(r[0]);
  40181. nCurves = EC_get_builtin_curves(r, nCurves);
  40182. for (i = 0; i < nCurves; i++) {
  40183. /* skip ECC_CURVE_INVALID */
  40184. if (r[i].nid != ECC_CURVE_INVALID) {
  40185. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  40186. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  40187. }
  40188. }
  40189. }
  40190. #endif
  40191. #endif
  40192. for (i = 0; i < maxIdx; i++) {
  40193. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  40194. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  40195. }
  40196. return EXPECT_RESULT();
  40197. }
  40198. static int test_wolfSSL_OBJ_sn(void)
  40199. {
  40200. EXPECT_DECLS;
  40201. int i = 0, maxIdx = 7;
  40202. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  40203. NID_stateOrProvinceName,NID_organizationName,
  40204. NID_organizationalUnitName,NID_emailAddress};
  40205. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  40206. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  40207. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  40208. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  40209. WOLFSSL_EMAIL_ADDR};
  40210. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  40211. for (i = 0; i < maxIdx; i++) {
  40212. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  40213. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  40214. }
  40215. return EXPECT_RESULT();
  40216. }
  40217. #if !defined(NO_BIO)
  40218. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  40219. {
  40220. return lh_strhash(s[3]);
  40221. }
  40222. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  40223. {
  40224. return XSTRCMP(a[3], b[3]);
  40225. }
  40226. #endif
  40227. static int test_wolfSSL_TXT_DB(void)
  40228. {
  40229. EXPECT_DECLS;
  40230. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40231. BIO *bio = NULL;
  40232. TXT_DB *db = NULL;
  40233. const int columns = 6;
  40234. const char *fields[6] = {
  40235. "V",
  40236. "320926161116Z",
  40237. "",
  40238. "12BD",
  40239. "unknown",
  40240. "/CN=rsa doe",
  40241. };
  40242. char** fields_copy = NULL;
  40243. /* Test read */
  40244. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  40245. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40246. ExpectNotNull(db = TXT_DB_read(bio, columns));
  40247. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40248. DYNAMIC_TYPE_OPENSSL));
  40249. if (fields_copy != NULL) {
  40250. XMEMCPY(fields_copy, fields, sizeof(fields));
  40251. }
  40252. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40253. if (EXPECT_FAIL()) {
  40254. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  40255. }
  40256. BIO_free(bio);
  40257. bio = NULL;
  40258. /* Test write */
  40259. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40260. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  40261. BIO_free(bio);
  40262. /* Test index */
  40263. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40264. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40265. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40266. fields[3] = "12DA";
  40267. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40268. fields[3] = "FFFF";
  40269. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40270. fields[3] = "";
  40271. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40272. TXT_DB_free(db);
  40273. #endif
  40274. return EXPECT_RESULT();
  40275. }
  40276. static int test_wolfSSL_NCONF(void)
  40277. {
  40278. EXPECT_DECLS;
  40279. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40280. const char* confFile = "./tests/NCONF_test.cnf";
  40281. CONF* conf = NULL;
  40282. long eline = 0;
  40283. long num = 0;
  40284. ExpectNotNull(conf = NCONF_new(NULL));
  40285. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40286. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40287. ExpectIntEQ(num, 1234);
  40288. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40289. ExpectIntEQ(num, 4321);
  40290. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40291. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40292. "./test-dir/file1");
  40293. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40294. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40295. NCONF_free(conf);
  40296. #endif
  40297. return EXPECT_RESULT();
  40298. }
  40299. #endif /* OPENSSL_ALL */
  40300. static int test_wolfSSL_X509V3_EXT_get(void) {
  40301. EXPECT_DECLS;
  40302. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40303. XFILE f = XBADFILE;
  40304. int numOfExt =0;
  40305. int extNid = 0;
  40306. int i = 0;
  40307. WOLFSSL_X509* x509 = NULL;
  40308. WOLFSSL_X509_EXTENSION* ext = NULL;
  40309. const WOLFSSL_v3_ext_method* method = NULL;
  40310. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40311. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40312. if (f != XBADFILE)
  40313. XFCLOSE(f);
  40314. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  40315. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40316. for (i = 0; i < numOfExt; i++) {
  40317. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40318. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  40319. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40320. ExpectIntEQ(method->ext_nid, extNid);
  40321. }
  40322. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  40323. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40324. wolfSSL_X509_free(x509);
  40325. #endif
  40326. return EXPECT_RESULT();
  40327. }
  40328. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40329. {
  40330. EXPECT_DECLS;
  40331. #ifdef OPENSSL_ALL
  40332. const char *ext_names[] = {
  40333. "subjectKeyIdentifier",
  40334. "authorityKeyIdentifier",
  40335. "subjectAltName",
  40336. "keyUsage",
  40337. "extendedKeyUsage",
  40338. };
  40339. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40340. int ext_nids[] = {
  40341. NID_subject_key_identifier,
  40342. NID_authority_key_identifier,
  40343. NID_subject_alt_name,
  40344. NID_key_usage,
  40345. NID_ext_key_usage,
  40346. };
  40347. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40348. const char *ext_values[] = {
  40349. "hash",
  40350. "hash",
  40351. "DNS:example.com, IP:127.0.0.1",
  40352. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  40353. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  40354. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  40355. "OCSPSigning",
  40356. };
  40357. size_t i;
  40358. X509_EXTENSION* ext = NULL;
  40359. X509* x509 = NULL;
  40360. unsigned int keyUsageFlags;
  40361. unsigned int extKeyUsageFlags;
  40362. ExpectNotNull(x509 = X509_new());
  40363. /* keyUsage / extKeyUsage should match string above */
  40364. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40365. | KU_NON_REPUDIATION
  40366. | KU_KEY_ENCIPHERMENT
  40367. | KU_DATA_ENCIPHERMENT
  40368. | KU_KEY_AGREEMENT
  40369. | KU_KEY_CERT_SIGN
  40370. | KU_CRL_SIGN
  40371. | KU_ENCIPHER_ONLY
  40372. | KU_DECIPHER_ONLY;
  40373. extKeyUsageFlags = XKU_SSL_CLIENT
  40374. | XKU_SSL_SERVER
  40375. | XKU_CODE_SIGN
  40376. | XKU_SMIME
  40377. | XKU_TIMESTAMP
  40378. | XKU_OCSP_SIGN;
  40379. for (i = 0; i < ext_names_count; i++) {
  40380. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  40381. ext_values[i]));
  40382. X509_EXTENSION_free(ext);
  40383. ext = NULL;
  40384. }
  40385. for (i = 0; i < ext_nids_count; i++) {
  40386. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  40387. ext_values[i]));
  40388. X509_EXTENSION_free(ext);
  40389. ext = NULL;
  40390. }
  40391. /* Test adding extension to X509 */
  40392. for (i = 0; i < ext_nids_count; i++) {
  40393. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  40394. ext_values[i]));
  40395. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40396. if (ext_nids[i] == NID_key_usage) {
  40397. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40398. }
  40399. else if (ext_nids[i] == NID_ext_key_usage) {
  40400. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40401. }
  40402. X509_EXTENSION_free(ext);
  40403. ext = NULL;
  40404. }
  40405. X509_free(x509);
  40406. #endif
  40407. return EXPECT_RESULT();
  40408. }
  40409. static int test_wolfSSL_X509V3_EXT(void) {
  40410. EXPECT_DECLS;
  40411. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40412. XFILE f = XBADFILE;
  40413. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  40414. char* str = NULL;
  40415. unsigned char* data = NULL;
  40416. const WOLFSSL_v3_ext_method* method = NULL;
  40417. WOLFSSL_X509* x509 = NULL;
  40418. WOLFSSL_X509_EXTENSION* ext = NULL;
  40419. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  40420. WOLFSSL_ASN1_OBJECT *obj = NULL;
  40421. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  40422. WOLFSSL_ASN1_STRING* asn1str = NULL;
  40423. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  40424. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  40425. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  40426. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  40427. WOLFSSL_GENERAL_NAME* gn = NULL;
  40428. /* Check NULL argument */
  40429. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40430. /* Using OCSP cert with X509V3 extensions */
  40431. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  40432. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40433. if (f != XBADFILE)
  40434. XFCLOSE(f);
  40435. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40436. /* Basic Constraints */
  40437. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40438. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40439. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40440. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40441. ExpectIntEQ(bc->ca, 1);
  40442. ExpectNull(bc->pathlen);
  40443. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40444. bc = NULL;
  40445. i++;
  40446. /* Subject Key Identifier */
  40447. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40448. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40449. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40450. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40451. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40452. asn1str));
  40453. X509_EXTENSION_free(ext2);
  40454. ext2 = NULL;
  40455. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40456. ExpectNotNull(method->i2s);
  40457. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40458. wolfSSL_ASN1_STRING_free(asn1str);
  40459. asn1str = NULL;
  40460. if (str != NULL) {
  40461. actual = strcmp(str,
  40462. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40463. }
  40464. ExpectIntEQ(actual, 0);
  40465. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40466. str = NULL;
  40467. i++;
  40468. /* Authority Key Identifier */
  40469. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40470. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40471. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40472. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  40473. ext));
  40474. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40475. ExpectNotNull(asn1str = aKeyId->keyid);
  40476. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  40477. asn1str));
  40478. asn1str = NULL;
  40479. if (str != NULL) {
  40480. actual = strcmp(str,
  40481. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40482. }
  40483. ExpectIntEQ(actual, 0);
  40484. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40485. str = NULL;
  40486. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40487. aKeyId = NULL;
  40488. i++;
  40489. /* Key Usage */
  40490. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40491. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40492. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40493. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40494. #if defined(WOLFSSL_QT)
  40495. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40496. #else
  40497. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40498. #endif
  40499. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40500. if (data != NULL) {
  40501. #ifdef BIG_ENDIAN_ORDER
  40502. actual = data[1];
  40503. #else
  40504. actual = data[0];
  40505. #endif
  40506. }
  40507. ExpectIntEQ(actual, expected);
  40508. wolfSSL_ASN1_STRING_free(asn1str);
  40509. asn1str = NULL;
  40510. #if 1
  40511. i++;
  40512. /* Authority Info Access */
  40513. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40514. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40515. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40516. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  40517. ext));
  40518. #if defined(WOLFSSL_QT)
  40519. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40520. #else
  40521. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40522. #endif
  40523. /* URI entry is an ACCESS_DESCRIPTION type */
  40524. #if defined(WOLFSSL_QT)
  40525. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40526. #else
  40527. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40528. #endif
  40529. ExpectNotNull(adObj = ad->method);
  40530. /* Make sure nid is OCSP */
  40531. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40532. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40533. ExpectNotNull(gn = ad->location);
  40534. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40535. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40536. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40537. #if defined(WOLFSSL_QT)
  40538. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  40539. #else
  40540. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  40541. #endif
  40542. if (str != NULL) {
  40543. actual = strcmp(str, "http://127.0.0.1:22220");
  40544. }
  40545. ExpectIntEQ(actual, 0);
  40546. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40547. aia = NULL;
  40548. #else
  40549. (void) aia; (void) ad; (void) adObj; (void) gn;
  40550. #endif
  40551. wolfSSL_X509_free(x509);
  40552. #endif
  40553. return EXPECT_RESULT();
  40554. }
  40555. static int test_wolfSSL_X509_get_extension_flags(void)
  40556. {
  40557. EXPECT_DECLS;
  40558. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40559. XFILE f = XBADFILE;
  40560. X509* x509 = NULL;
  40561. unsigned int extFlags;
  40562. unsigned int keyUsageFlags;
  40563. unsigned int extKeyUsageFlags;
  40564. /* client-int-cert.pem has the following extension flags. */
  40565. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40566. /* and the following key usage flags. */
  40567. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40568. | KU_NON_REPUDIATION
  40569. | KU_KEY_ENCIPHERMENT;
  40570. /* and the following extended key usage flags. */
  40571. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40572. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  40573. XBADFILE);
  40574. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40575. if (f != XBADFILE) {
  40576. XFCLOSE(f);
  40577. f = XBADFILE;
  40578. }
  40579. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  40580. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40581. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40582. X509_free(x509);
  40583. x509 = NULL;
  40584. /* client-cert-ext.pem has the following extension flags. */
  40585. extFlags = EXFLAG_KUSAGE;
  40586. /* and the following key usage flags. */
  40587. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40588. | KU_KEY_CERT_SIGN
  40589. | KU_CRL_SIGN;
  40590. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  40591. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40592. if (f != XBADFILE)
  40593. XFCLOSE(f);
  40594. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  40595. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40596. X509_free(x509);
  40597. #endif /* OPENSSL_ALL */
  40598. return EXPECT_RESULT();
  40599. }
  40600. static int test_wolfSSL_X509_get_ext(void)
  40601. {
  40602. EXPECT_DECLS;
  40603. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40604. int ret = 0;
  40605. XFILE f = XBADFILE;
  40606. WOLFSSL_X509* x509 = NULL;
  40607. WOLFSSL_X509_EXTENSION* foundExtension;
  40608. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40609. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40610. if (f != XBADFILE)
  40611. XFCLOSE(f);
  40612. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40613. /* wolfSSL_X509_get_ext() valid input */
  40614. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40615. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  40616. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40617. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40618. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  40619. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40620. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40621. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  40622. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40623. wolfSSL_X509_free(x509);
  40624. #endif
  40625. return EXPECT_RESULT();
  40626. }
  40627. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40628. {
  40629. EXPECT_DECLS;
  40630. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40631. int rc;
  40632. XFILE f = XBADFILE;
  40633. WOLFSSL_X509* x509 = NULL;
  40634. ASN1_OBJECT* obj = NULL;
  40635. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40636. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40637. if (f != XBADFILE)
  40638. XFCLOSE(f);
  40639. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40640. -1), 0);
  40641. /* Start search from last location (should fail) */
  40642. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40643. rc), -1);
  40644. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  40645. -2), -1);
  40646. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  40647. -1), -1);
  40648. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  40649. /* NID_ext_key_usage, check also its nid and oid */
  40650. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  40651. -1);
  40652. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  40653. x509, rc)));
  40654. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  40655. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40656. wolfSSL_X509_free(x509);
  40657. #endif
  40658. return EXPECT_RESULT();
  40659. }
  40660. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40661. {
  40662. EXPECT_DECLS;
  40663. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40664. int rc;
  40665. XFILE f = XBADFILE;
  40666. WOLFSSL_X509* x509 = NULL;
  40667. WOLFSSL_X509_EXTENSION* ext;
  40668. WOLFSSL_ASN1_STRING* sanString;
  40669. byte* sanDer;
  40670. const byte expectedDer[] = {
  40671. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  40672. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  40673. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40674. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40675. if (f != XBADFILE)
  40676. XFCLOSE(f);
  40677. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  40678. ExpectNotNull(ext = X509_get_ext(x509, rc));
  40679. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  40680. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40681. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  40682. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40683. X509_free(x509);
  40684. #endif
  40685. return EXPECT_RESULT();
  40686. }
  40687. static int test_wolfSSL_X509_EXTENSION_new(void)
  40688. {
  40689. EXPECT_DECLS;
  40690. #if defined (OPENSSL_ALL)
  40691. WOLFSSL_X509_EXTENSION* ext = NULL;
  40692. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40693. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40694. wolfSSL_X509_EXTENSION_free(ext);
  40695. #endif
  40696. return EXPECT_RESULT();
  40697. }
  40698. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40699. {
  40700. EXPECT_DECLS;
  40701. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40702. WOLFSSL_X509* x509 = NULL;
  40703. WOLFSSL_X509_EXTENSION* ext = NULL;
  40704. WOLFSSL_ASN1_OBJECT* o = NULL;
  40705. XFILE file = XBADFILE;
  40706. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40707. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40708. if (file != XBADFILE)
  40709. XFCLOSE(file);
  40710. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  40711. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40712. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  40713. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40714. ExpectIntEQ(o->nid, 128);
  40715. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  40716. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40717. wolfSSL_X509_free(x509);
  40718. #endif
  40719. return EXPECT_RESULT();
  40720. }
  40721. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40722. {
  40723. EXPECT_DECLS;
  40724. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40725. WOLFSSL_X509* x509 = NULL;
  40726. WOLFSSL_X509_EXTENSION* ext;
  40727. WOLFSSL_ASN1_STRING* str;
  40728. XFILE file = XBADFILE;
  40729. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40730. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40731. if (file != XBADFILE)
  40732. XFCLOSE(file);
  40733. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40734. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  40735. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40736. wolfSSL_X509_free(x509);
  40737. #endif
  40738. return EXPECT_RESULT();
  40739. }
  40740. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40741. {
  40742. EXPECT_DECLS;
  40743. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40744. WOLFSSL_X509* x509 = NULL;
  40745. WOLFSSL_X509_EXTENSION* ext;
  40746. XFILE file = XBADFILE;
  40747. int crit;
  40748. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40749. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40750. if (file != XBADFILE)
  40751. XFCLOSE(file);
  40752. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40753. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  40754. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  40755. wolfSSL_X509_free(x509);
  40756. #endif
  40757. return EXPECT_RESULT();
  40758. }
  40759. static int test_wolfSSL_X509V3_EXT_print(void)
  40760. {
  40761. EXPECT_DECLS;
  40762. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40763. !defined(NO_RSA)
  40764. {
  40765. XFILE f = XBADFILE;
  40766. WOLFSSL_X509* x509 = NULL;
  40767. X509_EXTENSION * ext = NULL;
  40768. int loc;
  40769. BIO *bio = NULL;
  40770. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40771. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40772. if (f != XBADFILE)
  40773. fclose(f);
  40774. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40775. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40776. NID_basic_constraints, -1), -1);
  40777. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40778. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40779. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40780. NID_subject_key_identifier, -1), -1);
  40781. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40782. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40783. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  40784. NID_authority_key_identifier, -1), -1);
  40785. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40786. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40787. wolfSSL_BIO_free(bio);
  40788. wolfSSL_X509_free(x509);
  40789. }
  40790. {
  40791. X509 *x509 = NULL;
  40792. BIO *bio = NULL;
  40793. X509_EXTENSION *ext;
  40794. unsigned int i;
  40795. unsigned int idx;
  40796. /* Some NIDs to test with */
  40797. int nids[] = {
  40798. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40799. * string, which messes up X509V3_EXT_print */
  40800. /* NID_ext_key_usage, */
  40801. NID_subject_alt_name,
  40802. };
  40803. int* n;
  40804. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  40805. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40806. WOLFSSL_FILETYPE_PEM));
  40807. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  40808. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40809. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40810. * update the index */
  40811. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40812. ExpectNotNull(ext = X509_get_ext(x509, idx));
  40813. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40814. ExpectIntGT(fprintf(stderr, "\n"), 0);
  40815. }
  40816. BIO_free(bio);
  40817. X509_free(x509);
  40818. }
  40819. #endif
  40820. return EXPECT_RESULT();
  40821. }
  40822. static int test_wolfSSL_X509_cmp(void)
  40823. {
  40824. EXPECT_DECLS;
  40825. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40826. XFILE file1 = XBADFILE;
  40827. XFILE file2 = XBADFILE;
  40828. WOLFSSL_X509* cert1 = NULL;
  40829. WOLFSSL_X509* cert2 = NULL;
  40830. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  40831. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  40832. XBADFILE);
  40833. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40834. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40835. if (file1 != XBADFILE)
  40836. fclose(file1);
  40837. if (file2 != XBADFILE)
  40838. fclose(file2);
  40839. /* wolfSSL_X509_cmp() testing matching certs */
  40840. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40841. /* wolfSSL_X509_cmp() testing mismatched certs */
  40842. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40843. /* wolfSSL_X509_cmp() testing NULL, valid args */
  40844. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40845. /* wolfSSL_X509_cmp() testing valid, NULL args */
  40846. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40847. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  40848. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40849. wolfSSL_X509_free(cert1);
  40850. wolfSSL_X509_free(cert2);
  40851. #endif
  40852. return EXPECT_RESULT();
  40853. }
  40854. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  40855. {
  40856. EXPECT_DECLS;
  40857. #if defined(OPENSSL_ALL)
  40858. EVP_PKEY* pkey;
  40859. pkey = EVP_PKEY_new();
  40860. ExpectNotNull(pkey);
  40861. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40862. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40863. EVP_PKEY_free(pkey);
  40864. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40865. EVP_PKEY_free(pkey);
  40866. EVP_PKEY_free(pkey);
  40867. #endif
  40868. return EXPECT_RESULT();
  40869. }
  40870. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40871. {
  40872. EXPECT_DECLS;
  40873. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40874. EVP_PKEY* pkey = NULL;
  40875. const unsigned char* p;
  40876. unsigned char *der = NULL;
  40877. unsigned char *tmp = NULL;
  40878. int derLen;
  40879. p = client_keypub_der_2048;
  40880. /* Check that key can be successfully decoded. */
  40881. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40882. sizeof_client_keypub_der_2048));
  40883. /* Check that key can be successfully encoded. */
  40884. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40885. /* Ensure that the encoded version matches the original. */
  40886. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  40887. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40888. /* Do same test except with pre-allocated buffer to ensure the der pointer
  40889. * is advanced. */
  40890. tmp = der;
  40891. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  40892. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  40893. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40894. ExpectTrue(der + derLen == tmp);
  40895. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40896. EVP_PKEY_free(pkey);
  40897. #endif
  40898. return EXPECT_RESULT();
  40899. }
  40900. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  40901. {
  40902. EXPECT_DECLS;
  40903. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  40904. !defined(NO_ASN) && !defined(NO_PWDBASED)
  40905. EVP_PKEY* pkey = NULL;
  40906. const unsigned char* p;
  40907. unsigned char *der = NULL;
  40908. unsigned char *tmp = NULL;
  40909. int derLen;
  40910. unsigned char pub_buf[65];
  40911. const int pub_len = 65;
  40912. BN_CTX* ctx;
  40913. EC_GROUP* curve = NULL;
  40914. EC_KEY* ephemeral_key = NULL;
  40915. const EC_POINT* h;
  40916. /* Generate an x963 key pair and get public part into pub_buf */
  40917. ExpectNotNull(ctx = BN_CTX_new());
  40918. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  40919. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  40920. NID_X9_62_prime256v1));
  40921. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  40922. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  40923. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  40924. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  40925. /* Prepare the EVP_PKEY */
  40926. ExpectNotNull(pkey = EVP_PKEY_new());
  40927. p = pub_buf;
  40928. /* Check that key can be successfully decoded. */
  40929. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  40930. pub_len));
  40931. /* Check that key can be successfully encoded. */
  40932. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40933. /* Ensure that the encoded version matches the original. */
  40934. ExpectIntEQ(derLen, pub_len);
  40935. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  40936. /* Do same test except with pre-allocated buffer to ensure the der pointer
  40937. * is advanced. */
  40938. tmp = der;
  40939. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  40940. ExpectIntEQ(derLen, pub_len);
  40941. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  40942. ExpectTrue(der + derLen == tmp);
  40943. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40944. EVP_PKEY_free(pkey);
  40945. EC_KEY_free(ephemeral_key);
  40946. EC_GROUP_free(curve);
  40947. #endif
  40948. return EXPECT_RESULT();
  40949. }
  40950. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40951. {
  40952. EXPECT_DECLS;
  40953. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40954. DSA* dsa = NULL;
  40955. byte derIn[] = {
  40956. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  40957. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  40958. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  40959. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  40960. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  40961. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  40962. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  40963. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  40964. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  40965. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  40966. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  40967. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  40968. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  40969. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  40970. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  40971. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  40972. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  40973. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  40974. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  40975. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  40976. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  40977. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  40978. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  40979. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  40980. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  40981. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  40982. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  40983. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  40984. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  40985. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  40986. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  40987. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  40988. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  40989. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  40990. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  40991. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  40992. 0x1e, 0xde, 0xc4
  40993. };
  40994. int derInLen = sizeof(derIn);
  40995. byte* derOut = NULL;
  40996. int derOutLen;
  40997. byte* p = derIn;
  40998. /* Check that params can be successfully decoded. */
  40999. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  41000. /* Check that params can be successfully encoded. */
  41001. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  41002. /* Ensure that the encoded version matches the original. */
  41003. ExpectIntEQ(derInLen, derOutLen);
  41004. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  41005. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  41006. DSA_free(dsa);
  41007. #endif
  41008. return EXPECT_RESULT();
  41009. }
  41010. static int test_wolfSSL_i2d_PrivateKey(void)
  41011. {
  41012. EXPECT_DECLS;
  41013. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  41014. !defined(NO_ASN) && !defined(NO_PWDBASED)
  41015. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41016. {
  41017. EVP_PKEY* pkey = NULL;
  41018. const unsigned char* server_key =
  41019. (const unsigned char*)server_key_der_2048;
  41020. unsigned char buf[FOURK_BUF];
  41021. unsigned char* pt = NULL;
  41022. int bufSz;
  41023. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  41024. (long)sizeof_server_key_der_2048));
  41025. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  41026. pt = buf;
  41027. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  41028. ExpectIntNE((pt - buf), 0);
  41029. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  41030. EVP_PKEY_free(pkey);
  41031. }
  41032. #endif
  41033. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41034. {
  41035. EVP_PKEY* pkey = NULL;
  41036. const unsigned char* client_key =
  41037. (const unsigned char*)ecc_clikey_der_256;
  41038. unsigned char buf[FOURK_BUF];
  41039. unsigned char* pt = NULL;
  41040. int bufSz;
  41041. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  41042. (long)sizeof_ecc_clikey_der_256)));
  41043. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  41044. pt = buf;
  41045. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  41046. ExpectIntNE((pt - buf), 0);
  41047. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  41048. EVP_PKEY_free(pkey);
  41049. }
  41050. #endif
  41051. #endif
  41052. return EXPECT_RESULT();
  41053. }
  41054. static int test_wolfSSL_OCSP_id_get0_info(void)
  41055. {
  41056. EXPECT_DECLS;
  41057. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  41058. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41059. X509* cert = NULL;
  41060. X509* issuer = NULL;
  41061. OCSP_CERTID* id = NULL;
  41062. OCSP_CERTID* id2 = NULL;
  41063. ASN1_STRING* name = NULL;
  41064. ASN1_OBJECT* pmd = NULL;
  41065. ASN1_STRING* keyHash = NULL;
  41066. ASN1_INTEGER* serial = NULL;
  41067. ASN1_INTEGER* x509Int;
  41068. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  41069. SSL_FILETYPE_PEM));
  41070. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  41071. SSL_FILETYPE_PEM));
  41072. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  41073. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  41074. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  41075. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  41076. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  41077. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  41078. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  41079. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  41080. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  41081. ExpectNotNull(serial);
  41082. /* compare serial number to one in cert, should be equal */
  41083. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  41084. ExpectIntEQ(x509Int->length, serial->length);
  41085. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  41086. /* test OCSP_id_cmp */
  41087. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  41088. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  41089. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  41090. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  41091. if (id != NULL) {
  41092. id->issuerHash[0] = ~id->issuerHash[0];
  41093. }
  41094. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  41095. OCSP_CERTID_free(id);
  41096. OCSP_CERTID_free(id2);
  41097. X509_free(cert); /* free's x509Int */
  41098. X509_free(issuer);
  41099. #endif
  41100. return EXPECT_RESULT();
  41101. }
  41102. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  41103. {
  41104. EXPECT_DECLS;
  41105. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41106. WOLFSSL_OCSP_CERTID certId;
  41107. byte* targetBuffer = NULL;
  41108. byte* p;
  41109. /* OCSP CertID bytes taken from PCAP */
  41110. byte rawCertId[] = {
  41111. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41112. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41113. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41114. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41115. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41116. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41117. 0xcf, 0xbc, 0x91
  41118. };
  41119. int ret = 0;
  41120. int i;
  41121. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  41122. certId.rawCertId = rawCertId;
  41123. certId.rawCertIdSize = sizeof(rawCertId);
  41124. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  41125. DYNAMIC_TYPE_TMP_BUFFER));
  41126. p = targetBuffer;
  41127. /* Function returns the size of the encoded data. */
  41128. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  41129. /* If target buffer is not null, function increments targetBuffer to point
  41130. * just past the end of the encoded data. */
  41131. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  41132. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  41133. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  41134. }
  41135. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41136. targetBuffer = NULL;
  41137. /* If target buffer is null, function allocates memory for a buffer and
  41138. * copies the encoded data into it. targetBuffer then points to the start of
  41139. * this newly allocate buffer. */
  41140. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  41141. sizeof(rawCertId));
  41142. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  41143. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  41144. }
  41145. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  41146. #endif
  41147. return EXPECT_RESULT();
  41148. }
  41149. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  41150. {
  41151. EXPECT_DECLS;
  41152. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  41153. WOLFSSL_OCSP_CERTID* certId;
  41154. WOLFSSL_OCSP_CERTID* certIdGood;
  41155. WOLFSSL_OCSP_CERTID* certIdBad;
  41156. const unsigned char* rawCertIdPtr;
  41157. const unsigned char rawCertId[] = {
  41158. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  41159. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  41160. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  41161. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  41162. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  41163. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  41164. 0xcf, 0xbc, 0x91
  41165. };
  41166. rawCertIdPtr = &rawCertId[0];
  41167. /* If the cert ID is NULL the function should allocate it and copy the
  41168. * data to it. */
  41169. certId = NULL;
  41170. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  41171. sizeof(rawCertId)));
  41172. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41173. if (certId != NULL) {
  41174. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41175. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  41176. }
  41177. /* If the cert ID is not NULL the function will just copy the data to it. */
  41178. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  41179. DYNAMIC_TYPE_TMP_BUFFER));
  41180. ExpectNotNull(certId);
  41181. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  41182. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  41183. rawCertIdPtr = &rawCertId[0];
  41184. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  41185. sizeof(rawCertId)));
  41186. ExpectPtrEq(certIdGood, certId);
  41187. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  41188. if (certId != NULL) {
  41189. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  41190. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41191. certId = NULL;
  41192. }
  41193. /* The below tests should fail when passed bad parameters. NULL should
  41194. * always be returned. */
  41195. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  41196. sizeof(rawCertId)));
  41197. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  41198. sizeof(rawCertId)));
  41199. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  41200. #endif
  41201. return EXPECT_RESULT();
  41202. }
  41203. static int test_wolfSSL_OCSP_id_cmp(void)
  41204. {
  41205. EXPECT_DECLS;
  41206. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41207. OCSP_CERTID id1;
  41208. OCSP_CERTID id2;
  41209. XMEMSET(&id1, 0, sizeof(id1));
  41210. XMEMSET(&id2, 0, sizeof(id2));
  41211. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  41212. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  41213. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  41214. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  41215. #endif
  41216. return EXPECT_RESULT();
  41217. }
  41218. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  41219. {
  41220. EXPECT_DECLS;
  41221. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41222. WOLFSSL_OCSP_SINGLERESP single;
  41223. const WOLFSSL_OCSP_CERTID* certId;
  41224. XMEMSET(&single, 0, sizeof(single));
  41225. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  41226. ExpectPtrEq(&single, certId);
  41227. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  41228. #endif
  41229. return EXPECT_RESULT();
  41230. }
  41231. static int test_wolfSSL_OCSP_single_get0_status(void)
  41232. {
  41233. EXPECT_DECLS;
  41234. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41235. WOLFSSL_OCSP_SINGLERESP single;
  41236. CertStatus certStatus;
  41237. WOLFSSL_ASN1_TIME* thisDate;
  41238. WOLFSSL_ASN1_TIME* nextDate;
  41239. int ret, i;
  41240. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41241. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  41242. /* Fill the date fields with some dummy data. */
  41243. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  41244. certStatus.thisDateParsed.data[i] = i;
  41245. certStatus.nextDateParsed.data[i] = i;
  41246. }
  41247. certStatus.status = CERT_GOOD;
  41248. single.status = &certStatus;
  41249. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  41250. &nextDate);
  41251. ExpectIntEQ(ret, CERT_GOOD);
  41252. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  41253. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  41254. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  41255. CERT_GOOD);
  41256. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  41257. NULL), CERT_GOOD);
  41258. #endif
  41259. return EXPECT_RESULT();
  41260. }
  41261. static int test_wolfSSL_OCSP_resp_count(void)
  41262. {
  41263. EXPECT_DECLS;
  41264. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41265. WOLFSSL_OCSP_BASICRESP basicResp;
  41266. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41267. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41268. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41269. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41270. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41271. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  41272. basicResp.single = &singleRespOne;
  41273. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  41274. singleRespOne.next = &singleRespTwo;
  41275. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  41276. #endif
  41277. return EXPECT_RESULT();
  41278. }
  41279. static int test_wolfSSL_OCSP_resp_get0(void)
  41280. {
  41281. EXPECT_DECLS;
  41282. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  41283. WOLFSSL_OCSP_BASICRESP basicResp;
  41284. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  41285. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  41286. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  41287. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41288. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  41289. basicResp.single = &singleRespOne;
  41290. singleRespOne.next = &singleRespTwo;
  41291. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  41292. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  41293. #endif
  41294. return EXPECT_RESULT();
  41295. }
  41296. static int test_wolfSSL_EVP_PKEY_derive(void)
  41297. {
  41298. EXPECT_DECLS;
  41299. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41300. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41301. EVP_PKEY_CTX *ctx = NULL;
  41302. unsigned char *skey = NULL;
  41303. size_t skeylen;
  41304. EVP_PKEY *pkey = NULL;
  41305. EVP_PKEY *peerkey = NULL;
  41306. const unsigned char* key;
  41307. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41308. /* DH */
  41309. key = dh_key_der_2048;
  41310. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41311. sizeof_dh_key_der_2048)));
  41312. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41313. key = dh_key_der_2048;
  41314. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41315. sizeof_dh_key_der_2048)));
  41316. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41317. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41318. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41319. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41320. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41321. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  41322. DYNAMIC_TYPE_OPENSSL));
  41323. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41324. EVP_PKEY_CTX_free(ctx);
  41325. ctx = NULL;
  41326. EVP_PKEY_free(peerkey);
  41327. peerkey = NULL;
  41328. EVP_PKEY_free(pkey);
  41329. pkey = NULL;
  41330. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41331. skey = NULL;
  41332. #endif
  41333. #ifdef HAVE_ECC
  41334. /* ECDH */
  41335. key = ecc_clikey_der_256;
  41336. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41337. sizeof_ecc_clikey_der_256)));
  41338. key = ecc_clikeypub_der_256;
  41339. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41340. sizeof_ecc_clikeypub_der_256)));
  41341. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41342. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41343. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41344. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41345. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  41346. DYNAMIC_TYPE_OPENSSL));
  41347. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41348. EVP_PKEY_CTX_free(ctx);
  41349. EVP_PKEY_free(peerkey);
  41350. EVP_PKEY_free(pkey);
  41351. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41352. #endif /* HAVE_ECC */
  41353. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41354. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41355. return EXPECT_RESULT();
  41356. }
  41357. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41358. {
  41359. EXPECT_DECLS;
  41360. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41361. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41362. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41363. int ret;
  41364. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41365. int pwdlen = sizeof(pwd);
  41366. const byte salt[] = {'N','a','C','l'};
  41367. int saltlen = sizeof(salt);
  41368. byte key[80];
  41369. word64 numOvr32 = (word64)INT32_MAX + 1;
  41370. /* expected derived key for N:16, r:1, p:1 */
  41371. const byte expectedKey[] = {
  41372. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41373. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41374. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41375. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41376. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41377. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41378. 0xE7, 0xE9, 0xC0, 0x9A};
  41379. /* N r p mx key keylen */
  41380. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41381. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  41382. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41383. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  41384. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41385. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  41386. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41387. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  41388. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41389. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  41390. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41391. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  41392. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41393. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  41394. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41395. ExpectIntEQ(ret, 1); /* should succeed */
  41396. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41397. key, 64);
  41398. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41399. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41400. key, 64);
  41401. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41402. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41403. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41404. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41405. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41406. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41407. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41408. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41409. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41410. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41411. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41412. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41413. ExpectIntEQ(ret, 1);
  41414. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41415. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41416. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41417. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41418. return EXPECT_RESULT();
  41419. }
  41420. static int test_no_op_functions(void)
  41421. {
  41422. EXPECT_DECLS;
  41423. #if defined(OPENSSL_EXTRA)
  41424. /* this makes sure wolfSSL can compile and run these no-op functions */
  41425. SSL_load_error_strings();
  41426. ENGINE_load_builtin_engines();
  41427. OpenSSL_add_all_ciphers();
  41428. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  41429. #endif
  41430. return EXPECT_RESULT();
  41431. }
  41432. static int test_wolfSSL_CRYPTO_memcmp(void)
  41433. {
  41434. EXPECT_DECLS;
  41435. #ifdef OPENSSL_EXTRA
  41436. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41437. "implementation of TLS/SSL for embedded devices to the cloud.";
  41438. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41439. "implementation of TLS/SSL for embedded devices to the cloud.";
  41440. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41441. "implementation of TLS/SSL for embedded devices to the cloud!";
  41442. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41443. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41444. #endif
  41445. return EXPECT_RESULT();
  41446. }
  41447. /*----------------------------------------------------------------------------*
  41448. | wolfCrypt ASN
  41449. *----------------------------------------------------------------------------*/
  41450. static int test_wc_CreateEncryptedPKCS8Key(void)
  41451. {
  41452. EXPECT_DECLS;
  41453. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41454. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41455. WC_RNG rng;
  41456. byte* encKey = NULL;
  41457. word32 encKeySz = 0;
  41458. word32 decKeySz = 0;
  41459. const char password[] = "Lorem ipsum dolor sit amet";
  41460. word32 passwordSz = (word32)XSTRLEN(password);
  41461. word32 tradIdx = 0;
  41462. XMEMSET(&rng, 0, sizeof(WC_RNG));
  41463. ExpectIntEQ(wc_InitRng(&rng), 0);
  41464. /* Call with NULL for out buffer to get necessary length. */
  41465. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41466. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41467. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41468. LENGTH_ONLY_E);
  41469. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41470. DYNAMIC_TYPE_TMP_BUFFER));
  41471. /* Call with the allocated out buffer. */
  41472. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41473. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41474. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41475. 0);
  41476. /* Decrypt the encrypted PKCS8 key we just made. */
  41477. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41478. passwordSz)), 0);
  41479. /* encKey now holds the decrypted key (decrypted in place). */
  41480. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41481. /* Check that the decrypted key matches the key prior to encryption. */
  41482. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41483. sizeof_server_key_der_2048), 0);
  41484. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41485. wc_FreeRng(&rng);
  41486. #endif
  41487. return EXPECT_RESULT();
  41488. }
  41489. static int test_wc_GetPkcs8TraditionalOffset(void)
  41490. {
  41491. EXPECT_DECLS;
  41492. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41493. int length;
  41494. int derSz = 0;
  41495. word32 inOutIdx;
  41496. const char* path = "./certs/server-keyPkcs8.der";
  41497. XFILE file = XBADFILE;
  41498. byte der[2048];
  41499. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  41500. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  41501. if (file != XBADFILE)
  41502. XFCLOSE(file);
  41503. /* valid case */
  41504. inOutIdx = 0;
  41505. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41506. 0);
  41507. /* inOutIdx > sz */
  41508. inOutIdx = 4000;
  41509. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41510. BAD_FUNC_ARG);
  41511. /* null input */
  41512. inOutIdx = 0;
  41513. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  41514. BAD_FUNC_ARG);
  41515. /* invalid input, fill buffer with 1's */
  41516. XMEMSET(der, 1, sizeof(der));
  41517. inOutIdx = 0;
  41518. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  41519. ASN_PARSE_E);
  41520. #endif /* NO_ASN */
  41521. return EXPECT_RESULT();
  41522. }
  41523. static int test_wc_SetSubjectRaw(void)
  41524. {
  41525. EXPECT_DECLS;
  41526. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41527. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41528. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41529. WOLFSSL_X509* x509 = NULL;
  41530. int peerCertSz;
  41531. const byte* peerCertBuf = NULL;
  41532. Cert forgedCert;
  41533. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41534. WOLFSSL_FILETYPE_ASN1));
  41535. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41536. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41537. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41538. wolfSSL_FreeX509(x509);
  41539. #endif
  41540. return EXPECT_RESULT();
  41541. }
  41542. static int test_wc_GetSubjectRaw(void)
  41543. {
  41544. EXPECT_DECLS;
  41545. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41546. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41547. Cert cert;
  41548. byte *subjectRaw;
  41549. ExpectIntEQ(0, wc_InitCert(&cert));
  41550. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41551. #endif
  41552. return EXPECT_RESULT();
  41553. }
  41554. static int test_wc_SetIssuerRaw(void)
  41555. {
  41556. EXPECT_DECLS;
  41557. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41558. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41559. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41560. WOLFSSL_X509* x509 = NULL;
  41561. int peerCertSz;
  41562. const byte* peerCertBuf;
  41563. Cert forgedCert;
  41564. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41565. WOLFSSL_FILETYPE_ASN1));
  41566. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41567. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41568. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41569. wolfSSL_FreeX509(x509);
  41570. #endif
  41571. return EXPECT_RESULT();
  41572. }
  41573. static int test_wc_SetIssueBuffer(void)
  41574. {
  41575. EXPECT_DECLS;
  41576. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41577. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41578. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41579. WOLFSSL_X509* x509 = NULL;
  41580. int peerCertSz;
  41581. const byte* peerCertBuf;
  41582. Cert forgedCert;
  41583. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  41584. WOLFSSL_FILETYPE_ASN1));
  41585. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41586. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  41587. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41588. wolfSSL_FreeX509(x509);
  41589. #endif
  41590. return EXPECT_RESULT();
  41591. }
  41592. /*
  41593. * Testing wc_SetSubjectKeyId
  41594. */
  41595. static int test_wc_SetSubjectKeyId(void)
  41596. {
  41597. EXPECT_DECLS;
  41598. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41599. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41600. Cert cert;
  41601. const char* file = "certs/ecc-client-keyPub.pem";
  41602. ExpectIntEQ(0, wc_InitCert(&cert));
  41603. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41604. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41605. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41606. #endif
  41607. return EXPECT_RESULT();
  41608. } /* END test_wc_SetSubjectKeyId */
  41609. /*
  41610. * Testing wc_SetSubject
  41611. */
  41612. static int test_wc_SetSubject(void)
  41613. {
  41614. EXPECT_DECLS;
  41615. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41616. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41617. Cert cert;
  41618. const char* file = "./certs/ca-ecc-cert.pem";
  41619. ExpectIntEQ(0, wc_InitCert(&cert));
  41620. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  41621. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41622. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41623. #endif
  41624. return EXPECT_RESULT();
  41625. } /* END test_wc_SetSubject */
  41626. static int test_CheckCertSignature(void)
  41627. {
  41628. EXPECT_DECLS;
  41629. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41630. WOLFSSL_CERT_MANAGER* cm = NULL;
  41631. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41632. XFILE fp = XBADFILE;
  41633. byte cert[4096];
  41634. int certSz;
  41635. #endif
  41636. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41637. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41638. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41639. #ifndef NO_RSA
  41640. #ifdef USE_CERT_BUFFERS_1024
  41641. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41642. sizeof_server_cert_der_1024, NULL, cm));
  41643. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41644. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41645. WOLFSSL_FILETYPE_ASN1));
  41646. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41647. sizeof_server_cert_der_1024, NULL, cm));
  41648. #elif defined(USE_CERT_BUFFERS_2048)
  41649. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41650. sizeof_server_cert_der_2048, NULL, cm));
  41651. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41652. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41653. WOLFSSL_FILETYPE_ASN1));
  41654. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41655. sizeof_server_cert_der_2048, NULL, cm));
  41656. #endif
  41657. #endif
  41658. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41659. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41660. sizeof_serv_ecc_der_256, NULL, cm));
  41661. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41662. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41663. WOLFSSL_FILETYPE_ASN1));
  41664. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41665. NULL, cm));
  41666. #endif
  41667. #if !defined(NO_FILESYSTEM)
  41668. wolfSSL_CertManagerFree(cm);
  41669. cm = NULL;
  41670. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41671. #ifndef NO_RSA
  41672. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  41673. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41674. if (fp != XBADFILE) {
  41675. XFCLOSE(fp);
  41676. fp = XBADFILE;
  41677. }
  41678. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41679. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41680. "./certs/ca-cert.pem", NULL));
  41681. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41682. #endif
  41683. #ifdef HAVE_ECC
  41684. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  41685. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41686. if (fp != XBADFILE) {
  41687. XFCLOSE(fp);
  41688. fp = XBADFILE;
  41689. }
  41690. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41691. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41692. "./certs/ca-ecc-cert.pem", NULL));
  41693. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41694. #endif
  41695. #endif
  41696. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41697. (void)fp;
  41698. (void)cert;
  41699. (void)certSz;
  41700. #endif
  41701. wolfSSL_CertManagerFree(cm);
  41702. #endif
  41703. return EXPECT_RESULT();
  41704. }
  41705. static int test_wc_ParseCert(void)
  41706. {
  41707. EXPECT_DECLS;
  41708. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41709. DecodedCert decodedCert;
  41710. const byte* rawCert = client_cert_der_2048;
  41711. const int rawCertSize = sizeof_client_cert_der_2048;
  41712. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41713. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41714. #ifndef IGNORE_NAME_CONSTRAINTS
  41715. /* check that the subjects emailAddress was not put in the alt name list */
  41716. ExpectNotNull(decodedCert.subjectEmail);
  41717. ExpectNull(decodedCert.altEmailNames);
  41718. #endif
  41719. wc_FreeDecodedCert(&decodedCert);
  41720. #endif
  41721. return EXPECT_RESULT();
  41722. }
  41723. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  41724. * the API safely errors out on badly-formed ASN input.
  41725. * NOTE: Test not compatible with released FIPS implementations!
  41726. */
  41727. static int test_wc_ParseCert_Error(void)
  41728. {
  41729. EXPECT_DECLS;
  41730. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  41731. (!defined(HAVE_FIPS) || \
  41732. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  41733. DecodedCert decodedCert;
  41734. int i;
  41735. /* Certificate data */
  41736. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  41737. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41738. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  41739. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  41740. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  41741. /* Test data */
  41742. const struct testStruct {
  41743. const byte* c;
  41744. const int cSz;
  41745. const int expRet;
  41746. } t[] = {
  41747. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  41748. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  41749. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  41750. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  41751. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  41752. };
  41753. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  41754. for (i = 0; i < tSz; i++) {
  41755. WOLFSSL_MSG_EX("i == %d", i);
  41756. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  41757. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  41758. wc_FreeDecodedCert(&decodedCert);
  41759. }
  41760. #endif
  41761. return EXPECT_RESULT();
  41762. }
  41763. static int test_MakeCertWithPathLen(void)
  41764. {
  41765. EXPECT_DECLS;
  41766. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  41767. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41768. const byte expectedPathLen = 7;
  41769. Cert cert;
  41770. DecodedCert decodedCert;
  41771. byte der[FOURK_BUF];
  41772. int derSize = 0;
  41773. WC_RNG rng;
  41774. ecc_key key;
  41775. int ret;
  41776. XMEMSET(&rng, 0, sizeof(WC_RNG));
  41777. XMEMSET(&key, 0, sizeof(ecc_key));
  41778. XMEMSET(&cert, 0, sizeof(Cert));
  41779. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  41780. ExpectIntEQ(wc_InitRng(&rng), 0);
  41781. ExpectIntEQ(wc_ecc_init(&key), 0);
  41782. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41783. ExpectIntEQ(wc_InitCert(&cert), 0);
  41784. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41785. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41786. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41787. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41788. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41789. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  41790. CTC_NAME_SIZE);
  41791. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  41792. CTC_NAME_SIZE);
  41793. cert.selfSigned = 1;
  41794. cert.isCA = 1;
  41795. cert.pathLen = expectedPathLen;
  41796. cert.pathLenSet = 1;
  41797. cert.sigType = CTC_SHA256wECDSA;
  41798. #ifdef WOLFSSL_CERT_EXT
  41799. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41800. #endif
  41801. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41802. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  41803. FOURK_BUF, NULL, &key, &rng), 0);
  41804. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41805. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41806. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  41807. wc_FreeDecodedCert(&decodedCert);
  41808. ret = wc_ecc_free(&key);
  41809. ExpectIntEQ(ret, 0);
  41810. ret = wc_FreeRng(&rng);
  41811. ExpectIntEQ(ret, 0);
  41812. #endif
  41813. return EXPECT_RESULT();
  41814. }
  41815. /*----------------------------------------------------------------------------*
  41816. | wolfCrypt ECC
  41817. *----------------------------------------------------------------------------*/
  41818. static int test_wc_ecc_get_curve_size_from_name(void)
  41819. {
  41820. EXPECT_DECLS;
  41821. #ifdef HAVE_ECC
  41822. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41823. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  41824. #endif
  41825. /* invalid case */
  41826. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  41827. /* NULL input */
  41828. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  41829. #endif /* HAVE_ECC */
  41830. return EXPECT_RESULT();
  41831. }
  41832. static int test_wc_ecc_get_curve_id_from_name(void)
  41833. {
  41834. EXPECT_DECLS;
  41835. #ifdef HAVE_ECC
  41836. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41837. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  41838. ECC_SECP256R1);
  41839. #endif
  41840. /* invalid case */
  41841. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  41842. /* NULL input */
  41843. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  41844. #endif /* HAVE_ECC */
  41845. return EXPECT_RESULT();
  41846. }
  41847. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41848. !defined(HAVE_SELFTEST) && \
  41849. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41850. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  41851. {
  41852. EXPECT_DECLS;
  41853. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41854. ecc_key* key;
  41855. const ecc_set_type* params = NULL;
  41856. int ret;
  41857. #endif
  41858. WOLFSSL_EC_KEY *ecKey = NULL;
  41859. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41860. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  41861. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  41862. if (EXPECT_SUCCESS()) {
  41863. ret = EC_KEY_generate_key(ecKey);
  41864. } else
  41865. ret = 0;
  41866. if (ret == 1) {
  41867. /* normal test */
  41868. key = (ecc_key*)ecKey->internal;
  41869. if (key != NULL) {
  41870. params = key->dp;
  41871. }
  41872. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  41873. ECC_SECP256R1);
  41874. }
  41875. #endif
  41876. /* invalid case, NULL input*/
  41877. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  41878. wolfSSL_EC_KEY_free(ecKey);
  41879. return EXPECT_RESULT();
  41880. }
  41881. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41882. static int test_wc_ecc_get_curve_id_from_params(void)
  41883. {
  41884. EXPECT_DECLS;
  41885. #ifdef HAVE_ECC
  41886. const byte prime[] =
  41887. {
  41888. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41889. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41890. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41891. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  41892. };
  41893. const byte primeInvalid[] =
  41894. {
  41895. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41896. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41897. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41898. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  41899. };
  41900. const byte Af[] =
  41901. {
  41902. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41903. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41904. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41905. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  41906. };
  41907. const byte Bf[] =
  41908. {
  41909. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  41910. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  41911. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  41912. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  41913. };
  41914. const byte order[] =
  41915. {
  41916. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  41917. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41918. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  41919. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  41920. };
  41921. const byte Gx[] =
  41922. {
  41923. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  41924. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  41925. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  41926. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  41927. };
  41928. const byte Gy[] =
  41929. {
  41930. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  41931. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  41932. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  41933. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  41934. };
  41935. int cofactor = 1;
  41936. int fieldSize = 256;
  41937. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41938. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  41939. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  41940. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  41941. ECC_SECP256R1);
  41942. #endif
  41943. /* invalid case, fieldSize = 0 */
  41944. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  41945. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41946. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  41947. /* invalid case, NULL prime */
  41948. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  41949. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41950. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  41951. /* invalid case, invalid prime */
  41952. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  41953. primeInvalid, sizeof(primeInvalid),
  41954. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41955. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  41956. #endif
  41957. return EXPECT_RESULT();
  41958. }
  41959. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  41960. {
  41961. EXPECT_DECLS;
  41962. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41963. !defined(HAVE_FAST_RSA)
  41964. WOLFSSL_RSA* rsa = NULL;
  41965. WOLFSSL_EVP_PKEY* pkey = NULL;
  41966. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41967. const char* in = "What is easy to do is easy not to do.";
  41968. size_t inlen = XSTRLEN(in);
  41969. size_t outEncLen = 0;
  41970. byte* outEnc = NULL;
  41971. byte* outDec = NULL;
  41972. size_t outDecLen = 0;
  41973. size_t rsaKeySz = 2048/8; /* Bytes */
  41974. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41975. byte* inTmp = NULL;
  41976. byte* outEncTmp = NULL;
  41977. byte* outDecTmp = NULL;
  41978. #endif
  41979. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  41980. DYNAMIC_TYPE_TMP_BUFFER));
  41981. if (outEnc != NULL) {
  41982. XMEMSET(outEnc, 0, rsaKeySz);
  41983. }
  41984. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  41985. DYNAMIC_TYPE_TMP_BUFFER));
  41986. if (outDec != NULL) {
  41987. XMEMSET(outDec, 0, rsaKeySz);
  41988. }
  41989. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41990. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41991. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41992. if (EXPECT_FAIL()) {
  41993. RSA_free(rsa);
  41994. }
  41995. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41996. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41997. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41998. WOLFSSL_SUCCESS);
  41999. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  42000. since ctx uses it.*/
  42001. ExpectIntEQ(pkey->ref.count, 2);
  42002. EVP_PKEY_free(pkey);
  42003. ExpectIntEQ(pkey->ref.count, 1);
  42004. /* Encrypt data */
  42005. /* Check that we can get the required output buffer length by passing in a
  42006. * NULL output buffer. */
  42007. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  42008. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42009. ExpectIntEQ(rsaKeySz, outEncLen);
  42010. /* Now do the actual encryption. */
  42011. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  42012. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  42013. /* Decrypt data */
  42014. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42015. /* Check that we can get the required output buffer length by passing in a
  42016. * NULL output buffer. */
  42017. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  42018. WOLFSSL_SUCCESS);
  42019. ExpectIntEQ(rsaKeySz, outDecLen);
  42020. /* Now do the actual decryption. */
  42021. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  42022. WOLFSSL_SUCCESS);
  42023. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  42024. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42025. /* The input length must be the same size as the RSA key.*/
  42026. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42027. DYNAMIC_TYPE_TMP_BUFFER));
  42028. if (inTmp != NULL) {
  42029. XMEMSET(inTmp, 9, rsaKeySz);
  42030. }
  42031. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42032. DYNAMIC_TYPE_TMP_BUFFER));
  42033. if (outEncTmp != NULL) {
  42034. XMEMSET(outEncTmp, 0, rsaKeySz);
  42035. }
  42036. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  42037. DYNAMIC_TYPE_TMP_BUFFER));
  42038. if (outDecTmp != NULL) {
  42039. XMEMSET(outDecTmp, 0, rsaKeySz);
  42040. }
  42041. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  42042. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42043. WOLFSSL_SUCCESS);
  42044. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  42045. WOLFSSL_SUCCESS);
  42046. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  42047. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  42048. outEncLen), WOLFSSL_SUCCESS);
  42049. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  42050. #endif
  42051. EVP_PKEY_CTX_free(ctx);
  42052. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42053. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42054. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  42055. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42056. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42057. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42058. #endif
  42059. #endif
  42060. return EXPECT_RESULT();
  42061. }
  42062. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42063. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42064. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42065. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42066. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42067. #endif
  42068. #endif
  42069. #endif
  42070. #if defined(OPENSSL_EXTRA)
  42071. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42072. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42073. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42074. #endif
  42075. #endif
  42076. #endif
  42077. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42078. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42079. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42080. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42081. #endif
  42082. #endif
  42083. #endif
  42084. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  42085. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  42086. {
  42087. EXPECT_DECLS;
  42088. #if defined(OPENSSL_EXTRA)
  42089. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42090. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42091. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42092. WOLFSSL_RSA* rsa = NULL;
  42093. #endif
  42094. #endif
  42095. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42096. WOLFSSL_DSA* dsa = NULL;
  42097. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42098. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42099. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42100. WOLFSSL_EC_KEY* ecKey = NULL;
  42101. #endif
  42102. #endif
  42103. WOLFSSL_EVP_PKEY* pkey = NULL;
  42104. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  42105. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  42106. const char* in = "What is easy to do is easy not to do.";
  42107. size_t inlen = XSTRLEN(in);
  42108. byte hash[SHA256_DIGEST_LENGTH] = {0};
  42109. byte zero[SHA256_DIGEST_LENGTH] = {0};
  42110. SHA256_CTX c;
  42111. byte* sig = NULL;
  42112. byte* sigVerify = NULL;
  42113. size_t siglen;
  42114. size_t siglenOnlyLen;
  42115. size_t keySz = 2048/8; /* Bytes */
  42116. ExpectNotNull(sig =
  42117. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42118. ExpectNotNull(sigVerify =
  42119. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  42120. siglen = keySz;
  42121. ExpectNotNull(XMEMSET(sig, 0, keySz));
  42122. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  42123. /* Generate hash */
  42124. SHA256_Init(&c);
  42125. SHA256_Update(&c, in, inlen);
  42126. SHA256_Final(hash, &c);
  42127. #ifdef WOLFSSL_SMALL_STACK_CACHE
  42128. /* workaround for small stack cache case */
  42129. wc_Sha256Free((wc_Sha256*)&c);
  42130. #endif
  42131. /* Generate key */
  42132. ExpectNotNull(pkey = EVP_PKEY_new());
  42133. switch (keyType) {
  42134. case EVP_PKEY_RSA:
  42135. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42136. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42137. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42138. {
  42139. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  42140. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42141. }
  42142. #endif
  42143. #endif
  42144. break;
  42145. case EVP_PKEY_DSA:
  42146. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42147. ExpectNotNull(dsa = DSA_new());
  42148. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  42149. NULL, 0, NULL, NULL, NULL), 1);
  42150. ExpectIntEQ(DSA_generate_key(dsa), 1);
  42151. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  42152. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42153. break;
  42154. case EVP_PKEY_EC:
  42155. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42156. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42157. {
  42158. ExpectNotNull(ecKey = EC_KEY_new());
  42159. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  42160. ExpectIntEQ(
  42161. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42162. if (EXPECT_FAIL()) {
  42163. EC_KEY_free(ecKey);
  42164. }
  42165. }
  42166. #endif
  42167. #endif
  42168. break;
  42169. }
  42170. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  42171. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42172. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42173. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42174. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42175. if (keyType == EVP_PKEY_RSA)
  42176. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42177. WOLFSSL_SUCCESS);
  42178. #endif
  42179. #endif
  42180. /* Check returning only length */
  42181. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  42182. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42183. ExpectIntGT(siglenOnlyLen, 0);
  42184. /* Sign data */
  42185. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  42186. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  42187. ExpectIntGE(siglenOnlyLen, siglen);
  42188. /* Verify signature */
  42189. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  42190. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42191. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42192. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42193. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42194. if (keyType == EVP_PKEY_RSA)
  42195. ExpectIntEQ(
  42196. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  42197. WOLFSSL_SUCCESS);
  42198. #endif
  42199. #endif
  42200. ExpectIntEQ(EVP_PKEY_verify(
  42201. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  42202. WOLFSSL_SUCCESS);
  42203. ExpectIntEQ(EVP_PKEY_verify(
  42204. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  42205. WOLFSSL_FAILURE);
  42206. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42207. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42208. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42209. if (keyType == EVP_PKEY_RSA) {
  42210. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  42211. /* Try RSA sign/verify with no padding. */
  42212. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42213. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  42214. WOLFSSL_SUCCESS);
  42215. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42216. siglen), WOLFSSL_SUCCESS);
  42217. ExpectIntGE(siglenOnlyLen, siglen);
  42218. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42219. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42220. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  42221. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42222. siglen), WOLFSSL_SUCCESS);
  42223. #endif
  42224. /* Wrong padding schemes. */
  42225. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42226. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  42227. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42228. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  42229. siglen), WOLFSSL_SUCCESS);
  42230. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  42231. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42232. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  42233. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  42234. siglen), WOLFSSL_SUCCESS);
  42235. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  42236. WOLFSSL_SUCCESS);
  42237. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  42238. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  42239. }
  42240. #endif
  42241. #endif
  42242. /* error cases */
  42243. siglen = keySz; /* Reset because sig size may vary slightly */
  42244. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  42245. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  42246. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  42247. WOLFSSL_SUCCESS);
  42248. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  42249. WOLFSSL_SUCCESS);
  42250. EVP_PKEY_free(pkey);
  42251. pkey = NULL;
  42252. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42253. DSA_free(dsa);
  42254. dsa = NULL;
  42255. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  42256. EVP_PKEY_CTX_free(ctx_verify);
  42257. ctx_verify = NULL;
  42258. EVP_PKEY_CTX_free(ctx);
  42259. ctx = NULL;
  42260. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42261. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  42262. #endif /* OPENSSL_EXTRA */
  42263. return EXPECT_RESULT();
  42264. }
  42265. #endif
  42266. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  42267. {
  42268. EXPECT_DECLS;
  42269. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  42270. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  42271. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42272. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  42273. #endif
  42274. #endif
  42275. return EXPECT_RESULT();
  42276. }
  42277. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  42278. {
  42279. EXPECT_DECLS;
  42280. #if defined(OPENSSL_EXTRA)
  42281. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  42282. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  42283. #endif
  42284. #endif
  42285. return EXPECT_RESULT();
  42286. }
  42287. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  42288. {
  42289. EXPECT_DECLS;
  42290. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42291. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42292. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  42293. #endif
  42294. #endif
  42295. return EXPECT_RESULT();
  42296. }
  42297. static int test_EVP_PKEY_rsa(void)
  42298. {
  42299. EXPECT_DECLS;
  42300. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  42301. WOLFSSL_RSA* rsa = NULL;
  42302. WOLFSSL_EVP_PKEY* pkey = NULL;
  42303. ExpectNotNull(rsa = wolfSSL_RSA_new());
  42304. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42305. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  42306. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  42307. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  42308. if (EXPECT_FAIL()) {
  42309. wolfSSL_RSA_free(rsa);
  42310. }
  42311. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  42312. wolfSSL_EVP_PKEY_free(pkey);
  42313. #endif
  42314. return EXPECT_RESULT();
  42315. }
  42316. static int test_EVP_PKEY_ec(void)
  42317. {
  42318. EXPECT_DECLS;
  42319. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42320. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  42321. WOLFSSL_EC_KEY* ecKey = NULL;
  42322. WOLFSSL_EVP_PKEY* pkey = NULL;
  42323. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  42324. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  42325. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  42326. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  42327. /* Should fail since ecKey is empty */
  42328. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  42329. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  42330. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  42331. if (EXPECT_FAIL()) {
  42332. wolfSSL_EC_KEY_free(ecKey);
  42333. }
  42334. wolfSSL_EVP_PKEY_free(pkey);
  42335. #endif
  42336. #endif
  42337. return EXPECT_RESULT();
  42338. }
  42339. static int test_EVP_PKEY_cmp(void)
  42340. {
  42341. EXPECT_DECLS;
  42342. #if defined(OPENSSL_EXTRA)
  42343. EVP_PKEY *a = NULL;
  42344. EVP_PKEY *b = NULL;
  42345. const unsigned char *in;
  42346. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  42347. in = client_key_der_2048;
  42348. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42349. &in, (long)sizeof_client_key_der_2048));
  42350. in = client_key_der_2048;
  42351. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42352. &in, (long)sizeof_client_key_der_2048));
  42353. /* Test success case RSA */
  42354. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42355. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  42356. #else
  42357. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42358. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42359. EVP_PKEY_free(b);
  42360. b = NULL;
  42361. EVP_PKEY_free(a);
  42362. a = NULL;
  42363. #endif
  42364. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42365. in = ecc_clikey_der_256;
  42366. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42367. &in, (long)sizeof_ecc_clikey_der_256));
  42368. in = ecc_clikey_der_256;
  42369. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42370. &in, (long)sizeof_ecc_clikey_der_256));
  42371. /* Test success case ECC */
  42372. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42373. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  42374. #else
  42375. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42376. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42377. EVP_PKEY_free(b);
  42378. b = NULL;
  42379. EVP_PKEY_free(a);
  42380. a = NULL;
  42381. #endif
  42382. /* Test failure cases */
  42383. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  42384. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  42385. in = client_key_der_2048;
  42386. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42387. &in, (long)sizeof_client_key_der_2048));
  42388. in = ecc_clikey_der_256;
  42389. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42390. &in, (long)sizeof_ecc_clikey_der_256));
  42391. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42392. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  42393. #else
  42394. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  42395. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42396. EVP_PKEY_free(b);
  42397. b = NULL;
  42398. EVP_PKEY_free(a);
  42399. a = NULL;
  42400. #endif
  42401. /* invalid or empty failure cases */
  42402. a = EVP_PKEY_new();
  42403. b = EVP_PKEY_new();
  42404. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42405. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42406. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42407. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42408. #ifdef NO_RSA
  42409. /* Type check will fail since RSA is the default EVP key type */
  42410. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  42411. #else
  42412. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  42413. #endif
  42414. #else
  42415. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42416. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42417. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42418. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  42419. #endif
  42420. EVP_PKEY_free(b);
  42421. EVP_PKEY_free(a);
  42422. (void)in;
  42423. #endif
  42424. return EXPECT_RESULT();
  42425. }
  42426. static int test_ERR_load_crypto_strings(void)
  42427. {
  42428. #if defined(OPENSSL_ALL)
  42429. ERR_load_crypto_strings();
  42430. return TEST_SUCCESS;
  42431. #else
  42432. return TEST_SKIPPED;
  42433. #endif
  42434. }
  42435. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42436. static void free_x509(X509* x)
  42437. {
  42438. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42439. }
  42440. #endif
  42441. static int test_sk_X509(void)
  42442. {
  42443. EXPECT_DECLS;
  42444. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42445. {
  42446. STACK_OF(X509)* s = NULL;
  42447. ExpectNotNull(s = sk_X509_new_null());
  42448. ExpectIntEQ(sk_X509_num(s), 0);
  42449. sk_X509_pop_free(s, NULL);
  42450. ExpectNotNull(s = sk_X509_new_null());
  42451. ExpectIntEQ(sk_X509_num(s), 0);
  42452. sk_X509_pop_free(s, NULL);
  42453. ExpectNotNull(s = sk_X509_new_null());
  42454. sk_X509_push(s, (X509*)1);
  42455. ExpectIntEQ(sk_X509_num(s), 1);
  42456. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42457. sk_X509_push(s, (X509*)2);
  42458. ExpectIntEQ(sk_X509_num(s), 2);
  42459. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42460. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42461. sk_X509_push(s, (X509*)2);
  42462. sk_X509_pop_free(s, free_x509);
  42463. }
  42464. {
  42465. /* Push a list of 10 X509s onto stack, then verify that
  42466. * value(), push(), shift(), and pop() behave as expected. */
  42467. STACK_OF(X509)* s = NULL;
  42468. X509* xList[10];
  42469. int i = 0;
  42470. const int len = (sizeof(xList) / sizeof(xList[0]));
  42471. for (i = 0; i < len; ++i) {
  42472. xList[i] = NULL;
  42473. ExpectNotNull(xList[i] = X509_new());
  42474. }
  42475. /* test push, pop, and free */
  42476. ExpectNotNull(s = sk_X509_new_null());
  42477. for (i = 0; i < len; ++i) {
  42478. sk_X509_push(s, xList[i]);
  42479. ExpectIntEQ(sk_X509_num(s), i + 1);
  42480. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42481. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42482. }
  42483. /* pop returns and removes last pushed on stack, which is index 0
  42484. * in sk_x509_value */
  42485. for (i = 0; i < len; ++i) {
  42486. X509 * x = sk_X509_value(s, 0);
  42487. X509 * y = sk_X509_pop(s);
  42488. X509 * z = xList[len - 1 - i];
  42489. ExpectIntEQ((x == y), 1);
  42490. ExpectIntEQ((x == z), 1);
  42491. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  42492. }
  42493. sk_free(s);
  42494. s = NULL;
  42495. /* test push, shift, and free */
  42496. ExpectNotNull(s = sk_X509_new_null());
  42497. for (i = 0; i < len; ++i) {
  42498. sk_X509_push(s, xList[i]);
  42499. ExpectIntEQ(sk_X509_num(s), i + 1);
  42500. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42501. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42502. }
  42503. /* shift returns and removes first pushed on stack, which is index i
  42504. * in sk_x509_value() */
  42505. for (i = 0; i < len; ++i) {
  42506. X509 * x = sk_X509_value(s, len - 1 - i);
  42507. X509 * y = sk_X509_shift(s);
  42508. X509 * z = xList[i];
  42509. ExpectIntEQ((x == y), 1);
  42510. ExpectIntEQ((x == z), 1);
  42511. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  42512. }
  42513. sk_free(s);
  42514. for (i = 0; i < len; ++i)
  42515. X509_free(xList[i]);
  42516. }
  42517. #endif
  42518. return EXPECT_RESULT();
  42519. }
  42520. static int test_sk_X509_CRL(void)
  42521. {
  42522. EXPECT_DECLS;
  42523. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42524. X509_CRL* crl = NULL;
  42525. XFILE fp = XBADFILE;
  42526. STACK_OF(X509_CRL)* s = NULL;
  42527. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  42528. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42529. NULL, NULL));
  42530. if (fp != XBADFILE)
  42531. XFCLOSE(fp);
  42532. ExpectNotNull(s = sk_X509_CRL_new());
  42533. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  42534. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  42535. if (EXPECT_FAIL()) {
  42536. X509_CRL_free(crl);
  42537. }
  42538. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  42539. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  42540. sk_X509_CRL_free(s);
  42541. #endif
  42542. return EXPECT_RESULT();
  42543. }
  42544. static int test_X509_get_signature_nid(void)
  42545. {
  42546. EXPECT_DECLS;
  42547. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42548. X509* x509 = NULL;
  42549. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  42550. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42551. SSL_FILETYPE_PEM));
  42552. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42553. X509_free(x509);
  42554. #endif
  42555. return EXPECT_RESULT();
  42556. }
  42557. static int test_X509_REQ(void)
  42558. {
  42559. EXPECT_DECLS;
  42560. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42561. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42562. X509_NAME* name = NULL;
  42563. #ifndef NO_RSA
  42564. X509_NAME* subject = NULL;
  42565. #endif
  42566. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42567. X509_REQ* req = NULL;
  42568. EVP_PKEY* priv = NULL;
  42569. EVP_PKEY* pub = NULL;
  42570. unsigned char* der = NULL;
  42571. int len;
  42572. #endif
  42573. #ifndef NO_RSA
  42574. EVP_MD_CTX *mctx = NULL;
  42575. EVP_PKEY_CTX *pkctx = NULL;
  42576. #ifdef USE_CERT_BUFFERS_1024
  42577. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42578. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42579. #elif defined(USE_CERT_BUFFERS_2048)
  42580. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42581. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42582. #endif
  42583. #endif
  42584. #ifdef HAVE_ECC
  42585. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42586. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42587. #endif
  42588. ExpectNotNull(name = X509_NAME_new());
  42589. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42590. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  42591. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42592. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  42593. #ifndef NO_RSA
  42594. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42595. (long)sizeof_client_key_der_2048));
  42596. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42597. (long)sizeof_client_keypub_der_2048));
  42598. ExpectNotNull(req = X509_REQ_new());
  42599. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42600. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42601. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42602. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42603. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42604. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42605. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42606. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42607. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42608. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42609. len = i2d_X509_REQ(req, &der);
  42610. DEBUG_WRITE_DER(der, len, "req.der");
  42611. #ifdef USE_CERT_BUFFERS_1024
  42612. ExpectIntEQ(len, 381);
  42613. #else
  42614. ExpectIntEQ(len, 643);
  42615. #endif
  42616. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42617. der = NULL;
  42618. mctx = EVP_MD_CTX_new();
  42619. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  42620. WOLFSSL_SUCCESS);
  42621. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42622. EVP_MD_CTX_free(mctx);
  42623. mctx = NULL;
  42624. X509_REQ_free(NULL);
  42625. X509_REQ_free(req);
  42626. req = NULL;
  42627. /* Test getting the subject from a newly created X509_REQ */
  42628. ExpectNotNull(req = X509_REQ_new());
  42629. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  42630. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42631. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42632. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42633. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42634. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42635. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42636. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42637. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42638. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42639. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42640. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42641. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42642. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42643. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42644. len = i2d_X509_REQ(req, &der);
  42645. DEBUG_WRITE_DER(der, len, "req2.der");
  42646. #ifdef USE_CERT_BUFFERS_1024
  42647. ExpectIntEQ(len, 435);
  42648. #else
  42649. ExpectIntEQ(len, 696);
  42650. #endif
  42651. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42652. der = NULL;
  42653. EVP_PKEY_free(pub);
  42654. pub = NULL;
  42655. EVP_PKEY_free(priv);
  42656. priv = NULL;
  42657. X509_REQ_free(req);
  42658. req = NULL;
  42659. #endif
  42660. #ifdef HAVE_ECC
  42661. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42662. sizeof_ecc_clikey_der_256));
  42663. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42664. sizeof_ecc_clikeypub_der_256));
  42665. ExpectNotNull(req = X509_REQ_new());
  42666. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42667. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42668. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42669. /* Signature is random and may be shorter or longer. */
  42670. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42671. ExpectIntLE(len, 253);
  42672. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42673. X509_REQ_free(req);
  42674. EVP_PKEY_free(pub);
  42675. EVP_PKEY_free(priv);
  42676. #ifdef FP_ECC
  42677. wc_ecc_fp_free();
  42678. #endif
  42679. #endif /* HAVE_ECC */
  42680. X509_NAME_free(name);
  42681. #endif
  42682. return EXPECT_RESULT();
  42683. }
  42684. static int test_wolfssl_PKCS7(void)
  42685. {
  42686. EXPECT_DECLS;
  42687. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42688. !defined(NO_RSA)
  42689. PKCS7* pkcs7 = NULL;
  42690. byte data[FOURK_BUF];
  42691. word32 len = sizeof(data);
  42692. const byte* p = data;
  42693. byte content[] = "Test data to encode.";
  42694. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42695. BIO* bio = NULL;
  42696. byte key[sizeof(client_key_der_2048)];
  42697. word32 keySz = (word32)sizeof(key);
  42698. byte* out = NULL;
  42699. #endif
  42700. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  42701. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  42702. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42703. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42704. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42705. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42706. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42707. PKCS7_free(pkcs7);
  42708. pkcs7 = NULL;
  42709. /* fail case, without PKCS7_NOVERIFY */
  42710. p = data;
  42711. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42712. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42713. 0), WOLFSSL_FAILURE);
  42714. PKCS7_free(pkcs7);
  42715. pkcs7 = NULL;
  42716. /* success case, with PKCS7_NOVERIFY */
  42717. p = data;
  42718. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42719. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42720. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42721. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42722. /* test i2d */
  42723. XMEMCPY(key, client_key_der_2048, keySz);
  42724. if (pkcs7 != NULL) {
  42725. pkcs7->privateKey = key;
  42726. pkcs7->privateKeySz = (word32)sizeof(key);
  42727. pkcs7->encryptOID = RSAk;
  42728. #ifdef NO_SHA
  42729. pkcs7->hashOID = SHA256h;
  42730. #else
  42731. pkcs7->hashOID = SHAh;
  42732. #endif
  42733. }
  42734. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42735. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42736. #ifndef NO_ASN_TIME
  42737. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42738. #else
  42739. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  42740. #endif
  42741. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42742. BIO_free(bio);
  42743. #endif
  42744. PKCS7_free(NULL);
  42745. PKCS7_free(pkcs7);
  42746. #endif
  42747. return EXPECT_RESULT();
  42748. }
  42749. static int test_wolfSSL_PKCS7_sign(void)
  42750. {
  42751. EXPECT_DECLS;
  42752. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42753. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42754. PKCS7* p7 = NULL;
  42755. PKCS7* p7Ver = NULL;
  42756. byte* out = NULL;
  42757. byte* tmpPtr = NULL;
  42758. int outLen = 0;
  42759. int flags = 0;
  42760. byte data[] = "Test data to encode.";
  42761. const char* cert = "./certs/server-cert.pem";
  42762. const char* key = "./certs/server-key.pem";
  42763. const char* ca = "./certs/ca-cert.pem";
  42764. WOLFSSL_BIO* certBio = NULL;
  42765. WOLFSSL_BIO* keyBio = NULL;
  42766. WOLFSSL_BIO* caBio = NULL;
  42767. WOLFSSL_BIO* inBio = NULL;
  42768. X509* signCert = NULL;
  42769. EVP_PKEY* signKey = NULL;
  42770. X509* caCert = NULL;
  42771. X509_STORE* store = NULL;
  42772. /* read signer cert/key into BIO */
  42773. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  42774. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  42775. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42776. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42777. /* read CA cert into store (for verify) */
  42778. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  42779. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42780. ExpectNotNull(store = X509_STORE_new());
  42781. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42782. /* data to be signed into BIO */
  42783. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42784. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42785. /* PKCS7_sign, bad args: signer NULL */
  42786. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42787. /* PKCS7_sign, bad args: signer key NULL */
  42788. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42789. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42790. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42791. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42792. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42793. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42794. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42795. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42796. {
  42797. flags = PKCS7_BINARY;
  42798. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42799. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42800. /* verify with d2i_PKCS7 */
  42801. tmpPtr = out;
  42802. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42803. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42804. PKCS7_free(p7Ver);
  42805. p7Ver = NULL;
  42806. /* verify with wc_PKCS7_VerifySignedData */
  42807. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42808. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42809. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42810. /* compare the signer found to expected signer */
  42811. ExpectIntNE(p7Ver->verifyCertSz, 0);
  42812. tmpPtr = NULL;
  42813. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42814. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42815. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42816. tmpPtr = NULL;
  42817. wc_PKCS7_Free(p7Ver);
  42818. p7Ver = NULL;
  42819. ExpectNotNull(out);
  42820. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42821. out = NULL;
  42822. PKCS7_free(p7);
  42823. p7 = NULL;
  42824. }
  42825. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42826. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42827. {
  42828. /* re-populate input BIO, may have been consumed */
  42829. BIO_free(inBio);
  42830. inBio = NULL;
  42831. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42832. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42833. flags = PKCS7_BINARY | PKCS7_STREAM;
  42834. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42835. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42836. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42837. /* PKCS7_final, bad args: PKCS7 null */
  42838. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42839. /* PKCS7_final, bad args: PKCS7 null */
  42840. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42841. tmpPtr = out;
  42842. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42843. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42844. PKCS7_free(p7Ver);
  42845. p7Ver = NULL;
  42846. ExpectNotNull(out);
  42847. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42848. out = NULL;
  42849. PKCS7_free(p7);
  42850. p7 = NULL;
  42851. }
  42852. /* TEST SUCCESS: Detached, not streaming, not MIME */
  42853. {
  42854. /* re-populate input BIO, may have been consumed */
  42855. BIO_free(inBio);
  42856. inBio = NULL;
  42857. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42858. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42859. flags = PKCS7_BINARY | PKCS7_DETACHED;
  42860. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42861. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42862. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42863. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42864. if (p7Ver != NULL) {
  42865. p7Ver->content = data;
  42866. p7Ver->contentSz = sizeof(data);
  42867. }
  42868. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42869. wc_PKCS7_Free(p7Ver);
  42870. p7Ver = NULL;
  42871. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  42872. * yet support detached content */
  42873. tmpPtr = out;
  42874. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42875. PKCS7_free(p7Ver);
  42876. p7Ver = NULL;
  42877. ExpectNotNull(out);
  42878. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42879. out = NULL;
  42880. PKCS7_free(p7);
  42881. p7 = NULL;
  42882. }
  42883. /* TEST SUCCESS: Detached, streaming, not MIME */
  42884. {
  42885. /* re-populate input BIO, may have been consumed */
  42886. BIO_free(inBio);
  42887. inBio = NULL;
  42888. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  42889. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42890. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  42891. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42892. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42893. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42894. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42895. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42896. if (p7Ver != NULL) {
  42897. p7Ver->content = data;
  42898. p7Ver->contentSz = sizeof(data);
  42899. }
  42900. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42901. wc_PKCS7_Free(p7Ver);
  42902. p7Ver = NULL;
  42903. ExpectNotNull(out);
  42904. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42905. PKCS7_free(p7);
  42906. p7 = NULL;
  42907. }
  42908. X509_STORE_free(store);
  42909. X509_free(caCert);
  42910. X509_free(signCert);
  42911. EVP_PKEY_free(signKey);
  42912. BIO_free(inBio);
  42913. BIO_free(keyBio);
  42914. BIO_free(certBio);
  42915. BIO_free(caBio);
  42916. #endif
  42917. return EXPECT_RESULT();
  42918. }
  42919. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  42920. {
  42921. EXPECT_DECLS;
  42922. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  42923. PKCS7_SIGNED* pkcs7 = NULL;
  42924. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  42925. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  42926. PKCS7_SIGNED_free(pkcs7);
  42927. #endif
  42928. return EXPECT_RESULT();
  42929. }
  42930. #ifndef NO_BIO
  42931. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  42932. {
  42933. EXPECT_DECLS;
  42934. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  42935. PKCS7* pkcs7 = NULL;
  42936. BIO* bio = NULL;
  42937. const byte* cert_buf = NULL;
  42938. int ret = 0;
  42939. WC_RNG rng;
  42940. const byte data[] = { /* Hello World */
  42941. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42942. 0x72,0x6c,0x64
  42943. };
  42944. #ifndef NO_RSA
  42945. #if defined(USE_CERT_BUFFERS_2048)
  42946. byte key[sizeof(client_key_der_2048)];
  42947. byte cert[sizeof(client_cert_der_2048)];
  42948. word32 keySz = (word32)sizeof(key);
  42949. word32 certSz = (word32)sizeof(cert);
  42950. XMEMSET(key, 0, keySz);
  42951. XMEMSET(cert, 0, certSz);
  42952. XMEMCPY(key, client_key_der_2048, keySz);
  42953. XMEMCPY(cert, client_cert_der_2048, certSz);
  42954. #elif defined(USE_CERT_BUFFERS_1024)
  42955. byte key[sizeof_client_key_der_1024];
  42956. byte cert[sizeof(sizeof_client_cert_der_1024)];
  42957. word32 keySz = (word32)sizeof(key);
  42958. word32 certSz = (word32)sizeof(cert);
  42959. XMEMSET(key, 0, keySz);
  42960. XMEMSET(cert, 0, certSz);
  42961. XMEMCPY(key, client_key_der_1024, keySz);
  42962. XMEMCPY(cert, client_cert_der_1024, certSz);
  42963. #else
  42964. unsigned char cert[ONEK_BUF];
  42965. unsigned char key[ONEK_BUF];
  42966. XFILE fp = XBADFILE;
  42967. int certSz;
  42968. int keySz;
  42969. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  42970. XBADFILE);
  42971. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  42972. fp), 0);
  42973. if (fp != XBADFILE) {
  42974. XFCLOSE(fp);
  42975. fp = XBADFILE;
  42976. }
  42977. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  42978. XBADFILE);
  42979. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  42980. 0);
  42981. if (fp != XBADFILE) {
  42982. XFCLOSE(fp);
  42983. fp = XBADFILE;
  42984. }
  42985. #endif
  42986. #elif defined(HAVE_ECC)
  42987. #if defined(USE_CERT_BUFFERS_256)
  42988. unsigned char cert[sizeof(cliecc_cert_der_256)];
  42989. unsigned char key[sizeof(ecc_clikey_der_256)];
  42990. int certSz = (int)sizeof(cert);
  42991. int keySz = (int)sizeof(key);
  42992. XMEMSET(cert, 0, certSz);
  42993. XMEMSET(key, 0, keySz);
  42994. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  42995. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  42996. #else
  42997. unsigned char cert[ONEK_BUF];
  42998. unsigned char key[ONEK_BUF];
  42999. XFILE fp = XBADFILE;
  43000. int certSz, keySz;
  43001. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  43002. XBADFILE);
  43003. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  43004. fp), 0);
  43005. if (fp != XBADFILE) {
  43006. XFCLOSE(fp);
  43007. fp = XBADFILE;
  43008. }
  43009. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  43010. XBADFILE);
  43011. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  43012. 0);
  43013. if (fp != XBADFILE) {
  43014. XFCLOSE(fp);
  43015. fp = XBADFILE;
  43016. }
  43017. #endif
  43018. #else
  43019. #error PKCS7 requires ECC or RSA
  43020. #endif
  43021. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  43022. /* initialize with DER encoded cert */
  43023. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  43024. /* init rng */
  43025. XMEMSET(&rng, 0, sizeof(WC_RNG));
  43026. ExpectIntEQ(wc_InitRng(&rng), 0);
  43027. if (pkcs7 != NULL) {
  43028. pkcs7->rng = &rng;
  43029. pkcs7->content = (byte*)data; /* not used for ex */
  43030. pkcs7->contentSz = (word32)sizeof(data);
  43031. pkcs7->contentOID = SIGNED_DATA;
  43032. pkcs7->privateKey = key;
  43033. pkcs7->privateKeySz = (word32)sizeof(key);
  43034. pkcs7->encryptOID = RSAk;
  43035. #ifdef NO_SHA
  43036. pkcs7->hashOID = SHA256h;
  43037. #else
  43038. pkcs7->hashOID = SHAh;
  43039. #endif
  43040. pkcs7->signedAttribs = NULL;
  43041. pkcs7->signedAttribsSz = 0;
  43042. }
  43043. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43044. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  43045. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  43046. /* Read PKCS#7 PEM from BIO */
  43047. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  43048. ExpectIntGE(ret, 0);
  43049. BIO_free(bio);
  43050. wc_PKCS7_Free(pkcs7);
  43051. wc_FreeRng(&rng);
  43052. #endif
  43053. return EXPECT_RESULT();
  43054. }
  43055. #ifdef HAVE_SMIME
  43056. static int test_wolfSSL_SMIME_read_PKCS7(void)
  43057. {
  43058. EXPECT_DECLS;
  43059. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  43060. !defined(NO_RSA)
  43061. PKCS7* pkcs7 = NULL;
  43062. BIO* bio = NULL;
  43063. BIO* bcont = NULL;
  43064. BIO* out = NULL;
  43065. const byte* outBuf = NULL;
  43066. int outBufLen = 0;
  43067. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  43068. XFILE smimeTestFile = XBADFILE;
  43069. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  43070. XBADFILE);
  43071. /* smime-test.p7s */
  43072. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  43073. ExpectNotNull(bio);
  43074. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43075. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43076. ExpectNotNull(pkcs7);
  43077. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43078. PKCS7_NOVERIFY), SSL_SUCCESS);
  43079. XFCLOSE(smimeTestFile);
  43080. if (bcont) BIO_free(bcont);
  43081. bcont = NULL;
  43082. wolfSSL_PKCS7_free(pkcs7);
  43083. pkcs7 = NULL;
  43084. /* smime-test-multipart.p7s */
  43085. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  43086. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43087. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43088. ExpectNotNull(pkcs7);
  43089. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43090. PKCS7_NOVERIFY), SSL_SUCCESS);
  43091. XFCLOSE(smimeTestFile);
  43092. if (bcont) BIO_free(bcont);
  43093. bcont = NULL;
  43094. wolfSSL_PKCS7_free(pkcs7);
  43095. pkcs7 = NULL;
  43096. /* smime-test-multipart-badsig.p7s */
  43097. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  43098. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43099. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43100. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  43101. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43102. PKCS7_NOVERIFY), SSL_FAILURE);
  43103. XFCLOSE(smimeTestFile);
  43104. if (bcont) BIO_free(bcont);
  43105. bcont = NULL;
  43106. wolfSSL_PKCS7_free(pkcs7);
  43107. pkcs7 = NULL;
  43108. /* smime-test-canon.p7s */
  43109. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43110. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43111. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43112. ExpectNotNull(pkcs7);
  43113. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  43114. PKCS7_NOVERIFY), SSL_SUCCESS);
  43115. XFCLOSE(smimeTestFile);
  43116. if (bcont) BIO_free(bcont);
  43117. bcont = NULL;
  43118. wolfSSL_PKCS7_free(pkcs7);
  43119. pkcs7 = NULL;
  43120. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  43121. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  43122. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  43123. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  43124. ExpectNotNull(pkcs7);
  43125. out = wolfSSL_BIO_new(BIO_s_mem());
  43126. ExpectNotNull(out);
  43127. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  43128. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  43129. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  43130. /* Content-Type should not show up at beginning of output buffer */
  43131. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  43132. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  43133. BIO_free(out);
  43134. BIO_free(bio);
  43135. if (bcont) BIO_free(bcont);
  43136. wolfSSL_PKCS7_free(pkcs7);
  43137. #endif
  43138. return EXPECT_RESULT();
  43139. }
  43140. static int test_wolfSSL_SMIME_write_PKCS7(void)
  43141. {
  43142. EXPECT_DECLS;
  43143. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  43144. PKCS7* p7 = NULL;
  43145. PKCS7* p7Ver = NULL;
  43146. int flags = 0;
  43147. byte data[] = "Test data to encode.";
  43148. const char* cert = "./certs/server-cert.pem";
  43149. const char* key = "./certs/server-key.pem";
  43150. const char* ca = "./certs/ca-cert.pem";
  43151. WOLFSSL_BIO* certBio = NULL;
  43152. WOLFSSL_BIO* keyBio = NULL;
  43153. WOLFSSL_BIO* caBio = NULL;
  43154. WOLFSSL_BIO* inBio = NULL;
  43155. WOLFSSL_BIO* outBio = NULL;
  43156. WOLFSSL_BIO* content = NULL;
  43157. X509* signCert = NULL;
  43158. EVP_PKEY* signKey = NULL;
  43159. X509* caCert = NULL;
  43160. X509_STORE* store = NULL;
  43161. /* read signer cert/key into BIO */
  43162. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  43163. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  43164. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  43165. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  43166. /* read CA cert into store (for verify) */
  43167. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  43168. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  43169. ExpectNotNull(store = X509_STORE_new());
  43170. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  43171. /* generate and verify SMIME: not detached */
  43172. {
  43173. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43174. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43175. flags = PKCS7_STREAM;
  43176. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43177. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43178. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43179. /* bad arg: out NULL */
  43180. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  43181. /* bad arg: pkcs7 NULL */
  43182. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  43183. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43184. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43185. BIO_free(content);
  43186. content = NULL;
  43187. BIO_free(inBio);
  43188. inBio = NULL;
  43189. BIO_free(outBio);
  43190. outBio = NULL;
  43191. PKCS7_free(p7Ver);
  43192. p7Ver = NULL;
  43193. PKCS7_free(p7);
  43194. p7 = NULL;
  43195. }
  43196. /* generate and verify SMIME: not detached, add Content-Type */
  43197. {
  43198. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43199. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43200. flags = PKCS7_STREAM | PKCS7_TEXT;
  43201. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43202. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43203. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43204. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43205. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  43206. BIO_free(content);
  43207. content = NULL;
  43208. BIO_free(inBio);
  43209. inBio = NULL;
  43210. BIO_free(outBio);
  43211. outBio = NULL;
  43212. PKCS7_free(p7Ver);
  43213. p7Ver = NULL;
  43214. PKCS7_free(p7);
  43215. p7 = NULL;
  43216. }
  43217. /* generate and verify SMIME: detached */
  43218. {
  43219. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43220. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43221. flags = PKCS7_DETACHED | PKCS7_STREAM;
  43222. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43223. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43224. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43225. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43226. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43227. BIO_free(content);
  43228. content = NULL;
  43229. BIO_free(inBio);
  43230. inBio = NULL;
  43231. BIO_free(outBio);
  43232. outBio = NULL;
  43233. PKCS7_free(p7Ver);
  43234. p7Ver = NULL;
  43235. PKCS7_free(p7);
  43236. p7 = NULL;
  43237. }
  43238. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  43239. {
  43240. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  43241. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  43242. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  43243. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  43244. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  43245. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  43246. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  43247. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  43248. BIO_free(content);
  43249. content = NULL;
  43250. BIO_free(inBio);
  43251. inBio = NULL;
  43252. BIO_free(outBio);
  43253. outBio = NULL;
  43254. PKCS7_free(p7Ver);
  43255. p7Ver = NULL;
  43256. PKCS7_free(p7);
  43257. p7 = NULL;
  43258. }
  43259. X509_STORE_free(store);
  43260. X509_free(caCert);
  43261. X509_free(signCert);
  43262. EVP_PKEY_free(signKey);
  43263. BIO_free(keyBio);
  43264. BIO_free(certBio);
  43265. BIO_free(caBio);
  43266. #endif
  43267. return EXPECT_RESULT();
  43268. }
  43269. #endif /* HAVE_SMIME */
  43270. #endif /* !NO_BIO */
  43271. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  43272. * returns 0) */
  43273. static int test_X509_STORE_No_SSL_CTX(void)
  43274. {
  43275. EXPECT_DECLS;
  43276. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  43277. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  43278. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43279. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43280. !defined(NO_RSA)
  43281. X509_STORE * store = NULL;
  43282. X509_STORE_CTX * storeCtx = NULL;
  43283. X509_CRL * crl = NULL;
  43284. X509 * ca = NULL;
  43285. X509 * cert = NULL;
  43286. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43287. const char srvCert[] = "./certs/server-cert.pem";
  43288. const char caCert[] = "./certs/ca-cert.pem";
  43289. const char caDir[] = "./certs/crl/hash_pem";
  43290. XFILE fp = XBADFILE;
  43291. X509_LOOKUP * lookup = NULL;
  43292. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43293. /* Set up store with CA */
  43294. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43295. SSL_FILETYPE_PEM)));
  43296. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43297. /* Add CRL lookup directory to store
  43298. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  43299. * of crl.pem */
  43300. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43301. X509_LOOKUP_hash_dir())));
  43302. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  43303. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  43304. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43305. SSL_SUCCESS);
  43306. /* Add CRL to store NOT containing the verified certificate, which
  43307. * forces use of the CRL lookup directory */
  43308. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43309. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43310. NULL, NULL));
  43311. if (fp != XBADFILE)
  43312. XFCLOSE(fp);
  43313. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43314. /* Create verification context outside of an SSL session */
  43315. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43316. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43317. SSL_FILETYPE_PEM)));
  43318. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43319. /* Perform verification, which should NOT indicate CRL missing due to the
  43320. * store CM's X509 store pointer being NULL */
  43321. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43322. X509_CRL_free(crl);
  43323. X509_STORE_free(store);
  43324. X509_STORE_CTX_free(storeCtx);
  43325. X509_free(cert);
  43326. X509_free(ca);
  43327. #endif
  43328. return EXPECT_RESULT();
  43329. }
  43330. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  43331. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  43332. static int test_X509_LOOKUP_add_dir(void)
  43333. {
  43334. EXPECT_DECLS;
  43335. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  43336. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  43337. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  43338. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  43339. !defined(NO_RSA)
  43340. X509_STORE * store = NULL;
  43341. X509_STORE_CTX * storeCtx = NULL;
  43342. X509_CRL * crl = NULL;
  43343. X509 * ca = NULL;
  43344. X509 * cert = NULL;
  43345. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  43346. const char srvCert[] = "./certs/server-cert.pem";
  43347. const char caCert[] = "./certs/ca-cert.pem";
  43348. const char caDir[] = "./certs/crl/hash_der";
  43349. XFILE fp = XBADFILE;
  43350. X509_LOOKUP * lookup = NULL;
  43351. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43352. /* Set up store with CA */
  43353. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43354. SSL_FILETYPE_PEM)));
  43355. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43356. /* Add CRL lookup directory to store.
  43357. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  43358. * of crl.der */
  43359. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43360. X509_LOOKUP_hash_dir())));
  43361. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  43362. SSL_SUCCESS);
  43363. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43364. SSL_SUCCESS);
  43365. /* Add CRL to store NOT containing the verified certificate, which
  43366. * forces use of the CRL lookup directory */
  43367. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43368. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43369. NULL, NULL));
  43370. if (fp != XBADFILE) {
  43371. XFCLOSE(fp);
  43372. fp = XBADFILE;
  43373. }
  43374. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43375. /* Create verification context outside of an SSL session */
  43376. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43377. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43378. SSL_FILETYPE_PEM)));
  43379. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43380. /* Perform verification, which should NOT return CRL missing */
  43381. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  43382. X509_CRL_free(crl);
  43383. crl = NULL;
  43384. X509_STORE_free(store);
  43385. store = NULL;
  43386. X509_STORE_CTX_free(storeCtx);
  43387. storeCtx = NULL;
  43388. X509_free(cert);
  43389. cert = NULL;
  43390. X509_free(ca);
  43391. ca = NULL;
  43392. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  43393. * We should get CRL_MISSING at the end, because the lookup
  43394. * dir has only ASN1 CRLs. */
  43395. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  43396. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  43397. SSL_FILETYPE_PEM)));
  43398. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  43399. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  43400. X509_LOOKUP_hash_dir())));
  43401. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  43402. SSL_SUCCESS);
  43403. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  43404. SSL_SUCCESS);
  43405. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  43406. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  43407. NULL, NULL));
  43408. if (fp != XBADFILE) {
  43409. XFCLOSE(fp);
  43410. fp = XBADFILE;
  43411. }
  43412. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  43413. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  43414. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  43415. SSL_FILETYPE_PEM)));
  43416. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  43417. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  43418. * in dir containing only ASN1/DER. */
  43419. ExpectIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  43420. X509_CRL_free(crl);
  43421. X509_STORE_free(store);
  43422. X509_STORE_CTX_free(storeCtx);
  43423. X509_free(cert);
  43424. X509_free(ca);
  43425. #endif
  43426. return EXPECT_RESULT();
  43427. }
  43428. /*----------------------------------------------------------------------------*
  43429. | Certificate Failure Checks
  43430. *----------------------------------------------------------------------------*/
  43431. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  43432. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  43433. #if !defined(NO_RSA) || defined(HAVE_ECC)
  43434. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  43435. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  43436. int type)
  43437. {
  43438. int ret;
  43439. WOLFSSL_CERT_MANAGER* cm = NULL;
  43440. switch (type) {
  43441. case TESTING_RSA:
  43442. #ifdef NO_RSA
  43443. fprintf(stderr, "RSA disabled, skipping test\n");
  43444. return ASN_SIG_CONFIRM_E;
  43445. #else
  43446. break;
  43447. #endif
  43448. case TESTING_ECC:
  43449. #ifndef HAVE_ECC
  43450. fprintf(stderr, "ECC disabled, skipping test\n");
  43451. return ASN_SIG_CONFIRM_E;
  43452. #else
  43453. break;
  43454. #endif
  43455. default:
  43456. fprintf(stderr, "Bad function argument\n");
  43457. return BAD_FUNC_ARG;
  43458. }
  43459. cm = wolfSSL_CertManagerNew();
  43460. if (cm == NULL) {
  43461. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  43462. return -1;
  43463. }
  43464. #ifndef NO_FILESYSTEM
  43465. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  43466. if (ret != WOLFSSL_SUCCESS) {
  43467. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  43468. wolfSSL_CertManagerFree(cm);
  43469. return ret;
  43470. }
  43471. #else
  43472. (void)ca;
  43473. #endif
  43474. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  43475. WOLFSSL_FILETYPE_ASN1);
  43476. /* Let ExpectIntEQ handle return code */
  43477. wolfSSL_CertManagerFree(cm);
  43478. return ret;
  43479. }
  43480. #endif
  43481. #if !defined(NO_FILESYSTEM)
  43482. static int test_RsaSigFailure_cm(void)
  43483. {
  43484. EXPECT_DECLS;
  43485. #ifndef NO_RSA
  43486. const char* ca_cert = "./certs/ca-cert.pem";
  43487. const char* server_cert = "./certs/server-cert.der";
  43488. byte* cert_buf = NULL;
  43489. size_t cert_sz = 0;
  43490. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  43491. if (cert_buf != NULL) {
  43492. /* corrupt DER - invert last byte, which is signature */
  43493. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43494. /* test bad cert */
  43495. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43496. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  43497. WOLFSSL_FATAL_ERROR);
  43498. #else
  43499. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  43500. ASN_SIG_CONFIRM_E);
  43501. #endif
  43502. }
  43503. /* load_file() uses malloc. */
  43504. if (cert_buf != NULL) {
  43505. free(cert_buf);
  43506. }
  43507. #endif /* !NO_RSA */
  43508. return EXPECT_RESULT();
  43509. }
  43510. static int test_EccSigFailure_cm(void)
  43511. {
  43512. EXPECT_DECLS;
  43513. #ifdef HAVE_ECC
  43514. /* self-signed ECC cert, so use server cert as CA */
  43515. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43516. const char* server_cert = "./certs/server-ecc.der";
  43517. byte* cert_buf = NULL;
  43518. size_t cert_sz = 0;
  43519. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  43520. if (cert_buf != NULL) {
  43521. /* corrupt DER - invert last byte, which is signature */
  43522. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43523. /* test bad cert */
  43524. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43525. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  43526. WOLFSSL_FATAL_ERROR);
  43527. #else
  43528. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  43529. ASN_SIG_CONFIRM_E);
  43530. #endif
  43531. }
  43532. /* load_file() uses malloc. */
  43533. if (cert_buf != NULL) {
  43534. free(cert_buf);
  43535. }
  43536. #ifdef FP_ECC
  43537. wc_ecc_fp_free();
  43538. #endif
  43539. #endif /* HAVE_ECC */
  43540. return EXPECT_RESULT();
  43541. }
  43542. #endif /* !NO_FILESYSTEM */
  43543. #endif /* NO_CERTS */
  43544. #ifdef WOLFSSL_TLS13
  43545. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43546. #ifdef WC_SHA384_DIGEST_SIZE
  43547. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43548. #else
  43549. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43550. #endif
  43551. #endif
  43552. #ifdef WOLFSSL_EARLY_DATA
  43553. static const char earlyData[] = "Early Data";
  43554. static char earlyDataBuffer[1];
  43555. #endif
  43556. static int test_tls13_apis(void)
  43557. {
  43558. EXPECT_DECLS;
  43559. int ret;
  43560. #ifndef WOLFSSL_NO_TLS12
  43561. #ifndef NO_WOLFSSL_CLIENT
  43562. WOLFSSL_CTX* clientTls12Ctx = NULL;
  43563. WOLFSSL* clientTls12Ssl = NULL;
  43564. #endif
  43565. #ifndef NO_WOLFSSL_SERVER
  43566. WOLFSSL_CTX* serverTls12Ctx = NULL;
  43567. WOLFSSL* serverTls12Ssl = NULL;
  43568. #endif
  43569. #endif
  43570. #ifndef NO_WOLFSSL_CLIENT
  43571. WOLFSSL_CTX* clientCtx = NULL;
  43572. WOLFSSL* clientSsl = NULL;
  43573. #endif
  43574. #ifndef NO_WOLFSSL_SERVER
  43575. WOLFSSL_CTX* serverCtx = NULL;
  43576. WOLFSSL* serverSsl = NULL;
  43577. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43578. const char* ourCert = svrCertFile;
  43579. const char* ourKey = svrKeyFile;
  43580. #endif
  43581. #endif
  43582. int required;
  43583. #ifdef WOLFSSL_EARLY_DATA
  43584. int outSz;
  43585. #endif
  43586. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43587. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43588. #ifdef HAVE_PQC
  43589. WOLFSSL_KYBER_LEVEL1
  43590. #else
  43591. WOLFSSL_ECC_SECP256R1
  43592. #endif
  43593. };
  43594. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43595. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43596. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43597. int numGroups = 2;
  43598. #endif
  43599. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43600. char groupList[] =
  43601. #ifndef NO_ECC_SECP
  43602. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43603. "P-521:"
  43604. #endif
  43605. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43606. "P-384:"
  43607. #endif
  43608. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43609. "P-256"
  43610. #ifdef HAVE_PQC
  43611. ":P256_KYBER_LEVEL1"
  43612. #endif
  43613. #endif
  43614. #endif /* !defined(NO_ECC_SECP) */
  43615. #ifdef HAVE_PQC
  43616. ":KYBER_LEVEL1"
  43617. #endif
  43618. "";
  43619. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43620. (void)ret;
  43621. #ifndef WOLFSSL_NO_TLS12
  43622. #ifndef NO_WOLFSSL_CLIENT
  43623. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43624. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43625. #endif
  43626. #ifndef NO_WOLFSSL_SERVER
  43627. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43628. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43629. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43630. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  43631. WOLFSSL_FILETYPE_PEM);
  43632. #endif
  43633. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43634. #endif
  43635. #endif
  43636. #ifndef NO_WOLFSSL_CLIENT
  43637. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43638. clientSsl = wolfSSL_new(clientCtx);
  43639. #endif
  43640. #ifndef NO_WOLFSSL_SERVER
  43641. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43642. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43643. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43644. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43645. #endif
  43646. serverSsl = wolfSSL_new(serverCtx);
  43647. ExpectNotNull(serverSsl);
  43648. #endif
  43649. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43650. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43651. #ifndef NO_WOLFSSL_CLIENT
  43652. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43653. #endif
  43654. #ifndef NO_WOLFSSL_SERVER
  43655. #ifndef WOLFSSL_NO_TLS12
  43656. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  43657. BAD_FUNC_ARG);
  43658. #endif
  43659. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43660. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43661. WOLFSSL_SUCCESS);
  43662. #endif
  43663. #endif
  43664. #ifdef HAVE_SUPPORTED_CURVES
  43665. #ifdef HAVE_ECC
  43666. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  43667. BAD_FUNC_ARG);
  43668. #ifndef NO_WOLFSSL_SERVER
  43669. do {
  43670. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43671. #ifdef WOLFSSL_ASYNC_CRYPT
  43672. if (ret == WC_PENDING_E)
  43673. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43674. #endif
  43675. }
  43676. while (ret == WC_PENDING_E);
  43677. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43678. #endif
  43679. #ifndef NO_WOLFSSL_CLIENT
  43680. #ifndef WOLFSSL_NO_TLS12
  43681. do {
  43682. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43683. #ifdef WOLFSSL_ASYNC_CRYPT
  43684. if (ret == WC_PENDING_E)
  43685. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43686. #endif
  43687. }
  43688. while (ret == WC_PENDING_E);
  43689. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43690. #endif
  43691. do {
  43692. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43693. #ifdef WOLFSSL_ASYNC_CRYPT
  43694. if (ret == WC_PENDING_E)
  43695. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43696. #endif
  43697. }
  43698. while (ret == WC_PENDING_E);
  43699. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43700. #endif
  43701. #elif defined(HAVE_CURVE25519)
  43702. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43703. #ifndef NO_WOLFSSL_SERVER
  43704. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43705. WOLFSSL_SUCCESS);
  43706. #endif
  43707. #ifndef NO_WOLFSSL_CLIENT
  43708. #ifndef WOLFSSL_NO_TLS12
  43709. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43710. WOLFSSL_SUCCESS);
  43711. #endif
  43712. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43713. WOLFSSL_SUCCESS);
  43714. #endif
  43715. #elif defined(HAVE_CURVE448)
  43716. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43717. #ifndef NO_WOLFSSL_SERVER
  43718. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43719. WOLFSSL_SUCCESS);
  43720. #endif
  43721. #ifndef NO_WOLFSSL_CLIENT
  43722. #ifndef WOLFSSL_NO_TLS12
  43723. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43724. WOLFSSL_SUCCESS);
  43725. #endif
  43726. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43727. WOLFSSL_SUCCESS);
  43728. #endif
  43729. #else
  43730. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  43731. BAD_FUNC_ARG);
  43732. #ifndef NO_WOLFSSL_CLIENT
  43733. #ifndef WOLFSSL_NO_TLS12
  43734. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43735. NOT_COMPILED_IN);
  43736. #endif
  43737. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43738. NOT_COMPILED_IN);
  43739. #endif
  43740. #endif
  43741. #if defined(HAVE_PQC)
  43742. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43743. #ifndef NO_WOLFSSL_SERVER
  43744. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43745. WOLFSSL_SUCCESS);
  43746. #endif
  43747. #ifndef NO_WOLFSSL_CLIENT
  43748. #ifndef WOLFSSL_NO_TLS12
  43749. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43750. BAD_FUNC_ARG);
  43751. #endif
  43752. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43753. WOLFSSL_SUCCESS);
  43754. #endif
  43755. #endif
  43756. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43757. #ifndef NO_WOLFSSL_SERVER
  43758. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43759. #endif
  43760. #ifndef NO_WOLFSSL_CLIENT
  43761. #ifndef WOLFSSL_NO_TLS12
  43762. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43763. #endif
  43764. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43765. #endif
  43766. #endif /* HAVE_SUPPORTED_CURVES */
  43767. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43768. #ifndef NO_WOLFSSL_CLIENT
  43769. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43770. #endif
  43771. #ifndef NO_WOLFSSL_SERVER
  43772. #ifndef WOLFSSL_NO_TLS12
  43773. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43774. #endif
  43775. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43776. #endif
  43777. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43778. #ifndef NO_WOLFSSL_CLIENT
  43779. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43780. #endif
  43781. #ifndef NO_WOLFSSL_SERVER
  43782. #ifndef WOLFSSL_NO_TLS12
  43783. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43784. #endif
  43785. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43786. #endif
  43787. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43788. #ifndef NO_WOLFSSL_CLIENT
  43789. #ifndef WOLFSSL_NO_TLS12
  43790. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43791. #endif
  43792. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43793. #endif
  43794. #ifndef NO_WOLFSSL_SERVER
  43795. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43796. #endif
  43797. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43798. #ifndef NO_WOLFSSL_CLIENT
  43799. #ifndef WOLFSSL_NO_TLS12
  43800. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43801. #endif
  43802. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43803. #endif
  43804. #ifndef NO_WOLFSSL_SERVER
  43805. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43806. #endif
  43807. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43808. #ifndef NO_WOLFSSL_CLIENT
  43809. #ifndef WOLFSSL_NO_TLS12
  43810. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43811. #endif
  43812. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43813. #endif
  43814. #ifndef NO_WOLFSSL_SERVER
  43815. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43816. #endif
  43817. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43818. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43819. #ifndef NO_WOLFSSL_CLIENT
  43820. #ifndef WOLFSSL_NO_TLS12
  43821. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43822. BAD_FUNC_ARG);
  43823. #endif
  43824. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43825. #endif
  43826. #ifndef NO_WOLFSSL_SERVER
  43827. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43828. #endif
  43829. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43830. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43831. #ifndef NO_WOLFSSL_SERVER
  43832. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43833. #endif
  43834. #ifndef NO_WOLFSSL_CLIENT
  43835. #ifndef WOLFSSL_NO_TLS12
  43836. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43837. BAD_FUNC_ARG);
  43838. #endif
  43839. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43840. #endif
  43841. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43842. #ifndef NO_WOLFSSL_SERVER
  43843. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43844. #endif
  43845. #ifndef NO_WOLFSSL_CLIENT
  43846. #ifndef WOLFSSL_NO_TLS12
  43847. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43848. BAD_FUNC_ARG);
  43849. #endif
  43850. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  43851. #endif
  43852. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  43853. #ifndef NO_WOLFSSL_CLIENT
  43854. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  43855. #endif
  43856. #ifndef NO_WOLFSSL_SERVER
  43857. #ifndef WOLFSSL_NO_TLS12
  43858. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  43859. BAD_FUNC_ARG);
  43860. #endif
  43861. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  43862. #endif
  43863. #endif
  43864. #ifdef HAVE_ECC
  43865. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  43866. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  43867. #ifndef NO_WOLFSSL_SERVER
  43868. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  43869. #endif
  43870. #ifndef NO_WOLFSSL_CLIENT
  43871. #ifndef WOLFSSL_NO_TLS12
  43872. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  43873. #endif
  43874. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  43875. #endif
  43876. #endif
  43877. #ifdef HAVE_SUPPORTED_CURVES
  43878. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43879. #ifndef NO_WOLFSSL_CLIENT
  43880. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  43881. #endif
  43882. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43883. #ifndef NO_WOLFSSL_CLIENT
  43884. #ifndef WOLFSSL_NO_TLS12
  43885. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  43886. BAD_FUNC_ARG);
  43887. #endif
  43888. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  43889. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43890. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  43891. WOLFSSL_SUCCESS);
  43892. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  43893. BAD_FUNC_ARG);
  43894. #endif
  43895. #ifndef NO_WOLFSSL_SERVER
  43896. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  43897. WOLFSSL_SUCCESS);
  43898. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  43899. BAD_FUNC_ARG);
  43900. #endif
  43901. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43902. #ifndef NO_WOLFSSL_CLIENT
  43903. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  43904. #endif
  43905. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43906. #ifndef NO_WOLFSSL_CLIENT
  43907. #ifndef WOLFSSL_NO_TLS12
  43908. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  43909. BAD_FUNC_ARG);
  43910. #endif
  43911. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  43912. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43913. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  43914. WOLFSSL_SUCCESS);
  43915. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  43916. BAD_FUNC_ARG);
  43917. #endif
  43918. #ifndef NO_WOLFSSL_SERVER
  43919. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  43920. WOLFSSL_SUCCESS);
  43921. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  43922. BAD_FUNC_ARG);
  43923. #endif
  43924. #ifdef OPENSSL_EXTRA
  43925. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43926. #ifndef NO_WOLFSSL_CLIENT
  43927. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  43928. WOLFSSL_FAILURE);
  43929. #endif
  43930. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  43931. WOLFSSL_FAILURE);
  43932. #ifndef NO_WOLFSSL_CLIENT
  43933. #ifndef WOLFSSL_NO_TLS12
  43934. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  43935. WOLFSSL_FAILURE);
  43936. #endif
  43937. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  43938. WOLFSSL_SUCCESS);
  43939. #endif
  43940. #ifndef NO_WOLFSSL_SERVER
  43941. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  43942. WOLFSSL_SUCCESS);
  43943. #endif
  43944. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43945. #ifndef NO_WOLFSSL_CLIENT
  43946. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  43947. #endif
  43948. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43949. #ifndef NO_WOLFSSL_CLIENT
  43950. #ifndef WOLFSSL_NO_TLS12
  43951. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  43952. WOLFSSL_FAILURE);
  43953. #endif
  43954. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  43955. WOLFSSL_SUCCESS);
  43956. #endif
  43957. #ifndef NO_WOLFSSL_SERVER
  43958. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  43959. WOLFSSL_SUCCESS);
  43960. #endif
  43961. #endif /* OPENSSL_EXTRA */
  43962. #endif /* HAVE_SUPPORTED_CURVES */
  43963. #endif /* HAVE_ECC */
  43964. #ifdef WOLFSSL_EARLY_DATA
  43965. #ifndef OPENSSL_EXTRA
  43966. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43967. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43968. #else
  43969. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43970. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43971. #endif
  43972. #ifndef NO_WOLFSSL_CLIENT
  43973. #ifndef OPENSSL_EXTRA
  43974. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43975. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43976. #else
  43977. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43978. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43979. #endif
  43980. #endif
  43981. #ifndef NO_WOLFSSL_SERVER
  43982. #ifndef WOLFSSL_NO_TLS12
  43983. #ifndef OPENSSL_EXTRA
  43984. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43985. BAD_FUNC_ARG);
  43986. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43987. #else
  43988. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43989. BAD_FUNC_ARG);
  43990. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43991. #endif
  43992. #endif
  43993. #ifndef OPENSSL_EXTRA
  43994. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43995. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  43996. WOLFSSL_SUCCESS);
  43997. #else
  43998. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  43999. #endif
  44000. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  44001. #else
  44002. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  44003. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  44004. #endif
  44005. #endif
  44006. #ifndef OPENSSL_EXTRA
  44007. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44008. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  44009. #else
  44010. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  44011. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  44012. #endif
  44013. #ifndef NO_WOLFSSL_CLIENT
  44014. #ifndef OPENSSL_EXTRA
  44015. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  44016. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  44017. #else
  44018. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  44019. #endif
  44020. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  44021. #else
  44022. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  44023. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  44024. #endif
  44025. #endif
  44026. #ifndef NO_WOLFSSL_SERVER
  44027. #ifndef WOLFSSL_NO_TLS12
  44028. #ifndef OPENSSL_EXTRA
  44029. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  44030. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  44031. #else
  44032. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  44033. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  44034. #endif
  44035. #endif
  44036. #ifndef OPENSSL_EXTRA
  44037. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  44038. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  44039. #else
  44040. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  44041. #endif
  44042. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  44043. #else
  44044. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  44045. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  44046. #endif
  44047. #endif
  44048. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  44049. &outSz), BAD_FUNC_ARG);
  44050. #ifndef NO_WOLFSSL_CLIENT
  44051. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  44052. &outSz), BAD_FUNC_ARG);
  44053. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  44054. BAD_FUNC_ARG);
  44055. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44056. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  44057. #endif
  44058. #ifndef NO_WOLFSSL_SERVER
  44059. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  44060. sizeof(earlyData), &outSz), SIDE_ERROR);
  44061. #endif
  44062. #ifndef NO_WOLFSSL_CLIENT
  44063. #ifndef WOLFSSL_NO_TLS12
  44064. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  44065. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  44066. #endif
  44067. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  44068. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  44069. #endif
  44070. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  44071. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44072. #ifndef NO_WOLFSSL_SERVER
  44073. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  44074. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44075. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  44076. &outSz), BAD_FUNC_ARG);
  44077. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44078. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  44079. #endif
  44080. #ifndef NO_WOLFSSL_CLIENT
  44081. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  44082. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  44083. #endif
  44084. #ifndef NO_WOLFSSL_SERVER
  44085. #ifndef WOLFSSL_NO_TLS12
  44086. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  44087. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  44088. #endif
  44089. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  44090. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  44091. #endif
  44092. #endif
  44093. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  44094. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  44095. #endif
  44096. #ifndef NO_WOLFSSL_SERVER
  44097. wolfSSL_free(serverSsl);
  44098. wolfSSL_CTX_free(serverCtx);
  44099. #endif
  44100. #ifndef NO_WOLFSSL_CLIENT
  44101. wolfSSL_free(clientSsl);
  44102. wolfSSL_CTX_free(clientCtx);
  44103. #endif
  44104. #ifndef WOLFSSL_NO_TLS12
  44105. #ifndef NO_WOLFSSL_SERVER
  44106. wolfSSL_free(serverTls12Ssl);
  44107. wolfSSL_CTX_free(serverTls12Ctx);
  44108. #endif
  44109. #ifndef NO_WOLFSSL_CLIENT
  44110. wolfSSL_free(clientTls12Ssl);
  44111. wolfSSL_CTX_free(clientTls12Ctx);
  44112. #endif
  44113. #endif
  44114. return EXPECT_RESULT();
  44115. }
  44116. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44117. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44118. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44119. /* Called when writing. */
  44120. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44121. {
  44122. (void)ssl;
  44123. (void)buf;
  44124. (void)sz;
  44125. (void)ctx;
  44126. /* Force error return from wolfSSL_accept_TLSv13(). */
  44127. return WANT_WRITE;
  44128. }
  44129. /* Called when reading. */
  44130. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  44131. {
  44132. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  44133. int len = (int)msg->length;
  44134. (void)ssl;
  44135. (void)sz;
  44136. /* Pass back as much of message as will fit in buffer. */
  44137. if (len > sz)
  44138. len = sz;
  44139. XMEMCPY(buf, msg->buffer, len);
  44140. /* Move over returned data. */
  44141. msg->buffer += len;
  44142. msg->length -= len;
  44143. /* Amount actually copied. */
  44144. return len;
  44145. }
  44146. #endif
  44147. static int test_tls13_cipher_suites(void)
  44148. {
  44149. EXPECT_DECLS;
  44150. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  44151. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  44152. defined(BUILD_TLS_AES_256_GCM_SHA384)
  44153. WOLFSSL_CTX* ctx = NULL;
  44154. WOLFSSL *ssl = NULL;
  44155. int i;
  44156. byte clientHello[] = {
  44157. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  44158. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  44159. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  44160. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  44161. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  44162. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  44163. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  44164. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  44165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  44167. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  44168. 0x13, 0x01,
  44169. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  44170. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  44171. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  44172. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  44173. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  44174. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  44175. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  44176. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  44177. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  44178. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  44179. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  44180. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  44181. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  44182. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  44183. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  44184. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  44185. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  44186. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  44187. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  44188. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  44189. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  44190. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  44191. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  44192. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  44193. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  44194. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  44195. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  44196. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  44197. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  44198. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  44199. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  44200. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  44201. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  44202. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  44203. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  44204. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44205. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44206. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  44207. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  44208. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  44209. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  44210. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  44211. };
  44212. WOLFSSL_BUFFER_INFO msg;
  44213. /* Offset into ClientHello message data of first cipher suite. */
  44214. const int csOff = 78;
  44215. /* Server cipher list. */
  44216. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  44217. /* Suite list with duplicates. */
  44218. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  44219. "TLS13-AES128-GCM-SHA256:"
  44220. "TLS13-AES256-GCM-SHA384:"
  44221. "TLS13-AES256-GCM-SHA384:"
  44222. "TLS13-AES128-GCM-SHA256";
  44223. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44224. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44225. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  44226. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44227. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  44228. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  44229. #endif
  44230. /* Set up wolfSSL context. */
  44231. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  44232. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  44233. WOLFSSL_FILETYPE_PEM));
  44234. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  44235. WOLFSSL_FILETYPE_PEM));
  44236. /* Read from 'msg'. */
  44237. wolfSSL_SetIORecv(ctx, CsRecv);
  44238. /* No where to send to - dummy sender. */
  44239. wolfSSL_SetIOSend(ctx, CsSend);
  44240. /* Test cipher suite list with many copies of a cipher suite. */
  44241. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44242. msg.buffer = clientHello;
  44243. msg.length = (unsigned int)sizeof(clientHello);
  44244. wolfSSL_SetIOReadCtx(ssl, &msg);
  44245. /* Force server to have as many occurrences of same cipher suite as
  44246. * possible. */
  44247. if (ssl != NULL) {
  44248. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  44249. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  44250. for (i = 0; i < suites->suiteSz; i += 2) {
  44251. suites->suites[i + 0] = TLS13_BYTE;
  44252. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  44253. }
  44254. }
  44255. /* Test multiple occurrences of same cipher suite. */
  44256. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44257. wolfSSL_free(ssl);
  44258. ssl = NULL;
  44259. /* Set client order opposite to server order:
  44260. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  44261. clientHello[csOff + 0] = TLS13_BYTE;
  44262. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  44263. clientHello[csOff + 2] = TLS13_BYTE;
  44264. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  44265. /* Test server order negotiation. */
  44266. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44267. msg.buffer = clientHello;
  44268. msg.length = (unsigned int)sizeof(clientHello);
  44269. wolfSSL_SetIOReadCtx(ssl, &msg);
  44270. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44271. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44272. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  44273. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44274. /* Check refined order - server order. */
  44275. ExpectIntEQ(ssl->suites->suiteSz, 4);
  44276. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44277. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44278. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44279. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44280. wolfSSL_free(ssl);
  44281. ssl = NULL;
  44282. /* Test client order negotiation. */
  44283. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44284. msg.buffer = clientHello;
  44285. msg.length = (unsigned int)sizeof(clientHello);
  44286. wolfSSL_SetIOReadCtx(ssl, &msg);
  44287. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  44288. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  44289. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  44290. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  44291. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  44292. /* Check refined order - client order. */
  44293. ExpectIntEQ(ssl->suites->suiteSz, 4);
  44294. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  44295. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44296. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  44297. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44298. wolfSSL_free(ssl);
  44299. ssl = NULL;
  44300. /* Check duplicate detection is working. */
  44301. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  44302. ExpectIntEQ(ctx->suites->suiteSz, 4);
  44303. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44304. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  44305. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44306. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  44307. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  44308. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  44309. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  44310. ExpectIntEQ(ctx->suites->suiteSz, 4);
  44311. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  44312. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  44313. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  44314. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  44315. #endif
  44316. wolfSSL_CTX_free(ctx);
  44317. #endif
  44318. return EXPECT_RESULT();
  44319. }
  44320. #endif
  44321. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  44322. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  44323. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44324. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  44325. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  44326. const unsigned char* priv, unsigned int privSz,
  44327. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  44328. unsigned char* out, unsigned int* outlen,
  44329. void* ctx)
  44330. {
  44331. int result;
  44332. /* Test fail when context associated with WOLFSSL is NULL */
  44333. if (ctx == NULL) {
  44334. return -1;
  44335. }
  44336. (void)ssl;
  44337. /* return 0 on success */
  44338. PRIVATE_KEY_UNLOCK();
  44339. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  44340. PRIVATE_KEY_LOCK();
  44341. return result;
  44342. }
  44343. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  44344. EXPECT_DECLS;
  44345. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  44346. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  44347. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  44348. WOLFSSL_SUCCESS);
  44349. #endif
  44350. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  44351. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  44352. WOLFSSL_SUCCESS);
  44353. #endif
  44354. return EXPECT_RESULT();
  44355. }
  44356. static int test_dh_ssl_setup(WOLFSSL* ssl)
  44357. {
  44358. EXPECT_DECLS;
  44359. static int dh_test_ctx = 1;
  44360. int ret;
  44361. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  44362. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  44363. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44364. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44365. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  44366. }
  44367. return EXPECT_RESULT();
  44368. }
  44369. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  44370. {
  44371. EXPECT_DECLS;
  44372. int ret;
  44373. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  44374. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  44375. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  44376. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  44377. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  44378. }
  44379. return EXPECT_RESULT();
  44380. }
  44381. #endif
  44382. static int test_DhCallbacks(void)
  44383. {
  44384. EXPECT_DECLS;
  44385. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  44386. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  44387. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  44388. WOLFSSL_CTX *ctx = NULL;
  44389. WOLFSSL *ssl = NULL;
  44390. int test;
  44391. test_ssl_cbf func_cb_client;
  44392. test_ssl_cbf func_cb_server;
  44393. /* Test that DH callback APIs work. */
  44394. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44395. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  44396. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  44397. /* load client ca cert */
  44398. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  44399. WOLFSSL_SUCCESS);
  44400. /* test with NULL arguments */
  44401. wolfSSL_SetDhAgreeCtx(NULL, &test);
  44402. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  44403. /* test success case */
  44404. test = 1;
  44405. ExpectNotNull(ssl = wolfSSL_new(ctx));
  44406. wolfSSL_SetDhAgreeCtx(ssl, &test);
  44407. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  44408. wolfSSL_free(ssl);
  44409. wolfSSL_CTX_free(ctx);
  44410. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  44411. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  44412. /* set callbacks to use DH functions */
  44413. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44414. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  44415. func_cb_client.method = wolfTLSv1_2_client_method;
  44416. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44417. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  44418. func_cb_server.method = wolfTLSv1_2_server_method;
  44419. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  44420. &func_cb_server, NULL), TEST_SUCCESS);
  44421. /* Test fail */
  44422. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  44423. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  44424. /* set callbacks to use DH functions */
  44425. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  44426. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  44427. func_cb_client.method = wolfTLSv1_2_client_method;
  44428. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  44429. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  44430. func_cb_server.method = wolfTLSv1_2_server_method;
  44431. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  44432. &func_cb_server, NULL), TEST_FAIL);
  44433. #endif
  44434. return EXPECT_RESULT();
  44435. }
  44436. #endif /* HAVE_PK_CALLBACKS */
  44437. #ifdef HAVE_HASHDRBG
  44438. #ifdef TEST_RESEED_INTERVAL
  44439. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44440. {
  44441. EXPECT_DECLS;
  44442. int i;
  44443. WC_RNG rng;
  44444. byte key[32];
  44445. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44446. ExpectIntEQ(wc_InitRng(&rng), 0);
  44447. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44448. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  44449. }
  44450. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44451. return EXPECT_RESULT();
  44452. }
  44453. #endif /* TEST_RESEED_INTERVAL */
  44454. static int test_wc_RNG_GenerateBlock(void)
  44455. {
  44456. EXPECT_DECLS;
  44457. int i;
  44458. WC_RNG rng;
  44459. byte key[32];
  44460. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44461. ExpectIntEQ(wc_InitRng(&rng), 0);
  44462. for (i = 0; i < 10; i++) {
  44463. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  44464. }
  44465. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44466. return EXPECT_RESULT();
  44467. }
  44468. #endif /* HAVE_HASHDRBG */
  44469. /*
  44470. * Testing get_rand_digit
  44471. */
  44472. static int test_get_rand_digit(void)
  44473. {
  44474. EXPECT_DECLS;
  44475. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44476. WC_RNG rng;
  44477. mp_digit d;
  44478. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44479. ExpectIntEQ(wc_InitRng(&rng), 0);
  44480. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  44481. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  44482. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  44483. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  44484. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44485. #endif
  44486. return EXPECT_RESULT();
  44487. } /* End test_get_rand_digit*/
  44488. /*
  44489. * Testing get_digit_count
  44490. */
  44491. static int test_get_digit_count(void)
  44492. {
  44493. EXPECT_DECLS;
  44494. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44495. mp_int a;
  44496. XMEMSET(&a, 0, sizeof(mp_int));
  44497. ExpectIntEQ(mp_init(&a), 0);
  44498. ExpectIntEQ(get_digit_count(NULL), 0);
  44499. ExpectIntEQ(get_digit_count(&a), 0);
  44500. mp_clear(&a);
  44501. #endif
  44502. return EXPECT_RESULT();
  44503. } /* End test_get_digit_count*/
  44504. /*
  44505. * Testing mp_cond_copy
  44506. */
  44507. static int test_mp_cond_copy(void)
  44508. {
  44509. EXPECT_DECLS;
  44510. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44511. defined(WOLFSSL_PUBLIC_MP)
  44512. mp_int a;
  44513. mp_int b;
  44514. int copy = 0;
  44515. XMEMSET(&a, 0, sizeof(mp_int));
  44516. XMEMSET(&b, 0, sizeof(mp_int));
  44517. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44518. ExpectIntEQ(mp_init(&b), MP_OKAY);
  44519. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  44520. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  44521. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  44522. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  44523. mp_clear(&a);
  44524. mp_clear(&b);
  44525. #endif
  44526. return EXPECT_RESULT();
  44527. } /* End test_mp_cond_copy*/
  44528. /*
  44529. * Testing mp_rand
  44530. */
  44531. static int test_mp_rand(void)
  44532. {
  44533. EXPECT_DECLS;
  44534. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44535. mp_int a;
  44536. WC_RNG rng;
  44537. int digits = 1;
  44538. XMEMSET(&a, 0, sizeof(mp_int));
  44539. XMEMSET(&rng, 0, sizeof(WC_RNG));
  44540. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44541. ExpectIntEQ(wc_InitRng(&rng), 0);
  44542. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  44543. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  44544. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  44545. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  44546. mp_clear(&a);
  44547. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  44548. #endif
  44549. return EXPECT_RESULT();
  44550. } /* End test_mp_rand*/
  44551. /*
  44552. * Testing get_digit
  44553. */
  44554. static int test_get_digit(void)
  44555. {
  44556. EXPECT_DECLS;
  44557. #if defined(WOLFSSL_PUBLIC_MP)
  44558. mp_int a;
  44559. int n = 0;
  44560. XMEMSET(&a, 0, sizeof(mp_int));
  44561. ExpectIntEQ(mp_init(&a), MP_OKAY);
  44562. ExpectIntEQ(get_digit(NULL, n), 0);
  44563. ExpectIntEQ(get_digit(&a, n), 0);
  44564. mp_clear(&a);
  44565. #endif
  44566. return EXPECT_RESULT();
  44567. } /* End test_get_digit*/
  44568. /*
  44569. * Testing wc_export_int
  44570. */
  44571. static int test_wc_export_int(void)
  44572. {
  44573. EXPECT_DECLS;
  44574. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44575. defined(WOLFSSL_PUBLIC_MP)
  44576. mp_int mp;
  44577. byte buf[32];
  44578. word32 keySz = (word32)sizeof(buf);
  44579. word32 len = (word32)sizeof(buf);
  44580. XMEMSET(&mp, 0, sizeof(mp_int));
  44581. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  44582. ExpectIntEQ(mp_set(&mp, 1234), 0);
  44583. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  44584. BAD_FUNC_ARG);
  44585. len = sizeof(buf)-1;
  44586. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  44587. BUFFER_E);
  44588. len = sizeof(buf);
  44589. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  44590. len = 4; /* test input too small */
  44591. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  44592. len = sizeof(buf);
  44593. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  44594. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44595. ExpectIntEQ(len, 5);
  44596. mp_clear(&mp);
  44597. #endif
  44598. return EXPECT_RESULT();
  44599. } /* End test_wc_export_int*/
  44600. static int test_wc_InitRngNonce(void)
  44601. {
  44602. EXPECT_DECLS;
  44603. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44604. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44605. HAVE_FIPS_VERSION >= 2))
  44606. WC_RNG rng;
  44607. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44608. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44609. word32 nonceSz = sizeof(nonce);
  44610. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  44611. ExpectIntEQ(wc_FreeRng(&rng), 0);
  44612. #endif
  44613. return EXPECT_RESULT();
  44614. } /* End test_wc_InitRngNonce*/
  44615. /*
  44616. * Testing wc_InitRngNonce_ex
  44617. */
  44618. static int test_wc_InitRngNonce_ex(void)
  44619. {
  44620. EXPECT_DECLS;
  44621. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44622. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44623. HAVE_FIPS_VERSION >= 2))
  44624. WC_RNG rng;
  44625. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44626. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44627. word32 nonceSz = sizeof(nonce);
  44628. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  44629. 0);
  44630. ExpectIntEQ(wc_FreeRng(&rng), 0);
  44631. #endif
  44632. return EXPECT_RESULT();
  44633. } /* End test_wc_InitRngNonce_ex */
  44634. static int test_wolfSSL_X509_CRL(void)
  44635. {
  44636. EXPECT_DECLS;
  44637. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44638. X509_CRL *crl = NULL;
  44639. char pem[][100] = {
  44640. "./certs/crl/crl.pem",
  44641. "./certs/crl/crl2.pem",
  44642. "./certs/crl/caEccCrl.pem",
  44643. "./certs/crl/eccCliCRL.pem",
  44644. "./certs/crl/eccSrvCRL.pem",
  44645. ""
  44646. };
  44647. #ifndef NO_BIO
  44648. BIO *bio = NULL;
  44649. #endif
  44650. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44651. char der[][100] = {
  44652. "./certs/crl/crl.der",
  44653. "./certs/crl/crl2.der",
  44654. ""};
  44655. #endif
  44656. XFILE fp = XBADFILE;
  44657. int i;
  44658. for (i = 0; pem[i][0] != '\0'; i++)
  44659. {
  44660. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  44661. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  44662. NULL, NULL));
  44663. ExpectNotNull(crl);
  44664. X509_CRL_free(crl);
  44665. if (fp != XBADFILE) {
  44666. XFCLOSE(fp);
  44667. fp = XBADFILE;
  44668. }
  44669. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  44670. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  44671. NULL));
  44672. if (EXPECT_FAIL()) {
  44673. crl = NULL;
  44674. }
  44675. ExpectNotNull(crl);
  44676. X509_CRL_free(crl);
  44677. crl = NULL;
  44678. if (fp != XBADFILE) {
  44679. XFCLOSE(fp);
  44680. fp = XBADFILE;
  44681. }
  44682. }
  44683. #ifndef NO_BIO
  44684. for (i = 0; pem[i][0] != '\0'; i++)
  44685. {
  44686. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  44687. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44688. X509_CRL_free(crl);
  44689. crl = NULL;
  44690. BIO_free(bio);
  44691. bio = NULL;
  44692. }
  44693. #endif
  44694. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44695. for (i = 0; der[i][0] != '\0'; i++) {
  44696. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  44697. ExpectTrue((fp != XBADFILE));
  44698. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44699. ExpectNotNull(crl);
  44700. X509_CRL_free(crl);
  44701. if (fp != XBADFILE) {
  44702. XFCLOSE(fp);
  44703. fp = XBADFILE;
  44704. }
  44705. fp = XFOPEN(der[i], "rb");
  44706. ExpectTrue((fp != XBADFILE));
  44707. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44708. if (EXPECT_FAIL()) {
  44709. crl = NULL;
  44710. }
  44711. ExpectNotNull(crl);
  44712. X509_CRL_free(crl);
  44713. crl = NULL;
  44714. if (fp != XBADFILE) {
  44715. XFCLOSE(fp);
  44716. fp = XBADFILE;
  44717. }
  44718. }
  44719. #endif
  44720. #endif
  44721. return EXPECT_RESULT();
  44722. }
  44723. static int test_wolfSSL_X509_load_crl_file(void)
  44724. {
  44725. EXPECT_DECLS;
  44726. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44727. !defined(NO_RSA) && !defined(NO_BIO)
  44728. int i;
  44729. char pem[][100] = {
  44730. "./certs/crl/crl.pem",
  44731. "./certs/crl/crl2.pem",
  44732. "./certs/crl/caEccCrl.pem",
  44733. "./certs/crl/eccCliCRL.pem",
  44734. "./certs/crl/eccSrvCRL.pem",
  44735. ""
  44736. };
  44737. char der[][100] = {
  44738. "./certs/crl/crl.der",
  44739. "./certs/crl/crl2.der",
  44740. ""
  44741. };
  44742. WOLFSSL_X509_STORE* store = NULL;
  44743. WOLFSSL_X509_LOOKUP* lookup = NULL;
  44744. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44745. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44746. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44747. X509_FILETYPE_PEM), 1);
  44748. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44749. X509_FILETYPE_PEM), 1);
  44750. if (store) {
  44751. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44752. WOLFSSL_FILETYPE_PEM), 1);
  44753. /* since store hasn't yet known the revoked cert*/
  44754. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44755. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  44756. }
  44757. for (i = 0; pem[i][0] != '\0'; i++) {
  44758. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  44759. 1);
  44760. }
  44761. if (store) {
  44762. /* since store knows crl list */
  44763. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44764. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  44765. CRL_CERT_REVOKED);
  44766. }
  44767. /* once feeing store */
  44768. X509_STORE_free(store);
  44769. store = NULL;
  44770. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44771. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44772. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44773. X509_FILETYPE_PEM), 1);
  44774. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44775. X509_FILETYPE_PEM), 1);
  44776. if (store) {
  44777. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44778. WOLFSSL_FILETYPE_PEM), 1);
  44779. /* since store hasn't yet known the revoked cert*/
  44780. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44781. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  44782. }
  44783. for (i = 0; der[i][0] != '\0'; i++) {
  44784. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  44785. 1);
  44786. }
  44787. if (store) {
  44788. /* since store knows crl list */
  44789. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  44790. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  44791. CRL_CERT_REVOKED);
  44792. }
  44793. /* test for incorrect parameter */
  44794. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44795. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44796. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44797. X509_STORE_free(store);
  44798. store = NULL;
  44799. #endif
  44800. return EXPECT_RESULT();
  44801. }
  44802. static int test_wolfSSL_d2i_X509_REQ(void)
  44803. {
  44804. EXPECT_DECLS;
  44805. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44806. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44807. !defined(WOLFSSL_SP_MATH)
  44808. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  44809. * were generated by libest
  44810. * ./certs/csr.attr.der contains sample attributes
  44811. * ./certs/csr.ext.der contains sample extensions */
  44812. const char* csrFile = "./certs/csr.signed.der";
  44813. const char* csrPopFile = "./certs/csr.attr.der";
  44814. const char* csrExtFile = "./certs/csr.ext.der";
  44815. /* ./certs/csr.dsa.pem is generated using
  44816. * openssl req -newkey dsa:certs/dsaparams.pem \
  44817. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44818. * -outform PEM
  44819. * with the passphrase "wolfSSL"
  44820. */
  44821. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44822. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44823. XFILE f = XBADFILE;
  44824. #endif
  44825. BIO* bio = NULL;
  44826. X509* req = NULL;
  44827. EVP_PKEY *pub_key = NULL;
  44828. {
  44829. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  44830. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  44831. /*
  44832. * Extract the public key from the CSR
  44833. */
  44834. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  44835. /*
  44836. * Verify the signature in the CSR
  44837. */
  44838. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44839. X509_free(req);
  44840. req = NULL;
  44841. BIO_free(bio);
  44842. bio = NULL;
  44843. EVP_PKEY_free(pub_key);
  44844. pub_key = NULL;
  44845. }
  44846. {
  44847. #ifdef OPENSSL_ALL
  44848. X509_ATTRIBUTE* attr;
  44849. ASN1_TYPE *at;
  44850. #endif
  44851. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  44852. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  44853. /*
  44854. * Extract the public key from the CSR
  44855. */
  44856. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  44857. /*
  44858. * Verify the signature in the CSR
  44859. */
  44860. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44861. #ifdef OPENSSL_ALL
  44862. /*
  44863. * Obtain the challenge password from the CSR
  44864. */
  44865. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  44866. -1), 1);
  44867. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  44868. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44869. ExpectNotNull(at->value.asn1_string);
  44870. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  44871. "2xIE+qqp/rhyTXP+");
  44872. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  44873. #endif
  44874. X509_free(req);
  44875. req = NULL;
  44876. BIO_free(bio);
  44877. bio = NULL;
  44878. EVP_PKEY_free(pub_key);
  44879. pub_key = NULL;
  44880. }
  44881. {
  44882. #ifdef OPENSSL_ALL
  44883. X509_ATTRIBUTE* attr;
  44884. ASN1_TYPE *at;
  44885. STACK_OF(X509_EXTENSION) *exts = NULL;
  44886. #endif
  44887. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  44888. /* This CSR contains an Extension Request attribute so
  44889. * we test extension parsing in a CSR attribute here. */
  44890. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  44891. /*
  44892. * Extract the public key from the CSR
  44893. */
  44894. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  44895. /*
  44896. * Verify the signature in the CSR
  44897. */
  44898. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44899. #ifdef OPENSSL_ALL
  44900. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  44901. req));
  44902. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  44903. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  44904. /*
  44905. * Obtain the challenge password from the CSR
  44906. */
  44907. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  44908. -1), 0);
  44909. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  44910. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44911. ExpectNotNull(at->value.asn1_string);
  44912. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  44913. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  44914. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  44915. #endif
  44916. X509_free(req);
  44917. req = NULL;
  44918. BIO_free(bio);
  44919. bio = NULL;
  44920. EVP_PKEY_free(pub_key);
  44921. pub_key = NULL;
  44922. }
  44923. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44924. {
  44925. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  44926. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  44927. /*
  44928. * Extract the public key from the CSR
  44929. */
  44930. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  44931. /*
  44932. * Verify the signature in the CSR
  44933. */
  44934. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44935. X509_free(req);
  44936. req = NULL;
  44937. BIO_free(bio);
  44938. /* Run the same test, but with a file pointer instead of a BIO.
  44939. * (PEM_read_X509_REQ)*/
  44940. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  44941. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  44942. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  44943. X509_free(req);
  44944. EVP_PKEY_free(pub_key);
  44945. }
  44946. #endif /* !NO_DSA && !HAVE_SELFTEST */
  44947. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  44948. return EXPECT_RESULT();
  44949. }
  44950. static int test_wolfSSL_PEM_read_X509(void)
  44951. {
  44952. EXPECT_DECLS;
  44953. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44954. !defined(NO_RSA)
  44955. X509 *x509 = NULL;
  44956. XFILE fp = XBADFILE;
  44957. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  44958. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  44959. X509_free(x509);
  44960. if (fp != XBADFILE)
  44961. XFCLOSE(fp);
  44962. #endif
  44963. return EXPECT_RESULT();
  44964. }
  44965. static int test_wolfSSL_PEM_read(void)
  44966. {
  44967. EXPECT_DECLS;
  44968. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44969. const char* filename = "./certs/server-keyEnc.pem";
  44970. XFILE fp = XBADFILE;
  44971. char* name = NULL;
  44972. char* header = NULL;
  44973. byte* data = NULL;
  44974. long len;
  44975. EVP_CIPHER_INFO cipher;
  44976. WOLFSSL_BIO* bio = NULL;
  44977. byte* fileData = NULL;
  44978. size_t fileDataSz = 0;
  44979. byte* out;
  44980. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  44981. /* Fail cases. */
  44982. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  44983. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  44984. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  44985. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  44986. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44987. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44988. ExpectIntGT(XSTRLEN(header), 0);
  44989. ExpectIntGT(len, 0);
  44990. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44991. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  44992. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44993. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44994. DYNAMIC_TYPE_TMP_BUFFER));
  44995. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44996. if (fp != XBADFILE) {
  44997. XFCLOSE(fp);
  44998. fp = XBADFILE;
  44999. }
  45000. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45001. /* Fail cases. */
  45002. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  45003. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  45004. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  45005. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  45006. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45007. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45008. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45009. /* Fail cases. */
  45010. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  45011. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  45012. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  45013. #ifndef NO_DES3
  45014. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  45015. #endif
  45016. /* Fail cases. */
  45017. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  45018. (void*)"yassl123"), WOLFSSL_FAILURE);
  45019. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  45020. (void*)"yassl123"), WOLFSSL_FAILURE);
  45021. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  45022. (void*)"yassl123"), WOLFSSL_FAILURE);
  45023. #if !defined(NO_DES3) && !defined(NO_MD5)
  45024. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  45025. (void*)"yassl123"), WOLFSSL_SUCCESS);
  45026. #endif
  45027. BIO_free(bio);
  45028. bio = NULL;
  45029. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45030. fileData = NULL;
  45031. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45032. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45033. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45034. name = NULL;
  45035. header = NULL;
  45036. data = NULL;
  45037. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  45038. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  45039. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  45040. ExpectIntEQ(XSTRLEN(header), 0);
  45041. ExpectIntGT(len, 0);
  45042. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  45043. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  45044. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  45045. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  45046. DYNAMIC_TYPE_TMP_BUFFER));
  45047. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  45048. if (fp != XBADFILE)
  45049. XFCLOSE(fp);
  45050. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45051. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  45052. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  45053. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  45054. BIO_free(bio);
  45055. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45056. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45057. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45058. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45059. #endif
  45060. return EXPECT_RESULT();
  45061. }
  45062. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  45063. {
  45064. EXPECT_DECLS;
  45065. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45066. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45067. const byte iv[12] = { 0 };
  45068. const byte key[16] = { 0 };
  45069. const byte cleartext[16] = { 0 };
  45070. const byte aad[] = {
  45071. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  45072. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  45073. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  45074. 0x4f
  45075. };
  45076. byte out1Part[16];
  45077. byte outTag1Part[16];
  45078. byte out2Part[16];
  45079. byte outTag2Part[16];
  45080. byte decryptBuf[16];
  45081. int len = 0;
  45082. int tlen;
  45083. EVP_CIPHER_CTX* ctx = NULL;
  45084. /* ENCRYPT */
  45085. /* Send AAD and data in 1 part */
  45086. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45087. tlen = 0;
  45088. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45089. 1);
  45090. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45091. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45092. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  45093. sizeof(cleartext)), 1);
  45094. tlen += len;
  45095. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  45096. tlen += len;
  45097. ExpectIntEQ(tlen, sizeof(cleartext));
  45098. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45099. outTag1Part), 1);
  45100. EVP_CIPHER_CTX_free(ctx);
  45101. ctx = NULL;
  45102. /* DECRYPT */
  45103. /* Send AAD and data in 1 part */
  45104. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45105. tlen = 0;
  45106. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45107. 1);
  45108. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45109. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  45110. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  45111. sizeof(cleartext)), 1);
  45112. tlen += len;
  45113. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45114. outTag1Part), 1);
  45115. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  45116. tlen += len;
  45117. ExpectIntEQ(tlen, sizeof(cleartext));
  45118. EVP_CIPHER_CTX_free(ctx);
  45119. ctx = NULL;
  45120. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45121. /* ENCRYPT */
  45122. /* Send AAD and data in 2 parts */
  45123. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45124. tlen = 0;
  45125. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45126. 1);
  45127. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45128. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45129. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45130. 1);
  45131. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  45132. tlen += len;
  45133. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  45134. sizeof(cleartext) - 1), 1);
  45135. tlen += len;
  45136. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  45137. tlen += len;
  45138. ExpectIntEQ(tlen, sizeof(cleartext));
  45139. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  45140. outTag2Part), 1);
  45141. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  45142. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  45143. EVP_CIPHER_CTX_free(ctx);
  45144. ctx = NULL;
  45145. /* DECRYPT */
  45146. /* Send AAD and data in 2 parts */
  45147. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  45148. tlen = 0;
  45149. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  45150. 1);
  45151. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  45152. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  45153. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  45154. 1);
  45155. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  45156. tlen += len;
  45157. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  45158. sizeof(cleartext) - 1), 1);
  45159. tlen += len;
  45160. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  45161. outTag1Part), 1);
  45162. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  45163. tlen += len;
  45164. ExpectIntEQ(tlen, sizeof(cleartext));
  45165. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  45166. /* Test AAD reuse */
  45167. EVP_CIPHER_CTX_free(ctx);
  45168. #endif
  45169. return EXPECT_RESULT();
  45170. }
  45171. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  45172. {
  45173. EXPECT_DECLS;
  45174. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45175. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45176. /* Zero length plain text */
  45177. byte key[] = {
  45178. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45179. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45180. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45181. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45182. }; /* align */
  45183. byte iv[] = {
  45184. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45185. }; /* align */
  45186. byte plaintxt[1];
  45187. int ivSz = 12;
  45188. int plaintxtSz = 0;
  45189. unsigned char tag[16];
  45190. unsigned char tag_kat[] = {
  45191. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  45192. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  45193. };
  45194. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45195. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45196. int ciphertxtSz = 0;
  45197. int decryptedtxtSz = 0;
  45198. int len = 0;
  45199. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45200. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45201. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  45202. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45203. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45204. plaintxtSz));
  45205. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45206. ciphertxtSz += len;
  45207. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  45208. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45209. ExpectIntEQ(0, ciphertxtSz);
  45210. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  45211. EVP_CIPHER_CTX_init(de);
  45212. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  45213. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45214. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45215. decryptedtxtSz = len;
  45216. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  45217. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45218. decryptedtxtSz += len;
  45219. ExpectIntEQ(0, decryptedtxtSz);
  45220. EVP_CIPHER_CTX_free(en);
  45221. EVP_CIPHER_CTX_free(de);
  45222. #endif
  45223. return EXPECT_RESULT();
  45224. }
  45225. static int test_wolfssl_EVP_aes_gcm(void)
  45226. {
  45227. EXPECT_DECLS;
  45228. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  45229. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45230. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45231. byte *key = (byte*)"01234567890123456789012345678901";
  45232. /* A 128 bit IV */
  45233. byte *iv = (byte*)"0123456789012345";
  45234. int ivSz = AES_BLOCK_SIZE;
  45235. /* Message to be encrypted */
  45236. byte *plaintxt = (byte*)"for things to change you have to change";
  45237. /* Additional non-confidential data */
  45238. byte *aad = (byte*)"Don't spend major time on minor things.";
  45239. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45240. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45241. int aadSz = (int)XSTRLEN((char*)aad);
  45242. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45243. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45244. int ciphertxtSz = 0;
  45245. int decryptedtxtSz = 0;
  45246. int len = 0;
  45247. int i = 0;
  45248. EVP_CIPHER_CTX en[2];
  45249. EVP_CIPHER_CTX de[2];
  45250. for (i = 0; i < 2; i++) {
  45251. EVP_CIPHER_CTX_init(&en[i]);
  45252. if (i == 0) {
  45253. /* Default uses 96-bits IV length */
  45254. #ifdef WOLFSSL_AES_128
  45255. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  45256. key, iv));
  45257. #elif defined(WOLFSSL_AES_192)
  45258. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  45259. key, iv));
  45260. #elif defined(WOLFSSL_AES_256)
  45261. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  45262. key, iv));
  45263. #endif
  45264. }
  45265. else {
  45266. #ifdef WOLFSSL_AES_128
  45267. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  45268. NULL, NULL));
  45269. #elif defined(WOLFSSL_AES_192)
  45270. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  45271. NULL, NULL));
  45272. #elif defined(WOLFSSL_AES_256)
  45273. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  45274. NULL, NULL));
  45275. #endif
  45276. /* non-default must to set the IV length first */
  45277. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  45278. ivSz, NULL));
  45279. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45280. }
  45281. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45282. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  45283. plaintxtSz));
  45284. ciphertxtSz = len;
  45285. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45286. ciphertxtSz += len;
  45287. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  45288. AES_BLOCK_SIZE, tag));
  45289. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45290. EVP_CIPHER_CTX_init(&de[i]);
  45291. if (i == 0) {
  45292. /* Default uses 96-bits IV length */
  45293. #ifdef WOLFSSL_AES_128
  45294. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  45295. key, iv));
  45296. #elif defined(WOLFSSL_AES_192)
  45297. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  45298. key, iv));
  45299. #elif defined(WOLFSSL_AES_256)
  45300. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  45301. key, iv));
  45302. #endif
  45303. }
  45304. else {
  45305. #ifdef WOLFSSL_AES_128
  45306. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  45307. NULL, NULL));
  45308. #elif defined(WOLFSSL_AES_192)
  45309. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  45310. NULL, NULL));
  45311. #elif defined(WOLFSSL_AES_256)
  45312. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  45313. NULL, NULL));
  45314. #endif
  45315. /* non-default must to set the IV length first */
  45316. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  45317. ivSz, NULL));
  45318. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45319. }
  45320. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45321. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45322. ciphertxtSz));
  45323. decryptedtxtSz = len;
  45324. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  45325. AES_BLOCK_SIZE, tag));
  45326. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45327. decryptedtxtSz += len;
  45328. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  45329. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45330. /* modify tag*/
  45331. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45332. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45333. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  45334. AES_BLOCK_SIZE, tag));
  45335. /* fail due to wrong tag */
  45336. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45337. ciphertxtSz));
  45338. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45339. ExpectIntEQ(0, len);
  45340. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45341. }
  45342. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45343. return EXPECT_RESULT();
  45344. }
  45345. static int test_wolfssl_EVP_aria_gcm(void)
  45346. {
  45347. int res = TEST_SKIPPED;
  45348. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  45349. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45350. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45351. byte *key = (byte*)"01234567890123456789012345678901";
  45352. /* A 128 bit IV */
  45353. byte *iv = (byte*)"0123456789012345";
  45354. int ivSz = ARIA_BLOCK_SIZE;
  45355. /* Message to be encrypted */
  45356. const int plaintxtSz = 40;
  45357. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  45358. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  45359. /* Additional non-confidential data */
  45360. byte *aad = (byte*)"Don't spend major time on minor things.";
  45361. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  45362. int aadSz = (int)XSTRLEN((char*)aad);
  45363. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  45364. byte decryptedtxt[plaintxtSz];
  45365. int ciphertxtSz = 0;
  45366. int decryptedtxtSz = 0;
  45367. int len = 0;
  45368. int i = 0;
  45369. #define TEST_ARIA_GCM_COUNT 6
  45370. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  45371. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  45372. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  45373. EVP_CIPHER_CTX_init(&en[i]);
  45374. switch (i) {
  45375. case 0:
  45376. /* Default uses 96-bits IV length */
  45377. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  45378. break;
  45379. case 1:
  45380. /* Default uses 96-bits IV length */
  45381. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  45382. break;
  45383. case 2:
  45384. /* Default uses 96-bits IV length */
  45385. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  45386. break;
  45387. case 3:
  45388. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  45389. /* non-default must to set the IV length first */
  45390. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45391. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45392. break;
  45393. case 4:
  45394. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  45395. /* non-default must to set the IV length first */
  45396. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45397. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45398. break;
  45399. case 5:
  45400. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  45401. /* non-default must to set the IV length first */
  45402. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45403. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45404. break;
  45405. }
  45406. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  45407. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45408. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  45409. ciphertxtSz = len;
  45410. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45411. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  45412. ciphertxtSz += len;
  45413. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  45414. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  45415. EVP_CIPHER_CTX_init(&de[i]);
  45416. switch (i) {
  45417. case 0:
  45418. /* Default uses 96-bits IV length */
  45419. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  45420. break;
  45421. case 1:
  45422. /* Default uses 96-bits IV length */
  45423. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  45424. break;
  45425. case 2:
  45426. /* Default uses 96-bits IV length */
  45427. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  45428. break;
  45429. case 3:
  45430. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  45431. /* non-default must to set the IV length first */
  45432. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45433. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45434. break;
  45435. case 4:
  45436. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  45437. /* non-default must to set the IV length first */
  45438. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45439. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45440. break;
  45441. case 5:
  45442. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  45443. /* non-default must to set the IV length first */
  45444. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45445. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45446. break;
  45447. }
  45448. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  45449. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45450. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45451. decryptedtxtSz = len;
  45452. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  45453. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45454. decryptedtxtSz += len;
  45455. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  45456. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45457. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  45458. /* modify tag*/
  45459. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45460. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45461. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  45462. /* fail due to wrong tag */
  45463. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  45464. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45465. AssertIntEQ(0, len);
  45466. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  45467. }
  45468. res = TEST_RES_CHECK(1);
  45469. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  45470. return res;
  45471. }
  45472. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  45473. {
  45474. EXPECT_DECLS;
  45475. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45476. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45477. /* Zero length plain text */
  45478. byte key[] = {
  45479. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45480. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45481. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45483. }; /* align */
  45484. byte iv[] = {
  45485. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45486. }; /* align */
  45487. byte plaintxt[1];
  45488. int ivSz = 12;
  45489. int plaintxtSz = 0;
  45490. unsigned char tag[16];
  45491. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45492. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45493. int ciphertxtSz = 0;
  45494. int decryptedtxtSz = 0;
  45495. int len = 0;
  45496. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45497. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45498. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  45499. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45500. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45501. plaintxtSz));
  45502. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45503. ciphertxtSz += len;
  45504. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  45505. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  45506. ExpectIntEQ(0, ciphertxtSz);
  45507. EVP_CIPHER_CTX_init(de);
  45508. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  45509. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  45510. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  45511. decryptedtxtSz = len;
  45512. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  45513. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  45514. decryptedtxtSz += len;
  45515. ExpectIntEQ(0, decryptedtxtSz);
  45516. EVP_CIPHER_CTX_free(en);
  45517. EVP_CIPHER_CTX_free(de);
  45518. #endif
  45519. return EXPECT_RESULT();
  45520. }
  45521. static int test_wolfssl_EVP_aes_ccm(void)
  45522. {
  45523. EXPECT_DECLS;
  45524. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  45525. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  45526. /* A 256 bit key, AES_128 will use the first 128 bit*/
  45527. byte *key = (byte*)"01234567890123456789012345678901";
  45528. /* A 128 bit IV */
  45529. byte *iv = (byte*)"0123456789012";
  45530. int ivSz = (int)XSTRLEN((char*)iv);
  45531. /* Message to be encrypted */
  45532. byte *plaintxt = (byte*)"for things to change you have to change";
  45533. /* Additional non-confidential data */
  45534. byte *aad = (byte*)"Don't spend major time on minor things.";
  45535. unsigned char tag[AES_BLOCK_SIZE] = {0};
  45536. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  45537. int aadSz = (int)XSTRLEN((char*)aad);
  45538. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  45539. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  45540. int ciphertxtSz = 0;
  45541. int decryptedtxtSz = 0;
  45542. int len = 0;
  45543. int i = 0;
  45544. int ret;
  45545. EVP_CIPHER_CTX en[2];
  45546. EVP_CIPHER_CTX de[2];
  45547. for (i = 0; i < 2; i++) {
  45548. EVP_CIPHER_CTX_init(&en[i]);
  45549. if (i == 0) {
  45550. /* Default uses 96-bits IV length */
  45551. #ifdef WOLFSSL_AES_128
  45552. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  45553. key, iv));
  45554. #elif defined(WOLFSSL_AES_192)
  45555. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  45556. key, iv));
  45557. #elif defined(WOLFSSL_AES_256)
  45558. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  45559. key, iv));
  45560. #endif
  45561. }
  45562. else {
  45563. #ifdef WOLFSSL_AES_128
  45564. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  45565. NULL, NULL));
  45566. #elif defined(WOLFSSL_AES_192)
  45567. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  45568. NULL, NULL));
  45569. #elif defined(WOLFSSL_AES_256)
  45570. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  45571. NULL, NULL));
  45572. #endif
  45573. /* non-default must to set the IV length first */
  45574. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  45575. ivSz, NULL));
  45576. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  45577. }
  45578. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  45579. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  45580. plaintxtSz));
  45581. ciphertxtSz = len;
  45582. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  45583. ciphertxtSz += len;
  45584. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  45585. AES_BLOCK_SIZE, tag));
  45586. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  45587. ExpectIntEQ(ret, 1);
  45588. EVP_CIPHER_CTX_init(&de[i]);
  45589. if (i == 0) {
  45590. /* Default uses 96-bits IV length */
  45591. #ifdef WOLFSSL_AES_128
  45592. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  45593. key, iv));
  45594. #elif defined(WOLFSSL_AES_192)
  45595. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  45596. key, iv));
  45597. #elif defined(WOLFSSL_AES_256)
  45598. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  45599. key, iv));
  45600. #endif
  45601. }
  45602. else {
  45603. #ifdef WOLFSSL_AES_128
  45604. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  45605. NULL, NULL));
  45606. #elif defined(WOLFSSL_AES_192)
  45607. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  45608. NULL, NULL));
  45609. #elif defined(WOLFSSL_AES_256)
  45610. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  45611. NULL, NULL));
  45612. #endif
  45613. /* non-default must to set the IV length first */
  45614. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  45615. ivSz, NULL));
  45616. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  45617. }
  45618. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45619. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45620. ciphertxtSz));
  45621. decryptedtxtSz = len;
  45622. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  45623. AES_BLOCK_SIZE, tag));
  45624. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45625. decryptedtxtSz += len;
  45626. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  45627. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  45628. /* modify tag*/
  45629. tag[AES_BLOCK_SIZE-1]+=0xBB;
  45630. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  45631. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  45632. AES_BLOCK_SIZE, tag));
  45633. /* fail due to wrong tag */
  45634. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  45635. ciphertxtSz));
  45636. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  45637. ExpectIntEQ(0, len);
  45638. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  45639. ExpectIntEQ(ret, 1);
  45640. }
  45641. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  45642. return EXPECT_RESULT();
  45643. }
  45644. static int test_wolfssl_EVP_chacha20_poly1305(void)
  45645. {
  45646. EXPECT_DECLS;
  45647. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45648. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45649. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45650. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45651. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45652. byte cipherText[sizeof(plainText)];
  45653. byte decryptedText[sizeof(plainText)];
  45654. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45655. EVP_CIPHER_CTX* ctx = NULL;
  45656. int outSz;
  45657. /* Encrypt. */
  45658. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45659. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45660. NULL), WOLFSSL_SUCCESS);
  45661. /* Invalid IV length. */
  45662. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45663. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45664. /* Valid IV length. */
  45665. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45666. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45667. /* Invalid tag length. */
  45668. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45669. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45670. /* Valid tag length. */
  45671. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45672. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45673. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45674. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45675. WOLFSSL_SUCCESS);
  45676. ExpectIntEQ(outSz, sizeof(aad));
  45677. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45678. sizeof(plainText)), WOLFSSL_SUCCESS);
  45679. ExpectIntEQ(outSz, sizeof(plainText));
  45680. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45681. ExpectIntEQ(outSz, 0);
  45682. /* Invalid tag length. */
  45683. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45684. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45685. /* Valid tag length. */
  45686. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45687. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45688. EVP_CIPHER_CTX_free(ctx);
  45689. ctx = NULL;
  45690. /* Decrypt. */
  45691. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45692. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45693. NULL), WOLFSSL_SUCCESS);
  45694. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45695. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45696. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45697. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45698. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45699. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45700. WOLFSSL_SUCCESS);
  45701. ExpectIntEQ(outSz, sizeof(aad));
  45702. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45703. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45704. ExpectIntEQ(outSz, sizeof(cipherText));
  45705. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45706. WOLFSSL_SUCCESS);
  45707. ExpectIntEQ(outSz, 0);
  45708. EVP_CIPHER_CTX_free(ctx);
  45709. ctx = NULL;
  45710. /* Test partial Inits. CipherInit() allow setting of key and iv
  45711. * in separate calls. */
  45712. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45713. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45714. key, NULL, 1), WOLFSSL_SUCCESS);
  45715. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45716. WOLFSSL_SUCCESS);
  45717. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45718. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45719. ExpectIntEQ(outSz, sizeof(aad));
  45720. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45721. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45722. ExpectIntEQ(outSz, sizeof(cipherText));
  45723. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45724. WOLFSSL_SUCCESS);
  45725. ExpectIntEQ(outSz, 0);
  45726. EVP_CIPHER_CTX_free(ctx);
  45727. #endif
  45728. return EXPECT_RESULT();
  45729. }
  45730. static int test_wolfssl_EVP_chacha20(void)
  45731. {
  45732. EXPECT_DECLS;
  45733. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45734. byte key[CHACHA_MAX_KEY_SZ];
  45735. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45736. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45737. byte cipherText[sizeof(plainText)];
  45738. byte decryptedText[sizeof(plainText)];
  45739. EVP_CIPHER_CTX* ctx = NULL;
  45740. int outSz;
  45741. /* Encrypt. */
  45742. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45743. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45744. NULL), WOLFSSL_SUCCESS);
  45745. /* Any tag length must fail - not an AEAD cipher. */
  45746. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45747. 16, NULL), WOLFSSL_FAILURE);
  45748. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45749. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45750. sizeof(plainText)), WOLFSSL_SUCCESS);
  45751. ExpectIntEQ(outSz, sizeof(plainText));
  45752. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45753. ExpectIntEQ(outSz, 0);
  45754. EVP_CIPHER_CTX_free(ctx);
  45755. ctx = NULL;
  45756. /* Decrypt. */
  45757. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45758. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45759. NULL), WOLFSSL_SUCCESS);
  45760. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45761. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45762. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45763. ExpectIntEQ(outSz, sizeof(cipherText));
  45764. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45765. WOLFSSL_SUCCESS);
  45766. ExpectIntEQ(outSz, 0);
  45767. EVP_CIPHER_CTX_free(ctx);
  45768. ctx = NULL;
  45769. /* Test partial Inits. CipherInit() allow setting of key and iv
  45770. * in separate calls. */
  45771. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45772. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45773. key, NULL, 1), WOLFSSL_SUCCESS);
  45774. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45775. WOLFSSL_SUCCESS);
  45776. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45777. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45778. ExpectIntEQ(outSz, sizeof(cipherText));
  45779. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45780. WOLFSSL_SUCCESS);
  45781. ExpectIntEQ(outSz, 0);
  45782. EVP_CIPHER_CTX_free(ctx);
  45783. #endif
  45784. return EXPECT_RESULT();
  45785. }
  45786. static int test_wolfssl_EVP_sm4_ecb(void)
  45787. {
  45788. int res = TEST_SKIPPED;
  45789. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  45790. EXPECT_DECLS;
  45791. byte key[SM4_KEY_SIZE];
  45792. byte plainText[SM4_BLOCK_SIZE] = {
  45793. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  45794. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45795. };
  45796. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45797. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45798. EVP_CIPHER_CTX* ctx;
  45799. int outSz;
  45800. XMEMSET(key, 0, sizeof(key));
  45801. /* Encrypt. */
  45802. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45803. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  45804. WOLFSSL_SUCCESS);
  45805. /* Any tag length must fail - not an AEAD cipher. */
  45806. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  45807. WOLFSSL_FAILURE);
  45808. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  45809. WOLFSSL_SUCCESS);
  45810. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45811. sizeof(plainText)), WOLFSSL_SUCCESS);
  45812. ExpectIntEQ(outSz, sizeof(plainText));
  45813. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  45814. WOLFSSL_SUCCESS);
  45815. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  45816. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  45817. EVP_CIPHER_CTX_free(ctx);
  45818. /* Decrypt. */
  45819. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45820. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  45821. WOLFSSL_SUCCESS);
  45822. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  45823. WOLFSSL_SUCCESS);
  45824. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45825. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45826. ExpectIntEQ(outSz, sizeof(plainText));
  45827. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  45828. WOLFSSL_SUCCESS);
  45829. ExpectIntEQ(outSz, 0);
  45830. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45831. EVP_CIPHER_CTX_free(ctx);
  45832. res = EXPECT_RESULT();
  45833. #endif
  45834. return res;
  45835. }
  45836. static int test_wolfssl_EVP_sm4_cbc(void)
  45837. {
  45838. int res = TEST_SKIPPED;
  45839. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  45840. EXPECT_DECLS;
  45841. byte key[SM4_KEY_SIZE];
  45842. byte iv[SM4_BLOCK_SIZE];
  45843. byte plainText[SM4_BLOCK_SIZE] = {
  45844. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  45845. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  45846. };
  45847. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45848. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  45849. EVP_CIPHER_CTX* ctx;
  45850. int outSz;
  45851. XMEMSET(key, 0, sizeof(key));
  45852. XMEMSET(iv, 0, sizeof(iv));
  45853. /* Encrypt. */
  45854. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45855. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  45856. WOLFSSL_SUCCESS);
  45857. /* Any tag length must fail - not an AEAD cipher. */
  45858. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  45859. WOLFSSL_FAILURE);
  45860. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45861. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45862. sizeof(plainText)), WOLFSSL_SUCCESS);
  45863. ExpectIntEQ(outSz, sizeof(plainText));
  45864. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  45865. WOLFSSL_SUCCESS);
  45866. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  45867. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  45868. EVP_CIPHER_CTX_free(ctx);
  45869. /* Decrypt. */
  45870. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45871. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  45872. WOLFSSL_SUCCESS);
  45873. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45874. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45875. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45876. ExpectIntEQ(outSz, sizeof(plainText));
  45877. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  45878. WOLFSSL_SUCCESS);
  45879. ExpectIntEQ(outSz, 0);
  45880. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45881. EVP_CIPHER_CTX_free(ctx);
  45882. /* Test partial Inits. CipherInit() allow setting of key and iv
  45883. * in separate calls. */
  45884. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45885. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  45886. WOLFSSL_SUCCESS);
  45887. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  45888. WOLFSSL_SUCCESS);
  45889. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45890. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45891. ExpectIntEQ(outSz, sizeof(plainText));
  45892. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  45893. WOLFSSL_SUCCESS);
  45894. ExpectIntEQ(outSz, 0);
  45895. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45896. EVP_CIPHER_CTX_free(ctx);
  45897. res = EXPECT_RESULT();
  45898. #endif
  45899. return res;
  45900. }
  45901. static int test_wolfssl_EVP_sm4_ctr(void)
  45902. {
  45903. int res = TEST_SKIPPED;
  45904. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  45905. EXPECT_DECLS;
  45906. byte key[SM4_KEY_SIZE];
  45907. byte iv[SM4_BLOCK_SIZE];
  45908. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45909. byte cipherText[sizeof(plainText)];
  45910. byte decryptedText[sizeof(plainText)];
  45911. EVP_CIPHER_CTX* ctx;
  45912. int outSz;
  45913. XMEMSET(key, 0, sizeof(key));
  45914. XMEMSET(iv, 0, sizeof(iv));
  45915. /* Encrypt. */
  45916. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45917. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  45918. WOLFSSL_SUCCESS);
  45919. /* Any tag length must fail - not an AEAD cipher. */
  45920. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  45921. WOLFSSL_FAILURE);
  45922. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45923. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45924. sizeof(plainText)), WOLFSSL_SUCCESS);
  45925. ExpectIntEQ(outSz, sizeof(plainText));
  45926. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45927. ExpectIntEQ(outSz, 0);
  45928. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  45929. EVP_CIPHER_CTX_free(ctx);
  45930. /* Decrypt. */
  45931. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45932. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  45933. WOLFSSL_SUCCESS);
  45934. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45935. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45936. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45937. ExpectIntEQ(outSz, sizeof(cipherText));
  45938. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45939. WOLFSSL_SUCCESS);
  45940. ExpectIntEQ(outSz, 0);
  45941. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45942. EVP_CIPHER_CTX_free(ctx);
  45943. /* Test partial Inits. CipherInit() allow setting of key and iv
  45944. * in separate calls. */
  45945. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  45946. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  45947. WOLFSSL_SUCCESS);
  45948. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45949. WOLFSSL_SUCCESS);
  45950. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45951. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45952. ExpectIntEQ(outSz, sizeof(cipherText));
  45953. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45954. WOLFSSL_SUCCESS);
  45955. ExpectIntEQ(outSz, 0);
  45956. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  45957. EVP_CIPHER_CTX_free(ctx);
  45958. res = EXPECT_RESULT();
  45959. #endif
  45960. return res;
  45961. }
  45962. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  45963. {
  45964. int res = TEST_SKIPPED;
  45965. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  45966. /* Zero length plain text */
  45967. EXPECT_DECLS;
  45968. byte key[] = {
  45969. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45970. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45971. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  45972. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45973. }; /* align */
  45974. byte iv[] = {
  45975. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  45976. }; /* align */
  45977. byte plaintxt[1];
  45978. int ivSz = 12;
  45979. int plaintxtSz = 0;
  45980. unsigned char tag[16];
  45981. unsigned char tag_kat[16] = {
  45982. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  45983. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  45984. };
  45985. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  45986. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  45987. int ciphertxtSz = 0;
  45988. int decryptedtxtSz = 0;
  45989. int len = 0;
  45990. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  45991. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  45992. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  45993. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  45994. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  45995. plaintxtSz));
  45996. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  45997. ciphertxtSz += len;
  45998. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  45999. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  46000. ExpectIntEQ(0, ciphertxtSz);
  46001. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  46002. EVP_CIPHER_CTX_init(de);
  46003. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  46004. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  46005. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  46006. decryptedtxtSz = len;
  46007. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  46008. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  46009. decryptedtxtSz += len;
  46010. ExpectIntEQ(0, decryptedtxtSz);
  46011. EVP_CIPHER_CTX_free(en);
  46012. EVP_CIPHER_CTX_free(de);
  46013. res = EXPECT_RESULT();
  46014. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  46015. return res;
  46016. }
  46017. static int test_wolfssl_EVP_sm4_gcm(void)
  46018. {
  46019. int res = TEST_SKIPPED;
  46020. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  46021. EXPECT_DECLS;
  46022. byte *key = (byte*)"0123456789012345";
  46023. /* A 128 bit IV */
  46024. byte *iv = (byte*)"0123456789012345";
  46025. int ivSz = SM4_BLOCK_SIZE;
  46026. /* Message to be encrypted */
  46027. byte *plaintxt = (byte*)"for things to change you have to change";
  46028. /* Additional non-confidential data */
  46029. byte *aad = (byte*)"Don't spend major time on minor things.";
  46030. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  46031. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  46032. int aadSz = (int)XSTRLEN((char*)aad);
  46033. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46034. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46035. int ciphertxtSz = 0;
  46036. int decryptedtxtSz = 0;
  46037. int len = 0;
  46038. int i = 0;
  46039. EVP_CIPHER_CTX en[2];
  46040. EVP_CIPHER_CTX de[2];
  46041. for (i = 0; i < 2; i++) {
  46042. EVP_CIPHER_CTX_init(&en[i]);
  46043. if (i == 0) {
  46044. /* Default uses 96-bits IV length */
  46045. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  46046. iv));
  46047. }
  46048. else {
  46049. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  46050. NULL));
  46051. /* non-default must to set the IV length first */
  46052. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  46053. ivSz, NULL));
  46054. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  46055. }
  46056. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  46057. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  46058. plaintxtSz));
  46059. ciphertxtSz = len;
  46060. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  46061. ciphertxtSz += len;
  46062. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  46063. SM4_BLOCK_SIZE, tag));
  46064. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  46065. EVP_CIPHER_CTX_init(&de[i]);
  46066. if (i == 0) {
  46067. /* Default uses 96-bits IV length */
  46068. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  46069. iv));
  46070. }
  46071. else {
  46072. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  46073. NULL));
  46074. /* non-default must to set the IV length first */
  46075. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  46076. ivSz, NULL));
  46077. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  46078. }
  46079. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46080. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46081. ciphertxtSz));
  46082. decryptedtxtSz = len;
  46083. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  46084. SM4_BLOCK_SIZE, tag));
  46085. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46086. decryptedtxtSz += len;
  46087. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  46088. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  46089. /* modify tag*/
  46090. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  46091. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46092. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  46093. SM4_BLOCK_SIZE, tag));
  46094. /* fail due to wrong tag */
  46095. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46096. ciphertxtSz));
  46097. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46098. ExpectIntEQ(0, len);
  46099. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  46100. }
  46101. res = EXPECT_RESULT();
  46102. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  46103. return res;
  46104. }
  46105. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  46106. {
  46107. int res = TEST_SKIPPED;
  46108. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  46109. /* Zero length plain text */
  46110. EXPECT_DECLS;
  46111. byte key[] = {
  46112. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46115. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46116. }; /* align */
  46117. byte iv[] = {
  46118. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  46119. }; /* align */
  46120. byte plaintxt[1];
  46121. int ivSz = 12;
  46122. int plaintxtSz = 0;
  46123. unsigned char tag[16];
  46124. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46125. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46126. int ciphertxtSz = 0;
  46127. int decryptedtxtSz = 0;
  46128. int len = 0;
  46129. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  46130. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  46131. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  46132. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  46133. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  46134. plaintxtSz));
  46135. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  46136. ciphertxtSz += len;
  46137. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  46138. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  46139. ExpectIntEQ(0, ciphertxtSz);
  46140. EVP_CIPHER_CTX_init(de);
  46141. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  46142. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  46143. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  46144. decryptedtxtSz = len;
  46145. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  46146. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  46147. decryptedtxtSz += len;
  46148. ExpectIntEQ(0, decryptedtxtSz);
  46149. EVP_CIPHER_CTX_free(en);
  46150. EVP_CIPHER_CTX_free(de);
  46151. res = EXPECT_RESULT();
  46152. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  46153. return res;
  46154. }
  46155. static int test_wolfssl_EVP_sm4_ccm(void)
  46156. {
  46157. int res = TEST_SKIPPED;
  46158. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  46159. EXPECT_DECLS;
  46160. byte *key = (byte*)"0123456789012345";
  46161. byte *iv = (byte*)"0123456789012";
  46162. int ivSz = (int)XSTRLEN((char*)iv);
  46163. /* Message to be encrypted */
  46164. byte *plaintxt = (byte*)"for things to change you have to change";
  46165. /* Additional non-confidential data */
  46166. byte *aad = (byte*)"Don't spend major time on minor things.";
  46167. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  46168. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  46169. int aadSz = (int)XSTRLEN((char*)aad);
  46170. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  46171. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  46172. int ciphertxtSz = 0;
  46173. int decryptedtxtSz = 0;
  46174. int len = 0;
  46175. int i = 0;
  46176. EVP_CIPHER_CTX en[2];
  46177. EVP_CIPHER_CTX de[2];
  46178. for (i = 0; i < 2; i++) {
  46179. EVP_CIPHER_CTX_init(&en[i]);
  46180. if (i == 0) {
  46181. /* Default uses 96-bits IV length */
  46182. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  46183. iv));
  46184. }
  46185. else {
  46186. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  46187. NULL));
  46188. /* non-default must to set the IV length first */
  46189. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  46190. ivSz, NULL));
  46191. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  46192. }
  46193. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  46194. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  46195. plaintxtSz));
  46196. ciphertxtSz = len;
  46197. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  46198. ciphertxtSz += len;
  46199. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  46200. SM4_BLOCK_SIZE, tag));
  46201. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  46202. EVP_CIPHER_CTX_init(&de[i]);
  46203. if (i == 0) {
  46204. /* Default uses 96-bits IV length */
  46205. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  46206. iv));
  46207. }
  46208. else {
  46209. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  46210. NULL));
  46211. /* non-default must to set the IV length first */
  46212. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  46213. ivSz, NULL));
  46214. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  46215. }
  46216. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46217. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46218. ciphertxtSz));
  46219. decryptedtxtSz = len;
  46220. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  46221. SM4_BLOCK_SIZE, tag));
  46222. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46223. decryptedtxtSz += len;
  46224. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  46225. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  46226. /* modify tag*/
  46227. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  46228. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  46229. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  46230. SM4_BLOCK_SIZE, tag));
  46231. /* fail due to wrong tag */
  46232. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  46233. ciphertxtSz));
  46234. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  46235. ExpectIntEQ(0, len);
  46236. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  46237. }
  46238. res = EXPECT_RESULT();
  46239. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  46240. return res;
  46241. }
  46242. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  46243. {
  46244. EXPECT_DECLS;
  46245. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  46246. EVP_PKEY_CTX* ctx = NULL;
  46247. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  46248. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  46249. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  46250. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  46251. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  46252. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  46253. byte outKey[34];
  46254. size_t outKeySz = sizeof(outKey);
  46255. /* These expected outputs were gathered by running the same test below using
  46256. * OpenSSL. */
  46257. const byte extractAndExpand[] = {
  46258. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  46259. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  46260. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  46261. };
  46262. const byte extractOnly[] = {
  46263. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  46264. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  46265. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  46266. };
  46267. const byte expandOnly[] = {
  46268. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  46269. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  46270. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  46271. };
  46272. const byte extractAndExpandAddInfo[] = {
  46273. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  46274. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  46275. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  46276. };
  46277. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  46278. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  46279. /* NULL ctx. */
  46280. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  46281. /* NULL md. */
  46282. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  46283. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  46284. /* NULL ctx. */
  46285. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  46286. WOLFSSL_FAILURE);
  46287. /* NULL salt is ok. */
  46288. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  46289. WOLFSSL_SUCCESS);
  46290. /* Salt length <= 0. */
  46291. /* Length 0 salt is ok. */
  46292. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  46293. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  46294. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  46295. WOLFSSL_SUCCESS);
  46296. /* NULL ctx. */
  46297. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  46298. WOLFSSL_FAILURE);
  46299. /* NULL key. */
  46300. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  46301. WOLFSSL_FAILURE);
  46302. /* Key length <= 0 */
  46303. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  46304. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  46305. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  46306. WOLFSSL_SUCCESS);
  46307. /* NULL ctx. */
  46308. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  46309. WOLFSSL_FAILURE);
  46310. /* NULL info is ok. */
  46311. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  46312. WOLFSSL_SUCCESS);
  46313. /* Info length <= 0 */
  46314. /* Length 0 info is ok. */
  46315. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  46316. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  46317. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  46318. WOLFSSL_SUCCESS);
  46319. /* NULL ctx. */
  46320. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  46321. WOLFSSL_FAILURE);
  46322. /* Extract and expand (default). */
  46323. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46324. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  46325. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  46326. /* Extract only. */
  46327. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  46328. WOLFSSL_SUCCESS);
  46329. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46330. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  46331. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  46332. outKeySz = sizeof(outKey);
  46333. /* Expand only. */
  46334. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  46335. WOLFSSL_SUCCESS);
  46336. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46337. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  46338. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  46339. outKeySz = sizeof(outKey);
  46340. /* Extract and expand with appended additional info. */
  46341. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  46342. WOLFSSL_SUCCESS);
  46343. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  46344. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  46345. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  46346. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  46347. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  46348. EVP_PKEY_CTX_free(ctx);
  46349. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  46350. return EXPECT_RESULT();
  46351. }
  46352. #ifndef NO_BIO
  46353. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  46354. {
  46355. EXPECT_DECLS;
  46356. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46357. BIO* bio = NULL;
  46358. X509_INFO* info = NULL;
  46359. STACK_OF(X509_INFO)* sk = NULL;
  46360. char* subject = NULL;
  46361. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  46362. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  46363. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  46364. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  46365. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  46366. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  46367. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  46368. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  46369. /* using dereference to maintain testing for Apache port*/
  46370. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  46371. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  46372. 0, 0));
  46373. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  46374. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  46375. X509_INFO_free(info);
  46376. info = NULL;
  46377. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  46378. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  46379. 0, 0));
  46380. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  46381. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  46382. X509_INFO_free(info);
  46383. ExpectNull(info = sk_X509_INFO_pop(sk));
  46384. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  46385. BIO_free(bio);
  46386. #endif
  46387. return EXPECT_RESULT();
  46388. }
  46389. #endif /* !NO_BIO */
  46390. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  46391. {
  46392. EXPECT_DECLS;
  46393. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46394. X509 *x509 = NULL;
  46395. X509_NAME* name = NULL;
  46396. int idx = 0;
  46397. X509_NAME_ENTRY *ne = NULL;
  46398. ASN1_OBJECT *object = NULL;
  46399. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  46400. WOLFSSL_FILETYPE_PEM));
  46401. ExpectNotNull(name = X509_get_subject_name(x509));
  46402. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  46403. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  46404. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  46405. X509_free(x509);
  46406. #endif
  46407. return EXPECT_RESULT();
  46408. }
  46409. static int test_wolfSSL_X509_STORE_get1_certs(void)
  46410. {
  46411. EXPECT_DECLS;
  46412. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  46413. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  46414. X509_STORE_CTX *storeCtx = NULL;
  46415. X509_STORE *store = NULL;
  46416. X509 *caX509 = NULL;
  46417. X509 *svrX509 = NULL;
  46418. X509_NAME *subject = NULL;
  46419. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  46420. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  46421. SSL_FILETYPE_PEM));
  46422. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  46423. SSL_FILETYPE_PEM)));
  46424. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  46425. ExpectNotNull(store = X509_STORE_new());
  46426. ExpectNotNull(subject = X509_get_subject_name(caX509));
  46427. /* Errors */
  46428. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  46429. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  46430. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  46431. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  46432. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  46433. SSL_SUCCESS);
  46434. /* Should find the cert */
  46435. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46436. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  46437. sk_X509_pop_free(certs, NULL);
  46438. certs = NULL;
  46439. /* Should not find the cert */
  46440. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  46441. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  46442. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  46443. sk_X509_pop_free(certs, NULL);
  46444. certs = NULL;
  46445. X509_STORE_free(store);
  46446. X509_STORE_CTX_free(storeCtx);
  46447. X509_free(svrX509);
  46448. X509_free(caX509);
  46449. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  46450. return EXPECT_RESULT();
  46451. }
  46452. static int test_wolfSSL_dup_CA_list(void)
  46453. {
  46454. int res = TEST_SKIPPED;
  46455. #if defined(OPENSSL_ALL)
  46456. EXPECT_DECLS;
  46457. STACK_OF(X509_NAME) *originalStack = NULL;
  46458. STACK_OF(X509_NAME) *copyStack = NULL;
  46459. int originalCount = 0;
  46460. int copyCount = 0;
  46461. X509_NAME *name = NULL;
  46462. int i;
  46463. originalStack = sk_X509_NAME_new_null();
  46464. ExpectNotNull(originalStack);
  46465. for (i = 0; i < 3; i++) {
  46466. name = X509_NAME_new();
  46467. ExpectNotNull(name);
  46468. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  46469. }
  46470. copyStack = SSL_dup_CA_list(originalStack);
  46471. ExpectNotNull(copyStack);
  46472. originalCount = sk_X509_NAME_num(originalStack);
  46473. copyCount = sk_X509_NAME_num(copyStack);
  46474. AssertIntEQ(originalCount, copyCount);
  46475. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  46476. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  46477. originalStack = NULL;
  46478. copyStack = NULL;
  46479. res = EXPECT_RESULT();
  46480. #endif /* OPENSSL_ALL */
  46481. return res;
  46482. }
  46483. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  46484. * have default (hidden) visibility, and in the absence of visibility, it's
  46485. * benign to mask out the library implementation.
  46486. */
  46487. #define WOLFSSL_MISC_INCLUDED
  46488. #include <wolfcrypt/src/misc.c>
  46489. static int test_ForceZero(void)
  46490. {
  46491. EXPECT_DECLS;
  46492. unsigned char data[32];
  46493. unsigned int i, j, len;
  46494. /* Test case with 0 length */
  46495. ForceZero(data, 0);
  46496. /* Test ForceZero */
  46497. for (i = 0; i < sizeof(data); i++) {
  46498. for (len = 1; len < sizeof(data) - i; len++) {
  46499. for (j = 0; j < sizeof(data); j++)
  46500. data[j] = j + 1;
  46501. ForceZero(data + i, len);
  46502. for (j = 0; j < sizeof(data); j++) {
  46503. if (j < i || j >= i + len) {
  46504. ExpectIntNE(data[j], 0x00);
  46505. }
  46506. else {
  46507. ExpectIntEQ(data[j], 0x00);
  46508. }
  46509. }
  46510. }
  46511. }
  46512. return EXPECT_RESULT();
  46513. }
  46514. #ifndef NO_BIO
  46515. static int test_wolfSSL_X509_print(void)
  46516. {
  46517. EXPECT_DECLS;
  46518. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46519. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  46520. X509 *x509 = NULL;
  46521. BIO *bio = NULL;
  46522. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46523. const X509_ALGOR *cert_sig_alg;
  46524. #endif
  46525. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  46526. WOLFSSL_FILETYPE_PEM));
  46527. /* print to memory */
  46528. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46529. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46530. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  46531. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  46532. /* Will print IP address subject alt name. */
  46533. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  46534. #elif defined(NO_ASN_TIME)
  46535. /* Will print IP address subject alt name but not Validity. */
  46536. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  46537. #else
  46538. /* Will print IP address subject alt name. */
  46539. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  46540. #endif
  46541. #elif defined(NO_ASN_TIME)
  46542. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  46543. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  46544. #else
  46545. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  46546. #endif
  46547. BIO_free(bio);
  46548. bio = NULL;
  46549. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46550. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  46551. /* Print signature */
  46552. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  46553. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  46554. #endif
  46555. /* print to stderr */
  46556. #if !defined(NO_WOLFSSL_DIR)
  46557. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  46558. #endif
  46559. /* print again */
  46560. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  46561. X509_free(x509);
  46562. BIO_free(bio);
  46563. #endif
  46564. return EXPECT_RESULT();
  46565. }
  46566. static int test_wolfSSL_X509_CRL_print(void)
  46567. {
  46568. EXPECT_DECLS;
  46569. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  46570. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  46571. X509_CRL* crl = NULL;
  46572. BIO *bio = NULL;
  46573. XFILE fp = XBADFILE;
  46574. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  46575. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  46576. NULL, NULL));
  46577. if (fp != XBADFILE)
  46578. XFCLOSE(fp);
  46579. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  46580. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  46581. X509_CRL_free(crl);
  46582. BIO_free(bio);
  46583. #endif
  46584. return EXPECT_RESULT();
  46585. }
  46586. static int test_wolfSSL_BIO_get_len(void)
  46587. {
  46588. EXPECT_DECLS;
  46589. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  46590. BIO *bio = NULL;
  46591. const char txt[] = "Some example text to push to the BIO.";
  46592. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  46593. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  46594. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  46595. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  46596. BIO_free(bio);
  46597. bio = NULL;
  46598. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46599. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  46600. BIO_free(bio);
  46601. #endif
  46602. return EXPECT_RESULT();
  46603. }
  46604. #endif /* !NO_BIO */
  46605. static int test_wolfSSL_RSA(void)
  46606. {
  46607. EXPECT_DECLS;
  46608. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  46609. defined(WOLFSSL_KEY_GEN)
  46610. RSA* rsa = NULL;
  46611. const BIGNUM *n;
  46612. const BIGNUM *e;
  46613. const BIGNUM *d;
  46614. const BIGNUM *p;
  46615. const BIGNUM *q;
  46616. const BIGNUM *dmp1;
  46617. const BIGNUM *dmq1;
  46618. const BIGNUM *iqmp;
  46619. ExpectNotNull(rsa = RSA_new());
  46620. ExpectIntEQ(RSA_size(NULL), 0);
  46621. ExpectIntEQ(RSA_size(rsa), 0);
  46622. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  46623. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  46624. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  46625. #ifdef WOLFSSL_RSA_KEY_CHECK
  46626. ExpectIntEQ(RSA_check_key(rsa), 0);
  46627. #endif
  46628. RSA_free(rsa);
  46629. rsa = NULL;
  46630. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46631. ExpectIntEQ(RSA_size(rsa), 256);
  46632. #ifdef WOLFSSL_RSA_KEY_CHECK
  46633. ExpectIntEQ(RSA_check_key(NULL), 0);
  46634. ExpectIntEQ(RSA_check_key(rsa), 1);
  46635. #endif
  46636. /* sanity check */
  46637. ExpectIntEQ(RSA_bits(NULL), 0);
  46638. /* key */
  46639. ExpectIntEQ(RSA_bits(rsa), 2048);
  46640. RSA_get0_key(rsa, &n, &e, &d);
  46641. ExpectPtrEq(rsa->n, n);
  46642. ExpectPtrEq(rsa->e, e);
  46643. ExpectPtrEq(rsa->d, d);
  46644. n = NULL;
  46645. e = NULL;
  46646. d = NULL;
  46647. ExpectNotNull(n = BN_new());
  46648. ExpectNotNull(e = BN_new());
  46649. ExpectNotNull(d = BN_new());
  46650. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  46651. if (EXPECT_FAIL()) {
  46652. BN_free((BIGNUM*)n);
  46653. BN_free((BIGNUM*)e);
  46654. BN_free((BIGNUM*)d);
  46655. }
  46656. ExpectPtrEq(rsa->n, n);
  46657. ExpectPtrEq(rsa->e, e);
  46658. ExpectPtrEq(rsa->d, d);
  46659. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  46660. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  46661. /* crt_params */
  46662. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  46663. ExpectPtrEq(rsa->dmp1, dmp1);
  46664. ExpectPtrEq(rsa->dmq1, dmq1);
  46665. ExpectPtrEq(rsa->iqmp, iqmp);
  46666. dmp1 = NULL;
  46667. dmq1 = NULL;
  46668. iqmp = NULL;
  46669. ExpectNotNull(dmp1 = BN_new());
  46670. ExpectNotNull(dmq1 = BN_new());
  46671. ExpectNotNull(iqmp = BN_new());
  46672. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46673. (BIGNUM*)iqmp), 1);
  46674. if (EXPECT_FAIL()) {
  46675. BN_free((BIGNUM*)dmp1);
  46676. BN_free((BIGNUM*)dmq1);
  46677. BN_free((BIGNUM*)iqmp);
  46678. }
  46679. ExpectPtrEq(rsa->dmp1, dmp1);
  46680. ExpectPtrEq(rsa->dmq1, dmq1);
  46681. ExpectPtrEq(rsa->iqmp, iqmp);
  46682. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  46683. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  46684. (BIGNUM*)iqmp), 0);
  46685. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  46686. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  46687. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  46688. ExpectNull(dmp1);
  46689. ExpectNull(dmq1);
  46690. ExpectNull(iqmp);
  46691. /* factors */
  46692. RSA_get0_factors(rsa, NULL, NULL);
  46693. RSA_get0_factors(rsa, &p, &q);
  46694. ExpectPtrEq(rsa->p, p);
  46695. ExpectPtrEq(rsa->q, q);
  46696. p = NULL;
  46697. q = NULL;
  46698. ExpectNotNull(p = BN_new());
  46699. ExpectNotNull(q = BN_new());
  46700. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  46701. if (EXPECT_FAIL()) {
  46702. BN_free((BIGNUM*)p);
  46703. BN_free((BIGNUM*)q);
  46704. }
  46705. ExpectPtrEq(rsa->p, p);
  46706. ExpectPtrEq(rsa->q, q);
  46707. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  46708. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  46709. RSA_get0_factors(NULL, NULL, NULL);
  46710. RSA_get0_factors(NULL, &p, &q);
  46711. ExpectNull(p);
  46712. ExpectNull(q);
  46713. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46714. ExpectIntEQ(RSA_bits(rsa), 21);
  46715. RSA_free(rsa);
  46716. rsa = NULL;
  46717. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46718. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46719. ExpectIntEQ(RSA_size(rsa), 384);
  46720. ExpectIntEQ(RSA_bits(rsa), 3072);
  46721. RSA_free(rsa);
  46722. rsa = NULL;
  46723. #endif
  46724. /* remove for now with odd key size until adjusting rsa key size check with
  46725. wc_MakeRsaKey()
  46726. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46727. RSA_free(rsa);
  46728. rsa = NULL;
  46729. */
  46730. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  46731. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46732. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46733. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  46734. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46735. {
  46736. byte buff[FOURK_BUF];
  46737. byte der[FOURK_BUF];
  46738. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46739. XFILE f = XBADFILE;
  46740. int bytes;
  46741. /* test loading encrypted RSA private pem w/o password */
  46742. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  46743. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  46744. if (f != XBADFILE)
  46745. XFCLOSE(f);
  46746. XMEMSET(der, 0, sizeof(der));
  46747. /* test that error value is returned with no password */
  46748. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  46749. 0);
  46750. }
  46751. #endif
  46752. #endif
  46753. return EXPECT_RESULT();
  46754. }
  46755. static int test_wolfSSL_RSA_DER(void)
  46756. {
  46757. EXPECT_DECLS;
  46758. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46759. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46760. RSA *rsa = NULL;
  46761. int i;
  46762. const unsigned char *buff = NULL;
  46763. unsigned char *newBuff = NULL;
  46764. struct tbl_s
  46765. {
  46766. const unsigned char *der;
  46767. int sz;
  46768. } tbl[] = {
  46769. #ifdef USE_CERT_BUFFERS_1024
  46770. {client_key_der_1024, sizeof_client_key_der_1024},
  46771. {server_key_der_1024, sizeof_server_key_der_1024},
  46772. #endif
  46773. #ifdef USE_CERT_BUFFERS_2048
  46774. {client_key_der_2048, sizeof_client_key_der_2048},
  46775. {server_key_der_2048, sizeof_server_key_der_2048},
  46776. #endif
  46777. {NULL, 0}
  46778. };
  46779. /* Public Key DER */
  46780. struct tbl_s pub[] = {
  46781. #ifdef USE_CERT_BUFFERS_1024
  46782. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46783. #endif
  46784. #ifdef USE_CERT_BUFFERS_2048
  46785. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46786. #endif
  46787. {NULL, 0}
  46788. };
  46789. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46790. buff = pub[0].der;
  46791. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46792. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46793. buff = tbl[0].der;
  46794. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46795. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46796. rsa = RSA_new();
  46797. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46798. RSA_free(rsa);
  46799. rsa = NULL;
  46800. for (i = 0; tbl[i].der != NULL; i++)
  46801. {
  46802. /* Passing in pointer results in pointer moving. */
  46803. buff = tbl[i].der;
  46804. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46805. ExpectNotNull(rsa);
  46806. RSA_free(rsa);
  46807. rsa = NULL;
  46808. }
  46809. for (i = 0; tbl[i].der != NULL; i++)
  46810. {
  46811. /* Passing in pointer results in pointer moving. */
  46812. buff = tbl[i].der;
  46813. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46814. ExpectNotNull(rsa);
  46815. RSA_free(rsa);
  46816. rsa = NULL;
  46817. }
  46818. for (i = 0; pub[i].der != NULL; i++)
  46819. {
  46820. buff = pub[i].der;
  46821. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46822. ExpectNotNull(rsa);
  46823. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46824. newBuff = NULL;
  46825. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46826. ExpectNotNull(newBuff);
  46827. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46828. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46829. RSA_free(rsa);
  46830. rsa = NULL;
  46831. }
  46832. #endif
  46833. return EXPECT_RESULT();
  46834. }
  46835. static int test_wolfSSL_RSA_print(void)
  46836. {
  46837. EXPECT_DECLS;
  46838. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46839. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46840. !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
  46841. BIO *bio = NULL;
  46842. WOLFSSL_RSA* rsa = NULL;
  46843. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  46844. ExpectNotNull(rsa = RSA_new());
  46845. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  46846. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46847. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  46848. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  46849. /* Some very large number of indent spaces. */
  46850. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  46851. /* RSA is empty. */
  46852. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  46853. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  46854. RSA_free(rsa);
  46855. rsa = NULL;
  46856. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46857. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  46858. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  46859. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  46860. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  46861. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  46862. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  46863. BIO_free(bio);
  46864. RSA_free(rsa);
  46865. #endif
  46866. return EXPECT_RESULT();
  46867. }
  46868. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  46869. {
  46870. EXPECT_DECLS;
  46871. #ifndef NO_RSA
  46872. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46873. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46874. RSA *rsa = NULL;
  46875. const unsigned char *derBuf = client_key_der_2048;
  46876. unsigned char em[256] = {0}; /* len = 2048/8 */
  46877. /* Random data simulating a hash */
  46878. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  46879. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46880. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46881. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46882. };
  46883. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46884. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  46885. RSA_PSS_SALTLEN_DIGEST), 0);
  46886. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  46887. RSA_PSS_SALTLEN_DIGEST), 0);
  46888. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  46889. RSA_PSS_SALTLEN_DIGEST), 0);
  46890. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  46891. RSA_PSS_SALTLEN_DIGEST), 0);
  46892. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  46893. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  46894. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46895. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  46896. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46897. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  46898. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46899. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  46900. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46901. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46902. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46903. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  46904. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46905. RSA_PSS_SALTLEN_DIGEST), 1);
  46906. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46907. RSA_PSS_SALTLEN_DIGEST), 1);
  46908. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46909. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46910. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46911. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46912. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46913. RSA_PSS_SALTLEN_MAX), 1);
  46914. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46915. RSA_PSS_SALTLEN_MAX), 1);
  46916. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  46917. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  46918. RSA_free(rsa);
  46919. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46920. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46921. #endif
  46922. return EXPECT_RESULT();
  46923. }
  46924. static int test_wolfSSL_RSA_sign_sha3(void)
  46925. {
  46926. EXPECT_DECLS;
  46927. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  46928. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46929. RSA* rsa = NULL;
  46930. const unsigned char *derBuf = client_key_der_2048;
  46931. unsigned char sigRet[256] = {0};
  46932. unsigned int sigLen = sizeof(sigRet);
  46933. /* Random data simulating a hash */
  46934. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  46935. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46936. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46937. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46938. };
  46939. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46940. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  46941. rsa), 1);
  46942. RSA_free(rsa);
  46943. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46944. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  46945. return EXPECT_RESULT();
  46946. }
  46947. static int test_wolfSSL_RSA_get0_key(void)
  46948. {
  46949. EXPECT_DECLS;
  46950. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  46951. RSA *rsa = NULL;
  46952. const BIGNUM* n = NULL;
  46953. const BIGNUM* e = NULL;
  46954. const BIGNUM* d = NULL;
  46955. const unsigned char* der;
  46956. int derSz;
  46957. #ifdef USE_CERT_BUFFERS_1024
  46958. der = client_key_der_1024;
  46959. derSz = sizeof_client_key_der_1024;
  46960. #elif defined(USE_CERT_BUFFERS_2048)
  46961. der = client_key_der_2048;
  46962. derSz = sizeof_client_key_der_2048;
  46963. #else
  46964. der = NULL;
  46965. derSz = 0;
  46966. #endif
  46967. if (der != NULL) {
  46968. RSA_get0_key(NULL, NULL, NULL, NULL);
  46969. RSA_get0_key(rsa, NULL, NULL, NULL);
  46970. RSA_get0_key(NULL, &n, &e, &d);
  46971. ExpectNull(n);
  46972. ExpectNull(e);
  46973. ExpectNull(d);
  46974. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  46975. ExpectNotNull(rsa);
  46976. RSA_get0_key(rsa, NULL, NULL, NULL);
  46977. RSA_get0_key(rsa, &n, NULL, NULL);
  46978. ExpectNotNull(n);
  46979. RSA_get0_key(rsa, NULL, &e, NULL);
  46980. ExpectNotNull(e);
  46981. RSA_get0_key(rsa, NULL, NULL, &d);
  46982. ExpectNotNull(d);
  46983. RSA_get0_key(rsa, &n, &e, &d);
  46984. ExpectNotNull(n);
  46985. ExpectNotNull(e);
  46986. ExpectNotNull(d);
  46987. RSA_free(rsa);
  46988. }
  46989. #endif
  46990. return EXPECT_RESULT();
  46991. }
  46992. static int test_wolfSSL_RSA_meth(void)
  46993. {
  46994. EXPECT_DECLS;
  46995. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46996. RSA *rsa = NULL;
  46997. RSA_METHOD *rsa_meth = NULL;
  46998. #ifdef WOLFSSL_KEY_GEN
  46999. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47000. RSA_free(rsa);
  47001. rsa = NULL;
  47002. #else
  47003. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47004. #endif
  47005. ExpectNotNull(RSA_get_default_method());
  47006. wolfSSL_RSA_meth_free(NULL);
  47007. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  47008. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  47009. RSA_METHOD_FLAG_NO_CHECK));
  47010. #ifndef NO_WOLFSSL_STUB
  47011. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  47012. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  47013. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  47014. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  47015. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  47016. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  47017. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  47018. #endif
  47019. ExpectIntEQ(RSA_flags(NULL), 0);
  47020. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  47021. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  47022. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  47023. ExpectNotNull(rsa = RSA_new());
  47024. /* No method set. */
  47025. ExpectIntEQ(RSA_flags(rsa), 0);
  47026. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47027. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47028. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47029. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  47030. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  47031. if (EXPECT_FAIL()) {
  47032. wolfSSL_RSA_meth_free(rsa_meth);
  47033. }
  47034. ExpectNull(RSA_get_method(NULL));
  47035. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  47036. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  47037. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47038. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47039. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  47040. RSA_METHOD_FLAG_NO_CHECK);
  47041. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  47042. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  47043. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  47044. /* rsa_meth is freed here */
  47045. RSA_free(rsa);
  47046. #endif
  47047. return EXPECT_RESULT();
  47048. }
  47049. static int test_wolfSSL_RSA_verify(void)
  47050. {
  47051. EXPECT_DECLS;
  47052. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  47053. !defined(NO_FILESYSTEM)
  47054. #ifndef NO_BIO
  47055. XFILE fp = XBADFILE;
  47056. RSA *pKey = NULL;
  47057. RSA *pubKey = NULL;
  47058. X509 *cert = NULL;
  47059. const char *text = "Hello wolfSSL !";
  47060. unsigned char hash[SHA256_DIGEST_LENGTH];
  47061. unsigned char signature[2048/8];
  47062. unsigned int signatureLength;
  47063. byte *buf = NULL;
  47064. BIO *bio = NULL;
  47065. SHA256_CTX c;
  47066. EVP_PKEY *evpPkey = NULL;
  47067. EVP_PKEY *evpPubkey = NULL;
  47068. size_t sz;
  47069. /* generate hash */
  47070. SHA256_Init(&c);
  47071. SHA256_Update(&c, text, strlen(text));
  47072. SHA256_Final(hash, &c);
  47073. #ifdef WOLFSSL_SMALL_STACK_CACHE
  47074. /* workaround for small stack cache case */
  47075. wc_Sha256Free((wc_Sha256*)&c);
  47076. #endif
  47077. /* read privete key file */
  47078. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  47079. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  47080. ExpectTrue((sz = XFTELL(fp)) > 0);
  47081. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  47082. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  47083. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  47084. if (fp != XBADFILE) {
  47085. XFCLOSE(fp);
  47086. fp = XBADFILE;
  47087. }
  47088. /* read private key and sign hash data */
  47089. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  47090. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  47091. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  47092. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  47093. signature, &signatureLength, pKey), SSL_SUCCESS);
  47094. /* read public key and verify signed data */
  47095. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  47096. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  47097. if (fp != XBADFILE)
  47098. XFCLOSE(fp);
  47099. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  47100. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  47101. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47102. signatureLength, pubKey), SSL_SUCCESS);
  47103. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  47104. signatureLength, NULL), SSL_FAILURE);
  47105. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  47106. signatureLength, pubKey), SSL_FAILURE);
  47107. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  47108. signatureLength, pubKey), SSL_FAILURE);
  47109. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  47110. signatureLength, NULL), SSL_FAILURE);
  47111. RSA_free(pKey);
  47112. EVP_PKEY_free(evpPkey);
  47113. RSA_free(pubKey);
  47114. EVP_PKEY_free(evpPubkey);
  47115. X509_free(cert);
  47116. BIO_free(bio);
  47117. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  47118. #endif
  47119. #endif
  47120. return EXPECT_RESULT();
  47121. }
  47122. static int test_wolfSSL_RSA_sign(void)
  47123. {
  47124. EXPECT_DECLS;
  47125. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47126. RSA *rsa;
  47127. unsigned char hash[SHA256_DIGEST_LENGTH];
  47128. #ifdef USE_CERT_BUFFERS_1024
  47129. const unsigned char* privDer = client_key_der_1024;
  47130. size_t privDerSz = sizeof_client_key_der_1024;
  47131. const unsigned char* pubDer = client_keypub_der_1024;
  47132. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47133. unsigned char signature[1024/8];
  47134. #else
  47135. const unsigned char* privDer = client_key_der_2048;
  47136. size_t privDerSz = sizeof_client_key_der_2048;
  47137. const unsigned char* pubDer = client_keypub_der_2048;
  47138. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47139. unsigned char signature[2048/8];
  47140. #endif
  47141. unsigned int signatureLen;
  47142. const unsigned char* der;
  47143. XMEMSET(hash, 0, sizeof(hash));
  47144. der = privDer;
  47145. rsa = NULL;
  47146. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47147. /* Invalid parameters. */
  47148. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  47149. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  47150. &signatureLen, rsa), 0);
  47151. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  47152. &signatureLen, rsa), 0);
  47153. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  47154. &signatureLen, rsa), 0);
  47155. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47156. NULL, rsa), 0);
  47157. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47158. &signatureLen, NULL), 0);
  47159. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  47160. &signatureLen, rsa), 1);
  47161. RSA_free(rsa);
  47162. der = pubDer;
  47163. rsa = NULL;
  47164. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47165. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47166. signatureLen, rsa), 1);
  47167. RSA_free(rsa);
  47168. #endif
  47169. return EXPECT_RESULT();
  47170. }
  47171. static int test_wolfSSL_RSA_sign_ex(void)
  47172. {
  47173. EXPECT_DECLS;
  47174. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47175. RSA *rsa = NULL;
  47176. unsigned char hash[SHA256_DIGEST_LENGTH];
  47177. #ifdef USE_CERT_BUFFERS_1024
  47178. const unsigned char* privDer = client_key_der_1024;
  47179. size_t privDerSz = sizeof_client_key_der_1024;
  47180. const unsigned char* pubDer = client_keypub_der_1024;
  47181. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47182. unsigned char signature[1024/8];
  47183. #else
  47184. const unsigned char* privDer = client_key_der_2048;
  47185. size_t privDerSz = sizeof_client_key_der_2048;
  47186. const unsigned char* pubDer = client_keypub_der_2048;
  47187. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47188. unsigned char signature[2048/8];
  47189. #endif
  47190. unsigned int signatureLen;
  47191. const unsigned char* der;
  47192. unsigned char encodedHash[51];
  47193. unsigned int encodedHashLen;
  47194. const unsigned char expEncHash[] = {
  47195. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  47196. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  47197. 0x00, 0x04, 0x20,
  47198. /* Hash data */
  47199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47200. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47201. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47203. };
  47204. XMEMSET(hash, 0, sizeof(hash));
  47205. ExpectNotNull(rsa = wolfSSL_RSA_new());
  47206. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47207. &signatureLen, rsa, 1), 0);
  47208. wolfSSL_RSA_free(rsa);
  47209. der = privDer;
  47210. rsa = NULL;
  47211. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47212. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  47213. -1), 0);
  47214. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  47215. signature, &signatureLen, rsa, 1), 0);
  47216. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47217. &signatureLen, rsa, 1), 0);
  47218. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47219. &signatureLen, rsa, 1), 0);
  47220. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47221. NULL, rsa, 1), 0);
  47222. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47223. &signatureLen, NULL, 1), 0);
  47224. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47225. &signatureLen, rsa, -1), 0);
  47226. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  47227. &signatureLen, rsa, 0), 0);
  47228. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  47229. &signatureLen, rsa, 0), 0);
  47230. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47231. NULL, rsa, 0), 0);
  47232. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  47233. &signatureLen, rsa, 1), 1);
  47234. /* Test returning encoded hash. */
  47235. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  47236. &encodedHashLen, rsa, 0), 1);
  47237. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  47238. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  47239. RSA_free(rsa);
  47240. der = pubDer;
  47241. rsa = NULL;
  47242. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47243. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  47244. signatureLen, rsa), 1);
  47245. RSA_free(rsa);
  47246. #endif
  47247. return EXPECT_RESULT();
  47248. }
  47249. static int test_wolfSSL_RSA_public_decrypt(void)
  47250. {
  47251. EXPECT_DECLS;
  47252. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47253. RSA *rsa;
  47254. unsigned char msg[SHA256_DIGEST_LENGTH];
  47255. #ifdef USE_CERT_BUFFERS_1024
  47256. const unsigned char* pubDer = client_keypub_der_1024;
  47257. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47258. unsigned char decMsg[1024/8];
  47259. const unsigned char encMsg[] = {
  47260. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47261. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47262. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47263. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47264. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47265. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47266. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47267. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47268. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47269. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47270. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47271. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47272. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47273. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47274. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47275. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47276. };
  47277. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47278. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47279. defined(WC_RSA_NO_PADDING)
  47280. const unsigned char encMsgNoPad[] = {
  47281. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47282. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47283. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47284. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47285. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47286. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47287. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47288. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47289. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47290. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47291. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47292. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47293. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47294. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47295. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47296. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47297. };
  47298. #endif
  47299. #else
  47300. const unsigned char* pubDer = client_keypub_der_2048;
  47301. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47302. unsigned char decMsg[2048/8];
  47303. const unsigned char encMsg[] = {
  47304. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47305. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47306. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47307. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47308. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47309. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47310. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47311. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47312. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47313. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47314. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47315. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47316. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47317. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47318. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47319. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47320. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47321. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47322. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47323. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47324. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47325. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47326. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47327. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47328. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47329. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47330. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47331. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47332. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47333. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47334. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47335. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47336. };
  47337. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47338. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47339. defined(WC_RSA_NO_PADDING)
  47340. const unsigned char encMsgNoPad[] = {
  47341. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47342. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47343. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47344. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47345. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47346. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47347. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47348. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47349. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47350. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47351. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47352. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47353. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47354. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47355. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47356. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47357. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47358. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47359. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47360. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47361. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47362. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47363. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47364. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47365. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47366. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47367. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47368. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47369. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47370. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47371. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47372. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47373. };
  47374. #endif
  47375. #endif
  47376. const unsigned char* der;
  47377. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47378. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47379. defined(WC_RSA_NO_PADDING)
  47380. int i;
  47381. #endif
  47382. XMEMSET(msg, 0, sizeof(msg));
  47383. der = pubDer;
  47384. rsa = NULL;
  47385. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47386. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  47387. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  47388. RSA_PKCS1_PADDING), -1);
  47389. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  47390. RSA_PKCS1_PADDING), -1);
  47391. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47392. RSA_PKCS1_PADDING), -1);
  47393. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  47394. RSA_PKCS1_PADDING), -1);
  47395. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47396. RSA_PKCS1_PSS_PADDING), -1);
  47397. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  47398. RSA_PKCS1_PADDING), 32);
  47399. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  47400. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  47401. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  47402. defined(WC_RSA_NO_PADDING)
  47403. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  47404. rsa, RSA_NO_PADDING), sizeof(decMsg));
  47405. /* Zeros before actual data. */
  47406. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  47407. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47408. }
  47409. /* Check actual data. */
  47410. XMEMSET(msg, 0x01, sizeof(msg));
  47411. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  47412. #endif
  47413. RSA_free(rsa);
  47414. #endif
  47415. return EXPECT_RESULT();
  47416. }
  47417. static int test_wolfSSL_RSA_private_encrypt(void)
  47418. {
  47419. EXPECT_DECLS;
  47420. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47421. RSA *rsa;
  47422. unsigned char msg[SHA256_DIGEST_LENGTH];
  47423. #ifdef USE_CERT_BUFFERS_1024
  47424. const unsigned char* privDer = client_key_der_1024;
  47425. size_t privDerSz = sizeof_client_key_der_1024;
  47426. unsigned char encMsg[1024/8];
  47427. const unsigned char expEncMsg[] = {
  47428. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  47429. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  47430. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  47431. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  47432. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  47433. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  47434. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  47435. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  47436. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  47437. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  47438. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  47439. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  47440. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  47441. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  47442. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  47443. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  47444. };
  47445. #ifdef WC_RSA_NO_PADDING
  47446. const unsigned char expEncMsgNoPad[] = {
  47447. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  47448. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  47449. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  47450. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  47451. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  47452. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  47453. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  47454. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  47455. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  47456. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  47457. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  47458. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  47459. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  47460. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  47461. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  47462. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  47463. };
  47464. #endif
  47465. #else
  47466. const unsigned char* privDer = client_key_der_2048;
  47467. size_t privDerSz = sizeof_client_key_der_2048;
  47468. unsigned char encMsg[2048/8];
  47469. const unsigned char expEncMsg[] = {
  47470. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  47471. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  47472. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  47473. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  47474. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  47475. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  47476. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  47477. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  47478. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  47479. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  47480. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  47481. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  47482. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  47483. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  47484. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  47485. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  47486. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  47487. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  47488. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  47489. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  47490. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  47491. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  47492. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  47493. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  47494. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  47495. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  47496. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  47497. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  47498. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  47499. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  47500. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  47501. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  47502. };
  47503. #ifdef WC_RSA_NO_PADDING
  47504. const unsigned char expEncMsgNoPad[] = {
  47505. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  47506. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  47507. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  47508. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  47509. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  47510. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  47511. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  47512. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  47513. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  47514. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  47515. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  47516. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  47517. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  47518. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  47519. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  47520. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  47521. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  47522. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  47523. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  47524. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  47525. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  47526. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  47527. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  47528. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  47529. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  47530. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  47531. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  47532. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  47533. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  47534. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  47535. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  47536. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  47537. };
  47538. #endif
  47539. #endif
  47540. const unsigned char* der;
  47541. XMEMSET(msg, 0x00, sizeof(msg));
  47542. der = privDer;
  47543. rsa = NULL;
  47544. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47545. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  47546. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  47547. -1);
  47548. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47549. RSA_PKCS1_PADDING), -1);
  47550. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  47551. RSA_PKCS1_PADDING), -1);
  47552. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  47553. RSA_PKCS1_PADDING), -1);
  47554. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47555. RSA_PKCS1_PSS_PADDING), -1);
  47556. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47557. RSA_PKCS1_PADDING), sizeof(encMsg));
  47558. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  47559. #ifdef WC_RSA_NO_PADDING
  47560. /* Non-zero message. */
  47561. XMEMSET(msg, 0x01, sizeof(msg));
  47562. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  47563. RSA_NO_PADDING), sizeof(encMsg));
  47564. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  47565. #endif
  47566. RSA_free(rsa);
  47567. #endif
  47568. return EXPECT_RESULT();
  47569. }
  47570. static int test_wolfSSL_RSA_public_encrypt(void)
  47571. {
  47572. EXPECT_DECLS;
  47573. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47574. RSA* rsa = NULL;
  47575. const unsigned char msg[2048/8] = { 0 };
  47576. unsigned char encMsg[2048/8];
  47577. ExpectNotNull(rsa = RSA_new());
  47578. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  47579. RSA_PKCS1_PADDING), -1);
  47580. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  47581. RSA_PKCS1_PADDING), -1);
  47582. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  47583. RSA_PKCS1_PADDING), -1);
  47584. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  47585. RSA_PKCS1_PADDING), -1);
  47586. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47587. RSA_PKCS1_PSS_PADDING), -1);
  47588. /* Empty RSA key. */
  47589. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  47590. RSA_PKCS1_PADDING), -1);
  47591. RSA_free(rsa);
  47592. #endif
  47593. return EXPECT_RESULT();
  47594. }
  47595. static int test_wolfSSL_RSA_private_decrypt(void)
  47596. {
  47597. EXPECT_DECLS;
  47598. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  47599. RSA* rsa = NULL;
  47600. unsigned char msg[2048/8];
  47601. const unsigned char encMsg[2048/8] = { 0 };
  47602. ExpectNotNull(rsa = RSA_new());
  47603. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  47604. RSA_PKCS1_PADDING), -1);
  47605. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  47606. RSA_PKCS1_PADDING), -1);
  47607. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  47608. RSA_PKCS1_PADDING), -1);
  47609. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  47610. RSA_PKCS1_PADDING), -1);
  47611. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47612. RSA_PKCS1_PSS_PADDING), -1);
  47613. /* Empty RSA key. */
  47614. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  47615. RSA_PKCS1_PADDING), -1);
  47616. RSA_free(rsa);
  47617. #endif
  47618. return EXPECT_RESULT();
  47619. }
  47620. static int test_wolfSSL_RSA_GenAdd(void)
  47621. {
  47622. EXPECT_DECLS;
  47623. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47624. RSA *rsa;
  47625. #ifdef USE_CERT_BUFFERS_1024
  47626. const unsigned char* privDer = client_key_der_1024;
  47627. size_t privDerSz = sizeof_client_key_der_1024;
  47628. const unsigned char* pubDer = client_keypub_der_1024;
  47629. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47630. #else
  47631. const unsigned char* privDer = client_key_der_2048;
  47632. size_t privDerSz = sizeof_client_key_der_2048;
  47633. const unsigned char* pubDer = client_keypub_der_2048;
  47634. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47635. #endif
  47636. const unsigned char* der;
  47637. der = privDer;
  47638. rsa = NULL;
  47639. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47640. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  47641. #ifndef RSA_LOW_MEM
  47642. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  47643. #else
  47644. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  47645. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47646. #endif
  47647. RSA_free(rsa);
  47648. der = pubDer;
  47649. rsa = NULL;
  47650. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47651. /* Need private values. */
  47652. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  47653. RSA_free(rsa);
  47654. #endif
  47655. return EXPECT_RESULT();
  47656. }
  47657. static int test_wolfSSL_RSA_blinding_on(void)
  47658. {
  47659. EXPECT_DECLS;
  47660. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  47661. RSA *rsa;
  47662. WOLFSSL_BN_CTX *bnCtx = NULL;
  47663. #ifdef USE_CERT_BUFFERS_1024
  47664. const unsigned char* privDer = client_key_der_1024;
  47665. size_t privDerSz = sizeof_client_key_der_1024;
  47666. #else
  47667. const unsigned char* privDer = client_key_der_2048;
  47668. size_t privDerSz = sizeof_client_key_der_2048;
  47669. #endif
  47670. const unsigned char* der;
  47671. der = privDer;
  47672. rsa = NULL;
  47673. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47674. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  47675. /* Does nothing so all parameters are valid. */
  47676. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  47677. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  47678. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  47679. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  47680. wolfSSL_BN_CTX_free(bnCtx);
  47681. RSA_free(rsa);
  47682. #endif
  47683. return EXPECT_RESULT();
  47684. }
  47685. static int test_wolfSSL_RSA_ex_data(void)
  47686. {
  47687. EXPECT_DECLS;
  47688. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  47689. RSA* rsa = NULL;
  47690. unsigned char data[1];
  47691. ExpectNotNull(rsa = RSA_new());
  47692. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  47693. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47694. #ifdef MAX_EX_DATA
  47695. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  47696. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  47697. #endif
  47698. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  47699. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  47700. #ifdef HAVE_EX_DATA
  47701. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  47702. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  47703. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  47704. #else
  47705. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  47706. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  47707. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  47708. #endif
  47709. RSA_free(rsa);
  47710. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47711. return EXPECT_RESULT();
  47712. }
  47713. static int test_wolfSSL_RSA_LoadDer(void)
  47714. {
  47715. EXPECT_DECLS;
  47716. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47717. defined(OPENSSL_EXTRA_X509_SMALL))
  47718. RSA *rsa = NULL;
  47719. #ifdef USE_CERT_BUFFERS_1024
  47720. const unsigned char* privDer = client_key_der_1024;
  47721. size_t privDerSz = sizeof_client_key_der_1024;
  47722. #else
  47723. const unsigned char* privDer = client_key_der_2048;
  47724. size_t privDerSz = sizeof_client_key_der_2048;
  47725. #endif
  47726. ExpectNotNull(rsa = RSA_new());
  47727. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47728. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47729. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47730. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47731. RSA_free(rsa);
  47732. #endif /* !NO_RSA && OPENSSL_EXTRA */
  47733. return EXPECT_RESULT();
  47734. }
  47735. /* Local API. */
  47736. static int test_wolfSSL_RSA_To_Der(void)
  47737. {
  47738. EXPECT_DECLS;
  47739. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47740. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47741. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47742. RSA* rsa;
  47743. #ifdef USE_CERT_BUFFERS_1024
  47744. const unsigned char* privDer = client_key_der_1024;
  47745. size_t privDerSz = sizeof_client_key_der_1024;
  47746. const unsigned char* pubDer = client_keypub_der_1024;
  47747. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47748. unsigned char out[sizeof(client_key_der_1024)];
  47749. #else
  47750. const unsigned char* privDer = client_key_der_2048;
  47751. size_t privDerSz = sizeof_client_key_der_2048;
  47752. const unsigned char* pubDer = client_keypub_der_2048;
  47753. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47754. unsigned char out[sizeof(client_key_der_2048)];
  47755. #endif
  47756. const unsigned char* der;
  47757. unsigned char* outDer = NULL;
  47758. der = privDer;
  47759. rsa = NULL;
  47760. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47761. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47762. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47763. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47764. outDer = out;
  47765. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47766. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47767. outDer = NULL;
  47768. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47769. ExpectNotNull(outDer);
  47770. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47771. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47772. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47773. outDer = out;
  47774. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47775. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47776. RSA_free(rsa);
  47777. ExpectNotNull(rsa = RSA_new());
  47778. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47779. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47780. RSA_free(rsa);
  47781. der = pubDer;
  47782. rsa = NULL;
  47783. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47784. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47785. RSA_free(rsa);
  47786. #endif
  47787. #endif
  47788. return EXPECT_RESULT();
  47789. }
  47790. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47791. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47792. {
  47793. EXPECT_DECLS;
  47794. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47795. XFILE file = XBADFILE;
  47796. const char* fname = "./certs/server-keyPub.pem";
  47797. RSA *rsa = NULL;
  47798. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47799. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  47800. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  47801. ExpectIntEQ(RSA_size(rsa), 256);
  47802. RSA_free(rsa);
  47803. if (file != XBADFILE)
  47804. XFCLOSE(file);
  47805. #endif
  47806. return EXPECT_RESULT();
  47807. }
  47808. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47809. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  47810. {
  47811. EXPECT_DECLS;
  47812. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  47813. defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  47814. RSA* rsa = NULL;
  47815. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  47816. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  47817. /* Valid but stub so returns 0. */
  47818. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  47819. #endif
  47820. return EXPECT_RESULT();
  47821. }
  47822. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47823. {
  47824. EXPECT_DECLS;
  47825. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47826. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47827. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47828. RSA* rsa = NULL;
  47829. #ifdef USE_CERT_BUFFERS_1024
  47830. const unsigned char* privDer = client_key_der_1024;
  47831. size_t privDerSz = sizeof_client_key_der_1024;
  47832. #else
  47833. const unsigned char* privDer = client_key_der_2048;
  47834. size_t privDerSz = sizeof_client_key_der_2048;
  47835. #endif
  47836. const unsigned char* der;
  47837. #ifndef NO_AES
  47838. unsigned char passwd[] = "password";
  47839. #endif
  47840. ExpectNotNull(rsa = RSA_new());
  47841. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47842. NULL, NULL), 0);
  47843. RSA_free(rsa);
  47844. der = privDer;
  47845. rsa = NULL;
  47846. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47847. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47848. NULL, NULL), 0);
  47849. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  47850. NULL, NULL), 0);
  47851. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  47852. NULL, NULL), 1);
  47853. #ifndef NO_AES
  47854. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47855. NULL, 0, NULL, NULL), 1);
  47856. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  47857. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  47858. #endif
  47859. RSA_free(rsa);
  47860. #endif
  47861. return EXPECT_RESULT();
  47862. }
  47863. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  47864. {
  47865. EXPECT_DECLS;
  47866. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47867. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47868. defined(WOLFSSL_DER_TO_PEM))
  47869. RSA* rsa = NULL;
  47870. #ifdef USE_CERT_BUFFERS_1024
  47871. const unsigned char* privDer = client_key_der_1024;
  47872. size_t privDerSz = sizeof_client_key_der_1024;
  47873. #else
  47874. const unsigned char* privDer = client_key_der_2048;
  47875. size_t privDerSz = sizeof_client_key_der_2048;
  47876. #endif
  47877. const unsigned char* der;
  47878. #ifndef NO_AES
  47879. unsigned char passwd[] = "password";
  47880. #endif
  47881. unsigned char* pem = NULL;
  47882. int plen;
  47883. ExpectNotNull(rsa = RSA_new());
  47884. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47885. &plen), 0);
  47886. RSA_free(rsa);
  47887. der = privDer;
  47888. rsa = NULL;
  47889. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47890. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  47891. &plen), 0);
  47892. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  47893. &plen), 0);
  47894. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47895. NULL), 0);
  47896. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47897. &plen), 1);
  47898. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47899. pem = NULL;
  47900. #ifndef NO_AES
  47901. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47902. NULL, 0, &pem, &plen), 1);
  47903. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47904. pem = NULL;
  47905. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47906. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  47907. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47908. #endif
  47909. RSA_free(rsa);
  47910. #endif
  47911. return EXPECT_RESULT();
  47912. }
  47913. static int test_wolfSSL_DH(void)
  47914. {
  47915. EXPECT_DECLS;
  47916. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47917. DH *dh = NULL;
  47918. BIGNUM* p;
  47919. BIGNUM* q;
  47920. BIGNUM* g;
  47921. BIGNUM* pub = NULL;
  47922. BIGNUM* priv = NULL;
  47923. #if defined(OPENSSL_ALL)
  47924. #if !defined(HAVE_FIPS) || \
  47925. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47926. FILE* f = NULL;
  47927. unsigned char buf[268];
  47928. const unsigned char* pt = buf;
  47929. long len = 0;
  47930. dh = NULL;
  47931. XMEMSET(buf, 0, sizeof(buf));
  47932. /* Test 2048 bit parameters */
  47933. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  47934. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  47935. if (f != XBADFILE)
  47936. XFCLOSE(f);
  47937. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47938. ExpectNotNull(dh->p);
  47939. ExpectNotNull(dh->g);
  47940. ExpectTrue(pt == buf);
  47941. ExpectIntEQ(DH_generate_key(dh), 1);
  47942. ExpectIntEQ(DH_generate_key(dh), 1);
  47943. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  47944. ExpectNotNull(pub = BN_new());
  47945. ExpectIntEQ(BN_set_word(pub, 1), 1);
  47946. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  47947. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  47948. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  47949. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  47950. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  47951. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  47952. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  47953. BN_free(pub);
  47954. pub = NULL;
  47955. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47956. (const BIGNUM**)&q,
  47957. (const BIGNUM**)&g);
  47958. ExpectPtrEq(p, dh->p);
  47959. ExpectPtrEq(q, dh->q);
  47960. ExpectPtrEq(g, dh->g);
  47961. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47962. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47963. ExpectPtrEq(pub, dh->pub_key);
  47964. ExpectPtrEq(priv, dh->priv_key);
  47965. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  47966. ExpectPtrEq(pub, dh->pub_key);
  47967. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  47968. ExpectPtrEq(priv, dh->priv_key);
  47969. pub = NULL;
  47970. priv = NULL;
  47971. ExpectNotNull(pub = BN_new());
  47972. ExpectNotNull(priv = BN_new());
  47973. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  47974. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  47975. if (EXPECT_FAIL()) {
  47976. BN_free(pub);
  47977. BN_free(priv);
  47978. }
  47979. pub = NULL;
  47980. priv = NULL;
  47981. ExpectNotNull(pub = BN_new());
  47982. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  47983. if (EXPECT_FAIL()) {
  47984. BN_free(pub);
  47985. }
  47986. ExpectNotNull(priv = BN_new());
  47987. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  47988. if (EXPECT_FAIL()) {
  47989. BN_free(priv);
  47990. }
  47991. ExpectPtrEq(pub, dh->pub_key);
  47992. ExpectPtrEq(priv, dh->priv_key);
  47993. pub = NULL;
  47994. priv = NULL;
  47995. DH_free(dh);
  47996. dh = NULL;
  47997. ExpectNotNull(dh = DH_new());
  47998. p = NULL;
  47999. ExpectNotNull(p = BN_new());
  48000. ExpectIntEQ(BN_set_word(p, 1), 1);
  48001. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  48002. ExpectNotNull(pub = BN_new());
  48003. ExpectNotNull(priv = BN_new());
  48004. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  48005. if (EXPECT_FAIL()) {
  48006. BN_free(pub);
  48007. BN_free(priv);
  48008. }
  48009. pub = NULL;
  48010. priv = NULL;
  48011. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  48012. BN_free(p);
  48013. p = NULL;
  48014. DH_free(dh);
  48015. dh = NULL;
  48016. #ifdef WOLFSSL_KEY_GEN
  48017. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  48018. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  48019. DH_free(dh);
  48020. #endif
  48021. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  48022. #endif /* OPENSSL_ALL */
  48023. (void)dh;
  48024. (void)p;
  48025. (void)q;
  48026. (void)g;
  48027. (void)pub;
  48028. (void)priv;
  48029. ExpectNotNull(dh = wolfSSL_DH_new());
  48030. /* invalid parameters test */
  48031. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  48032. (const BIGNUM**)&q,
  48033. (const BIGNUM**)&g);
  48034. DH_get0_pqg(dh, NULL,
  48035. (const BIGNUM**)&q,
  48036. (const BIGNUM**)&g);
  48037. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  48038. DH_get0_pqg(dh, NULL, NULL, NULL);
  48039. DH_get0_pqg(dh, (const BIGNUM**)&p,
  48040. (const BIGNUM**)&q,
  48041. (const BIGNUM**)&g);
  48042. ExpectPtrEq(p, NULL);
  48043. ExpectPtrEq(q, NULL);
  48044. ExpectPtrEq(g, NULL);
  48045. DH_free(dh);
  48046. dh = NULL;
  48047. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  48048. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  48049. #if defined(OPENSSL_ALL) || \
  48050. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  48051. dh = wolfSSL_DH_new();
  48052. ExpectNotNull(dh);
  48053. p = wolfSSL_BN_new();
  48054. ExpectNotNull(p);
  48055. ExpectIntEQ(BN_set_word(p, 11), 1);
  48056. g = wolfSSL_BN_new();
  48057. ExpectNotNull(g);
  48058. ExpectIntEQ(BN_set_word(g, 2), 1);
  48059. q = wolfSSL_BN_new();
  48060. ExpectNotNull(q);
  48061. ExpectIntEQ(BN_set_word(q, 5), 1);
  48062. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  48063. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  48064. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  48065. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  48066. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  48067. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  48068. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  48069. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  48070. /* Don't need q. */
  48071. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48072. if (EXPECT_FAIL()) {
  48073. BN_free(p);
  48074. BN_free(g);
  48075. }
  48076. p = NULL;
  48077. g = NULL;
  48078. /* Setting again will free the p and g. */
  48079. wolfSSL_BN_free(q);
  48080. q = NULL;
  48081. DH_free(dh);
  48082. dh = NULL;
  48083. dh = wolfSSL_DH_new();
  48084. ExpectNotNull(dh);
  48085. p = wolfSSL_BN_new();
  48086. ExpectNotNull(p);
  48087. ExpectIntEQ(BN_set_word(p, 11), 1);
  48088. g = wolfSSL_BN_new();
  48089. ExpectNotNull(g);
  48090. ExpectIntEQ(BN_set_word(g, 2), 1);
  48091. q = wolfSSL_BN_new();
  48092. ExpectNotNull(q);
  48093. ExpectIntEQ(BN_set_word(q, 5), 1);
  48094. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  48095. /* p, q and g are now owned by dh - don't free. */
  48096. if (EXPECT_FAIL()) {
  48097. BN_free(p);
  48098. BN_free(q);
  48099. BN_free(g);
  48100. }
  48101. p = NULL;
  48102. q = NULL;
  48103. g = NULL;
  48104. p = wolfSSL_BN_new();
  48105. ExpectNotNull(p);
  48106. ExpectIntEQ(BN_set_word(p, 11), 1);
  48107. g = wolfSSL_BN_new();
  48108. ExpectNotNull(g);
  48109. ExpectIntEQ(BN_set_word(g, 2), 1);
  48110. q = wolfSSL_BN_new();
  48111. ExpectNotNull(q);
  48112. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  48113. if (EXPECT_FAIL()) {
  48114. BN_free(p);
  48115. }
  48116. p = NULL;
  48117. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  48118. if (EXPECT_FAIL()) {
  48119. BN_free(q);
  48120. }
  48121. q = NULL;
  48122. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  48123. if (EXPECT_FAIL()) {
  48124. BN_free(g);
  48125. }
  48126. g = NULL;
  48127. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  48128. /* p, q and g are now owned by dh - don't free. */
  48129. DH_free(dh);
  48130. dh = NULL;
  48131. ExpectIntEQ(DH_generate_key(NULL), 0);
  48132. ExpectNotNull(dh = DH_new());
  48133. ExpectIntEQ(DH_generate_key(dh), 0);
  48134. p = wolfSSL_BN_new();
  48135. ExpectNotNull(p);
  48136. ExpectIntEQ(BN_set_word(p, 0), 1);
  48137. g = wolfSSL_BN_new();
  48138. ExpectNotNull(g);
  48139. ExpectIntEQ(BN_set_word(g, 2), 1);
  48140. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48141. if (EXPECT_FAIL()) {
  48142. BN_free(p);
  48143. BN_free(g);
  48144. }
  48145. p = NULL;
  48146. g = NULL;
  48147. ExpectIntEQ(DH_generate_key(dh), 0);
  48148. DH_free(dh);
  48149. dh = NULL;
  48150. #endif
  48151. #endif
  48152. /* Test DH_up_ref() */
  48153. dh = wolfSSL_DH_new();
  48154. ExpectNotNull(dh);
  48155. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  48156. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  48157. DH_free(dh); /* decrease ref count */
  48158. DH_free(dh); /* free WOLFSSL_DH */
  48159. q = NULL;
  48160. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  48161. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  48162. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  48163. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  48164. #ifdef HAVE_FFDHE_2048
  48165. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48166. DH_free(dh);
  48167. q = NULL;
  48168. #endif
  48169. #ifdef HAVE_FFDHE_3072
  48170. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  48171. DH_free(dh);
  48172. q = NULL;
  48173. #endif
  48174. #ifdef HAVE_FFDHE_4096
  48175. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  48176. DH_free(dh);
  48177. q = NULL;
  48178. #endif
  48179. #else
  48180. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  48181. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  48182. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  48183. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  48184. #endif /* OPENSSL_EXTRA && !NO_DH */
  48185. return EXPECT_RESULT();
  48186. }
  48187. static int test_wolfSSL_DH_dup(void)
  48188. {
  48189. EXPECT_DECLS;
  48190. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  48191. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  48192. defined(OPENSSL_EXTRA)
  48193. DH *dh = NULL;
  48194. DH *dhDup = NULL;
  48195. ExpectNotNull(dh = wolfSSL_DH_new());
  48196. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  48197. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  48198. #if defined(OPENSSL_ALL) || \
  48199. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  48200. {
  48201. WOLFSSL_BIGNUM* p = NULL;
  48202. WOLFSSL_BIGNUM* g = NULL;
  48203. ExpectNotNull(p = wolfSSL_BN_new());
  48204. ExpectNotNull(g = wolfSSL_BN_new());
  48205. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  48206. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  48207. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  48208. if (EXPECT_FAIL()) {
  48209. wolfSSL_BN_free(p);
  48210. wolfSSL_BN_free(g);
  48211. }
  48212. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  48213. wolfSSL_DH_free(dhDup);
  48214. }
  48215. #endif
  48216. wolfSSL_DH_free(dh);
  48217. #endif
  48218. #endif
  48219. return EXPECT_RESULT();
  48220. }
  48221. static int test_wolfSSL_DH_check(void)
  48222. {
  48223. EXPECT_DECLS;
  48224. #ifdef OPENSSL_ALL
  48225. #ifndef NO_DH
  48226. #ifndef NO_BIO
  48227. #ifndef NO_DSA
  48228. byte buf[6000];
  48229. char file[] = "./certs/dsaparams.pem";
  48230. XFILE f = XBADFILE;
  48231. int bytes;
  48232. BIO* bio = NULL;
  48233. DSA* dsa = NULL;
  48234. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48235. static const byte dh2048[] = {
  48236. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48237. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48238. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48239. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48240. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48241. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48242. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48243. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48244. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48245. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48246. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48247. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48248. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48249. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48250. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48251. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48252. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48253. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48254. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48255. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48256. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48257. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48258. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48259. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48260. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48261. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48262. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48263. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48264. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48265. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48266. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48267. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48268. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48269. 0x93, 0x02, 0x01, 0x02
  48270. };
  48271. const byte* params;
  48272. #endif
  48273. DH* dh = NULL;
  48274. WOLFSSL_BIGNUM* p = NULL;
  48275. WOLFSSL_BIGNUM* g = NULL;
  48276. WOLFSSL_BIGNUM* pTmp = NULL;
  48277. WOLFSSL_BIGNUM* gTmp = NULL;
  48278. int codes = -1;
  48279. #ifndef NO_DSA
  48280. /* Initialize DH */
  48281. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  48282. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  48283. if (f != XBADFILE)
  48284. XFCLOSE(f);
  48285. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  48286. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  48287. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  48288. ExpectNotNull(dh);
  48289. BIO_free(bio);
  48290. DSA_free(dsa);
  48291. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  48292. params = dh2048;
  48293. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  48294. (long)sizeof(dh2048)));
  48295. #else
  48296. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  48297. #endif
  48298. /* Test assumed to be valid dh.
  48299. * Should return WOLFSSL_SUCCESS
  48300. * codes should be 0
  48301. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  48302. */
  48303. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48304. ExpectIntEQ(codes, 0);
  48305. /* Test NULL dh: expected BAD_FUNC_ARG */
  48306. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  48307. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  48308. if (dh != NULL) {
  48309. pTmp = dh->p;
  48310. dh->p = NULL;
  48311. }
  48312. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48313. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48314. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48315. /* set dh->p back to normal so it won't fail on next tests */
  48316. if (dh != NULL) {
  48317. dh->p = pTmp;
  48318. pTmp = NULL;
  48319. }
  48320. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  48321. if (dh != NULL) {
  48322. gTmp = dh->g;
  48323. dh->g = NULL;
  48324. }
  48325. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48326. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48327. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  48328. if (dh != NULL) {
  48329. dh->g = gTmp;
  48330. gTmp = NULL;
  48331. }
  48332. /* Cleanup */
  48333. DH_free(dh);
  48334. dh = NULL;
  48335. dh = DH_new();
  48336. ExpectNotNull(dh);
  48337. /* Check empty DH. */
  48338. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48339. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48340. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  48341. /* Check non-prime valued p. */
  48342. ExpectNotNull(p = BN_new());
  48343. ExpectIntEQ(BN_set_word(p, 4), 1);
  48344. ExpectNotNull(g = BN_new());
  48345. ExpectIntEQ(BN_set_word(g, 2), 1);
  48346. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  48347. if (EXPECT_FAIL()) {
  48348. wolfSSL_BN_free(p);
  48349. wolfSSL_BN_free(g);
  48350. }
  48351. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  48352. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  48353. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  48354. DH_free(dh);
  48355. #endif
  48356. #endif /* !NO_DH && !NO_DSA */
  48357. #endif
  48358. return EXPECT_RESULT();
  48359. }
  48360. static int test_wolfSSL_DH_prime(void)
  48361. {
  48362. EXPECT_DECLS;
  48363. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48364. WOLFSSL_BIGNUM* bn = NULL;
  48365. #if WOLFSSL_MAX_BN_BITS >= 768
  48366. WOLFSSL_BIGNUM* bn2 = NULL;
  48367. #endif
  48368. bn = wolfSSL_DH_768_prime(NULL);
  48369. #if WOLFSSL_MAX_BN_BITS >= 768
  48370. ExpectNotNull(bn);
  48371. bn2 = wolfSSL_DH_768_prime(bn);
  48372. ExpectNotNull(bn2);
  48373. ExpectTrue(bn == bn2);
  48374. wolfSSL_BN_free(bn);
  48375. bn = NULL;
  48376. #else
  48377. ExpectNull(bn);
  48378. #endif
  48379. bn = wolfSSL_DH_1024_prime(NULL);
  48380. #if WOLFSSL_MAX_BN_BITS >= 1024
  48381. ExpectNotNull(bn);
  48382. wolfSSL_BN_free(bn);
  48383. bn = NULL;
  48384. #else
  48385. ExpectNull(bn);
  48386. #endif
  48387. bn = wolfSSL_DH_2048_prime(NULL);
  48388. #if WOLFSSL_MAX_BN_BITS >= 2048
  48389. ExpectNotNull(bn);
  48390. wolfSSL_BN_free(bn);
  48391. bn = NULL;
  48392. #else
  48393. ExpectNull(bn);
  48394. #endif
  48395. bn = wolfSSL_DH_3072_prime(NULL);
  48396. #if WOLFSSL_MAX_BN_BITS >= 3072
  48397. ExpectNotNull(bn);
  48398. wolfSSL_BN_free(bn);
  48399. bn = NULL;
  48400. #else
  48401. ExpectNull(bn);
  48402. #endif
  48403. bn = wolfSSL_DH_4096_prime(NULL);
  48404. #if WOLFSSL_MAX_BN_BITS >= 4096
  48405. ExpectNotNull(bn);
  48406. wolfSSL_BN_free(bn);
  48407. bn = NULL;
  48408. #else
  48409. ExpectNull(bn);
  48410. #endif
  48411. bn = wolfSSL_DH_6144_prime(NULL);
  48412. #if WOLFSSL_MAX_BN_BITS >= 6144
  48413. ExpectNotNull(bn);
  48414. wolfSSL_BN_free(bn);
  48415. bn = NULL;
  48416. #else
  48417. ExpectNull(bn);
  48418. #endif
  48419. bn = wolfSSL_DH_8192_prime(NULL);
  48420. #if WOLFSSL_MAX_BN_BITS >= 8192
  48421. ExpectNotNull(bn);
  48422. wolfSSL_BN_free(bn);
  48423. bn = NULL;
  48424. #else
  48425. ExpectNull(bn);
  48426. #endif
  48427. #endif
  48428. return EXPECT_RESULT();
  48429. }
  48430. static int test_wolfSSL_DH_1536_prime(void)
  48431. {
  48432. EXPECT_DECLS;
  48433. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48434. BIGNUM* bn = NULL;
  48435. unsigned char bits[200];
  48436. int sz = 192; /* known binary size */
  48437. const byte expected[] = {
  48438. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48439. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  48440. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  48441. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  48442. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  48443. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  48444. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  48445. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  48446. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  48447. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  48448. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  48449. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  48450. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  48451. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  48452. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  48453. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  48454. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  48455. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  48456. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  48457. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  48458. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  48459. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  48460. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  48461. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48462. };
  48463. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  48464. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  48465. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  48466. BN_free(bn);
  48467. #endif
  48468. return EXPECT_RESULT();
  48469. }
  48470. static int test_wolfSSL_DH_get_2048_256(void)
  48471. {
  48472. EXPECT_DECLS;
  48473. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  48474. WOLFSSL_DH* dh = NULL;
  48475. const WOLFSSL_BIGNUM* pBn;
  48476. const WOLFSSL_BIGNUM* gBn;
  48477. const WOLFSSL_BIGNUM* qBn;
  48478. const byte pExpected[] = {
  48479. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  48480. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  48481. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  48482. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  48483. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  48484. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  48485. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  48486. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  48487. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  48488. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  48489. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  48490. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  48491. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  48492. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  48493. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  48494. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  48495. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  48496. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  48497. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  48498. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  48499. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  48500. 0x1E, 0x1A, 0x15, 0x97
  48501. };
  48502. const byte gExpected[] = {
  48503. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  48504. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  48505. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  48506. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  48507. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  48508. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  48509. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  48510. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  48511. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  48512. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  48513. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  48514. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  48515. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  48516. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  48517. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  48518. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  48519. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  48520. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  48521. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  48522. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  48523. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  48524. 0x6C, 0xC4, 0x16, 0x59
  48525. };
  48526. const byte qExpected[] = {
  48527. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  48528. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  48529. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  48530. };
  48531. int pSz;
  48532. int qSz;
  48533. int gSz;
  48534. byte* pReturned = NULL;
  48535. byte* qReturned = NULL;
  48536. byte* gReturned = NULL;
  48537. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  48538. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  48539. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  48540. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  48541. DYNAMIC_TYPE_TMP_BUFFER));
  48542. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  48543. ExpectIntEQ(pSz, sizeof(pExpected));
  48544. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  48545. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  48546. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  48547. DYNAMIC_TYPE_TMP_BUFFER));
  48548. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  48549. ExpectIntEQ(qSz, sizeof(qExpected));
  48550. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  48551. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  48552. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  48553. DYNAMIC_TYPE_TMP_BUFFER));
  48554. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  48555. ExpectIntEQ(gSz, sizeof(gExpected));
  48556. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  48557. wolfSSL_DH_free(dh);
  48558. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48559. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48560. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  48561. #endif
  48562. return EXPECT_RESULT();
  48563. }
  48564. static int test_wolfSSL_PEM_write_DHparams(void)
  48565. {
  48566. EXPECT_DECLS;
  48567. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  48568. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  48569. DH* dh = NULL;
  48570. BIO* bio = NULL;
  48571. XFILE fp = XBADFILE;
  48572. byte pem[2048];
  48573. int pemSz = 0;
  48574. const char expected[] =
  48575. "-----BEGIN DH PARAMETERS-----\n"
  48576. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  48577. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  48578. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  48579. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  48580. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  48581. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  48582. "-----END DH PARAMETERS-----\n";
  48583. const char badPem[] =
  48584. "-----BEGIN DH PARAMETERS-----\n"
  48585. "-----END DH PARAMETERS-----\n";
  48586. const char emptySeqPem[] =
  48587. "-----BEGIN DH PARAMETERS-----\n"
  48588. "MAA=\n"
  48589. "-----END DH PARAMETERS-----\n";
  48590. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  48591. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48592. if (fp != XBADFILE) {
  48593. XFCLOSE(fp);
  48594. fp = XBADFILE;
  48595. }
  48596. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  48597. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48598. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48599. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  48600. (int)sizeof(badPem));
  48601. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48602. BIO_free(bio);
  48603. bio = NULL;
  48604. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48605. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48606. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  48607. (int)sizeof(emptySeqPem));
  48608. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48609. BIO_free(bio);
  48610. bio = NULL;
  48611. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  48612. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  48613. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  48614. BIO_free(bio);
  48615. bio = NULL;
  48616. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  48617. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  48618. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  48619. DH_free(dh);
  48620. dh = NULL;
  48621. dh = wolfSSL_DH_new();
  48622. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  48623. if (fp != XBADFILE) {
  48624. XFCLOSE(fp);
  48625. fp = XBADFILE;
  48626. }
  48627. wolfSSL_DH_free(dh);
  48628. dh = NULL;
  48629. /* check results */
  48630. XMEMSET(pem, 0, sizeof(pem));
  48631. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  48632. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  48633. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  48634. if (fp != XBADFILE)
  48635. XFCLOSE(fp);
  48636. #endif
  48637. return EXPECT_RESULT();
  48638. }
  48639. static int test_wolfSSL_d2i_DHparams(void)
  48640. {
  48641. EXPECT_DECLS;
  48642. #ifdef OPENSSL_ALL
  48643. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48644. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48645. XFILE f = XBADFILE;
  48646. unsigned char buf[4096];
  48647. const unsigned char* pt = buf;
  48648. #ifdef HAVE_FFDHE_2048
  48649. const char* params1 = "./certs/dh2048.der";
  48650. #endif
  48651. #ifdef HAVE_FFDHE_3072
  48652. const char* params2 = "./certs/dh3072.der";
  48653. #endif
  48654. long len = 0;
  48655. WOLFSSL_DH* dh = NULL;
  48656. XMEMSET(buf, 0, sizeof(buf));
  48657. /* Test 2048 bit parameters */
  48658. #ifdef HAVE_FFDHE_2048
  48659. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  48660. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48661. if (f != XBADFILE) {
  48662. XFCLOSE(f);
  48663. f = XBADFILE;
  48664. }
  48665. /* Valid case */
  48666. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48667. ExpectNotNull(dh->p);
  48668. ExpectNotNull(dh->g);
  48669. ExpectTrue(pt == buf);
  48670. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  48671. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  48672. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  48673. /* Invalid cases */
  48674. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48675. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48676. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  48677. DH_free(dh);
  48678. dh = NULL;
  48679. *buf = 0;
  48680. pt = buf;
  48681. #endif /* HAVE_FFDHE_2048 */
  48682. /* Test 3072 bit parameters */
  48683. #ifdef HAVE_FFDHE_3072
  48684. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  48685. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48686. if (f != XBADFILE) {
  48687. XFCLOSE(f);
  48688. f = XBADFILE;
  48689. }
  48690. /* Valid case */
  48691. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  48692. ExpectNotNull(dh->p);
  48693. ExpectNotNull(dh->g);
  48694. ExpectTrue(pt != buf);
  48695. ExpectIntEQ(DH_generate_key(dh), 1);
  48696. /* Invalid cases */
  48697. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  48698. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  48699. DH_free(dh);
  48700. dh = NULL;
  48701. #endif /* HAVE_FFDHE_3072 */
  48702. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48703. #endif /* !NO_DH */
  48704. #endif
  48705. return EXPECT_RESULT();
  48706. }
  48707. static int test_wolfSSL_DH_LoadDer(void)
  48708. {
  48709. EXPECT_DECLS;
  48710. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  48711. defined(OPENSSL_EXTRA)
  48712. static const byte dh2048[] = {
  48713. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  48714. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  48715. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  48716. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  48717. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  48718. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  48719. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  48720. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  48721. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  48722. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  48723. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  48724. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  48725. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  48726. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  48727. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  48728. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  48729. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  48730. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  48731. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  48732. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  48733. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  48734. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  48735. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  48736. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  48737. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  48738. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  48739. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  48740. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  48741. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  48742. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  48743. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  48744. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  48745. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  48746. 0x93, 0x02, 0x01, 0x02
  48747. };
  48748. WOLFSSL_DH* dh = NULL;
  48749. ExpectNotNull(dh = wolfSSL_DH_new());
  48750. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  48751. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  48752. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  48753. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  48754. wolfSSL_DH_free(dh);
  48755. #endif
  48756. return EXPECT_RESULT();
  48757. }
  48758. static int test_wolfSSL_i2d_DHparams(void)
  48759. {
  48760. EXPECT_DECLS;
  48761. #ifdef OPENSSL_ALL
  48762. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  48763. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48764. XFILE f = XBADFILE;
  48765. unsigned char buf[4096];
  48766. const unsigned char* pt;
  48767. unsigned char* pt2;
  48768. #ifdef HAVE_FFDHE_2048
  48769. const char* params1 = "./certs/dh2048.der";
  48770. #endif
  48771. #ifdef HAVE_FFDHE_3072
  48772. const char* params2 = "./certs/dh3072.der";
  48773. #endif
  48774. long len;
  48775. WOLFSSL_DH* dh = NULL;
  48776. /* Test 2048 bit parameters */
  48777. #ifdef HAVE_FFDHE_2048
  48778. pt = buf;
  48779. pt2 = buf;
  48780. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  48781. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48782. if (f != XBADFILE) {
  48783. XFCLOSE(f);
  48784. f = XBADFILE;
  48785. }
  48786. /* Valid case */
  48787. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48788. ExpectTrue(pt == buf);
  48789. ExpectIntEQ(DH_generate_key(dh), 1);
  48790. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  48791. /* Invalid case */
  48792. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48793. /* Return length only */
  48794. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  48795. DH_free(dh);
  48796. *buf = 0;
  48797. #endif
  48798. /* Test 3072 bit parameters */
  48799. #ifdef HAVE_FFDHE_3072
  48800. pt = buf;
  48801. pt2 = buf;
  48802. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  48803. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  48804. if (f != XBADFILE) {
  48805. XFCLOSE(f);
  48806. f = XBADFILE;
  48807. }
  48808. /* Valid case */
  48809. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  48810. ExpectTrue(pt == buf);
  48811. ExpectIntEQ(DH_generate_key(dh), 1);
  48812. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  48813. /* Invalid case */
  48814. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  48815. /* Return length only */
  48816. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  48817. DH_free(dh);
  48818. #endif
  48819. dh = DH_new();
  48820. ExpectNotNull(dh);
  48821. pt2 = buf;
  48822. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48823. DH_free(dh);
  48824. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48825. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48826. #endif
  48827. return EXPECT_RESULT();
  48828. }
  48829. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  48830. /*----------------------------------------------------------------------------*
  48831. | EC
  48832. *----------------------------------------------------------------------------*/
  48833. static int test_wolfSSL_EC_GROUP(void)
  48834. {
  48835. EXPECT_DECLS;
  48836. #ifdef OPENSSL_EXTRA
  48837. EC_GROUP *group = NULL;
  48838. EC_GROUP *group2 = NULL;
  48839. EC_GROUP *group3 = NULL;
  48840. #ifndef HAVE_ECC_BRAINPOOL
  48841. EC_GROUP *group4 = NULL;
  48842. #endif
  48843. WOLFSSL_BIGNUM* order = NULL;
  48844. int group_bits;
  48845. int i;
  48846. static const int knownEccNids[] = {
  48847. NID_X9_62_prime192v1,
  48848. NID_X9_62_prime192v2,
  48849. NID_X9_62_prime192v3,
  48850. NID_X9_62_prime239v1,
  48851. NID_X9_62_prime239v2,
  48852. NID_X9_62_prime239v3,
  48853. NID_X9_62_prime256v1,
  48854. NID_secp112r1,
  48855. NID_secp112r2,
  48856. NID_secp128r1,
  48857. NID_secp128r2,
  48858. NID_secp160r1,
  48859. NID_secp160r2,
  48860. NID_secp224r1,
  48861. NID_secp384r1,
  48862. NID_secp521r1,
  48863. NID_secp160k1,
  48864. NID_secp192k1,
  48865. NID_secp224k1,
  48866. NID_secp256k1,
  48867. NID_brainpoolP160r1,
  48868. NID_brainpoolP192r1,
  48869. NID_brainpoolP224r1,
  48870. NID_brainpoolP256r1,
  48871. NID_brainpoolP320r1,
  48872. NID_brainpoolP384r1,
  48873. NID_brainpoolP512r1,
  48874. };
  48875. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  48876. static const int knownEccEnums[] = {
  48877. ECC_SECP192R1,
  48878. ECC_PRIME192V2,
  48879. ECC_PRIME192V3,
  48880. ECC_PRIME239V1,
  48881. ECC_PRIME239V2,
  48882. ECC_PRIME239V3,
  48883. ECC_SECP256R1,
  48884. ECC_SECP112R1,
  48885. ECC_SECP112R2,
  48886. ECC_SECP128R1,
  48887. ECC_SECP128R2,
  48888. ECC_SECP160R1,
  48889. ECC_SECP160R2,
  48890. ECC_SECP224R1,
  48891. ECC_SECP384R1,
  48892. ECC_SECP521R1,
  48893. ECC_SECP160K1,
  48894. ECC_SECP192K1,
  48895. ECC_SECP224K1,
  48896. ECC_SECP256K1,
  48897. ECC_BRAINPOOLP160R1,
  48898. ECC_BRAINPOOLP192R1,
  48899. ECC_BRAINPOOLP224R1,
  48900. ECC_BRAINPOOLP256R1,
  48901. ECC_BRAINPOOLP320R1,
  48902. ECC_BRAINPOOLP384R1,
  48903. ECC_BRAINPOOLP512R1,
  48904. };
  48905. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  48906. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  48907. ExpectNotNull(group2 = EC_GROUP_dup(group));
  48908. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  48909. #ifndef HAVE_ECC_BRAINPOOL
  48910. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  48911. NID_brainpoolP256r1));
  48912. #endif
  48913. ExpectNull(EC_GROUP_dup(NULL));
  48914. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  48915. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  48916. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  48917. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  48918. #ifndef HAVE_ECC_BRAINPOOL
  48919. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  48920. #endif
  48921. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  48922. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  48923. ExpectNotNull(order = BN_new());
  48924. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  48925. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  48926. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  48927. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  48928. wolfSSL_BN_free(order);
  48929. ExpectNotNull(EC_GROUP_method_of(group));
  48930. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  48931. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  48932. NID_X9_62_prime_field);
  48933. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  48934. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  48935. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  48936. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  48937. #ifndef NO_WOLFSSL_STUB
  48938. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  48939. #endif
  48940. #ifndef HAVE_ECC_BRAINPOOL
  48941. EC_GROUP_free(group4);
  48942. #endif
  48943. EC_GROUP_free(group3);
  48944. EC_GROUP_free(group2);
  48945. EC_GROUP_free(group);
  48946. for (i = 0; i < knowEccNidsLen; i++) {
  48947. group = NULL;
  48948. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  48949. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  48950. EC_GROUP_free(group);
  48951. }
  48952. for (i = 0; i < knowEccEnumsLen; i++) {
  48953. group = NULL;
  48954. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  48955. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  48956. EC_GROUP_free(group);
  48957. }
  48958. #endif
  48959. return EXPECT_RESULT();
  48960. }
  48961. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  48962. {
  48963. EXPECT_DECLS;
  48964. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  48965. EC_GROUP *group = NULL;
  48966. BIO* bio = NULL;
  48967. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  48968. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  48969. EC_GROUP *ret = NULL;
  48970. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  48971. "BgUrgQQAIg==\n"
  48972. "-----END EC PARAMETERS-----";
  48973. #endif
  48974. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  48975. "MAA=\n"
  48976. "-----END EC PARAMETERS-----";
  48977. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  48978. "BgA=\n"
  48979. "-----END EC PARAMETERS-----";
  48980. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  48981. "BgE=\n"
  48982. "-----END EC PARAMETERS-----";
  48983. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  48984. "BgE*\n"
  48985. "-----END EC PARAMETERS-----";
  48986. /* Test that first parameter, bio, being NULL fails. */
  48987. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  48988. /* Test that reading named parameters works. */
  48989. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  48990. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  48991. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  48992. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  48993. BIO_free(bio);
  48994. bio = NULL;
  48995. EC_GROUP_free(group);
  48996. group = NULL;
  48997. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  48998. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  48999. /* Test that reusing group works. */
  49000. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  49001. sizeof(ec_nc_p384)));
  49002. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  49003. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  49004. BIO_free(bio);
  49005. bio = NULL;
  49006. EC_GROUP_free(group);
  49007. group = NULL;
  49008. /* Test that returning through group works. */
  49009. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  49010. sizeof(ec_nc_p384)));
  49011. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  49012. ExpectIntEQ(group == ret, 1);
  49013. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  49014. BIO_free(bio);
  49015. bio = NULL;
  49016. EC_GROUP_free(group);
  49017. group = NULL;
  49018. #endif
  49019. /* Test 0x30, 0x00 (not and object id) fails. */
  49020. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  49021. sizeof(ec_nc_bad_1)));
  49022. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49023. BIO_free(bio);
  49024. bio = NULL;
  49025. /* Test 0x06, 0x00 (empty object id) fails. */
  49026. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  49027. sizeof(ec_nc_bad_2)));
  49028. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49029. BIO_free(bio);
  49030. bio = NULL;
  49031. /* Test 0x06, 0x01 (badly formed object id) fails. */
  49032. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  49033. sizeof(ec_nc_bad_3)));
  49034. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49035. BIO_free(bio);
  49036. bio = NULL;
  49037. /* Test invalid PEM encoding - invalid character. */
  49038. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  49039. sizeof(ec_nc_bad_4)));
  49040. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  49041. BIO_free(bio);
  49042. #endif
  49043. return EXPECT_RESULT();
  49044. }
  49045. static int test_wolfSSL_EC_POINT(void)
  49046. {
  49047. EXPECT_DECLS;
  49048. #if !defined(WOLFSSL_SP_MATH) && \
  49049. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  49050. #ifdef OPENSSL_EXTRA
  49051. BN_CTX* ctx = NULL;
  49052. EC_GROUP* group = NULL;
  49053. #ifndef HAVE_ECC_BRAINPOOL
  49054. EC_GROUP* group2 = NULL;
  49055. #endif
  49056. EC_POINT* Gxy = NULL;
  49057. EC_POINT* new_point = NULL;
  49058. EC_POINT* set_point = NULL;
  49059. EC_POINT* infinity = NULL;
  49060. BIGNUM* k = NULL;
  49061. BIGNUM* Gx = NULL;
  49062. BIGNUM* Gy = NULL;
  49063. BIGNUM* Gz = NULL;
  49064. BIGNUM* X = NULL;
  49065. BIGNUM* Y = NULL;
  49066. BIGNUM* set_point_bn = NULL;
  49067. char* hexStr = NULL;
  49068. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  49069. "17AF381913FF7A96314EA47055EA0FD0";
  49070. /* NISTP256R1 Gx/Gy */
  49071. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  49072. "77037D812DEB33A0F4A13945D898C296";
  49073. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  49074. "2BCE33576B315ECECBB6406837BF51F5";
  49075. #ifndef HAVE_SELFTEST
  49076. EC_POINT *tmp = NULL;
  49077. size_t bin_len;
  49078. unsigned int blen = 0;
  49079. unsigned char* buf = NULL;
  49080. unsigned char bufInf[1] = { 0x00 };
  49081. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  49082. "77037D812DEB33A0F4A13945D898C296"
  49083. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  49084. "2BCE33576B315ECECBB6406837BF51F5";
  49085. const unsigned char binUncompG[] = {
  49086. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49087. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49088. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49089. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49090. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49091. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49092. };
  49093. const unsigned char binUncompGBad[] = {
  49094. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49095. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49096. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49097. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49098. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49099. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49100. };
  49101. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  49102. "77037D812DEB33A0F4A13945D898C296";
  49103. #ifdef HAVE_COMP_KEY
  49104. const unsigned char binCompG[] = {
  49105. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49106. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49107. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49108. };
  49109. #endif
  49110. #endif
  49111. ExpectNotNull(ctx = BN_CTX_new());
  49112. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49113. #ifndef HAVE_ECC_BRAINPOOL
  49114. /* Used to make groups curve_idx == -1. */
  49115. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  49116. #endif
  49117. ExpectNull(EC_POINT_new(NULL));
  49118. ExpectNotNull(Gxy = EC_POINT_new(group));
  49119. ExpectNotNull(new_point = EC_POINT_new(group));
  49120. ExpectNotNull(set_point = EC_POINT_new(group));
  49121. ExpectNotNull(X = BN_new());
  49122. ExpectNotNull(Y = BN_new());
  49123. ExpectNotNull(set_point_bn = BN_new());
  49124. ExpectNotNull(infinity = EC_POINT_new(group));
  49125. /* load test values */
  49126. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  49127. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  49128. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  49129. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  49130. /* populate coordinates for input point */
  49131. if (Gxy != NULL) {
  49132. Gxy->X = Gx;
  49133. Gxy->Y = Gy;
  49134. Gxy->Z = Gz;
  49135. }
  49136. /* Test handling of NULL point. */
  49137. EC_POINT_clear_free(NULL);
  49138. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49139. NULL, NULL, ctx), 0);
  49140. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  49141. NULL, NULL, ctx), 0);
  49142. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  49143. NULL, NULL, ctx), 0);
  49144. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49145. X, NULL, ctx), 0);
  49146. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  49147. NULL, Y, ctx), 0);
  49148. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  49149. X, Y, ctx), 0);
  49150. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  49151. X, Y, ctx), 0);
  49152. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  49153. NULL, Y, ctx), 0);
  49154. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  49155. X, NULL, ctx), 0);
  49156. /* Getting point at infinity returns an error. */
  49157. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  49158. X, Y, ctx), 0);
  49159. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  49160. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  49161. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49162. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  49163. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  49164. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  49165. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  49166. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  49167. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  49168. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  49169. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  49170. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  49171. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  49172. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  49173. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  49174. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  49175. /* perform point multiplication */
  49176. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  49177. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49178. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49179. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49180. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  49181. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49182. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49183. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49184. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  49185. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49186. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49187. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49188. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  49189. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  49190. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  49191. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  49192. /* Set point to something. */
  49193. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  49194. #else
  49195. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  49196. ctx), 1);
  49197. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49198. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  49199. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  49200. #endif
  49201. /* check if point X coordinate is zero */
  49202. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  49203. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  49204. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  49205. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  49206. #endif
  49207. /* extract the coordinates from point */
  49208. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49209. ctx), WOLFSSL_SUCCESS);
  49210. /* check if point X coordinate is zero */
  49211. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  49212. /* set the same X and Y points in another object */
  49213. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  49214. ctx), WOLFSSL_SUCCESS);
  49215. /* compare points as they should be the same */
  49216. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  49217. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  49218. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  49219. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  49220. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  49221. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  49222. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  49223. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  49224. /* Test copying */
  49225. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  49226. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  49227. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  49228. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  49229. /* Test inverting */
  49230. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  49231. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  49232. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  49233. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  49234. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  49235. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  49236. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49237. {
  49238. EC_POINT* orig_point = NULL;
  49239. ExpectNotNull(orig_point = EC_POINT_new(group));
  49240. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  49241. 1);
  49242. /* new_point should be set_point inverted so adding it will revert
  49243. * the point back to set_point */
  49244. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  49245. NULL), 1);
  49246. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  49247. EC_POINT_free(orig_point);
  49248. }
  49249. #endif
  49250. /* Test getting affine converts from projective. */
  49251. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  49252. /* Force non-affine coordinates */
  49253. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49254. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  49255. if (new_point != NULL) {
  49256. new_point->inSet = 0;
  49257. }
  49258. /* extract the coordinates from point */
  49259. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  49260. ctx), WOLFSSL_SUCCESS);
  49261. /* check if point ordinates have changed. */
  49262. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  49263. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  49264. /* Test check for infinity */
  49265. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  49266. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  49267. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  49268. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  49269. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  49270. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  49271. #else
  49272. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  49273. #endif
  49274. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  49275. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  49276. /* check bn2hex */
  49277. hexStr = BN_bn2hex(k);
  49278. ExpectStrEQ(hexStr, kTest);
  49279. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49280. BN_print_fp(stderr, k);
  49281. fprintf(stderr, "\n");
  49282. #endif
  49283. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49284. hexStr = BN_bn2hex(Gx);
  49285. ExpectStrEQ(hexStr, kGx);
  49286. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49287. BN_print_fp(stderr, Gx);
  49288. fprintf(stderr, "\n");
  49289. #endif
  49290. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49291. hexStr = BN_bn2hex(Gy);
  49292. ExpectStrEQ(hexStr, kGy);
  49293. #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
  49294. BN_print_fp(stderr, Gy);
  49295. fprintf(stderr, "\n");
  49296. #endif
  49297. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49298. #ifndef HAVE_SELFTEST
  49299. /* Test point to hex */
  49300. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49301. ctx));
  49302. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49303. ctx));
  49304. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49305. ctx));
  49306. #ifndef HAVE_ECC_BRAINPOOL
  49307. /* Group not supported in wolfCrypt. */
  49308. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49309. ctx));
  49310. #endif
  49311. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  49312. ExpectNotNull(hexStr);
  49313. ExpectStrEQ(hexStr, uncompG);
  49314. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49315. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  49316. ExpectNotNull(hexStr);
  49317. ExpectStrEQ(hexStr, compG);
  49318. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  49319. /* Test point to oct */
  49320. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49321. NULL, 0, ctx), 0);
  49322. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49323. NULL, 0, ctx), 0);
  49324. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  49325. NULL, 0, ctx), 0);
  49326. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49327. NULL, 0, ctx);
  49328. ExpectIntEQ(bin_len, sizeof(binUncompG));
  49329. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49330. DYNAMIC_TYPE_ECC));
  49331. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  49332. buf, bin_len, ctx), bin_len);
  49333. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49334. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49335. /* Infinity (x=0, y=0) encodes as '0x00'. */
  49336. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49337. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  49338. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49339. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  49340. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  49341. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  49342. ExpectIntEQ(bufInf[0], 0);
  49343. wolfSSL_EC_POINT_dump(NULL, NULL);
  49344. /* Test point i2d */
  49345. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  49346. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  49347. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  49348. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  49349. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  49350. ExpectIntEQ(blen, sizeof(binUncompG));
  49351. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  49352. blen -= 1;
  49353. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  49354. blen += 1;
  49355. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  49356. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  49357. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49358. #ifdef HAVE_COMP_KEY
  49359. /* Test point to oct compressed */
  49360. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  49361. 0, ctx);
  49362. ExpectIntEQ(bin_len, sizeof(binCompG));
  49363. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  49364. DYNAMIC_TYPE_ECC));
  49365. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  49366. bin_len, ctx), bin_len);
  49367. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  49368. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  49369. #endif
  49370. /* Test point BN */
  49371. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  49372. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49373. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  49374. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49375. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  49376. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  49377. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  49378. /* Test oct to point */
  49379. ExpectNotNull(tmp = EC_POINT_new(group));
  49380. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  49381. ctx), 0);
  49382. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  49383. ctx), 0);
  49384. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  49385. ctx), 0);
  49386. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  49387. sizeof(binUncompGBad), ctx), 0);
  49388. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  49389. ctx), 1);
  49390. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49391. EC_POINT_free(tmp);
  49392. tmp = NULL;
  49393. /* Test setting BN ordinates. */
  49394. ExpectNotNull(tmp = EC_POINT_new(group));
  49395. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49396. NULL, ctx), 0);
  49397. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  49398. NULL, ctx), 0);
  49399. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  49400. NULL, ctx), 0);
  49401. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  49402. NULL, ctx), 0);
  49403. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  49404. Gy, ctx), 0);
  49405. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  49406. ctx), 0);
  49407. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  49408. ctx), 0);
  49409. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  49410. Gy, ctx), 0);
  49411. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  49412. NULL, ctx), 0);
  49413. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  49414. ctx), 1);
  49415. EC_POINT_free(tmp);
  49416. tmp = NULL;
  49417. /* Test point d2i */
  49418. ExpectNotNull(tmp = EC_POINT_new(group));
  49419. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  49420. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  49421. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  49422. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  49423. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  49424. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  49425. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  49426. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  49427. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  49428. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49429. EC_POINT_free(tmp);
  49430. tmp = NULL;
  49431. #ifdef HAVE_COMP_KEY
  49432. /* Test oct compressed to point */
  49433. ExpectNotNull(tmp = EC_POINT_new(group));
  49434. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  49435. 1);
  49436. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49437. EC_POINT_free(tmp);
  49438. tmp = NULL;
  49439. /* Test point d2i - compressed */
  49440. ExpectNotNull(tmp = EC_POINT_new(group));
  49441. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  49442. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  49443. EC_POINT_free(tmp);
  49444. tmp = NULL;
  49445. #endif
  49446. #endif
  49447. /* test BN_mod_add */
  49448. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  49449. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  49450. 1);
  49451. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  49452. /* cleanup */
  49453. BN_free(X);
  49454. BN_free(Y);
  49455. BN_free(k);
  49456. BN_free(set_point_bn);
  49457. EC_POINT_free(infinity);
  49458. EC_POINT_free(new_point);
  49459. EC_POINT_free(set_point);
  49460. EC_POINT_clear_free(Gxy);
  49461. #ifndef HAVE_ECC_BRAINPOOL
  49462. EC_GROUP_free(group2);
  49463. #endif
  49464. EC_GROUP_free(group);
  49465. BN_CTX_free(ctx);
  49466. #endif
  49467. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  49468. return EXPECT_RESULT();
  49469. }
  49470. static int test_wolfSSL_SPAKE(void)
  49471. {
  49472. EXPECT_DECLS;
  49473. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  49474. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  49475. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  49476. BIGNUM* x = NULL; /* kdc priv */
  49477. BIGNUM* y = NULL; /* client priv */
  49478. BIGNUM* w = NULL; /* shared value */
  49479. byte M_bytes[] = {
  49480. /* uncompressed */
  49481. 0x04,
  49482. /* x */
  49483. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  49484. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  49485. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  49486. /* y */
  49487. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  49488. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  49489. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  49490. };
  49491. EC_POINT* M = NULL; /* shared value */
  49492. byte N_bytes[] = {
  49493. /* uncompressed */
  49494. 0x04,
  49495. /* x */
  49496. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  49497. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  49498. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  49499. /* y */
  49500. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  49501. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  49502. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  49503. };
  49504. EC_POINT* N = NULL; /* shared value */
  49505. EC_POINT* T = NULL; /* kdc pub */
  49506. EC_POINT* tmp1 = NULL; /* kdc pub */
  49507. EC_POINT* tmp2 = NULL; /* kdc pub */
  49508. EC_POINT* S = NULL; /* client pub */
  49509. EC_POINT* client_secret = NULL;
  49510. EC_POINT* kdc_secret = NULL;
  49511. EC_GROUP* group = NULL;
  49512. BN_CTX* bn_ctx = NULL;
  49513. /* Values taken from a test run of Kerberos 5 */
  49514. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49515. ExpectNotNull(bn_ctx = BN_CTX_new());
  49516. ExpectNotNull(M = EC_POINT_new(group));
  49517. ExpectNotNull(N = EC_POINT_new(group));
  49518. ExpectNotNull(T = EC_POINT_new(group));
  49519. ExpectNotNull(tmp1 = EC_POINT_new(group));
  49520. ExpectNotNull(tmp2 = EC_POINT_new(group));
  49521. ExpectNotNull(S = EC_POINT_new(group));
  49522. ExpectNotNull(client_secret = EC_POINT_new(group));
  49523. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  49524. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  49525. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  49526. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  49527. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  49528. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  49529. "506AB395478F0AAB647752CF117B36250"), 1);
  49530. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  49531. 1);
  49532. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  49533. 1);
  49534. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  49535. /* kdc */
  49536. /* T=x*P+w*M */
  49537. /* All in one function call */
  49538. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  49539. /* Spread into separate calls */
  49540. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  49541. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  49542. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  49543. 1);
  49544. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  49545. /* client */
  49546. /* S=y*P+w*N */
  49547. /* All in one function call */
  49548. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  49549. /* Spread into separate calls */
  49550. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  49551. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  49552. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  49553. 1);
  49554. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  49555. /* K=y*(T-w*M) */
  49556. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  49557. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  49558. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  49559. 1);
  49560. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  49561. bn_ctx), 1);
  49562. /* kdc */
  49563. /* K=x*(S-w*N) */
  49564. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  49565. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  49566. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  49567. 1);
  49568. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  49569. 1);
  49570. /* kdc_secret == client_secret */
  49571. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  49572. BN_free(x);
  49573. BN_free(y);
  49574. BN_free(w);
  49575. EC_POINT_free(M);
  49576. EC_POINT_free(N);
  49577. EC_POINT_free(T);
  49578. EC_POINT_free(tmp1);
  49579. EC_POINT_free(tmp2);
  49580. EC_POINT_free(S);
  49581. EC_POINT_free(client_secret);
  49582. EC_POINT_free(kdc_secret);
  49583. EC_GROUP_free(group);
  49584. BN_CTX_free(bn_ctx);
  49585. #endif
  49586. return EXPECT_RESULT();
  49587. }
  49588. static int test_wolfSSL_EC_KEY_generate(void)
  49589. {
  49590. EXPECT_DECLS;
  49591. #ifdef OPENSSL_EXTRA
  49592. WOLFSSL_EC_KEY* key = NULL;
  49593. #ifndef HAVE_ECC_BRAINPOOL
  49594. WOLFSSL_EC_GROUP* group = NULL;
  49595. #endif
  49596. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49597. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  49598. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  49599. wolfSSL_EC_KEY_free(key);
  49600. key = NULL;
  49601. #ifndef HAVE_ECC_BRAINPOOL
  49602. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  49603. NID_brainpoolP256r1));
  49604. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  49605. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  49606. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  49607. wolfSSL_EC_KEY_free(key);
  49608. wolfSSL_EC_GROUP_free(group);
  49609. #endif
  49610. #endif
  49611. return EXPECT_RESULT();
  49612. }
  49613. static int test_EC_i2d(void)
  49614. {
  49615. EXPECT_DECLS;
  49616. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  49617. EC_KEY *key = NULL;
  49618. EC_KEY *copy = NULL;
  49619. int len = 0;
  49620. unsigned char *buf = NULL;
  49621. unsigned char *p = NULL;
  49622. const unsigned char *tmp = NULL;
  49623. const unsigned char octBad[] = {
  49624. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  49625. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  49626. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  49627. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  49628. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  49629. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  49630. };
  49631. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49632. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  49633. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  49634. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49635. DYNAMIC_TYPE_TMP_BUFFER));
  49636. p = buf;
  49637. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  49638. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  49639. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  49640. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  49641. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49642. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  49643. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  49644. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  49645. tmp = buf;
  49646. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  49647. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  49648. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  49649. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  49650. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  49651. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  49652. tmp = buf;
  49653. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  49654. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  49655. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  49656. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  49657. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  49658. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  49659. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  49660. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  49661. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  49662. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  49663. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  49664. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  49665. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  49666. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49667. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  49668. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49669. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  49670. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49671. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  49672. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49673. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  49674. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  49675. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49676. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49677. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  49678. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  49679. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  49680. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49681. buf = NULL;
  49682. buf = NULL;
  49683. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  49684. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49685. DYNAMIC_TYPE_TMP_BUFFER));
  49686. p = buf;
  49687. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  49688. p = NULL;
  49689. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  49690. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49691. p = NULL;
  49692. /* Bad point is also an invalid private key. */
  49693. tmp = octBad;
  49694. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  49695. tmp = buf;
  49696. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  49697. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49698. buf = NULL;
  49699. buf = NULL;
  49700. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  49701. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  49702. DYNAMIC_TYPE_TMP_BUFFER));
  49703. p = buf;
  49704. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49705. p = NULL;
  49706. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  49707. tmp = buf;
  49708. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  49709. tmp = octBad;
  49710. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  49711. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  49712. ExpectIntEQ(EC_KEY_check_key(key), 1);
  49713. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  49714. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  49715. EC_KEY_free(key);
  49716. EC_KEY_free(copy);
  49717. #endif
  49718. return EXPECT_RESULT();
  49719. }
  49720. static int test_wolfSSL_EC_curve(void)
  49721. {
  49722. EXPECT_DECLS;
  49723. #if defined(OPENSSL_EXTRA)
  49724. int nid = NID_secp160k1;
  49725. const char* nid_name = NULL;
  49726. ExpectNull(EC_curve_nid2nist(NID_sha256));
  49727. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  49728. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49729. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  49730. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  49731. #endif
  49732. return EXPECT_RESULT();
  49733. }
  49734. static int test_wolfSSL_EC_KEY_dup(void)
  49735. {
  49736. EXPECT_DECLS;
  49737. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49738. WOLFSSL_EC_KEY* ecKey = NULL;
  49739. WOLFSSL_EC_KEY* dupKey = NULL;
  49740. ecc_key* srcKey = NULL;
  49741. ecc_key* destKey = NULL;
  49742. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49743. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49744. /* Valid cases */
  49745. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49746. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  49747. /* Compare pubkey */
  49748. if (ecKey != NULL) {
  49749. srcKey = (ecc_key*)ecKey->internal;
  49750. }
  49751. if (dupKey != NULL) {
  49752. destKey = (ecc_key*)dupKey->internal;
  49753. }
  49754. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  49755. /* compare EC_GROUP */
  49756. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  49757. /* compare EC_POINT */
  49758. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  49759. dupKey->pub_key, NULL), MP_EQ);
  49760. /* compare BIGNUM */
  49761. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  49762. wolfSSL_EC_KEY_free(dupKey);
  49763. dupKey = NULL;
  49764. /* Invalid cases */
  49765. /* NULL key */
  49766. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  49767. /* NULL ecc_key */
  49768. if (ecKey != NULL) {
  49769. wc_ecc_free((ecc_key*)ecKey->internal);
  49770. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  49771. ecKey->internal = NULL; /* Set ecc_key to NULL */
  49772. }
  49773. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49774. wolfSSL_EC_KEY_free(ecKey);
  49775. ecKey = NULL;
  49776. wolfSSL_EC_KEY_free(dupKey);
  49777. dupKey = NULL;
  49778. /* NULL Group */
  49779. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49780. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49781. if (ecKey != NULL) {
  49782. wolfSSL_EC_GROUP_free(ecKey->group);
  49783. ecKey->group = NULL; /* Set group to NULL */
  49784. }
  49785. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49786. wolfSSL_EC_KEY_free(ecKey);
  49787. ecKey = NULL;
  49788. wolfSSL_EC_KEY_free(dupKey);
  49789. dupKey = NULL;
  49790. /* NULL public key */
  49791. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49792. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49793. if (ecKey != NULL) {
  49794. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  49795. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  49796. }
  49797. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49798. if (ecKey != NULL) {
  49799. wolfSSL_EC_POINT_free(ecKey->pub_key);
  49800. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  49801. }
  49802. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49803. wolfSSL_EC_KEY_free(ecKey);
  49804. ecKey = NULL;
  49805. wolfSSL_EC_KEY_free(dupKey);
  49806. dupKey = NULL;
  49807. /* NULL private key */
  49808. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49809. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49810. if (ecKey != NULL) {
  49811. wolfSSL_BN_free(ecKey->priv_key);
  49812. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  49813. }
  49814. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49815. wolfSSL_EC_KEY_free(ecKey);
  49816. ecKey = NULL;
  49817. wolfSSL_EC_KEY_free(dupKey);
  49818. dupKey = NULL;
  49819. /* Test EC_KEY_up_ref */
  49820. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49821. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  49822. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  49823. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  49824. /* reference count doesn't follow duplicate */
  49825. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  49826. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  49827. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  49828. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  49829. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  49830. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  49831. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  49832. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  49833. #endif
  49834. return EXPECT_RESULT();
  49835. }
  49836. static int test_wolfSSL_EC_KEY_set_group(void)
  49837. {
  49838. EXPECT_DECLS;
  49839. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  49840. defined(OPENSSL_EXTRA)
  49841. EC_KEY *key = NULL;
  49842. EC_GROUP *group = NULL;
  49843. const EC_GROUP *group2 = NULL;
  49844. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  49845. ExpectNotNull(key = EC_KEY_new());
  49846. ExpectNull(EC_KEY_get0_group(NULL));
  49847. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  49848. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  49849. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  49850. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  49851. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  49852. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  49853. EC_GROUP_free(group);
  49854. EC_KEY_free(key);
  49855. #endif
  49856. return EXPECT_RESULT();
  49857. }
  49858. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  49859. {
  49860. EXPECT_DECLS;
  49861. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49862. BIO* bio = NULL;
  49863. EC_KEY* key = NULL;
  49864. /* Error condition: NULL key. */
  49865. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  49866. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  49867. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  49868. /* Conversion form defaults to uncompressed. */
  49869. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49870. #ifdef HAVE_COMP_KEY
  49871. /* Explicitly set to compressed. */
  49872. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  49873. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  49874. #else
  49875. /* Will still work just won't change anything. */
  49876. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  49877. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49878. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  49879. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  49880. #endif
  49881. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  49882. BIO_free(bio);
  49883. EC_KEY_free(key);
  49884. #endif
  49885. return EXPECT_RESULT();
  49886. }
  49887. static int test_wolfSSL_EC_KEY_private_key(void)
  49888. {
  49889. EXPECT_DECLS;
  49890. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49891. WOLFSSL_EC_KEY* key = NULL;
  49892. WOLFSSL_BIGNUM* priv = NULL;
  49893. WOLFSSL_BIGNUM* priv2 = NULL;
  49894. WOLFSSL_BIGNUM* bn;
  49895. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49896. ExpectNotNull(priv = wolfSSL_BN_new());
  49897. ExpectNotNull(priv2 = wolfSSL_BN_new());
  49898. ExpectIntNE(BN_set_word(priv, 2), 0);
  49899. ExpectIntNE(BN_set_word(priv2, 2), 0);
  49900. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  49901. /* No private key set. */
  49902. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  49903. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  49904. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  49905. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  49906. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  49907. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  49908. ExpectPtrNE(bn, priv);
  49909. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  49910. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  49911. ExpectPtrNE(bn, priv2);
  49912. wolfSSL_BN_free(priv2);
  49913. wolfSSL_BN_free(priv);
  49914. wolfSSL_EC_KEY_free(key);
  49915. #endif
  49916. return EXPECT_RESULT();
  49917. }
  49918. static int test_wolfSSL_EC_KEY_public_key(void)
  49919. {
  49920. EXPECT_DECLS;
  49921. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  49922. WOLFSSL_EC_KEY* key = NULL;
  49923. WOLFSSL_EC_POINT* pub = NULL;
  49924. WOLFSSL_EC_POINT* point = NULL;
  49925. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  49926. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  49927. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  49928. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  49929. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  49930. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  49931. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  49932. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  49933. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  49934. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  49935. ExpectPtrEq(point, pub);
  49936. wolfSSL_EC_KEY_free(key);
  49937. #endif
  49938. return EXPECT_RESULT();
  49939. }
  49940. static int test_wolfSSL_EC_KEY_print_fp(void)
  49941. {
  49942. EXPECT_DECLS;
  49943. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  49944. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  49945. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  49946. !defined(NO_STDIO_FILESYSTEM)
  49947. EC_KEY* key = NULL;
  49948. /* Bad file pointer. */
  49949. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  49950. /* NULL key. */
  49951. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  49952. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  49953. /* Negative indent. */
  49954. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  49955. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49956. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  49957. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49958. wolfSSL_EC_KEY_free(key);
  49959. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  49960. NID_X9_62_prime256v1)));
  49961. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  49962. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  49963. wolfSSL_EC_KEY_free(key);
  49964. #endif
  49965. return EXPECT_RESULT();
  49966. }
  49967. static int test_wolfSSL_EC_get_builtin_curves(void)
  49968. {
  49969. EXPECT_DECLS;
  49970. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  49971. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49972. EC_builtin_curve* curves = NULL;
  49973. size_t crv_len = 0;
  49974. size_t i = 0;
  49975. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  49976. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  49977. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  49978. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  49979. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  49980. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  49981. if (curves[i].comment != NULL) {
  49982. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  49983. }
  49984. }
  49985. if (crv_len > 1) {
  49986. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  49987. }
  49988. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49989. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  49990. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  49991. return EXPECT_RESULT();
  49992. }
  49993. static int test_wolfSSL_ECDSA_SIG(void)
  49994. {
  49995. EXPECT_DECLS;
  49996. #ifdef OPENSSL_EXTRA
  49997. WOLFSSL_ECDSA_SIG* sig = NULL;
  49998. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  49999. WOLFSSL_BIGNUM* r = NULL;
  50000. WOLFSSL_BIGNUM* s = NULL;
  50001. const WOLFSSL_BIGNUM* r2 = NULL;
  50002. const WOLFSSL_BIGNUM* s2 = NULL;
  50003. const unsigned char* cp = NULL;
  50004. unsigned char* p = NULL;
  50005. unsigned char outSig[8];
  50006. unsigned char sigData[8] =
  50007. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  50008. unsigned char sigDataBad[8] =
  50009. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  50010. wolfSSL_ECDSA_SIG_free(NULL);
  50011. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  50012. ExpectNotNull(r = wolfSSL_BN_new());
  50013. ExpectNotNull(s = wolfSSL_BN_new());
  50014. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  50015. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  50016. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  50017. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  50018. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  50019. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  50020. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  50021. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  50022. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  50023. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  50024. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  50025. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  50026. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  50027. r2 = NULL;
  50028. s2 = NULL;
  50029. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  50030. ExpectNull(r2);
  50031. ExpectNull(s2);
  50032. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  50033. if (EXPECT_FAIL()) {
  50034. wolfSSL_BN_free(r);
  50035. wolfSSL_BN_free(s);
  50036. }
  50037. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  50038. ExpectPtrEq(r2, r);
  50039. ExpectPtrEq(s2, s);
  50040. r2 = NULL;
  50041. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  50042. ExpectPtrEq(r2, r);
  50043. s2 = NULL;
  50044. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  50045. ExpectPtrEq(s2, s);
  50046. /* r and s are freed when sig is freed. */
  50047. wolfSSL_ECDSA_SIG_free(sig);
  50048. sig = NULL;
  50049. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  50050. cp = sigDataBad;
  50051. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  50052. cp = sigData;
  50053. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  50054. ExpectIntEQ((cp == sigData + 8), 1);
  50055. cp = sigData;
  50056. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  50057. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  50058. ExpectIntEQ((sig == sig2), 1);
  50059. cp = outSig;
  50060. p = outSig;
  50061. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  50062. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  50063. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  50064. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  50065. ExpectIntEQ((p == outSig + 8), 1);
  50066. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  50067. wolfSSL_ECDSA_SIG_free(sig);
  50068. #endif
  50069. return EXPECT_RESULT();
  50070. }
  50071. static int test_ECDSA_size_sign(void)
  50072. {
  50073. EXPECT_DECLS;
  50074. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  50075. EC_KEY* key = NULL;
  50076. ECDSA_SIG* ecdsaSig = NULL;
  50077. int id;
  50078. byte hash[WC_MAX_DIGEST_SIZE];
  50079. byte hash2[WC_MAX_DIGEST_SIZE];
  50080. byte sig[ECC_MAX_SIG_SIZE];
  50081. unsigned int sigSz = sizeof(sig);
  50082. XMEMSET(hash, 123, sizeof(hash));
  50083. XMEMSET(hash2, 234, sizeof(hash2));
  50084. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  50085. ExpectIntEQ(id, ECC_SECP256R1);
  50086. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50087. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  50088. ExpectIntGE(ECDSA_size(NULL), 0);
  50089. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  50090. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  50091. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  50092. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  50093. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  50094. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  50095. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  50096. ExpectIntGE(ECDSA_size(key), sigSz);
  50097. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  50098. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  50099. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  50100. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  50101. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  50102. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  50103. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  50104. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  50105. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  50106. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  50107. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  50108. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  50109. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  50110. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  50111. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  50112. ECDSA_SIG_free(ecdsaSig);
  50113. EC_KEY_free(key);
  50114. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  50115. return EXPECT_RESULT();
  50116. }
  50117. static int test_ECDH_compute_key(void)
  50118. {
  50119. EXPECT_DECLS;
  50120. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  50121. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  50122. EC_KEY* key1 = NULL;
  50123. EC_KEY* key2 = NULL;
  50124. EC_POINT* pub1 = NULL;
  50125. EC_POINT* pub2 = NULL;
  50126. byte secret1[32];
  50127. byte secret2[32];
  50128. int i;
  50129. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50130. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  50131. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  50132. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50133. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  50134. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  50135. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  50136. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  50137. 0);
  50138. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  50139. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  50140. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  50141. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  50142. 0);
  50143. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  50144. 0);
  50145. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  50146. NULL), 0);
  50147. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  50148. sizeof(secret1));
  50149. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  50150. sizeof(secret2));
  50151. for (i = 0; i < (int)sizeof(secret1); i++) {
  50152. ExpectIntEQ(secret1[i], secret2[i]);
  50153. }
  50154. EC_KEY_free(key2);
  50155. EC_KEY_free(key1);
  50156. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  50157. * !WOLF_CRYPTO_CB_ONLY_ECC */
  50158. return EXPECT_RESULT();
  50159. }
  50160. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  50161. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  50162. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  50163. !defined(NO_ASN_TIME)
  50164. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  50165. int expectedDerSz)
  50166. {
  50167. EXPECT_DECLS;
  50168. X509* x509 = NULL;
  50169. BIGNUM* serial_number = NULL;
  50170. X509_NAME* name = NULL;
  50171. time_t epoch_off = 0;
  50172. ASN1_INTEGER* asn1_serial_number;
  50173. long not_before, not_after;
  50174. int derSz;
  50175. ExpectNotNull(x509 = X509_new());
  50176. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  50177. ExpectNotNull(serial_number = BN_new());
  50178. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  50179. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  50180. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  50181. /* version 3 */
  50182. ExpectIntNE(X509_set_version(x509, 2L), 0);
  50183. ExpectNotNull(name = X509_NAME_new());
  50184. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  50185. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  50186. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  50187. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  50188. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  50189. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  50190. not_before = (long)wc_Time(NULL);
  50191. not_after = not_before + (365 * 24 * 60 * 60);
  50192. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  50193. &epoch_off));
  50194. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  50195. &epoch_off));
  50196. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  50197. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  50198. ExpectIntGE(derSz, expectedDerSz);
  50199. BN_free(serial_number);
  50200. X509_NAME_free(name);
  50201. X509_free(x509);
  50202. return EXPECT_RESULT();
  50203. }
  50204. #endif
  50205. static int test_openssl_generate_key_and_cert(void)
  50206. {
  50207. EXPECT_DECLS;
  50208. #if defined(OPENSSL_EXTRA)
  50209. EVP_PKEY* pkey = NULL;
  50210. #ifdef HAVE_ECC
  50211. EC_KEY* ec_key = NULL;
  50212. #endif
  50213. #if !defined(NO_RSA)
  50214. int expectedDerSz;
  50215. int key_length = 2048;
  50216. BIGNUM* exponent = NULL;
  50217. RSA* rsa = NULL;
  50218. ExpectNotNull(pkey = EVP_PKEY_new());
  50219. ExpectNotNull(exponent = BN_new());
  50220. ExpectNotNull(rsa = RSA_new());
  50221. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  50222. #ifndef WOLFSSL_KEY_GEN
  50223. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  50224. #if defined(USE_CERT_BUFFERS_1024)
  50225. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  50226. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  50227. key_length = 1024;
  50228. #elif defined(USE_CERT_BUFFERS_2048)
  50229. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  50230. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  50231. #else
  50232. RSA_free(rsa);
  50233. rsa = NULL;
  50234. #endif
  50235. #else
  50236. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  50237. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  50238. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  50239. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  50240. #endif
  50241. if (rsa) {
  50242. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  50243. if (EXPECT_FAIL()) {
  50244. RSA_free(rsa);
  50245. }
  50246. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  50247. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  50248. expectedDerSz = 743;
  50249. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  50250. expectedDerSz), TEST_SUCCESS);
  50251. #endif
  50252. }
  50253. (void)expectedDerSz;
  50254. EVP_PKEY_free(pkey);
  50255. pkey = NULL;
  50256. BN_free(exponent);
  50257. #endif /* !NO_RSA */
  50258. #ifdef HAVE_ECC
  50259. ExpectNotNull(pkey = EVP_PKEY_new());
  50260. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  50261. #ifndef NO_WOLFSSL_STUB
  50262. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  50263. #endif
  50264. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  50265. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  50266. if (EXPECT_FAIL()) {
  50267. EC_KEY_free(ec_key);
  50268. }
  50269. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  50270. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  50271. expectedDerSz = 344;
  50272. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  50273. TEST_SUCCESS);
  50274. #endif
  50275. EVP_PKEY_free(pkey);
  50276. #endif /* HAVE_ECC */
  50277. (void)pkey;
  50278. #endif /* OPENSSL_EXTRA */
  50279. return EXPECT_RESULT();
  50280. }
  50281. static int test_stubs_are_stubs(void)
  50282. {
  50283. EXPECT_DECLS;
  50284. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  50285. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  50286. WOLFSSL_CTX* ctx = NULL;
  50287. WOLFSSL_CTX* ctxN = NULL;
  50288. #ifndef NO_WOLFSSL_CLIENT
  50289. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50290. #elif !defined(NO_WOLFSSL_SERVER)
  50291. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50292. #endif
  50293. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  50294. ExpectIntEQ((int) x(z), 0)
  50295. /* test logic, all stubs return same result regardless of ctx being NULL
  50296. * as there are no sanity checks, it's just a stub! If at some
  50297. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  50298. * if invalid inputs are supplied. Test calling both
  50299. * with and without valid inputs, if a stub functionality remains unchanged.
  50300. */
  50301. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  50302. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  50303. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  50304. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  50305. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  50306. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  50307. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  50308. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  50309. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  50310. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  50311. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  50312. wolfSSL_CTX_free(ctx);
  50313. ctx = NULL;
  50314. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  50315. * !NO_WOLFSSL_SERVER) */
  50316. return EXPECT_RESULT();
  50317. }
  50318. static int test_CONF_modules_xxx(void)
  50319. {
  50320. int res = TEST_SKIPPED;
  50321. #if defined(OPENSSL_EXTRA)
  50322. CONF_modules_free();
  50323. CONF_modules_unload(0);
  50324. CONF_modules_unload(1);
  50325. CONF_modules_unload(-1);
  50326. res = TEST_SUCCESS;
  50327. #endif /* OPENSSL_EXTRA */
  50328. return res;
  50329. }
  50330. static int test_CRYPTO_set_dynlock_xxx(void)
  50331. {
  50332. int res = TEST_SKIPPED;
  50333. #if defined(OPENSSL_EXTRA)
  50334. CRYPTO_set_dynlock_create_callback(
  50335. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  50336. CRYPTO_set_dynlock_create_callback(
  50337. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  50338. CRYPTO_set_dynlock_destroy_callback(
  50339. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  50340. CRYPTO_set_dynlock_destroy_callback(
  50341. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  50342. CRYPTO_set_dynlock_lock_callback(
  50343. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  50344. CRYPTO_set_dynlock_lock_callback(
  50345. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  50346. res = TEST_SUCCESS;
  50347. #endif /* OPENSSL_EXTRA */
  50348. return res;
  50349. }
  50350. static int test_CRYPTO_THREADID_xxx(void)
  50351. {
  50352. EXPECT_DECLS;
  50353. #if defined(OPENSSL_EXTRA)
  50354. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  50355. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  50356. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  50357. #endif /* OPENSSL_EXTRA */
  50358. return EXPECT_RESULT();
  50359. }
  50360. static int test_ENGINE_cleanup(void)
  50361. {
  50362. int res = TEST_SKIPPED;
  50363. #if defined(OPENSSL_EXTRA)
  50364. ENGINE_cleanup();
  50365. res = TEST_SUCCESS;
  50366. #endif /* OPENSSL_EXTRA */
  50367. return res;
  50368. }
  50369. static int test_wolfSSL_CTX_LoadCRL(void)
  50370. {
  50371. EXPECT_DECLS;
  50372. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  50373. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  50374. WOLFSSL_CTX* ctx = NULL;
  50375. WOLFSSL* ssl = NULL;
  50376. const char* badPath = "dummypath";
  50377. const char* validPath = "./certs/crl";
  50378. const char* validFilePath = "./certs/crl/cliCrl.pem";
  50379. const char* issuerCert = "./certs/client-cert.pem";
  50380. int derType = WOLFSSL_FILETYPE_ASN1;
  50381. int pemType = WOLFSSL_FILETYPE_PEM;
  50382. #ifdef HAVE_CRL_MONITOR
  50383. int monitor = WOLFSSL_CRL_MONITOR;
  50384. #else
  50385. int monitor = 0;
  50386. #endif
  50387. WOLFSSL_CERT_MANAGER* cm = NULL;
  50388. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50389. BAD_FUNC_ARG)
  50390. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50391. NOT_COMPILED_IN)
  50392. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  50393. WOLFSSL_SUCCESS)
  50394. #ifndef NO_WOLFSSL_CLIENT
  50395. #define NEW_CTX(ctx) AssertNotNull( \
  50396. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  50397. #elif !defined(NO_WOLFSSL_SERVER)
  50398. #define NEW_CTX(ctx) AssertNotNull( \
  50399. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  50400. #else
  50401. #define NEW_CTX(ctx) return
  50402. #endif
  50403. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50404. NEW_CTX(ctx);
  50405. #ifndef HAVE_CRL_MONITOR
  50406. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  50407. wolfSSL_CTX_free(ctx);
  50408. NEW_CTX(ctx);
  50409. #endif
  50410. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  50411. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  50412. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  50413. wolfSSL_CTX_free(ctx);
  50414. ctx = NULL;
  50415. NEW_CTX(ctx);
  50416. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50417. WOLFSSL_SUCCESS);
  50418. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  50419. wolfSSL_CTX_free(ctx);
  50420. ctx = NULL;
  50421. NEW_CTX(ctx);
  50422. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  50423. WOLFSSL_SUCCESS);
  50424. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50425. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  50426. wolfSSL_free(ssl);
  50427. ssl = NULL;
  50428. wolfSSL_CTX_free(ctx);
  50429. ctx = NULL;
  50430. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  50431. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  50432. WOLFSSL_SUCCESS);
  50433. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  50434. WOLFSSL_SUCCESS);
  50435. wolfSSL_CertManagerFree(cm);
  50436. #endif
  50437. return EXPECT_RESULT();
  50438. }
  50439. static int test_SetTmpEC_DHE_Sz(void)
  50440. {
  50441. EXPECT_DECLS;
  50442. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  50443. WOLFSSL_CTX *ctx = NULL;
  50444. WOLFSSL *ssl = NULL;
  50445. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50446. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  50447. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50448. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  50449. wolfSSL_free(ssl);
  50450. wolfSSL_CTX_free(ctx);
  50451. #endif
  50452. return EXPECT_RESULT();
  50453. }
  50454. static int test_wolfSSL_CTX_get0_privatekey(void)
  50455. {
  50456. EXPECT_DECLS;
  50457. #ifdef OPENSSL_ALL
  50458. WOLFSSL_CTX* ctx = NULL;
  50459. (void)ctx;
  50460. #ifndef NO_RSA
  50461. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50462. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50463. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50464. WOLFSSL_FILETYPE_PEM));
  50465. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50466. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50467. WOLFSSL_FILETYPE_PEM));
  50468. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  50469. wolfSSL_CTX_free(ctx);
  50470. ctx = NULL;
  50471. #endif
  50472. #ifdef HAVE_ECC
  50473. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  50474. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50475. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  50476. WOLFSSL_FILETYPE_PEM));
  50477. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  50478. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  50479. WOLFSSL_FILETYPE_PEM));
  50480. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  50481. wolfSSL_CTX_free(ctx);
  50482. #endif
  50483. #endif
  50484. return EXPECT_RESULT();
  50485. }
  50486. static int test_wolfSSL_dtls_set_mtu(void)
  50487. {
  50488. EXPECT_DECLS;
  50489. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  50490. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
  50491. WOLFSSL_CTX* ctx = NULL;
  50492. WOLFSSL* ssl = NULL;
  50493. const char* testCertFile;
  50494. const char* testKeyFile;
  50495. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  50496. #ifndef NO_RSA
  50497. testCertFile = svrCertFile;
  50498. testKeyFile = svrKeyFile;
  50499. #elif defined(HAVE_ECC)
  50500. testCertFile = eccCertFile;
  50501. testKeyFile = eccKeyFile;
  50502. #endif
  50503. if (testCertFile != NULL && testKeyFile != NULL) {
  50504. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  50505. WOLFSSL_FILETYPE_PEM));
  50506. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50507. WOLFSSL_FILETYPE_PEM));
  50508. }
  50509. ExpectNotNull(ssl = wolfSSL_new(ctx));
  50510. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50511. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  50512. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  50513. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  50514. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  50515. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  50516. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  50517. wolfSSL_free(ssl);
  50518. wolfSSL_CTX_free(ctx);
  50519. #endif
  50520. return EXPECT_RESULT();
  50521. }
  50522. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50523. defined(WOLFSSL_DTLS)
  50524. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  50525. enum HandShakeType hsType, word16 length)
  50526. {
  50527. size_t idx = 0;
  50528. byte* l;
  50529. /* record layer */
  50530. /* handshake type */
  50531. out[idx++] = handshake;
  50532. /* protocol version */
  50533. out[idx++] = 0xfe;
  50534. out[idx++] = 0xfd; /* DTLS 1.2 */
  50535. /* epoch 0 */
  50536. XMEMSET(out + idx, 0, 2);
  50537. idx += 2;
  50538. /* sequence number */
  50539. XMEMSET(out + idx, 0, 6);
  50540. c32toa(seq, out + idx + 2);
  50541. idx += 6;
  50542. /* length in BE */
  50543. if (length)
  50544. c16toa(length, out + idx);
  50545. else
  50546. c16toa(outSz - idx - 2, out + idx);
  50547. idx += 2;
  50548. /* handshake layer */
  50549. /* handshake type */
  50550. out[idx++] = (byte)hsType;
  50551. /* length */
  50552. l = out + idx;
  50553. idx += 3;
  50554. /* message seq */
  50555. c16toa(0, out + idx);
  50556. idx += 2;
  50557. /* frag offset */
  50558. c32to24(0, out + idx);
  50559. idx += 3;
  50560. /* frag length */
  50561. c32to24((word32)outSz - (word32)idx - 3, l);
  50562. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  50563. idx += 3;
  50564. XMEMSET(out + idx, 0, outSz - idx);
  50565. }
  50566. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  50567. {
  50568. byte msg[] = "This is a msg for the client";
  50569. byte reply[40];
  50570. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50571. reply[sizeof(reply) - 1] = '\0';
  50572. fprintf(stderr, "Client message: %s\n", reply);
  50573. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50574. }
  50575. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  50576. {
  50577. byte ch[50];
  50578. int fd = wolfSSL_get_fd(ssl);
  50579. byte msg[] = "This is a msg for the server";
  50580. byte reply[40];
  50581. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  50582. /* Server should ignore this datagram */
  50583. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50584. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  50585. /* Server should ignore this datagram */
  50586. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  50587. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  50588. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  50589. reply[sizeof(reply) - 1] = '\0';
  50590. fprintf(stderr, "Server response: %s\n", reply);
  50591. }
  50592. static int test_wolfSSL_dtls_plaintext(void)
  50593. {
  50594. callback_functions func_cb_client;
  50595. callback_functions func_cb_server;
  50596. size_t i;
  50597. struct test_params {
  50598. method_provider client_meth;
  50599. method_provider server_meth;
  50600. ssl_callback on_result_server;
  50601. ssl_callback on_result_client;
  50602. } params[] = {
  50603. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50604. test_wolfSSL_dtls_plaintext_server,
  50605. test_wolfSSL_dtls_plaintext_client},
  50606. };
  50607. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50608. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50609. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50610. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50611. func_cb_server.method = params[i].server_meth;
  50612. func_cb_client.method = params[i].client_meth;
  50613. func_cb_client.on_result = params[i].on_result_client;
  50614. func_cb_server.on_result = params[i].on_result_server;
  50615. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50616. if (!func_cb_client.return_code)
  50617. return TEST_FAIL;
  50618. if (!func_cb_server.return_code)
  50619. return TEST_FAIL;
  50620. }
  50621. return TEST_RES_CHECK(1);
  50622. }
  50623. #else
  50624. static int test_wolfSSL_dtls_plaintext(void) {
  50625. return TEST_SKIPPED;
  50626. }
  50627. #endif
  50628. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  50629. defined(WOLFSSL_DTLS)
  50630. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  50631. {
  50632. byte b[1100]; /* buffer for the messages to send */
  50633. size_t idx = 0;
  50634. size_t seq_offset = 0;
  50635. size_t msg_offset = 0;
  50636. int i;
  50637. int fd = wolfSSL_get_fd(ssl);
  50638. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50639. word32 seq_number = 100; /* start high so server definitely reads this */
  50640. word16 msg_number = 50; /* start high so server has to buffer this */
  50641. AssertIntEQ(ret, 1);
  50642. /* Now let's start spamming the peer with fragments it needs to store */
  50643. XMEMSET(b, -1, sizeof(b));
  50644. /* record layer */
  50645. /* handshake type */
  50646. b[idx++] = 22;
  50647. /* protocol version */
  50648. b[idx++] = 0xfe;
  50649. b[idx++] = 0xfd; /* DTLS 1.2 */
  50650. /* epoch 0 */
  50651. XMEMSET(b + idx, 0, 2);
  50652. idx += 2;
  50653. /* sequence number */
  50654. XMEMSET(b + idx, 0, 6);
  50655. seq_offset = idx + 2; /* increment only the low 32 bits */
  50656. idx += 6;
  50657. /* static length in BE */
  50658. c16toa(42, b + idx);
  50659. idx += 2;
  50660. /* handshake layer */
  50661. /* cert type */
  50662. b[idx++] = 11;
  50663. /* length */
  50664. c32to24(1000, b + idx);
  50665. idx += 3;
  50666. /* message seq */
  50667. c16toa(0, b + idx);
  50668. msg_offset = idx;
  50669. idx += 2;
  50670. /* frag offset */
  50671. c32to24(500, b + idx);
  50672. idx += 3;
  50673. /* frag length */
  50674. c32to24(30, b + idx);
  50675. idx += 3;
  50676. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  50677. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  50678. seq_number++, msg_number++, i++) {
  50679. struct timespec delay;
  50680. XMEMSET(&delay, 0, sizeof(delay));
  50681. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50682. c32toa(seq_number, b + seq_offset);
  50683. c16toa(msg_number, b + msg_offset);
  50684. ret = (int)send(fd, b, 55, 0);
  50685. nanosleep(&delay, NULL);
  50686. }
  50687. }
  50688. #ifdef WOLFSSL_DTLS13
  50689. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  50690. {
  50691. const word16 sendCountMax = 100;
  50692. byte b[150]; /* buffer for the messages to send */
  50693. size_t idx = 0;
  50694. size_t msg_offset = 0;
  50695. int fd = wolfSSL_get_fd(ssl);
  50696. word16 msg_number = 10; /* start high so server has to buffer this */
  50697. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  50698. AssertIntEQ(ret, 1);
  50699. /* Now let's start spamming the peer with fragments it needs to store */
  50700. XMEMSET(b, -1, sizeof(b));
  50701. /* handshake type */
  50702. b[idx++] = 11;
  50703. /* length */
  50704. c32to24(10000, b + idx);
  50705. idx += 3;
  50706. /* message_seq */
  50707. msg_offset = idx;
  50708. idx += 2;
  50709. /* fragment_offset */
  50710. c32to24(5000, b + idx);
  50711. idx += 3;
  50712. /* fragment_length */
  50713. c32to24(100, b + idx);
  50714. idx += 3;
  50715. /* fragment contents */
  50716. idx += 100;
  50717. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  50718. byte sendBuf[150];
  50719. int sendSz = sizeof(sendBuf);
  50720. struct timespec delay;
  50721. XMEMSET(&delay, 0, sizeof(delay));
  50722. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  50723. c16toa(msg_number, b + msg_offset);
  50724. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  50725. (int)idx, handshake, 0, 0, 0);
  50726. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  50727. nanosleep(&delay, NULL);
  50728. }
  50729. }
  50730. #endif
  50731. static int test_wolfSSL_dtls_fragments(void)
  50732. {
  50733. EXPECT_DECLS;
  50734. callback_functions func_cb_client;
  50735. callback_functions func_cb_server;
  50736. size_t i;
  50737. struct test_params {
  50738. method_provider client_meth;
  50739. method_provider server_meth;
  50740. ssl_callback spammer;
  50741. } params[] = {
  50742. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  50743. test_wolfSSL_dtls12_fragments_spammer},
  50744. #ifdef WOLFSSL_DTLS13
  50745. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  50746. test_wolfSSL_dtls13_fragments_spammer},
  50747. #endif
  50748. };
  50749. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  50750. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  50751. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  50752. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  50753. func_cb_server.method = params[i].server_meth;
  50754. func_cb_client.method = params[i].client_meth;
  50755. func_cb_client.ssl_ready = params[i].spammer;
  50756. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  50757. ExpectFalse(func_cb_client.return_code);
  50758. ExpectFalse(func_cb_server.return_code);
  50759. /* The socket should be closed by the server resulting in a
  50760. * socket error, fatal error or reading a close notify alert */
  50761. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  50762. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  50763. func_cb_client.last_err != FATAL_ERROR) {
  50764. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  50765. }
  50766. /* Check the server returned an error indicating the msg buffer
  50767. * was full */
  50768. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  50769. if (EXPECT_FAIL())
  50770. break;
  50771. }
  50772. return EXPECT_RESULT();
  50773. }
  50774. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  50775. {
  50776. int fd, ret;
  50777. byte alert_msg[] = {
  50778. 0x15, /* alert type */
  50779. 0xfe, 0xfd, /* version */
  50780. 0x00, 0x00, /* epoch */
  50781. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  50782. 0x00, 0x02, /* length */
  50783. 0x02, /* level: fatal */
  50784. 0x46 /* protocol version */
  50785. };
  50786. fd = wolfSSL_get_fd(ssl);
  50787. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  50788. AssertIntGT(ret, 0);
  50789. }
  50790. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  50791. {
  50792. callback_functions client_cbs, server_cbs;
  50793. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50794. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50795. client_cbs.doUdp = server_cbs.doUdp = 1;
  50796. if (version12) {
  50797. client_cbs.method = wolfDTLSv1_2_client_method;
  50798. server_cbs.method = wolfDTLSv1_2_server_method;
  50799. }
  50800. else {
  50801. #ifdef WOLFSSL_DTLS13
  50802. client_cbs.method = wolfDTLSv1_3_client_method;
  50803. server_cbs.method = wolfDTLSv1_3_server_method;
  50804. #else
  50805. return TEST_SKIPPED;
  50806. #endif /* WOLFSSL_DTLS13 */
  50807. }
  50808. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  50809. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50810. if (!client_cbs.return_code)
  50811. return TEST_FAIL;
  50812. if (!server_cbs.return_code)
  50813. return TEST_FAIL;
  50814. return TEST_SUCCESS;
  50815. }
  50816. static int test_wolfSSL_ignore_alert_before_cookie(void)
  50817. {
  50818. int ret;
  50819. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  50820. if (ret != 0)
  50821. return ret;
  50822. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  50823. if (ret != 0)
  50824. return ret;
  50825. return 0;
  50826. }
  50827. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  50828. {
  50829. int ret;
  50830. int fd;
  50831. byte bad_msg[] = {
  50832. 0x17, /* app data */
  50833. 0xaa, 0xfd, /* bad version */
  50834. 0x00, 0x01, /* epoch 1 */
  50835. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  50836. 0x00, 0x26, /* length: 38 bytes */
  50837. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  50838. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  50839. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  50840. 0x03, 0x18, 0x72
  50841. };
  50842. fd = wolfSSL_get_fd(ssl);
  50843. AssertIntGE(fd, 0);
  50844. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  50845. AssertIntEQ(ret, sizeof(bad_msg));
  50846. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  50847. AssertIntEQ(ret, sizeof("badrecordtest"));
  50848. }
  50849. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  50850. {
  50851. byte buf[100];
  50852. int ret;
  50853. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  50854. AssertIntGT(ret, 0);
  50855. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  50856. }
  50857. static int _test_wolfSSL_dtls_bad_record(
  50858. method_provider client_method, method_provider server_method)
  50859. {
  50860. callback_functions client_cbs, server_cbs;
  50861. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  50862. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  50863. client_cbs.doUdp = server_cbs.doUdp = 1;
  50864. client_cbs.method = client_method;
  50865. server_cbs.method = server_method;
  50866. client_cbs.on_result = test_wolfSSL_send_bad_record;
  50867. server_cbs.on_result = test_wolfSSL_read_string;
  50868. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  50869. if (!client_cbs.return_code)
  50870. return TEST_FAIL;
  50871. if (!server_cbs.return_code)
  50872. return TEST_FAIL;
  50873. return TEST_SUCCESS;
  50874. }
  50875. static int test_wolfSSL_dtls_bad_record(void)
  50876. {
  50877. int ret;
  50878. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  50879. wolfDTLSv1_2_server_method);
  50880. #ifdef WOLFSSL_DTLS13
  50881. if (ret != TEST_SUCCESS)
  50882. return ret;
  50883. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  50884. wolfDTLSv1_3_server_method);
  50885. #else
  50886. return ret;
  50887. #endif /* WOLFSSL_DTLS13 */
  50888. }
  50889. #else
  50890. static int test_wolfSSL_dtls_fragments(void) {
  50891. return TEST_SKIPPED;
  50892. }
  50893. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  50894. return TEST_SKIPPED;
  50895. }
  50896. static int test_wolfSSL_dtls_bad_record(void) {
  50897. return TEST_SKIPPED;
  50898. }
  50899. #endif
  50900. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  50901. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  50902. defined(HAVE_IO_TESTS_DEPENDENCIES)
  50903. static byte test_AEAD_fail_decryption = 0;
  50904. static byte test_AEAD_seq_num = 0;
  50905. static byte test_AEAD_done = 0;
  50906. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  50907. {
  50908. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  50909. if (ret > 0) {
  50910. if (test_AEAD_fail_decryption) {
  50911. /* Modify the packet to trigger a decryption failure */
  50912. buf[ret/2] ^= 0xFF;
  50913. if (test_AEAD_fail_decryption == 1)
  50914. test_AEAD_fail_decryption = 0;
  50915. }
  50916. }
  50917. (void)ctx;
  50918. return ret;
  50919. }
  50920. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  50921. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  50922. {
  50923. if (sendLimit)
  50924. w64Zero(sendLimit);
  50925. switch (ssl->specs.bulk_cipher_algorithm) {
  50926. case wolfssl_aes_gcm:
  50927. if (sendLimit)
  50928. *sendLimit = AEAD_AES_LIMIT;
  50929. FALL_THROUGH;
  50930. case wolfssl_chacha:
  50931. if (hardLimit)
  50932. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  50933. if (keyUpdateLimit)
  50934. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  50935. break;
  50936. case wolfssl_aes_ccm:
  50937. if (sendLimit)
  50938. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  50939. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  50940. if (hardLimit)
  50941. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  50942. if (keyUpdateLimit)
  50943. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  50944. }
  50945. else {
  50946. if (hardLimit)
  50947. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  50948. if (keyUpdateLimit)
  50949. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  50950. }
  50951. break;
  50952. default:
  50953. fprintf(stderr, "Unrecognized bulk cipher");
  50954. AssertFalse(1);
  50955. break;
  50956. }
  50957. }
  50958. static void test_AEAD_limit_client(WOLFSSL* ssl)
  50959. {
  50960. int ret;
  50961. int i;
  50962. int didReKey = 0;
  50963. char msgBuf[20];
  50964. w64wrapper hardLimit;
  50965. w64wrapper keyUpdateLimit;
  50966. w64wrapper counter;
  50967. w64wrapper sendLimit;
  50968. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  50969. w64Zero(&counter);
  50970. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  50971. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  50972. for (i = 0; i < 10; i++) {
  50973. /* Test some failed decryptions */
  50974. test_AEAD_fail_decryption = 1;
  50975. w64Increment(&counter);
  50976. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50977. /* Should succeed since decryption failures are dropped */
  50978. AssertIntGT(ret, 0);
  50979. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  50980. }
  50981. test_AEAD_fail_decryption = 1;
  50982. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  50983. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  50984. /* 100 read calls should be enough to complete the key update */
  50985. w64Zero(&counter);
  50986. for (i = 0; i < 100; i++) {
  50987. /* Key update should be sent and negotiated */
  50988. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  50989. AssertIntGT(ret, 0);
  50990. /* Epoch after one key update is 4 */
  50991. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  50992. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  50993. didReKey = 1;
  50994. break;
  50995. }
  50996. }
  50997. AssertTrue(didReKey);
  50998. if (!w64IsZero(sendLimit)) {
  50999. /* Test the sending limit for AEAD ciphers */
  51000. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  51001. test_AEAD_seq_num = 1;
  51002. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  51003. AssertIntGT(ret, 0);
  51004. didReKey = 0;
  51005. w64Zero(&counter);
  51006. /* 100 read calls should be enough to complete the key update */
  51007. for (i = 0; i < 100; i++) {
  51008. /* Key update should be sent and negotiated */
  51009. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51010. AssertIntGT(ret, 0);
  51011. /* Epoch after another key update is 5 */
  51012. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  51013. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  51014. didReKey = 1;
  51015. break;
  51016. }
  51017. }
  51018. AssertTrue(didReKey);
  51019. }
  51020. test_AEAD_fail_decryption = 2;
  51021. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  51022. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  51023. /* Connection should fail with a DECRYPT_ERROR */
  51024. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51025. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  51026. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  51027. test_AEAD_done = 1;
  51028. }
  51029. int counter = 0;
  51030. static void test_AEAD_limit_server(WOLFSSL* ssl)
  51031. {
  51032. char msgBuf[] = "Sending data";
  51033. int ret = WOLFSSL_SUCCESS;
  51034. w64wrapper sendLimit;
  51035. SOCKET_T fd = wolfSSL_get_fd(ssl);
  51036. struct timespec delay;
  51037. XMEMSET(&delay, 0, sizeof(delay));
  51038. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  51039. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  51040. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  51041. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  51042. while (!test_AEAD_done && ret > 0) {
  51043. counter++;
  51044. if (test_AEAD_seq_num) {
  51045. /* We need to update the seq number so that we can understand the
  51046. * peer. Otherwise we will incorrectly interpret the seq number. */
  51047. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  51048. AssertNotNull(e);
  51049. e->nextPeerSeqNumber = sendLimit;
  51050. test_AEAD_seq_num = 0;
  51051. }
  51052. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  51053. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  51054. nanosleep(&delay, NULL);
  51055. }
  51056. }
  51057. static int test_wolfSSL_dtls_AEAD_limit(void)
  51058. {
  51059. callback_functions func_cb_client;
  51060. callback_functions func_cb_server;
  51061. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  51062. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  51063. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  51064. func_cb_server.method = wolfDTLSv1_3_server_method;
  51065. func_cb_client.method = wolfDTLSv1_3_client_method;
  51066. func_cb_server.on_result = test_AEAD_limit_server;
  51067. func_cb_client.on_result = test_AEAD_limit_client;
  51068. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  51069. if (!func_cb_client.return_code)
  51070. return TEST_FAIL;
  51071. if (!func_cb_server.return_code)
  51072. return TEST_FAIL;
  51073. return TEST_SUCCESS;
  51074. }
  51075. #else
  51076. static int test_wolfSSL_dtls_AEAD_limit(void)
  51077. {
  51078. return TEST_SKIPPED;
  51079. }
  51080. #endif
  51081. #if defined(WOLFSSL_DTLS) && \
  51082. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  51083. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  51084. {
  51085. int fd, ret;
  51086. byte ch_msg[] = {
  51087. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51088. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51089. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  51090. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  51091. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  51092. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  51093. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  51094. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  51095. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  51096. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  51097. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  51098. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  51099. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  51100. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  51101. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  51102. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  51103. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  51104. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  51105. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  51106. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  51107. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  51108. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  51109. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  51110. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  51111. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  51112. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  51113. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  51114. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  51115. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  51116. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  51117. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  51118. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  51119. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  51120. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  51121. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  51122. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  51123. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  51124. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  51125. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  51126. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  51127. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  51128. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  51129. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  51130. 0x31, 0x68, 0x4c
  51131. };
  51132. fd = wolfSSL_get_fd(ssl);
  51133. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  51134. AssertIntGT(ret, 0);
  51135. /* consume the HRR otherwise handshake will fail */
  51136. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  51137. AssertIntGT(ret, 0);
  51138. }
  51139. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51140. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  51141. {
  51142. int fd, ret;
  51143. byte ch_msh_invalid_cookie[] = {
  51144. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  51145. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  51146. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  51147. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  51148. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  51149. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  51150. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  51151. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  51152. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  51153. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  51154. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  51155. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  51156. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  51157. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  51158. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51159. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51160. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51161. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51162. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  51163. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  51164. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  51165. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  51166. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  51167. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  51168. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  51169. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  51170. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  51171. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  51172. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  51173. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  51174. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  51175. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  51176. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  51177. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  51178. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  51179. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  51180. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  51181. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  51182. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  51183. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  51184. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  51185. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  51186. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  51187. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  51188. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  51189. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  51190. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  51191. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  51192. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  51193. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  51194. 0x23, 0x00, 0x00
  51195. };
  51196. byte alert_reply[50];
  51197. byte expected_alert_reply[] = {
  51198. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  51199. 0x02, 0x02, 0x2f
  51200. };
  51201. fd = wolfSSL_get_fd(ssl);
  51202. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  51203. AssertIntGT(ret, 0);
  51204. /* should reply with an illegal_parameter reply */
  51205. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  51206. AssertIntEQ(ret, sizeof(expected_alert_reply));
  51207. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  51208. }
  51209. #endif
  51210. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  51211. {
  51212. #ifndef NO_MD5
  51213. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  51214. #elif !defined(NO_SHA)
  51215. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  51216. #elif !defined(NO_SHA256)
  51217. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  51218. #else
  51219. #error "We need a digest to hash the WOLFSSL object"
  51220. #endif
  51221. byte hashBuf[WC_MAX_DIGEST_SIZE];
  51222. wc_HashAlg hash;
  51223. const TLSX* exts = ssl->extensions;
  51224. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  51225. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  51226. * InitHandshakeHashes */
  51227. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  51228. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  51229. /* Following fields are not important to compare */
  51230. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  51231. sslCopy.buffers.inputBuffer.buffer = NULL;
  51232. sslCopy.buffers.inputBuffer.bufferSize = 0;
  51233. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  51234. sslCopy.buffers.inputBuffer.offset = 0;
  51235. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  51236. sslCopy.buffers.outputBuffer.buffer = NULL;
  51237. sslCopy.buffers.outputBuffer.bufferSize = 0;
  51238. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  51239. sslCopy.buffers.outputBuffer.offset = 0;
  51240. sslCopy.error = 0;
  51241. sslCopy.curSize = 0;
  51242. sslCopy.keys.curSeq_lo = 0;
  51243. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  51244. #ifdef WOLFSSL_DTLS13
  51245. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  51246. sslCopy.dtls13FastTimeout = 0;
  51247. #endif
  51248. sslCopy.keys.dtls_peer_handshake_number = 0;
  51249. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  51250. sslCopy.hsHashes = NULL;
  51251. #ifdef WOLFSSL_ASYNC_IO
  51252. #ifdef WOLFSSL_ASYNC_CRYPT
  51253. sslCopy.asyncDev = NULL;
  51254. #endif
  51255. sslCopy.async = NULL;
  51256. #endif
  51257. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  51258. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  51259. /* hash extension list */
  51260. while (exts != NULL) {
  51261. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  51262. exts = exts->next;
  51263. }
  51264. /* Hash suites */
  51265. if (sslCopy.suites != NULL) {
  51266. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  51267. sizeof(struct Suites)), 0);
  51268. }
  51269. /* Hash hsHashes */
  51270. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  51271. sizeof(*hsHashes)), 0);
  51272. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  51273. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  51274. return MakeWordFromHash(hashBuf);
  51275. }
  51276. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  51277. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  51278. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  51279. char *buf, int sz, void *ctx)
  51280. {
  51281. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  51282. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  51283. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  51284. }
  51285. else {
  51286. return WOLFSSL_CBIO_ERR_WANT_READ;
  51287. }
  51288. }
  51289. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  51290. {
  51291. /* Compare the ssl object before and after one ClientHello msg */
  51292. SOCKET_T fd = wolfSSL_get_fd(ssl);
  51293. int res;
  51294. int err;
  51295. word32 initHash;
  51296. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  51297. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  51298. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  51299. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  51300. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  51301. (void)initHash;
  51302. res = tcp_select(fd, 5);
  51303. /* We are expecting a msg. A timeout indicates failure. */
  51304. AssertIntEQ(res, TEST_RECV_READY);
  51305. res = wolfSSL_accept(ssl);
  51306. err = wolfSSL_get_error(ssl, res);
  51307. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  51308. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  51309. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  51310. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  51311. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  51312. }
  51313. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51314. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  51315. {
  51316. int ret;
  51317. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  51318. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  51319. test_wolfSSL_dtls_compare_stateless(ssl);
  51320. }
  51321. #endif
  51322. static int test_wolfSSL_dtls_stateless(void)
  51323. {
  51324. callback_functions client_cbs, server_cbs;
  51325. size_t i;
  51326. struct {
  51327. method_provider client_meth;
  51328. method_provider server_meth;
  51329. ssl_callback client_ssl_ready;
  51330. ssl_callback server_ssl_ready;
  51331. } test_params[] = {
  51332. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  51333. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  51334. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  51335. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  51336. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  51337. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  51338. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  51339. #endif
  51340. };
  51341. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  51342. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  51343. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  51344. client_cbs.doUdp = server_cbs.doUdp = 1;
  51345. client_cbs.method = test_params[i].client_meth;
  51346. server_cbs.method = test_params[i].server_meth;
  51347. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  51348. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  51349. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  51350. if (!client_cbs.return_code)
  51351. return TEST_FAIL;
  51352. if (!server_cbs.return_code)
  51353. return TEST_FAIL;
  51354. }
  51355. return TEST_SUCCESS;
  51356. }
  51357. #else
  51358. static int test_wolfSSL_dtls_stateless(void)
  51359. {
  51360. return TEST_SKIPPED;
  51361. }
  51362. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  51363. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  51364. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  51365. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  51366. !defined(WOLFSSL_NO_CLIENT_AUTH))
  51367. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  51368. {
  51369. int ret;
  51370. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  51371. fprintf(stderr, "loading cert %s failed\n", certA);
  51372. fprintf(stderr, "Error: (%d): %s\n", ret,
  51373. wolfSSL_ERR_reason_error_string(ret));
  51374. return -1;
  51375. }
  51376. return 0;
  51377. }
  51378. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  51379. {
  51380. int ret;
  51381. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  51382. != WOLFSSL_SUCCESS) {
  51383. fprintf(stderr, "could not verify the cert: %s\n", certA);
  51384. fprintf(stderr, "Error: (%d): %s\n", ret,
  51385. wolfSSL_ERR_reason_error_string(ret));
  51386. return -1;
  51387. }
  51388. else {
  51389. fprintf(stderr, "successfully verified: %s\n", certA);
  51390. }
  51391. return 0;
  51392. }
  51393. #define LOAD_ONE_CA(a, b, c, d) \
  51394. do { \
  51395. (a) = load_ca_into_cm(c, d); \
  51396. if ((a) != 0) \
  51397. return (b); \
  51398. else \
  51399. (b)--; \
  51400. } while(0)
  51401. #define VERIFY_ONE_CERT(a, b, c, d) \
  51402. do { \
  51403. (a) = verify_cert_with_cm(c, d);\
  51404. if ((a) != 0) \
  51405. return (b); \
  51406. else \
  51407. (b)--; \
  51408. } while(0)
  51409. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  51410. {
  51411. int ret;
  51412. int i = -1;
  51413. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  51414. char chainGArr[9][50] = {"certs/ca-cert.pem",
  51415. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  51416. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  51417. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  51418. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  51419. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  51420. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  51421. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  51422. "certs/test-pathlen/chainG-entity.pem"};
  51423. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  51424. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  51425. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  51426. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  51427. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  51428. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  51429. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  51430. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  51431. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  51432. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  51433. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  51434. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  51435. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  51436. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  51437. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  51438. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  51439. /* test validating the entity twice, should have no effect on pathLen since
  51440. * entity/leaf cert */
  51441. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  51442. return ret;
  51443. }
  51444. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  51445. {
  51446. int ret;
  51447. int i = -1;
  51448. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  51449. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  51450. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  51451. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  51452. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  51453. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  51454. */
  51455. char chainHArr[6][50] = {"certs/ca-cert.pem",
  51456. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  51457. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  51458. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  51459. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  51460. "certs/test-pathlen/chainH-entity.pem"};
  51461. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  51462. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  51463. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  51464. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  51465. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  51466. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  51467. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  51468. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  51469. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  51470. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  51471. return ret;
  51472. }
  51473. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  51474. {
  51475. int ret;
  51476. int i = -1;
  51477. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  51478. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  51479. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  51480. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  51481. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  51482. */
  51483. char chainIArr[5][50] = {"certs/ca-cert.pem",
  51484. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  51485. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  51486. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  51487. "certs/test-pathlen/chainI-entity.pem"};
  51488. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  51489. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  51490. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  51491. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  51492. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  51493. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  51494. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  51495. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  51496. return ret;
  51497. }
  51498. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  51499. {
  51500. int ret;
  51501. int i = -1;
  51502. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  51503. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  51504. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  51505. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  51506. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  51507. */
  51508. char chainJArr[6][50] = {"certs/ca-cert.pem",
  51509. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  51510. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  51511. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  51512. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  51513. "certs/test-pathlen/chainJ-entity.pem"};
  51514. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  51515. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  51516. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  51517. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  51518. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  51519. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  51520. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  51521. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  51522. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  51523. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  51524. return ret;
  51525. }
  51526. static int test_various_pathlen_chains(void)
  51527. {
  51528. EXPECT_DECLS;
  51529. WOLFSSL_CERT_MANAGER* cm = NULL;
  51530. /* Test chain G (large chain with varying pathLens) */
  51531. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51532. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51533. ExpectIntEQ(test_chainG(cm), -1);
  51534. #else
  51535. ExpectIntEQ(test_chainG(cm), 0);
  51536. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51537. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51538. wolfSSL_CertManagerFree(cm);
  51539. /* end test chain G */
  51540. /* Test chain H (5 chain with same pathLens) */
  51541. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51542. ExpectIntLT(test_chainH(cm), 0);
  51543. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51544. wolfSSL_CertManagerFree(cm);
  51545. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51546. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51547. wolfSSL_CertManagerFree(cm);
  51548. /* end test chain H */
  51549. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  51550. * followed by 2 ICA's, should pass) */
  51551. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51552. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  51553. ExpectIntEQ(test_chainI(cm), -1);
  51554. #else
  51555. ExpectIntEQ(test_chainI(cm), 0);
  51556. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  51557. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51558. wolfSSL_CertManagerFree(cm);
  51559. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51560. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51561. wolfSSL_CertManagerFree(cm);
  51562. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  51563. * this time followed by 3 ICA's, should fail */
  51564. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51565. ExpectIntLT(test_chainJ(cm), 0);
  51566. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51567. wolfSSL_CertManagerFree(cm);
  51568. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  51569. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  51570. wolfSSL_CertManagerFree(cm);
  51571. return EXPECT_RESULT();
  51572. }
  51573. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  51574. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51575. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  51576. {
  51577. EXPECT_DECLS;
  51578. byte ekm[100] = {0};
  51579. (void)ctx;
  51580. /* Success Cases */
  51581. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51582. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  51583. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51584. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  51585. /* Use some random context */
  51586. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51587. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  51588. /* Failure cases */
  51589. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51590. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  51591. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51592. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  51593. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51594. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  51595. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51596. "extended master secret", XSTR_SIZEOF("extended master secret"),
  51597. NULL, 0, 0), 0);
  51598. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  51599. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  51600. return EXPECT_RESULT();
  51601. }
  51602. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  51603. {
  51604. wolfSSL_KeepArrays(ssl);
  51605. return TEST_SUCCESS;
  51606. }
  51607. static int test_export_keying_material(void)
  51608. {
  51609. EXPECT_DECLS;
  51610. test_ssl_cbf serverCb;
  51611. test_ssl_cbf clientCb;
  51612. XMEMSET(&serverCb, 0, sizeof(serverCb));
  51613. XMEMSET(&clientCb, 0, sizeof(clientCb));
  51614. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  51615. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  51616. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  51617. return EXPECT_RESULT();
  51618. }
  51619. #endif /* HAVE_KEYING_MATERIAL */
  51620. static int test_wolfSSL_THREADID_hash(void)
  51621. {
  51622. EXPECT_DECLS;
  51623. #if defined(OPENSSL_EXTRA)
  51624. CRYPTO_THREADID id;
  51625. CRYPTO_THREADID_current(NULL);
  51626. /* Hash result is unsigned long. */
  51627. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  51628. XMEMSET(&id, 0, sizeof(id));
  51629. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  51630. #endif /* OPENSSL_EXTRA */
  51631. return EXPECT_RESULT();
  51632. }
  51633. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  51634. {
  51635. EXPECT_DECLS;
  51636. #if defined(OPENSSL_EXTRA)
  51637. WOLFSSL_CTX* ctx = NULL;
  51638. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  51639. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  51640. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  51641. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  51642. #endif /* OPENSSL_EXTRA */
  51643. return EXPECT_RESULT();
  51644. }
  51645. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  51646. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  51647. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  51648. {
  51649. EXPECT_DECLS;
  51650. callback_functions* callbacks = NULL;
  51651. WOLFSSL_CTX* ctx = NULL;
  51652. WOLFSSL* ssl = NULL;
  51653. SOCKET_T sfd = 0;
  51654. SOCKET_T cfd = 0;
  51655. word16 port;
  51656. char msg[] = "I hear you fa shizzle!";
  51657. int len = (int) XSTRLEN(msg);
  51658. char input[1024];
  51659. int ret;
  51660. int err = 0;
  51661. if (!args)
  51662. return 0;
  51663. ((func_args*)args)->return_code = TEST_FAIL;
  51664. callbacks = ((func_args*)args)->callbacks;
  51665. ctx = wolfSSL_CTX_new(callbacks->method());
  51666. #if defined(USE_WINDOWS_API)
  51667. port = ((func_args*)args)->signal->port;
  51668. #else
  51669. /* Let tcp_listen assign port */
  51670. port = 0;
  51671. #endif
  51672. #ifdef WOLFSSL_TIRTOS
  51673. fdOpenSession(Task_self());
  51674. #endif
  51675. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  51676. caCertFile, 0));
  51677. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  51678. svrCertFile, WOLFSSL_FILETYPE_PEM));
  51679. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  51680. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  51681. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  51682. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  51683. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  51684. #elif !defined(NO_DH)
  51685. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  51686. #endif
  51687. if (callbacks->ctx_ready)
  51688. callbacks->ctx_ready(ctx);
  51689. ssl = wolfSSL_new(ctx);
  51690. ExpectNotNull(ssl);
  51691. /* listen and accept */
  51692. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  51693. CloseSocket(sfd);
  51694. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  51695. if (callbacks->ssl_ready)
  51696. callbacks->ssl_ready(ssl);
  51697. if (EXPECT_SUCCESS()) {
  51698. do {
  51699. err = 0; /* Reset error */
  51700. ret = wolfSSL_accept(ssl);
  51701. if (ret != WOLFSSL_SUCCESS) {
  51702. err = wolfSSL_get_error(ssl, 0);
  51703. }
  51704. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51705. }
  51706. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51707. /* read and write data */
  51708. XMEMSET(input, 0, sizeof(input));
  51709. while (EXPECT_SUCCESS()) {
  51710. ret = wolfSSL_read(ssl, input, sizeof(input));
  51711. if (ret > 0) {
  51712. break;
  51713. }
  51714. else {
  51715. err = wolfSSL_get_error(ssl,ret);
  51716. if (err == WOLFSSL_ERROR_WANT_READ) {
  51717. continue;
  51718. }
  51719. break;
  51720. }
  51721. }
  51722. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  51723. do {
  51724. ret = wolfSSL_write(ssl, msg, len);
  51725. if (ret > 0) {
  51726. break;
  51727. }
  51728. } while (ret < 0);
  51729. }
  51730. /* bidirectional shutdown */
  51731. while (EXPECT_SUCCESS()) {
  51732. ret = wolfSSL_shutdown(ssl);
  51733. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  51734. if (ret == WOLFSSL_SUCCESS) {
  51735. break;
  51736. }
  51737. }
  51738. if (EXPECT_SUCCESS()) {
  51739. /* wait for the peer to disconnect the tcp connection */
  51740. do {
  51741. ret = wolfSSL_read(ssl, input, sizeof(input));
  51742. err = wolfSSL_get_error(ssl, ret);
  51743. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  51744. }
  51745. /* detect TCP disconnect */
  51746. ExpectIntLE(ret,WOLFSSL_FAILURE);
  51747. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  51748. ((func_args*)args)->return_code = EXPECT_RESULT();
  51749. wolfSSL_free(ssl);
  51750. wolfSSL_CTX_free(ctx);
  51751. CloseSocket(cfd);
  51752. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51753. wc_ecc_fp_free(); /* free per thread cache */
  51754. #endif
  51755. #ifndef WOLFSSL_TIRTOS
  51756. return 0;
  51757. #endif
  51758. }
  51759. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  51760. {
  51761. EXPECT_DECLS;
  51762. callback_functions* callbacks = NULL;
  51763. WOLFSSL_CTX* ctx = NULL;
  51764. WOLFSSL* ssl = NULL;
  51765. SOCKET_T sfd = 0;
  51766. char msg[] = "hello wolfssl server!";
  51767. int len = (int) XSTRLEN(msg);
  51768. char input[1024];
  51769. int idx;
  51770. int ret, err;
  51771. if (!args)
  51772. return 0;
  51773. ((func_args*)args)->return_code = TEST_FAIL;
  51774. callbacks = ((func_args*)args)->callbacks;
  51775. ctx = wolfSSL_CTX_new(callbacks->method());
  51776. #ifdef WOLFSSL_TIRTOS
  51777. fdOpenSession(Task_self());
  51778. #endif
  51779. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  51780. caCertFile, 0));
  51781. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  51782. cliCertFile, WOLFSSL_FILETYPE_PEM));
  51783. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  51784. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  51785. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  51786. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  51787. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  51788. if (EXPECT_SUCCESS()) {
  51789. do {
  51790. err = 0; /* Reset error */
  51791. ret = wolfSSL_connect(ssl);
  51792. if (ret != WOLFSSL_SUCCESS) {
  51793. err = wolfSSL_get_error(ssl, 0);
  51794. }
  51795. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  51796. }
  51797. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  51798. if (EXPECT_SUCCESS()) {
  51799. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  51800. input[idx] = 0;
  51801. }
  51802. }
  51803. if (EXPECT_SUCCESS()) {
  51804. ret = wolfSSL_shutdown(ssl);
  51805. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  51806. ret = wolfSSL_shutdown(ssl);
  51807. }
  51808. }
  51809. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51810. ((func_args*)args)->return_code = EXPECT_RESULT();
  51811. wolfSSL_free(ssl);
  51812. wolfSSL_CTX_free(ctx);
  51813. CloseSocket(sfd);
  51814. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  51815. wc_ecc_fp_free(); /* free per thread cache */
  51816. #endif
  51817. #ifndef WOLFSSL_TIRTOS
  51818. return 0;
  51819. #endif
  51820. }
  51821. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  51822. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  51823. /* This test is to check wolfSSL_read behaves as same as
  51824. * openSSL when it is called after SSL_shutdown completes.
  51825. */
  51826. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  51827. {
  51828. EXPECT_DECLS;
  51829. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  51830. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  51831. tcp_ready ready;
  51832. func_args client_args;
  51833. func_args server_args;
  51834. THREAD_TYPE serverThread;
  51835. THREAD_TYPE clientThread;
  51836. callback_functions server_cbf;
  51837. callback_functions client_cbf;
  51838. #ifdef WOLFSSL_TIRTOS
  51839. fdOpenSession(Task_self());
  51840. #endif
  51841. StartTCP();
  51842. InitTcpReady(&ready);
  51843. #if defined(USE_WINDOWS_API)
  51844. /* use RNG to get random port if using windows */
  51845. ready.port = GetRandomPort();
  51846. #endif
  51847. XMEMSET(&client_args, 0, sizeof(func_args));
  51848. XMEMSET(&server_args, 0, sizeof(func_args));
  51849. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  51850. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  51851. server_cbf.method = wolfTLSv1_2_server_method;
  51852. client_cbf.method = wolfTLSv1_2_client_method;
  51853. server_args.callbacks = &server_cbf;
  51854. client_args.callbacks = &client_cbf;
  51855. server_args.signal = &ready;
  51856. client_args.signal = &ready;
  51857. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  51858. wait_tcp_ready(&server_args);
  51859. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  51860. join_thread(clientThread);
  51861. join_thread(serverThread);
  51862. ExpectTrue(client_args.return_code);
  51863. ExpectTrue(server_args.return_code);
  51864. FreeTcpReady(&ready);
  51865. #endif
  51866. return EXPECT_RESULT();
  51867. }
  51868. static int test_wolfSSL_CTX_get_min_proto_version(void)
  51869. {
  51870. EXPECT_DECLS;
  51871. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  51872. WOLFSSL_CTX *ctx = NULL;
  51873. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51874. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  51875. WOLFSSL_SUCCESS);
  51876. #ifdef WOLFSSL_ALLOW_SSLV3
  51877. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  51878. #else
  51879. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  51880. #endif
  51881. wolfSSL_CTX_free(ctx);
  51882. ctx = NULL;
  51883. #ifdef WOLFSSL_ALLOW_TLSV10
  51884. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  51885. #else
  51886. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51887. #endif
  51888. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  51889. WOLFSSL_SUCCESS);
  51890. #ifdef WOLFSSL_ALLOW_TLSV10
  51891. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  51892. #else
  51893. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  51894. #endif
  51895. wolfSSL_CTX_free(ctx);
  51896. ctx = NULL;
  51897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  51898. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  51899. WOLFSSL_SUCCESS);
  51900. #ifndef NO_OLD_TLS
  51901. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  51902. #else
  51903. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  51904. #endif
  51905. wolfSSL_CTX_free(ctx);
  51906. ctx = NULL;
  51907. #ifndef WOLFSSL_NO_TLS12
  51908. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  51909. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  51910. WOLFSSL_SUCCESS);
  51911. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  51912. wolfSSL_CTX_free(ctx);
  51913. ctx = NULL;
  51914. #endif
  51915. #ifdef WOLFSSL_TLS13
  51916. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  51917. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  51918. WOLFSSL_SUCCESS);
  51919. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  51920. wolfSSL_CTX_free(ctx);
  51921. ctx = NULL;
  51922. #endif
  51923. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  51924. return EXPECT_RESULT();
  51925. }
  51926. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  51927. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  51928. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  51929. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  51930. static int test_wolfSSL_set_SSL_CTX(void)
  51931. {
  51932. EXPECT_DECLS;
  51933. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  51934. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  51935. !defined(NO_RSA)
  51936. WOLFSSL_CTX *ctx1 = NULL;
  51937. WOLFSSL_CTX *ctx2 = NULL;
  51938. WOLFSSL *ssl = NULL;
  51939. const byte *session_id1 = (const byte *)"CTX1";
  51940. const byte *session_id2 = (const byte *)"CTX2";
  51941. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  51942. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  51943. WOLFSSL_FILETYPE_PEM));
  51944. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  51945. WOLFSSL_FILETYPE_PEM));
  51946. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  51947. WOLFSSL_SUCCESS);
  51948. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  51949. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  51950. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  51951. WOLFSSL_SUCCESS);
  51952. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  51953. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  51954. WOLFSSL_FILETYPE_PEM));
  51955. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  51956. WOLFSSL_FILETYPE_PEM));
  51957. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  51958. WOLFSSL_SUCCESS);
  51959. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  51960. WOLFSSL_SUCCESS);
  51961. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  51962. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  51963. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  51964. WOLFSSL_SUCCESS);
  51965. #ifdef HAVE_SESSION_TICKET
  51966. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  51967. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  51968. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  51969. #endif
  51970. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  51971. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  51972. #ifdef WOLFSSL_INT_H
  51973. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  51974. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  51975. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  51976. #endif
  51977. #ifdef HAVE_SESSION_TICKET
  51978. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  51979. #endif
  51980. /* Set the ctx1 that has TLSv1.3 as max proto version */
  51981. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  51982. /* MUST not change proto versions of ssl */
  51983. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  51984. #ifdef HAVE_SESSION_TICKET
  51985. /* MUST not change */
  51986. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  51987. #endif
  51988. /* MUST change */
  51989. #ifdef WOLFSSL_INT_H
  51990. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  51991. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  51992. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  51993. #endif
  51994. wolfSSL_free(ssl);
  51995. wolfSSL_CTX_free(ctx1);
  51996. wolfSSL_CTX_free(ctx2);
  51997. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  51998. return EXPECT_RESULT();
  51999. }
  52000. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52001. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52002. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52003. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  52004. static int test_wolfSSL_security_level(void)
  52005. {
  52006. EXPECT_DECLS;
  52007. #if defined(OPENSSL_EXTRA)
  52008. SSL_CTX *ctx = NULL;
  52009. #ifdef WOLFSSL_TLS13
  52010. #ifdef NO_WOLFSSL_SERVER
  52011. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52012. #else
  52013. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52014. #endif
  52015. SSL_CTX_set_security_level(NULL, 1);
  52016. SSL_CTX_set_security_level(ctx, 1);
  52017. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  52018. /* Stub so nothing happens. */
  52019. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  52020. SSL_CTX_free(ctx);
  52021. #else
  52022. (void)ctx;
  52023. #endif
  52024. #endif
  52025. return EXPECT_RESULT();
  52026. }
  52027. static int test_wolfSSL_SSL_in_init(void)
  52028. {
  52029. EXPECT_DECLS;
  52030. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  52031. SSL_CTX* ctx = NULL;
  52032. SSL* ssl = NULL;
  52033. const char* testCertFile;
  52034. const char* testKeyFile;
  52035. #ifdef WOLFSSL_TLS13
  52036. #ifdef NO_WOLFSSL_SERVER
  52037. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52038. #else
  52039. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52040. #endif
  52041. #else
  52042. #ifdef NO_WOLFSSL_SERVER
  52043. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52044. #else
  52045. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52046. #endif
  52047. #endif
  52048. #ifndef NO_RSA
  52049. testCertFile = svrCertFile;
  52050. testKeyFile = svrKeyFile;
  52051. #elif defined(HAVE_ECC)
  52052. testCertFile = eccCertFile;
  52053. testKeyFile = eccKeyFile;
  52054. #else
  52055. testCertFile = NULL;
  52056. testKeyFile = NULL;
  52057. #endif
  52058. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  52059. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52060. SSL_FILETYPE_PEM));
  52061. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52062. SSL_FILETYPE_PEM));
  52063. }
  52064. ExpectNotNull(ssl = SSL_new(ctx));
  52065. ExpectIntEQ(SSL_in_init(ssl), 1);
  52066. SSL_CTX_free(ctx);
  52067. SSL_free(ssl);
  52068. #endif
  52069. return EXPECT_RESULT();
  52070. }
  52071. static int test_wolfSSL_CTX_set_timeout(void)
  52072. {
  52073. EXPECT_DECLS;
  52074. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  52075. int timeout;
  52076. WOLFSSL_CTX* ctx = NULL;
  52077. (void)timeout;
  52078. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52079. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  52080. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  52081. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  52082. */
  52083. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  52084. /* giving 0 as timeout value sets default timeout */
  52085. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  52086. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  52087. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  52088. #else
  52089. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  52090. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  52091. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  52092. #endif
  52093. wolfSSL_CTX_free(ctx);
  52094. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  52095. return EXPECT_RESULT();
  52096. }
  52097. static int test_wolfSSL_OpenSSL_version(void)
  52098. {
  52099. EXPECT_DECLS;
  52100. #if defined(OPENSSL_EXTRA)
  52101. const char* ver;
  52102. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52103. ExpectNotNull(ver = OpenSSL_version(0));
  52104. #else
  52105. ExpectNotNull(ver = OpenSSL_version());
  52106. #endif
  52107. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  52108. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  52109. #endif
  52110. return EXPECT_RESULT();
  52111. }
  52112. static int test_CONF_CTX_CMDLINE(void)
  52113. {
  52114. EXPECT_DECLS;
  52115. #if defined(OPENSSL_ALL)
  52116. SSL_CTX* ctx = NULL;
  52117. SSL_CONF_CTX* cctx = NULL;
  52118. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  52119. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52120. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  52121. /* set flags */
  52122. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  52123. WOLFSSL_CONF_FLAG_CMDLINE);
  52124. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  52125. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  52126. /* cmd invalid command */
  52127. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  52128. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  52129. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  52130. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  52131. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  52132. /* cmd Certificate and Private Key*/
  52133. {
  52134. #if !defined(NO_CERTS) && !defined(NO_RSA)
  52135. const char* ourCert = svrCertFile;
  52136. const char* ourKey = svrKeyFile;
  52137. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  52138. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  52139. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  52140. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  52141. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52142. #endif
  52143. }
  52144. /* cmd curves */
  52145. {
  52146. #if defined(HAVE_ECC)
  52147. const char* curve = "secp256r1";
  52148. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  52149. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  52150. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52151. #endif
  52152. }
  52153. /* cmd CipherString */
  52154. {
  52155. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  52156. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  52157. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  52158. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52159. }
  52160. /* cmd DH parameter */
  52161. {
  52162. #if !defined(NO_DH) && !defined(NO_BIO)
  52163. const char* ourdhcert = "./certs/dh2048.pem";
  52164. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  52165. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  52166. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52167. #endif
  52168. }
  52169. SSL_CTX_free(ctx);
  52170. SSL_CONF_CTX_free(cctx);
  52171. #endif /* OPENSSL_EXTRA */
  52172. return EXPECT_RESULT();
  52173. }
  52174. static int test_CONF_CTX_FILE(void)
  52175. {
  52176. EXPECT_DECLS;
  52177. #if defined(OPENSSL_ALL)
  52178. SSL_CTX* ctx = NULL;
  52179. SSL_CONF_CTX* cctx = NULL;
  52180. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  52181. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52182. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  52183. /* set flags */
  52184. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  52185. WOLFSSL_CONF_FLAG_FILE);
  52186. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  52187. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  52188. /* sanity check */
  52189. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  52190. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  52191. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  52192. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  52193. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  52194. /* cmd Certificate and Private Key*/
  52195. {
  52196. #if !defined(NO_CERTS) && !defined(NO_RSA)
  52197. const char* ourCert = svrCertFile;
  52198. const char* ourKey = svrKeyFile;
  52199. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  52200. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  52201. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  52202. WOLFSSL_SUCCESS);
  52203. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  52204. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52205. #endif
  52206. }
  52207. /* cmd curves */
  52208. {
  52209. #if defined(HAVE_ECC)
  52210. const char* curve = "secp256r1";
  52211. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  52212. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  52213. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52214. #endif
  52215. }
  52216. /* cmd CipherString */
  52217. {
  52218. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  52219. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  52220. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  52221. WOLFSSL_SUCCESS);
  52222. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52223. }
  52224. /* cmd DH parameter */
  52225. {
  52226. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  52227. const char* ourdhcert = "./certs/dh3072.pem";
  52228. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  52229. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  52230. WOLFSSL_SUCCESS);
  52231. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  52232. #endif
  52233. }
  52234. SSL_CTX_free(ctx);
  52235. SSL_CONF_CTX_free(cctx);
  52236. #endif /* OPENSSL_EXTRA */
  52237. return EXPECT_RESULT();
  52238. }
  52239. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  52240. {
  52241. EXPECT_DECLS;
  52242. #ifdef HAVE_EX_DATA
  52243. int idx1, idx2;
  52244. /* test for unsupported class index */
  52245. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  52246. 0,NULL, NULL, NULL, NULL ), -1);
  52247. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  52248. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  52249. 0,NULL, NULL, NULL, NULL ), -1);
  52250. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  52251. 0,NULL, NULL, NULL, NULL ), -1);
  52252. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  52253. 0,NULL, NULL, NULL, NULL ), -1);
  52254. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  52255. 0,NULL, NULL, NULL, NULL ), -1);
  52256. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  52257. 0,NULL, NULL, NULL, NULL ), -1);
  52258. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  52259. 0,NULL, NULL, NULL, NULL ), -1);
  52260. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  52261. 0,NULL, NULL, NULL, NULL ), -1);
  52262. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  52263. 0,NULL, NULL, NULL, NULL ), -1);
  52264. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  52265. 0,NULL, NULL, NULL, NULL ), -1);
  52266. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  52267. 0,NULL, NULL, NULL, NULL ), -1);
  52268. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  52269. 0,NULL, NULL, NULL, NULL ), -1);
  52270. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  52271. 0,NULL, NULL, NULL, NULL ), -1);
  52272. /* test for supported class index */
  52273. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  52274. 0,NULL, NULL, NULL, NULL );
  52275. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  52276. 0,NULL, NULL, NULL, NULL );
  52277. ExpectIntNE(idx1, -1);
  52278. ExpectIntNE(idx2, -1);
  52279. ExpectIntNE(idx1, idx2);
  52280. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  52281. 0,NULL, NULL, NULL, NULL );
  52282. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  52283. 0,NULL, NULL, NULL, NULL );
  52284. ExpectIntNE(idx1, -1);
  52285. ExpectIntNE(idx2, -1);
  52286. ExpectIntNE(idx1, idx2);
  52287. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  52288. 0,NULL, NULL, NULL, NULL );
  52289. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  52290. 0,NULL, NULL, NULL, NULL );
  52291. ExpectIntNE(idx1, -1);
  52292. ExpectIntNE(idx2, -1);
  52293. ExpectIntNE(idx1, idx2);
  52294. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  52295. 0,NULL, NULL, NULL, NULL );
  52296. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  52297. 0,NULL, NULL, NULL, NULL );
  52298. ExpectIntNE(idx1, -1);
  52299. ExpectIntNE(idx2, -1);
  52300. ExpectIntNE(idx1, idx2);
  52301. #endif /* HAVE_EX_DATA */
  52302. return EXPECT_RESULT();
  52303. }
  52304. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  52305. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  52306. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  52307. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  52308. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  52309. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  52310. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  52311. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  52312. #define SESSION_NEW_IDX_PTR "Testing"
  52313. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  52314. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  52315. {
  52316. AssertNotNull(p);
  52317. AssertNull(ptr);
  52318. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  52319. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  52320. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  52321. }
  52322. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  52323. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  52324. void* arg)
  52325. {
  52326. EXPECT_DECLS;
  52327. ExpectNotNull(out);
  52328. ExpectNotNull(in);
  52329. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  52330. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  52331. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  52332. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  52333. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  52334. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  52335. if (EXPECT_SUCCESS()) {
  52336. return SSL_SUCCESS;
  52337. }
  52338. else {
  52339. return SSL_FAILURE;
  52340. }
  52341. }
  52342. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  52343. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  52344. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  52345. {
  52346. EXPECT_DECLS;
  52347. ExpectNotNull(p);
  52348. ExpectNull(ptr);
  52349. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  52350. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  52351. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  52352. if (EXPECT_SUCCESS()) {
  52353. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  52354. }
  52355. }
  52356. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  52357. {
  52358. EXPECT_DECLS;
  52359. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  52360. (void*)SESSION_NEW_IDX_PTR,
  52361. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  52362. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  52363. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  52364. SSL_SESSION* s = SSL_SESSION_new();
  52365. SSL_SESSION* d = NULL;
  52366. ExpectNotNull(s);
  52367. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  52368. ExpectNotNull(d = SSL_SESSION_dup(s));
  52369. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  52370. SSL_SESSION_free(s);
  52371. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  52372. SSL_SESSION_free(d);
  52373. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  52374. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  52375. crypto_ex_cb_ctx_session = NULL;
  52376. return EXPECT_RESULT();
  52377. }
  52378. #else
  52379. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  52380. {
  52381. return TEST_SKIPPED;
  52382. }
  52383. #endif
  52384. static int test_wolfSSL_set_psk_use_session_callback(void)
  52385. {
  52386. EXPECT_DECLS;
  52387. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  52388. SSL_CTX* ctx = NULL;
  52389. SSL* ssl = NULL;
  52390. const char* testCertFile;
  52391. const char* testKeyFile;
  52392. #ifdef WOLFSSL_TLS13
  52393. #ifdef NO_WOLFSSL_SERVER
  52394. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  52395. #else
  52396. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  52397. #endif
  52398. #else
  52399. #ifdef NO_WOLFSSL_SERVER
  52400. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52401. #else
  52402. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52403. #endif
  52404. #endif
  52405. #ifndef NO_RSA
  52406. testCertFile = svrCertFile;
  52407. testKeyFile = svrKeyFile;
  52408. #elif defined(HAVE_ECC)
  52409. testCertFile = eccCertFile;
  52410. testKeyFile = eccKeyFile;
  52411. #else
  52412. testCertFile = NULL;
  52413. testKeyFile = NULL;
  52414. #endif
  52415. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  52416. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52417. SSL_FILETYPE_PEM));
  52418. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52419. SSL_FILETYPE_PEM));
  52420. }
  52421. ExpectNotNull(ssl = SSL_new(ctx));
  52422. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  52423. SSL_CTX_free(ctx);
  52424. SSL_free(ssl);
  52425. #endif
  52426. return EXPECT_RESULT();
  52427. }
  52428. static int test_wolfSSL_ERR_strings(void)
  52429. {
  52430. EXPECT_DECLS;
  52431. #if !defined(NO_ERROR_STRINGS)
  52432. const char* err1 = "unsupported cipher suite";
  52433. const char* err2 = "wolfSSL PEM routines";
  52434. const char* err = NULL;
  52435. (void)err;
  52436. (void)err1;
  52437. (void)err2;
  52438. #if defined(OPENSSL_EXTRA)
  52439. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  52440. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  52441. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  52442. ExpectIntEQ((*err == '\0'), 1);
  52443. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  52444. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  52445. #else
  52446. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  52447. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  52448. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  52449. ExpectIntEQ((*err == '\0'), 1);
  52450. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  52451. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  52452. ExpectIntEQ((*err == '\0'), 1);
  52453. #endif
  52454. #endif
  52455. return EXPECT_RESULT();
  52456. }
  52457. static int test_wolfSSL_EVP_shake128(void)
  52458. {
  52459. EXPECT_DECLS;
  52460. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  52461. defined(WOLFSSL_SHAKE128)
  52462. const EVP_MD* md = NULL;
  52463. ExpectNotNull(md = EVP_shake128());
  52464. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  52465. #endif
  52466. return EXPECT_RESULT();
  52467. }
  52468. static int test_wolfSSL_EVP_shake256(void)
  52469. {
  52470. EXPECT_DECLS;
  52471. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  52472. defined(WOLFSSL_SHAKE256)
  52473. const EVP_MD* md = NULL;
  52474. ExpectNotNull(md = EVP_shake256());
  52475. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  52476. #endif
  52477. return EXPECT_RESULT();
  52478. }
  52479. /*
  52480. * Testing EVP digest API with SM3
  52481. */
  52482. static int test_wolfSSL_EVP_sm3(void)
  52483. {
  52484. int res = TEST_SKIPPED;
  52485. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  52486. EXPECT_DECLS;
  52487. const EVP_MD* md = NULL;
  52488. EVP_MD_CTX* mdCtx = NULL;
  52489. byte data[WC_SM3_BLOCK_SIZE * 4];
  52490. byte hash[WC_SM3_DIGEST_SIZE];
  52491. byte calcHash[WC_SM3_DIGEST_SIZE];
  52492. byte expHash[WC_SM3_DIGEST_SIZE] = {
  52493. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  52494. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  52495. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  52496. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  52497. };
  52498. word32 chunk;
  52499. word32 i;
  52500. unsigned int sz;
  52501. int ret;
  52502. XMEMSET(data, 0, sizeof(data));
  52503. md = EVP_sm3();
  52504. ExpectTrue(md != NULL);
  52505. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  52506. mdCtx = EVP_MD_CTX_new();
  52507. ExpectTrue(mdCtx != NULL);
  52508. /* Invalid Parameters */
  52509. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  52510. /* Valid Parameters */
  52511. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  52512. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  52513. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  52514. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  52515. /* Valid Parameters */
  52516. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  52517. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  52518. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  52519. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  52520. WOLFSSL_SUCCESS);
  52521. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  52522. WOLFSSL_SUCCESS);
  52523. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  52524. WOLFSSL_SUCCESS);
  52525. /* Ensure too many bytes for lengths. */
  52526. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  52527. WOLFSSL_SUCCESS);
  52528. /* Invalid Parameters */
  52529. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  52530. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  52531. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  52532. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  52533. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  52534. /* Valid Parameters */
  52535. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  52536. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  52537. /* Chunk tests. */
  52538. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  52539. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  52540. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  52541. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  52542. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  52543. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  52544. WOLFSSL_SUCCESS);
  52545. }
  52546. if (i < (word32)sizeof(data)) {
  52547. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  52548. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  52549. }
  52550. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  52551. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  52552. }
  52553. /* Not testing when the low 32-bit length overflows. */
  52554. ret = EVP_MD_CTX_cleanup(mdCtx);
  52555. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  52556. wolfSSL_EVP_MD_CTX_free(mdCtx);
  52557. res = EXPECT_RESULT();
  52558. #endif
  52559. return res;
  52560. } /* END test_EVP_sm3 */
  52561. static int test_EVP_blake2(void)
  52562. {
  52563. EXPECT_DECLS;
  52564. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  52565. const EVP_MD* md = NULL;
  52566. (void)md;
  52567. #if defined(HAVE_BLAKE2)
  52568. ExpectNotNull(md = EVP_blake2b512());
  52569. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  52570. #endif
  52571. #if defined(HAVE_BLAKE2S)
  52572. ExpectNotNull(md = EVP_blake2s256());
  52573. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  52574. #endif
  52575. #endif
  52576. return EXPECT_RESULT();
  52577. }
  52578. #if defined(OPENSSL_EXTRA)
  52579. static void list_md_fn(const EVP_MD* m, const char* from,
  52580. const char* to, void* arg)
  52581. {
  52582. const char* mn;
  52583. BIO *bio;
  52584. (void) from;
  52585. (void) to;
  52586. (void) arg;
  52587. (void) mn;
  52588. (void) bio;
  52589. if (!m) {
  52590. /* alias */
  52591. AssertNull(m);
  52592. AssertNotNull(to);
  52593. }
  52594. else {
  52595. AssertNotNull(m);
  52596. AssertNull(to);
  52597. }
  52598. AssertNotNull(from);
  52599. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52600. mn = EVP_get_digestbyname(from);
  52601. /* print to stderr */
  52602. AssertNotNull(arg);
  52603. bio = BIO_new(BIO_s_file());
  52604. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52605. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  52606. BIO_free(bio);
  52607. #endif
  52608. }
  52609. #endif
  52610. static int test_EVP_MD_do_all(void)
  52611. {
  52612. int res = TEST_SKIPPED;
  52613. #if defined(OPENSSL_EXTRA)
  52614. EVP_MD_do_all(NULL, stderr);
  52615. EVP_MD_do_all(list_md_fn, stderr);
  52616. res = TEST_SUCCESS;
  52617. #endif
  52618. return res;
  52619. }
  52620. #if defined(OPENSSL_EXTRA)
  52621. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  52622. {
  52623. (void)arg;
  52624. (void)nm;
  52625. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  52626. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  52627. /* print to stderr */
  52628. AssertNotNull(arg);
  52629. BIO *bio = BIO_new(BIO_s_file());
  52630. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  52631. BIO_printf(bio, "%s\n", nm);
  52632. BIO_free(bio);
  52633. #endif
  52634. }
  52635. #endif
  52636. static int test_OBJ_NAME_do_all(void)
  52637. {
  52638. int res = TEST_SKIPPED;
  52639. #if defined(OPENSSL_EXTRA)
  52640. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  52641. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  52642. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  52643. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  52644. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  52645. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  52646. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  52647. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  52648. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  52649. res = TEST_SUCCESS;
  52650. #endif
  52651. return res;
  52652. }
  52653. static int test_SSL_CIPHER_get_xxx(void)
  52654. {
  52655. EXPECT_DECLS;
  52656. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  52657. !defined(NO_FILESYSTEM)
  52658. const SSL_CIPHER* cipher = NULL;
  52659. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  52660. int i, numCiphers = 0;
  52661. SSL_CTX* ctx = NULL;
  52662. SSL* ssl = NULL;
  52663. const char* testCertFile;
  52664. const char* testKeyFile;
  52665. char buf[256] = {0};
  52666. const char* cipher_id = NULL;
  52667. int expect_nid1 = NID_undef;
  52668. int expect_nid2 = NID_undef;
  52669. int expect_nid3 = NID_undef;
  52670. int expect_nid4 = NID_undef;
  52671. int expect_nid5 = 0;
  52672. const char* cipher_id2 = NULL;
  52673. int expect_nid21 = NID_undef;
  52674. int expect_nid22 = NID_undef;
  52675. int expect_nid23 = NID_undef;
  52676. int expect_nid24 = NID_undef;
  52677. int expect_nid25 = 0;
  52678. (void)cipher;
  52679. (void)supportedCiphers;
  52680. (void)i;
  52681. (void)numCiphers;
  52682. (void)ctx;
  52683. (void)ssl;
  52684. (void)testCertFile;
  52685. (void)testKeyFile;
  52686. #if defined(WOLFSSL_TLS13)
  52687. cipher_id = "TLS13-AES128-GCM-SHA256";
  52688. expect_nid1 = NID_auth_rsa;
  52689. expect_nid2 = NID_aes_128_gcm;
  52690. expect_nid3 = NID_sha256;
  52691. expect_nid4 = NID_kx_any;
  52692. expect_nid5 = 1;
  52693. #if !defined(WOLFSSL_NO_TLS12)
  52694. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  52695. expect_nid21 = NID_auth_rsa;
  52696. expect_nid22 = NID_aes_256_gcm;
  52697. expect_nid23 = NID_sha384;
  52698. expect_nid24 = NID_kx_ecdhe;
  52699. expect_nid25 = 1;
  52700. #endif
  52701. #endif
  52702. #ifdef NO_WOLFSSL_SERVER
  52703. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  52704. #else
  52705. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  52706. #endif
  52707. if (cipher_id) {
  52708. #ifndef NO_RSA
  52709. testCertFile = svrCertFile;
  52710. testKeyFile = svrKeyFile;
  52711. #elif defined(HAVE_ECC)
  52712. testCertFile = eccCertFile;
  52713. testKeyFile = eccKeyFile;
  52714. #else
  52715. testCertFile = NULL;
  52716. testKeyFile = NULL;
  52717. #endif
  52718. if (testCertFile != NULL && testKeyFile != NULL) {
  52719. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  52720. SSL_FILETYPE_PEM));
  52721. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  52722. SSL_FILETYPE_PEM));
  52723. }
  52724. ExpectNotNull(ssl = SSL_new(ctx));
  52725. ExpectIntEQ(SSL_in_init(ssl), 1);
  52726. supportedCiphers = SSL_get_ciphers(ssl);
  52727. numCiphers = sk_num(supportedCiphers);
  52728. for (i = 0; i < numCiphers; ++i) {
  52729. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52730. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52731. }
  52732. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  52733. break;
  52734. }
  52735. }
  52736. /* test case for */
  52737. if (i != numCiphers) {
  52738. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  52739. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  52740. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  52741. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  52742. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  52743. }
  52744. if (cipher_id2) {
  52745. for (i = 0; i < numCiphers; ++i) {
  52746. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  52747. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  52748. }
  52749. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  52750. break;
  52751. }
  52752. }
  52753. /* test case for */
  52754. if (i != numCiphers) {
  52755. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  52756. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  52757. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  52758. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  52759. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  52760. }
  52761. }
  52762. }
  52763. SSL_CTX_free(ctx);
  52764. SSL_free(ssl);
  52765. #endif
  52766. return EXPECT_RESULT();
  52767. }
  52768. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  52769. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  52770. int* keyFormat)
  52771. {
  52772. int ret;
  52773. byte* key_buf = NULL;
  52774. size_t key_sz = 0;
  52775. EncryptedInfo encInfo;
  52776. XMEMSET(&encInfo, 0, sizeof(encInfo));
  52777. ret = load_file(privKeyFile, &key_buf, &key_sz);
  52778. if (ret == 0) {
  52779. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  52780. NULL, &encInfo, keyFormat);
  52781. }
  52782. if (key_buf != NULL) {
  52783. free(key_buf); key_buf = NULL;
  52784. }
  52785. (void)encInfo; /* not used in this test */
  52786. #ifdef DEBUG_WOLFSSL
  52787. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  52788. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  52789. (*pDer)->length);
  52790. #endif
  52791. return ret;
  52792. }
  52793. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  52794. {
  52795. int ret = CRYPTOCB_UNAVAILABLE;
  52796. const char* privKeyFile = (const char*)ctx;
  52797. DerBuffer* pDer = NULL;
  52798. int keyFormat = 0;
  52799. if (info->algo_type == WC_ALGO_TYPE_PK) {
  52800. #ifdef DEBUG_WOLFSSL
  52801. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  52802. #endif
  52803. #ifndef NO_RSA
  52804. if (info->pk.type == WC_PK_TYPE_RSA) {
  52805. switch (info->pk.rsa.type) {
  52806. case RSA_PUBLIC_ENCRYPT:
  52807. case RSA_PUBLIC_DECRYPT:
  52808. /* perform software based RSA public op */
  52809. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  52810. break;
  52811. case RSA_PRIVATE_ENCRYPT:
  52812. case RSA_PRIVATE_DECRYPT:
  52813. {
  52814. RsaKey key;
  52815. /* perform software based RSA private op */
  52816. #ifdef DEBUG_WOLFSSL
  52817. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  52818. #endif
  52819. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  52820. &keyFormat);
  52821. if (ret != 0) {
  52822. return ret;
  52823. }
  52824. ret = wc_InitRsaKey(&key, HEAP_HINT);
  52825. if (ret == 0) {
  52826. word32 keyIdx = 0;
  52827. /* load RSA private key and perform private transform */
  52828. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  52829. &key, pDer->length);
  52830. if (ret == 0) {
  52831. ret = wc_RsaFunction(
  52832. info->pk.rsa.in, info->pk.rsa.inLen,
  52833. info->pk.rsa.out, info->pk.rsa.outLen,
  52834. info->pk.rsa.type, &key, info->pk.rsa.rng);
  52835. }
  52836. else {
  52837. /* if decode fails, then fall-back to software based crypto */
  52838. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  52839. "key decode failed %d, falling back to "
  52840. "software\n", ret);
  52841. ret = CRYPTOCB_UNAVAILABLE;
  52842. }
  52843. wc_FreeRsaKey(&key);
  52844. }
  52845. wc_FreeDer(&pDer); pDer = NULL;
  52846. break;
  52847. }
  52848. }
  52849. #ifdef DEBUG_WOLFSSL
  52850. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  52851. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  52852. #endif
  52853. }
  52854. #endif /* !NO_RSA */
  52855. #ifdef HAVE_ECC
  52856. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  52857. /* mark this key as ephemeral */
  52858. if (info->pk.eckg.key != NULL) {
  52859. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  52860. sizeof(info->pk.eckg.key->label));
  52861. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  52862. }
  52863. }
  52864. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  52865. ecc_key key;
  52866. /* perform software based ECC sign */
  52867. #ifdef DEBUG_WOLFSSL
  52868. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  52869. #endif
  52870. if (info->pk.eccsign.key != NULL &&
  52871. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  52872. /* this is an empheral key */
  52873. #ifdef DEBUG_WOLFSSL
  52874. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  52875. "ephemeral key\n");
  52876. #endif
  52877. return CRYPTOCB_UNAVAILABLE;
  52878. }
  52879. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  52880. if (ret != 0) {
  52881. return ret;
  52882. }
  52883. ret = wc_ecc_init(&key);
  52884. if (ret == 0) {
  52885. word32 keyIdx = 0;
  52886. /* load ECC private key and perform private transform */
  52887. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  52888. &key, pDer->length);
  52889. if (ret == 0) {
  52890. ret = wc_ecc_sign_hash(
  52891. info->pk.eccsign.in, info->pk.eccsign.inlen,
  52892. info->pk.eccsign.out, info->pk.eccsign.outlen,
  52893. info->pk.eccsign.rng, &key);
  52894. }
  52895. else {
  52896. /* if decode fails, then fall-back to software based crypto */
  52897. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  52898. "decode failed %d, falling back to software\n", ret);
  52899. ret = CRYPTOCB_UNAVAILABLE;
  52900. }
  52901. wc_ecc_free(&key);
  52902. }
  52903. wc_FreeDer(&pDer); pDer = NULL;
  52904. #ifdef DEBUG_WOLFSSL
  52905. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  52906. ret, *info->pk.eccsign.outlen);
  52907. #endif
  52908. }
  52909. #endif /* HAVE_ECC */
  52910. #ifdef HAVE_ED25519
  52911. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  52912. ed25519_key key;
  52913. /* perform software based ED25519 sign */
  52914. #ifdef DEBUG_WOLFSSL
  52915. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  52916. #endif
  52917. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  52918. if (ret != 0) {
  52919. return ret;
  52920. }
  52921. ret = wc_ed25519_init(&key);
  52922. if (ret == 0) {
  52923. word32 keyIdx = 0;
  52924. /* load ED25519 private key and perform private transform */
  52925. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  52926. &key, pDer->length);
  52927. if (ret == 0) {
  52928. /* calculate public key */
  52929. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  52930. if (ret == 0) {
  52931. key.pubKeySet = 1;
  52932. ret = wc_ed25519_sign_msg_ex(
  52933. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  52934. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  52935. &key, info->pk.ed25519sign.type,
  52936. info->pk.ed25519sign.context,
  52937. info->pk.ed25519sign.contextLen);
  52938. }
  52939. }
  52940. else {
  52941. /* if decode fails, then fall-back to software based crypto */
  52942. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  52943. "decode failed %d, falling back to software\n", ret);
  52944. ret = CRYPTOCB_UNAVAILABLE;
  52945. }
  52946. wc_ed25519_free(&key);
  52947. }
  52948. wc_FreeDer(&pDer); pDer = NULL;
  52949. #ifdef DEBUG_WOLFSSL
  52950. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  52951. ret, *info->pk.ed25519sign.outLen);
  52952. #endif
  52953. }
  52954. #endif /* HAVE_ED25519 */
  52955. }
  52956. (void)thisDevId;
  52957. (void)keyFormat;
  52958. return ret;
  52959. }
  52960. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  52961. static int test_wc_CryptoCb_TLS(int tlsVer,
  52962. const char* cliCaPemFile, const char* cliCertPemFile,
  52963. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  52964. const char* svrCaPemFile, const char* svrCertPemFile,
  52965. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  52966. {
  52967. EXPECT_DECLS;
  52968. callback_functions client_cbf;
  52969. callback_functions server_cbf;
  52970. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  52971. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  52972. if (tlsVer == WOLFSSL_TLSV1_3) {
  52973. #ifdef WOLFSSL_TLS13
  52974. server_cbf.method = wolfTLSv1_3_server_method;
  52975. client_cbf.method = wolfTLSv1_3_client_method;
  52976. #endif
  52977. }
  52978. else if (tlsVer == WOLFSSL_TLSV1_2) {
  52979. #ifndef WOLFSSL_NO_TLS12
  52980. server_cbf.method = wolfTLSv1_2_server_method;
  52981. client_cbf.method = wolfTLSv1_2_client_method;
  52982. #endif
  52983. }
  52984. else if (tlsVer == WOLFSSL_TLSV1_1) {
  52985. #ifndef NO_OLD_TLS
  52986. server_cbf.method = wolfTLSv1_1_server_method;
  52987. client_cbf.method = wolfTLSv1_1_client_method;
  52988. #endif
  52989. }
  52990. else if (tlsVer == WOLFSSL_TLSV1) {
  52991. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  52992. server_cbf.method = wolfTLSv1_server_method;
  52993. client_cbf.method = wolfTLSv1_client_method;
  52994. #endif
  52995. }
  52996. else if (tlsVer == WOLFSSL_SSLV3) {
  52997. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  52998. defined(WOLFSSL_STATIC_RSA)
  52999. server_cbf.method = wolfSSLv3_server_method;
  53000. client_cbf.method = wolfSSLv3_client_method;
  53001. #endif
  53002. }
  53003. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  53004. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53005. server_cbf.method = wolfDTLSv1_2_server_method;
  53006. client_cbf.method = wolfDTLSv1_2_client_method;
  53007. #endif
  53008. }
  53009. else if (tlsVer == WOLFSSL_DTLSV1) {
  53010. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  53011. server_cbf.method = wolfDTLSv1_server_method;
  53012. client_cbf.method = wolfDTLSv1_client_method;
  53013. #endif
  53014. }
  53015. if (server_cbf.method == NULL) {
  53016. /* not enabled */
  53017. return TEST_SUCCESS;
  53018. }
  53019. /* Setup the keys for the TLS test */
  53020. client_cbf.certPemFile = cliCertPemFile;
  53021. client_cbf.keyPemFile = cliPubKeyPemFile;
  53022. client_cbf.caPemFile = cliCaPemFile;
  53023. server_cbf.certPemFile = svrCertPemFile;
  53024. server_cbf.keyPemFile = svrPubKeyPemFile;
  53025. server_cbf.caPemFile = svrCaPemFile;
  53026. /* Setup a crypto callback with pointer to private key file for testing */
  53027. client_cbf.devId = 1;
  53028. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  53029. (void*)cliPrivKeyPemFile);
  53030. server_cbf.devId = 2;
  53031. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  53032. (void*)svrPrivKeyPemFile);
  53033. /* Perform TLS server and client test */
  53034. /* First test is at WOLFSSL_CTX level */
  53035. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53036. /* Check for success */
  53037. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53038. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53039. if (EXPECT_SUCCESS()) {
  53040. /* Second test is a WOLFSSL object level */
  53041. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  53042. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53043. }
  53044. /* Check for success */
  53045. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53046. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53047. /* Un register the devId's */
  53048. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  53049. client_cbf.devId = INVALID_DEVID;
  53050. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  53051. server_cbf.devId = INVALID_DEVID;
  53052. return EXPECT_RESULT();
  53053. }
  53054. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  53055. static int test_wc_CryptoCb(void)
  53056. {
  53057. EXPECT_DECLS;
  53058. #ifdef WOLF_CRYPTO_CB
  53059. /* TODO: Add crypto callback API tests */
  53060. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53061. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  53062. int tlsVer;
  53063. #endif
  53064. #ifndef NO_RSA
  53065. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53066. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53067. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  53068. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  53069. TEST_SUCCESS);
  53070. }
  53071. #endif
  53072. #ifdef HAVE_ECC
  53073. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53074. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53075. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  53076. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  53077. TEST_SUCCESS);
  53078. }
  53079. #endif
  53080. #ifdef HAVE_ED25519
  53081. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  53082. if (tlsVer == WOLFSSL_DTLSV1) continue;
  53083. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  53084. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  53085. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  53086. TEST_SUCCESS);
  53087. }
  53088. #endif
  53089. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  53090. #endif /* WOLF_CRYPTO_CB */
  53091. return EXPECT_RESULT();
  53092. }
  53093. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  53094. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  53095. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  53096. const char* cliCaPemFile, const char* cliCertPemFile,
  53097. const char* cliPrivKeyPemFile,
  53098. const char* svrCaPemFile, const char* svrCertPemFile,
  53099. const char* svrPrivKeyPemFile,
  53100. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  53101. {
  53102. EXPECT_DECLS;
  53103. callback_functions client_cbf;
  53104. callback_functions server_cbf;
  53105. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  53106. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  53107. if (tlsVer == WOLFSSL_TLSV1_3) {
  53108. #ifdef WOLFSSL_TLS13
  53109. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  53110. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  53111. #endif
  53112. }
  53113. else if (tlsVer == WOLFSSL_TLSV1_2) {
  53114. #ifndef WOLFSSL_NO_TLS12
  53115. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  53116. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  53117. #endif
  53118. }
  53119. else if (tlsVer == WOLFSSL_TLSV1_1) {
  53120. #ifndef NO_OLD_TLS
  53121. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  53122. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  53123. #endif
  53124. }
  53125. else if (tlsVer == WOLFSSL_TLSV1) {
  53126. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  53127. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  53128. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  53129. #endif
  53130. }
  53131. else if (tlsVer == WOLFSSL_SSLV3) {
  53132. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  53133. defined(WOLFSSL_STATIC_RSA)
  53134. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  53135. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  53136. #endif
  53137. }
  53138. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  53139. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  53140. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  53141. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  53142. #endif
  53143. }
  53144. else if (tlsVer == WOLFSSL_DTLSV1) {
  53145. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  53146. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  53147. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  53148. #endif
  53149. }
  53150. if (server_cbf.method_ex == NULL) {
  53151. /* not enabled */
  53152. return TEST_SUCCESS;
  53153. }
  53154. /* Setup the keys for the TLS test */
  53155. client_cbf.certPemFile = cliCertPemFile;
  53156. client_cbf.keyPemFile = cliPrivKeyPemFile;
  53157. client_cbf.caPemFile = cliCaPemFile;
  53158. server_cbf.certPemFile = svrCertPemFile;
  53159. server_cbf.keyPemFile = svrPrivKeyPemFile;
  53160. server_cbf.caPemFile = svrCaPemFile;
  53161. client_cbf.mem = cliMem;
  53162. client_cbf.memSz = cliMemSz;
  53163. server_cbf.mem = svrMem;
  53164. server_cbf.memSz = svrMemSz;
  53165. client_cbf.devId = INVALID_DEVID;
  53166. server_cbf.devId = INVALID_DEVID;
  53167. /* Perform TLS server and client test */
  53168. /* First test is at WOLFSSL_CTX level */
  53169. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53170. /* Check for success */
  53171. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53172. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53173. if (EXPECT_SUCCESS()) {
  53174. /* Second test is a WOLFSSL object level */
  53175. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  53176. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  53177. }
  53178. /* Check for success */
  53179. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  53180. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  53181. return EXPECT_RESULT();
  53182. }
  53183. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  53184. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  53185. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  53186. {
  53187. EXPECT_DECLS;
  53188. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  53189. WOLFSSL_MEM_STATS mem_stats;
  53190. WOLFSSL_MEM_CONN_STATS ssl_stats;
  53191. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  53192. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  53193. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53194. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  53195. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  53196. #endif
  53197. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  53198. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  53199. /* this should fail because kMaxCtxClients == 2 */
  53200. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  53201. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  53202. #ifdef DEBUG_WOLFSSL
  53203. wolfSSL_PrintStatsConn(&ssl_stats);
  53204. #endif
  53205. (void)ssl_stats;
  53206. }
  53207. /* display collected statistics */
  53208. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  53209. #ifdef DEBUG_WOLFSSL
  53210. wolfSSL_PrintStats(&mem_stats);
  53211. #endif
  53212. (void)mem_stats;
  53213. }
  53214. wolfSSL_free(ssl1);
  53215. wolfSSL_free(ssl2);
  53216. return EXPECT_RESULT();
  53217. }
  53218. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  53219. static int test_wolfSSL_CTX_StaticMemory(void)
  53220. {
  53221. EXPECT_DECLS;
  53222. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  53223. wolfSSL_method_func method_func;
  53224. WOLFSSL_CTX* ctx;
  53225. const int kMaxCtxClients = 2;
  53226. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53227. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  53228. int tlsVer;
  53229. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  53230. #endif
  53231. #endif
  53232. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  53233. #ifndef NO_WOLFSSL_SERVER
  53234. #ifndef WOLFSSL_NO_TLS12
  53235. method_func = wolfTLSv1_2_server_method_ex;
  53236. #else
  53237. method_func = wolfTLSv1_3_server_method_ex;
  53238. #endif
  53239. #else
  53240. #ifndef WOLFSSL_NO_TLS12
  53241. method_func = wolfTLSv1_2_client_method_ex;
  53242. #else
  53243. method_func = wolfTLSv1_3_client_method_ex;
  53244. #endif
  53245. #endif
  53246. /* Test creating CTX directly from static memory pool */
  53247. ctx = NULL;
  53248. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  53249. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  53250. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  53251. wolfSSL_CTX_free(ctx);
  53252. ctx = NULL;
  53253. /* Test for heap allocated CTX, then assigning static pool to it */
  53254. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  53255. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  53256. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  53257. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  53258. wolfSSL_CTX_free(ctx);
  53259. /* TLS Level Tests using static memory */
  53260. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  53261. #ifndef NO_RSA
  53262. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53263. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53264. svrCertFile, cliCertFile, cliKeyFile,
  53265. cliCertFile, svrCertFile, svrKeyFile,
  53266. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53267. TEST_SUCCESS);
  53268. }
  53269. #endif
  53270. #ifdef HAVE_ECC
  53271. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  53272. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53273. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  53274. cliEccCertFile, eccCertFile, eccKeyFile,
  53275. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53276. TEST_SUCCESS);
  53277. }
  53278. #endif
  53279. #ifdef HAVE_ED25519
  53280. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  53281. if (tlsVer == WOLFSSL_DTLSV1) continue;
  53282. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  53283. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  53284. cliEdCertFile, edCertFile, edKeyFile,
  53285. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  53286. TEST_SUCCESS);
  53287. }
  53288. #endif
  53289. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  53290. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  53291. return EXPECT_RESULT();
  53292. }
  53293. static int test_openssl_FIPS_drbg(void)
  53294. {
  53295. EXPECT_DECLS;
  53296. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  53297. DRBG_CTX* dctx = NULL;
  53298. byte data1[32], data2[32], zeroData[32];
  53299. byte testSeed[16];
  53300. size_t dlen = sizeof(data1);
  53301. int i;
  53302. XMEMSET(data1, 0, dlen);
  53303. XMEMSET(data2, 0, dlen);
  53304. XMEMSET(zeroData, 0, sizeof(zeroData));
  53305. for (i = 0; i < (int)sizeof(testSeed); i++) {
  53306. testSeed[i] = (byte)i;
  53307. }
  53308. ExpectNotNull(dctx = FIPS_get_default_drbg());
  53309. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  53310. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  53311. WOLFSSL_SUCCESS);
  53312. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  53313. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  53314. WOLFSSL_SUCCESS);
  53315. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  53316. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  53317. WOLFSSL_SUCCESS);
  53318. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  53319. WOLFSSL_SUCCESS);
  53320. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  53321. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  53322. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  53323. #ifndef HAVE_GLOBAL_RNG
  53324. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  53325. wolfSSL_FIPS_drbg_free(dctx);
  53326. #endif
  53327. #endif
  53328. return EXPECT_RESULT();
  53329. }
  53330. static int test_wolfSSL_FIPS_mode(void)
  53331. {
  53332. EXPECT_DECLS;
  53333. #if defined(OPENSSL_ALL)
  53334. #ifdef HAVE_FIPS
  53335. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  53336. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  53337. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  53338. #else
  53339. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  53340. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  53341. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  53342. #endif
  53343. #endif
  53344. return EXPECT_RESULT();
  53345. }
  53346. #ifdef WOLFSSL_DTLS
  53347. /* Prints out the current window */
  53348. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  53349. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  53350. int i;
  53351. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  53352. word32 b = w[i];
  53353. int j;
  53354. /* Prints out a 32 bit binary number in big endian order */
  53355. for (j = 0; j < 32; j++, b <<= 1) {
  53356. if (b & (((word32)1) << 31))
  53357. fprintf(stderr, "1");
  53358. else
  53359. fprintf(stderr, "0");
  53360. }
  53361. fprintf(stderr, " ");
  53362. }
  53363. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  53364. #else
  53365. (void)h;
  53366. (void)l;
  53367. (void)w;
  53368. #endif
  53369. }
  53370. /* a - cur_hi
  53371. * b - cur_lo
  53372. * c - next_hi
  53373. * d - next_lo
  53374. * e - window
  53375. * f - expected next_hi
  53376. * g - expected next_lo
  53377. * h - expected window[1]
  53378. * i - expected window[0]
  53379. */
  53380. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  53381. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  53382. DUW_TEST_print_window_binary((a), (b), (e)); \
  53383. ExpectIntEQ((c), (f)); \
  53384. ExpectIntEQ((d), (g)); \
  53385. ExpectIntEQ((e)[1], (h)); \
  53386. ExpectIntEQ((e)[0], (i)); \
  53387. } while (0)
  53388. static int test_wolfSSL_DtlsUpdateWindow(void)
  53389. {
  53390. EXPECT_DECLS;
  53391. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  53392. word32 next_lo = 0;
  53393. word16 next_hi = 0;
  53394. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  53395. fprintf(stderr, "\n");
  53396. #endif
  53397. XMEMSET(window, 0, sizeof window);
  53398. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  53399. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  53400. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  53401. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  53402. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  53403. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  53404. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  53405. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  53406. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  53407. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  53408. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  53409. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  53410. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  53411. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  53412. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  53413. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  53414. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  53415. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  53416. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  53417. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  53418. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  53419. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  53420. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  53421. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  53422. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  53423. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  53424. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  53425. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53426. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53427. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  53428. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  53429. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  53430. return EXPECT_RESULT();
  53431. }
  53432. #endif /* WOLFSSL_DTLS */
  53433. #ifdef WOLFSSL_DTLS
  53434. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  53435. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  53436. {
  53437. DtlsMsg* cur;
  53438. static byte msg[100];
  53439. static byte msgInit = 0;
  53440. if (!msgInit) {
  53441. int i;
  53442. for (i = 0; i < 100; i++)
  53443. msg[i] = i + 1;
  53444. msgInit = 1;
  53445. }
  53446. /* Sanitize test parameters */
  53447. if (len > sizeof(msg))
  53448. return -1;
  53449. if (f_offset + f_len > sizeof(msg))
  53450. return -1;
  53451. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  53452. if (ssl->dtls_rx_msg_list == NULL)
  53453. return -100;
  53454. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  53455. return -200;
  53456. if (cur->fragBucketListCount != f_count)
  53457. return -300;
  53458. if (cur->ready != ready)
  53459. return -400;
  53460. if (cur->bytesReceived != bytesReceived)
  53461. return -500;
  53462. if (ready) {
  53463. if (cur->fragBucketList != NULL)
  53464. return -600;
  53465. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  53466. return -700;
  53467. }
  53468. else {
  53469. DtlsFragBucket* fb;
  53470. if (cur->fragBucketList == NULL)
  53471. return -800;
  53472. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  53473. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  53474. return -900;
  53475. }
  53476. }
  53477. return 0;
  53478. }
  53479. static int test_wolfSSL_DTLS_fragment_buckets(void)
  53480. {
  53481. EXPECT_DECLS;
  53482. WOLFSSL ssl[1];
  53483. XMEMSET(ssl, 0, sizeof(*ssl));
  53484. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  53485. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53486. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  53487. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  53488. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  53489. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  53490. /* Test all permutations of 3 regions */
  53491. /* 1 2 3 */
  53492. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53493. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  53494. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  53495. /* 1 3 2 */
  53496. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53497. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  53498. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  53499. /* 2 1 3 */
  53500. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  53501. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  53502. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  53503. /* 2 3 1 */
  53504. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  53505. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  53506. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  53507. /* 3 1 2 */
  53508. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  53509. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  53510. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  53511. /* 3 2 1 */
  53512. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  53513. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  53514. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  53515. /* Test overlapping regions */
  53516. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  53517. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  53518. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  53519. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  53520. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  53521. /* Test overlapping multiple regions */
  53522. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53523. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  53524. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  53525. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  53526. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  53527. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  53528. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  53529. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  53530. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  53531. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  53532. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  53533. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  53534. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  53535. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  53536. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  53537. ssl->dtls_rx_msg_list = NULL;
  53538. ssl->dtls_rx_msg_list_sz = 0;
  53539. return EXPECT_RESULT();
  53540. }
  53541. #endif
  53542. #if !defined(NO_FILESYSTEM) && \
  53543. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53544. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53545. static int test_wolfSSL_dtls_stateless2(void)
  53546. {
  53547. EXPECT_DECLS;
  53548. WOLFSSL *ssl_c = NULL;
  53549. WOLFSSL *ssl_c2 = NULL;
  53550. WOLFSSL *ssl_s = NULL;
  53551. struct test_memio_ctx test_ctx;
  53552. WOLFSSL_CTX *ctx_c = NULL;
  53553. WOLFSSL_CTX *ctx_s = NULL;
  53554. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53555. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53556. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53557. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  53558. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53559. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53560. /* send CH */
  53561. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53562. (ssl_c2->error == WANT_READ));
  53563. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53564. (ssl_s->error == WANT_READ));
  53565. ExpectIntNE(test_ctx.c_len, 0);
  53566. /* consume HRR */
  53567. test_ctx.c_len = 0;
  53568. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53569. wolfSSL_free(ssl_c2);
  53570. wolfSSL_free(ssl_c);
  53571. wolfSSL_free(ssl_s);
  53572. wolfSSL_CTX_free(ctx_c);
  53573. wolfSSL_CTX_free(ctx_s);
  53574. return EXPECT_RESULT();
  53575. }
  53576. #ifdef HAVE_MAX_FRAGMENT
  53577. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  53578. {
  53579. EXPECT_DECLS;
  53580. WOLFSSL *ssl_c = NULL;
  53581. WOLFSSL *ssl_c2 = NULL;
  53582. WOLFSSL *ssl_s = NULL;
  53583. struct test_memio_ctx test_ctx;
  53584. WOLFSSL_CTX *ctx_c = NULL;
  53585. WOLFSSL_CTX *ctx_s = NULL;
  53586. word16 max_fragment = 0;
  53587. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53588. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53589. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53590. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  53591. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  53592. WOLFSSL_SUCCESS);
  53593. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53594. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53595. if (ssl_s != NULL) {
  53596. max_fragment = ssl_s->max_fragment;
  53597. }
  53598. /* send CH */
  53599. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53600. (ssl_c2->error == WANT_READ));
  53601. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53602. (ssl_s->error == WANT_READ));
  53603. /* CH without cookie shouldn't change state */
  53604. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  53605. ExpectIntNE(test_ctx.c_len, 0);
  53606. /* consume HRR from buffer */
  53607. test_ctx.c_len = 0;
  53608. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53609. wolfSSL_free(ssl_c2);
  53610. wolfSSL_free(ssl_c);
  53611. wolfSSL_free(ssl_s);
  53612. wolfSSL_CTX_free(ctx_c);
  53613. wolfSSL_CTX_free(ctx_s);
  53614. return EXPECT_RESULT();
  53615. }
  53616. #endif /* HAVE_MAX_FRAGMENT */
  53617. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  53618. #define ROUNDS_WITH_HVR 4
  53619. #define ROUNDS_WITHOUT_HVR 2
  53620. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  53621. static int buf_is_hvr(const byte *data, int len)
  53622. {
  53623. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  53624. return 0;
  53625. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  53626. }
  53627. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  53628. {
  53629. EXPECT_DECLS;
  53630. struct test_memio_ctx test_ctx;
  53631. WOLFSSL_CTX *ctx_c = NULL;
  53632. WOLFSSL_CTX *ctx_s = NULL;
  53633. WOLFSSL *ssl_c = NULL;
  53634. WOLFSSL *ssl_s = NULL;
  53635. WOLFSSL_SESSION *sess = NULL;
  53636. int round_trips;
  53637. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53638. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  53639. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53640. #ifdef HAVE_SESSION_TICKET
  53641. if (useticket) {
  53642. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  53643. }
  53644. #endif
  53645. round_trips = ROUNDS_WITH_HVR;
  53646. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  53647. &round_trips), 0);
  53648. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  53649. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  53650. wolfSSL_shutdown(ssl_c);
  53651. wolfSSL_shutdown(ssl_s);
  53652. wolfSSL_free(ssl_c);
  53653. ssl_c = NULL;
  53654. wolfSSL_free(ssl_s);
  53655. ssl_s = NULL;
  53656. test_ctx.c_len = test_ctx.s_len = 0;
  53657. /* make resumption invalid */
  53658. if (bad && (sess != NULL)) {
  53659. if (useticket) {
  53660. #ifdef HAVE_SESSION_TICKET
  53661. if (sess->ticket != NULL) {
  53662. sess->ticket[0] = !sess->ticket[0];
  53663. }
  53664. #endif /* HAVE_SESSION_TICKET */
  53665. }
  53666. else {
  53667. sess->sessionID[0] = !sess->sessionID[0];
  53668. }
  53669. }
  53670. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  53671. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  53672. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  53673. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  53674. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  53675. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  53676. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  53677. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53678. (ssl_c->error == WANT_READ));
  53679. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53680. (ssl_s->error == WANT_READ));
  53681. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  53682. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  53683. if (!useticket) {
  53684. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  53685. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  53686. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  53687. }
  53688. wolfSSL_SESSION_free(sess);
  53689. wolfSSL_free(ssl_c);
  53690. wolfSSL_free(ssl_s);
  53691. wolfSSL_CTX_free(ctx_c);
  53692. wolfSSL_CTX_free(ctx_s);
  53693. return EXPECT_RESULT();
  53694. }
  53695. static int test_wolfSSL_dtls_stateless_resume(void)
  53696. {
  53697. EXPECT_DECLS;
  53698. #ifdef HAVE_SESSION_TICKET
  53699. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  53700. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  53701. #endif /* HAVE_SESION_TICKET */
  53702. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  53703. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  53704. return EXPECT_RESULT();
  53705. }
  53706. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  53707. #if !defined(NO_OLD_TLS)
  53708. static int test_wolfSSL_dtls_stateless_downgrade(void)
  53709. {
  53710. EXPECT_DECLS;
  53711. WOLFSSL_CTX *ctx_c = NULL;
  53712. WOLFSSL_CTX *ctx_c2 = NULL;
  53713. WOLFSSL_CTX *ctx_s = NULL;
  53714. WOLFSSL *ssl_c = NULL;
  53715. WOLFSSL *ssl_c2 = NULL;
  53716. WOLFSSL *ssl_s = NULL;
  53717. struct test_memio_ctx test_ctx;
  53718. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53719. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53720. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  53721. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  53722. WOLFSSL_SUCCESS);
  53723. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  53724. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  53725. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  53726. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  53727. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  53728. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  53729. /* send CH */
  53730. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  53731. (ssl_c2->error == WANT_READ));
  53732. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53733. (ssl_s->error == WANT_READ));
  53734. ExpectIntNE(test_ctx.c_len, 0);
  53735. /* consume HRR */
  53736. test_ctx.c_len = 0;
  53737. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53738. wolfSSL_free(ssl_c2);
  53739. wolfSSL_free(ssl_c);
  53740. wolfSSL_free(ssl_s);
  53741. wolfSSL_CTX_free(ctx_c);
  53742. wolfSSL_CTX_free(ctx_c2);
  53743. wolfSSL_CTX_free(ctx_s);
  53744. return EXPECT_RESULT();
  53745. }
  53746. #endif /* !defined(NO_OLD_TLS) */
  53747. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53748. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  53749. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  53750. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53751. static int test_WOLFSSL_dtls_version_alert(void)
  53752. {
  53753. EXPECT_DECLS;
  53754. struct test_memio_ctx test_ctx;
  53755. WOLFSSL_CTX *ctx_c = NULL;
  53756. WOLFSSL_CTX *ctx_s = NULL;
  53757. WOLFSSL *ssl_c = NULL;
  53758. WOLFSSL *ssl_s = NULL;
  53759. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53760. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53761. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  53762. /* client hello */
  53763. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53764. (ssl_c->error == WANT_READ));
  53765. /* hrr */
  53766. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53767. (ssl_s->error == WANT_READ));
  53768. /* client hello 1 */
  53769. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53770. (ssl_c->error == WANT_READ));
  53771. /* server hello */
  53772. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53773. (ssl_s->error == WANT_READ));
  53774. /* should fail */
  53775. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  53776. (ssl_c->error == VERSION_ERROR));
  53777. /* shuould fail */
  53778. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  53779. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  53780. wolfSSL_free(ssl_c);
  53781. wolfSSL_free(ssl_s);
  53782. wolfSSL_CTX_free(ctx_c);
  53783. wolfSSL_CTX_free(ctx_s);
  53784. return EXPECT_RESULT();
  53785. }
  53786. #else
  53787. static int test_WOLFSSL_dtls_version_alert(void)
  53788. {
  53789. return TEST_SKIPPED;
  53790. }
  53791. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  53792. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  53793. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  53794. */
  53795. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  53796. && defined(WOLFSSL_TLS13) && \
  53797. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  53798. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53799. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  53800. {
  53801. struct test_memio_ctx *test_ctx;
  53802. byte buf[2048];
  53803. int idx, sz;
  53804. word32 tmp;
  53805. int ret;
  53806. idx = 5; /* space for record header */
  53807. buf[idx] = session_ticket; /* type */
  53808. idx++;
  53809. tmp = OPAQUE32_LEN +
  53810. OPAQUE32_LEN +
  53811. OPAQUE8_LEN + nonceLength +
  53812. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  53813. c32to24(tmp, buf + idx);
  53814. idx += OPAQUE24_LEN;
  53815. c32toa((word32)12345, buf+idx); /* lifetime */
  53816. idx += OPAQUE32_LEN;
  53817. c32toa((word32)12345, buf+idx); /* add */
  53818. idx += OPAQUE32_LEN;
  53819. buf[idx] = nonceLength; /* nonce length */
  53820. idx++;
  53821. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  53822. idx += nonceLength;
  53823. tmp = 1; /* ticket len */
  53824. c16toa((word16)tmp, buf+idx);
  53825. idx += 2;
  53826. buf[idx] = 0xFF; /* ticket */
  53827. idx++;
  53828. tmp = 0; /* ext len */
  53829. c16toa((word16)tmp, buf+idx);
  53830. idx += 2;
  53831. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  53832. handshake, 0, 0, 0);
  53833. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  53834. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  53835. return !(ret == sz);
  53836. }
  53837. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  53838. {
  53839. int ret = 0;
  53840. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  53841. ret = -1;
  53842. }
  53843. else {
  53844. int i;
  53845. for (i = 0; i < len; i++) {
  53846. if (sess->ticketNonce.data[i] != len) {
  53847. ret = -1;
  53848. break;
  53849. }
  53850. }
  53851. }
  53852. return ret;
  53853. }
  53854. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  53855. {
  53856. EXPECT_DECLS;
  53857. char *buf[1024];
  53858. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  53859. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  53860. (ssl_c->error == WANT_READ));
  53861. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  53862. return EXPECT_RESULT();
  53863. }
  53864. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  53865. {
  53866. EXPECT_DECLS;
  53867. WOLFSSL_SESSION *sess = NULL;
  53868. WOLFSSL_SESSION *cached = NULL;
  53869. WOLFSSL_CTX *ctx = ssl_c->ctx;
  53870. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  53871. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  53872. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  53873. NULL, ssl_c->options.side, 1,NULL), 0);
  53874. ExpectNotNull(cached = wolfSSL_SESSION_new());
  53875. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  53876. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  53877. wolfSSL_SESSION_free(cached);
  53878. wolfSSL_SESSION_free(sess);
  53879. return EXPECT_RESULT();
  53880. }
  53881. static int test_ticket_nonce_malloc(void)
  53882. {
  53883. EXPECT_DECLS;
  53884. struct test_memio_ctx test_ctx;
  53885. WOLFSSL_CTX *ctx_c = NULL;
  53886. WOLFSSL_CTX *ctx_s = NULL;
  53887. WOLFSSL *ssl_c = NULL;
  53888. WOLFSSL *ssl_s = NULL;
  53889. byte small;
  53890. byte medium;
  53891. byte big;
  53892. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53893. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53894. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  53895. /* will send ticket manually */
  53896. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  53897. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  53898. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  53899. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  53900. (ssl_s->options.handShakeDone == 0)) {
  53901. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  53902. (ssl_c->error == WANT_READ));
  53903. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  53904. (ssl_s->error == WANT_READ));
  53905. }
  53906. small = TLS13_TICKET_NONCE_STATIC_SZ;
  53907. medium = small + 20 <= 255 ? small + 20 : 255;
  53908. big = medium + 20 <= 255 ? small + 20 : 255;
  53909. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  53910. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  53911. ssl_c->session->ticketNonce.dataStatic);
  53912. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  53913. TEST_SUCCESS);
  53914. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  53915. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  53916. TEST_SUCCESS);
  53917. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  53918. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  53919. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  53920. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  53921. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  53922. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  53923. wolfSSL_free(ssl_c);
  53924. wolfSSL_free(ssl_s);
  53925. wolfSSL_CTX_free(ctx_c);
  53926. wolfSSL_CTX_free(ctx_s);
  53927. return EXPECT_RESULT();
  53928. }
  53929. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  53930. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  53931. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  53932. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  53933. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  53934. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  53935. static int test_ticket_ret_create(void)
  53936. {
  53937. EXPECT_DECLS;
  53938. WOLFSSL_CTX *ctx_c = NULL;
  53939. WOLFSSL_CTX *ctx_s = NULL;
  53940. WOLFSSL *ssl_c = NULL;
  53941. WOLFSSL *ssl_s = NULL;
  53942. byte ticket[SESSION_TICKET_LEN];
  53943. struct test_memio_ctx test_ctx;
  53944. WOLFSSL_SESSION *sess = NULL;
  53945. word16 ticketLen = 0;
  53946. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  53947. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  53948. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  53949. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  53950. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  53951. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  53952. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53953. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  53954. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  53955. if (sess != NULL) {
  53956. ticketLen = sess->ticketLen;
  53957. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  53958. }
  53959. wolfSSL_free(ssl_c);
  53960. ssl_c = NULL;
  53961. wolfSSL_free(ssl_s);
  53962. ssl_s = NULL;
  53963. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  53964. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  53965. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  53966. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  53967. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  53968. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  53969. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  53970. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  53971. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  53972. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  53973. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  53974. wolfSSL_SESSION_free(sess);
  53975. wolfSSL_free(ssl_c);
  53976. wolfSSL_free(ssl_s);
  53977. wolfSSL_CTX_free(ctx_c);
  53978. wolfSSL_CTX_free(ctx_s);
  53979. return EXPECT_RESULT();
  53980. }
  53981. #else
  53982. static int test_ticket_ret_create(void)
  53983. {
  53984. return TEST_SKIPPED;
  53985. }
  53986. #endif
  53987. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  53988. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  53989. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  53990. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  53991. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  53992. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  53993. {
  53994. int ret;
  53995. WOLFSSL_SESSION* session = NULL;
  53996. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  53997. if (!wolfSSL_is_server(ssl)) {
  53998. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  53999. AssertNotNull(session);
  54000. }
  54001. do {
  54002. ret = wolfSSL_shutdown(ssl);
  54003. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  54004. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  54005. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  54006. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  54007. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  54008. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  54009. #endif
  54010. if (!wolfSSL_is_server(ssl)) {
  54011. /* client */
  54012. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54013. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54014. wolfSSL_set_session(ssl, session);
  54015. wolfSSL_SESSION_free(session);
  54016. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  54017. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  54018. }
  54019. else {
  54020. /* server */
  54021. /* Different ciphersuite so that the ticket will be invalidated based on
  54022. * the ciphersuite */
  54023. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  54024. WOLFSSL_SUCCESS);
  54025. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  54026. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  54027. }
  54028. }
  54029. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  54030. {
  54031. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  54032. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54033. WOLFSSL_SUCCESS);
  54034. }
  54035. static int test_ticket_and_psk_mixing(void)
  54036. {
  54037. EXPECT_DECLS;
  54038. /* Test mixing tickets and regular PSK */
  54039. callback_functions client_cbs, server_cbs;
  54040. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54041. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54042. client_cbs.method = wolfTLSv1_3_client_method;
  54043. server_cbs.method = wolfTLSv1_3_server_method;
  54044. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  54045. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  54046. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  54047. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54048. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  54049. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  54050. return EXPECT_RESULT();
  54051. }
  54052. #else
  54053. static int test_ticket_and_psk_mixing(void)
  54054. {
  54055. return TEST_SKIPPED;
  54056. }
  54057. #endif
  54058. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  54059. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  54060. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54061. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  54062. static int test_prioritize_psk_cb_called = FALSE;
  54063. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  54064. const char* identity, unsigned char* key, unsigned int key_max_len,
  54065. const char** ciphersuite)
  54066. {
  54067. test_prioritize_psk_cb_called = TRUE;
  54068. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  54069. }
  54070. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  54071. {
  54072. int ret;
  54073. WOLFSSL_SESSION* session = NULL;
  54074. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  54075. if (!wolfSSL_is_server(ssl)) {
  54076. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  54077. AssertNotNull(session);
  54078. }
  54079. do {
  54080. ret = wolfSSL_shutdown(ssl);
  54081. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  54082. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  54083. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  54084. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  54085. * important. */
  54086. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54087. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54088. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  54089. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  54090. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  54091. #endif
  54092. if (!wolfSSL_is_server(ssl)) {
  54093. /* client */
  54094. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  54095. wolfSSL_set_session(ssl, session);
  54096. wolfSSL_SESSION_free(session);
  54097. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  54098. }
  54099. else {
  54100. /* server */
  54101. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  54102. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  54103. #ifdef WOLFSSL_PRIORITIZE_PSK
  54104. /* The ticket should be first tried with all ciphersuites and chosen */
  54105. AssertFalse(test_prioritize_psk_cb_called);
  54106. #else
  54107. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  54108. * callback that sets this var. */
  54109. AssertTrue(test_prioritize_psk_cb_called);
  54110. #endif
  54111. }
  54112. }
  54113. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  54114. {
  54115. if (!wolfSSL_is_server(ssl))
  54116. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54117. WOLFSSL_SUCCESS);
  54118. else
  54119. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54120. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  54121. }
  54122. static int test_prioritize_psk(void)
  54123. {
  54124. EXPECT_DECLS;
  54125. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  54126. * of the PSK's will be followed instead of the ciphersuite. */
  54127. callback_functions client_cbs, server_cbs;
  54128. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54129. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54130. client_cbs.method = wolfTLSv1_3_client_method;
  54131. server_cbs.method = wolfTLSv1_3_server_method;
  54132. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  54133. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  54134. client_cbs.on_result = test_prioritize_psk_on_result;
  54135. server_cbs.on_result = test_prioritize_psk_on_result;
  54136. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54137. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  54138. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  54139. return EXPECT_RESULT();
  54140. }
  54141. #else
  54142. static int test_prioritize_psk(void)
  54143. {
  54144. return TEST_SKIPPED;
  54145. }
  54146. #endif
  54147. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  54148. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  54149. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54150. !defined(WOLFSSL_NO_TLS12)
  54151. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  54152. {
  54153. EXPECT_DECLS;
  54154. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  54155. /* Set TLS 1.3 specific suite */
  54156. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  54157. return EXPECT_RESULT();
  54158. }
  54159. static int test_wolfSSL_CTX_set_ciphersuites(void)
  54160. {
  54161. EXPECT_DECLS;
  54162. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  54163. * do a 1.2 connection. */
  54164. test_ssl_cbf client_cbs, server_cbs;
  54165. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54166. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54167. client_cbs.method = wolfTLSv1_2_client_method;
  54168. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  54169. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  54170. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54171. &server_cbs, NULL), TEST_SUCCESS);
  54172. return EXPECT_RESULT();
  54173. }
  54174. #else
  54175. static int test_wolfSSL_CTX_set_ciphersuites(void)
  54176. {
  54177. return TEST_SKIPPED;
  54178. }
  54179. #endif
  54180. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  54181. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54182. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  54183. {
  54184. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54185. return TEST_SUCCESS;
  54186. }
  54187. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  54188. {
  54189. EXPECT_DECLS;
  54190. WOLFSSL_ALERT_HISTORY h;
  54191. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  54192. ExpectIntEQ(h.last_rx.level, alert_fatal);
  54193. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  54194. return EXPECT_RESULT();
  54195. }
  54196. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  54197. {
  54198. EXPECT_DECLS;
  54199. test_ssl_cbf client_cbs, server_cbs;
  54200. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54201. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54202. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  54203. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  54204. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  54205. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  54206. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  54207. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54208. &server_cbs, NULL), TEST_FAIL);
  54209. return EXPECT_RESULT();
  54210. }
  54211. #else
  54212. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  54213. {
  54214. return TEST_SKIPPED;
  54215. }
  54216. #endif
  54217. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  54218. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  54219. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  54220. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  54221. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  54222. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  54223. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  54224. static int test_TLS_13_ticket_different_ciphers_run = 0;
  54225. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  54226. {
  54227. EXPECT_DECLS;
  54228. switch (test_TLS_13_ticket_different_ciphers_run) {
  54229. case 0:
  54230. /* First run */
  54231. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  54232. WOLFSSL_SUCCESS);
  54233. if (wolfSSL_is_server(ssl)) {
  54234. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  54235. wolfSSL_get_SSL_CTX(ssl));
  54236. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  54237. test_TLS_13_ticket_different_ciphers_ctx));
  54238. }
  54239. break;
  54240. case 1:
  54241. /* Second run */
  54242. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  54243. "TLS13-AES128-GCM-SHA256"),
  54244. WOLFSSL_SUCCESS);
  54245. if (!wolfSSL_is_server(ssl)) {
  54246. ExpectIntEQ(wolfSSL_set_session(ssl,
  54247. test_TLS_13_ticket_different_ciphers_session),
  54248. WOLFSSL_SUCCESS);
  54249. }
  54250. break;
  54251. default:
  54252. /* Bad state? */
  54253. Fail(("Should not enter here"), ("Should not enter here"));
  54254. }
  54255. return EXPECT_RESULT();
  54256. }
  54257. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  54258. {
  54259. EXPECT_DECLS;
  54260. switch (test_TLS_13_ticket_different_ciphers_run) {
  54261. case 0:
  54262. /* First run */
  54263. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  54264. wolfSSL_get1_session(ssl));
  54265. break;
  54266. case 1:
  54267. /* Second run */
  54268. ExpectTrue(wolfSSL_session_reused(ssl));
  54269. break;
  54270. default:
  54271. /* Bad state? */
  54272. Fail(("Should not enter here"), ("Should not enter here"));
  54273. }
  54274. return EXPECT_RESULT();
  54275. }
  54276. static int test_TLS_13_ticket_different_ciphers(void)
  54277. {
  54278. EXPECT_DECLS;
  54279. /* Check that we handle the connection when the ticket doesn't match
  54280. * the first ciphersuite. */
  54281. test_ssl_cbf client_cbs, server_cbs;
  54282. struct test_params {
  54283. method_provider client_meth;
  54284. method_provider server_meth;
  54285. int doUdp;
  54286. } params[] = {
  54287. #ifdef WOLFSSL_DTLS13
  54288. /* Test that the stateless code handles sessions correctly */
  54289. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  54290. #endif
  54291. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  54292. };
  54293. size_t i;
  54294. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54295. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54296. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54297. test_TLS_13_ticket_different_ciphers_run = 0;
  54298. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  54299. client_cbs.method = params[i].client_meth;
  54300. server_cbs.method = params[i].server_meth;
  54301. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  54302. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  54303. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  54304. server_cbs.ticNoInit = 1;
  54305. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54306. &server_cbs, NULL), TEST_SUCCESS);
  54307. test_TLS_13_ticket_different_ciphers_run++;
  54308. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  54309. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54310. &server_cbs, NULL), TEST_SUCCESS);
  54311. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  54312. test_TLS_13_ticket_different_ciphers_session = NULL;
  54313. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  54314. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  54315. }
  54316. return EXPECT_RESULT();
  54317. }
  54318. #else
  54319. static int test_TLS_13_ticket_different_ciphers(void)
  54320. {
  54321. return TEST_SKIPPED;
  54322. }
  54323. #endif
  54324. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  54325. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  54326. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  54327. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  54328. byte test_extra_alerts_wrong_cs_sh[] = {
  54329. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  54330. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  54331. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  54332. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  54333. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  54334. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  54335. 0xbb, 0x07, 0x54, 0x1c,
  54336. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  54337. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  54338. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  54339. };
  54340. static int test_extra_alerts_wrong_cs(void)
  54341. {
  54342. EXPECT_DECLS;
  54343. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  54344. struct test_memio_ctx test_ctx;
  54345. WOLFSSL_CTX *ctx_c = NULL;
  54346. WOLFSSL_ALERT_HISTORY h;
  54347. WOLFSSL *ssl_c = NULL;
  54348. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54349. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  54350. wolfTLSv1_2_client_method, NULL), 0);
  54351. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  54352. WOLFSSL_SUCCESS);
  54353. /* CH */
  54354. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54355. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54356. WOLFSSL_ERROR_WANT_READ);
  54357. /* consume CH */
  54358. test_ctx.s_len = 0;
  54359. /* inject SH */
  54360. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  54361. sizeof(test_extra_alerts_wrong_cs_sh));
  54362. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  54363. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54364. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54365. WOLFSSL_ERROR_WANT_READ);
  54366. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54367. ExpectIntEQ(h.last_tx.code, illegal_parameter);
  54368. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54369. wolfSSL_free(ssl_c);
  54370. wolfSSL_CTX_free(ctx_c);
  54371. #endif
  54372. return EXPECT_RESULT();
  54373. }
  54374. #else
  54375. static int test_extra_alerts_wrong_cs(void)
  54376. {
  54377. return TEST_SKIPPED;
  54378. }
  54379. #endif
  54380. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  54381. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  54382. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  54383. {
  54384. tail_len -= msg_length;
  54385. XMEMMOVE(msg, msg + msg_length, tail_len);
  54386. *len = *len - msg_length;
  54387. }
  54388. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  54389. byte *found)
  54390. {
  54391. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  54392. const unsigned int _RECORD_HEADER_SZ = 5;
  54393. const int _change_cipher_hs = 55;
  54394. const int _change_cipher = 20;
  54395. const int _handshake = 22;
  54396. unsigned int tail_len;
  54397. byte *idx, *curr;
  54398. word8 currType;
  54399. word16 rLength;
  54400. word32 hLength;
  54401. idx = buf;
  54402. tail_len = *len;
  54403. *found = 0;
  54404. while (tail_len > _RECORD_HEADER_SZ) {
  54405. curr = idx;
  54406. currType = *idx;
  54407. ato16(idx + 3, &rLength);
  54408. idx += _RECORD_HEADER_SZ;
  54409. tail_len -= _RECORD_HEADER_SZ;
  54410. if (tail_len < rLength)
  54411. return -1;
  54412. if (type == _change_cipher_hs && currType == _change_cipher) {
  54413. if (rLength != 1)
  54414. return -1;
  54415. /* match */
  54416. test_remove_msg(curr, *len - (int)(curr - buf),
  54417. len, _RECORD_HEADER_SZ + 1);
  54418. *found = 1;
  54419. return 0;
  54420. }
  54421. if (currType != _handshake) {
  54422. idx += rLength;
  54423. tail_len -= rLength;
  54424. continue;
  54425. }
  54426. if (rLength < _HANDSHAKE_HEADER_SZ)
  54427. return -1;
  54428. currType = *idx;
  54429. ato24(idx+1, &hLength);
  54430. hLength += _HANDSHAKE_HEADER_SZ;
  54431. if (tail_len < hLength)
  54432. return -1;
  54433. if (currType != type) {
  54434. idx += hLength;
  54435. tail_len -= hLength;
  54436. continue;
  54437. }
  54438. /* match */
  54439. test_remove_msg(curr, *len - (int)(curr - buf), len,
  54440. hLength + _RECORD_HEADER_SZ);
  54441. *found = 1;
  54442. return 0;
  54443. }
  54444. /* not found */
  54445. return 0;
  54446. }
  54447. static int test_remove_hs_message(byte hs_message_type,
  54448. int extra_round, byte alert_type)
  54449. {
  54450. EXPECT_DECLS;
  54451. WOLFSSL_CTX *ctx_c = NULL;
  54452. WOLFSSL_CTX *ctx_s = NULL;
  54453. WOLFSSL *ssl_c = NULL;
  54454. WOLFSSL *ssl_s = NULL;
  54455. struct test_memio_ctx test_ctx;
  54456. WOLFSSL_ALERT_HISTORY h;
  54457. byte found = 0;
  54458. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54459. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54460. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  54461. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54462. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54463. WOLFSSL_ERROR_WANT_READ);
  54464. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54465. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54466. WOLFSSL_ERROR_WANT_READ);
  54467. if (extra_round) {
  54468. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54469. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54470. WOLFSSL_ERROR_WANT_READ);
  54471. /* this will complete handshake from server side */
  54472. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54473. }
  54474. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  54475. &test_ctx.c_len, hs_message_type, &found), 0);
  54476. if (!found) {
  54477. wolfSSL_free(ssl_c);
  54478. wolfSSL_CTX_free(ctx_c);
  54479. wolfSSL_free(ssl_s);
  54480. wolfSSL_CTX_free(ctx_s);
  54481. return TEST_SKIPPED;
  54482. }
  54483. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54484. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54485. WOLFSSL_ERROR_WANT_READ);
  54486. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54487. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  54488. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54489. wolfSSL_free(ssl_c);
  54490. wolfSSL_CTX_free(ctx_c);
  54491. wolfSSL_free(ssl_s);
  54492. wolfSSL_CTX_free(ctx_s);
  54493. return EXPECT_RESULT();
  54494. }
  54495. static int test_extra_alerts_skip_hs(void)
  54496. {
  54497. EXPECT_DECLS;
  54498. const byte _server_key_exchange = 12;
  54499. const byte _server_hello = 2;
  54500. const byte _certificate = 11;
  54501. /* server_hello */
  54502. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  54503. unexpected_message), TEST_FAIL);
  54504. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  54505. 0xff), TEST_FAIL);
  54506. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  54507. unexpected_message), TEST_FAIL);
  54508. return EXPECT_RESULT();
  54509. }
  54510. #else
  54511. static int test_extra_alerts_skip_hs(void)
  54512. {
  54513. return TEST_SKIPPED;
  54514. }
  54515. #endif
  54516. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  54517. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  54518. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  54519. unsigned char* key, unsigned int key_max_len)
  54520. {
  54521. (void)ssl;
  54522. (void)id;
  54523. (void)key_max_len;
  54524. /* zero means error */
  54525. key[0] = 0x10;
  54526. return 1;
  54527. }
  54528. static int test_extra_alerts_bad_psk(void)
  54529. {
  54530. EXPECT_DECLS;
  54531. WOLFSSL_CTX *ctx_c = NULL;
  54532. WOLFSSL_CTX *ctx_s = NULL;
  54533. WOLFSSL *ssl_c = NULL;
  54534. WOLFSSL *ssl_s = NULL;
  54535. struct test_memio_ctx test_ctx;
  54536. WOLFSSL_ALERT_HISTORY h;
  54537. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54538. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54539. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  54540. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  54541. WOLFSSL_SUCCESS);
  54542. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  54543. WOLFSSL_SUCCESS);
  54544. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  54545. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54546. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54547. WOLFSSL_ERROR_WANT_READ);
  54548. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54549. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  54550. WOLFSSL_ERROR_WANT_READ);
  54551. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54552. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54553. WOLFSSL_ERROR_WANT_READ);
  54554. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  54555. ExpectIntEQ(h.last_tx.code, handshake_failure);
  54556. ExpectIntEQ(h.last_tx.level, alert_fatal);
  54557. wolfSSL_free(ssl_c);
  54558. wolfSSL_CTX_free(ctx_c);
  54559. wolfSSL_free(ssl_s);
  54560. wolfSSL_CTX_free(ctx_s);
  54561. return EXPECT_RESULT();
  54562. }
  54563. #else
  54564. static int test_extra_alerts_bad_psk(void)
  54565. {
  54566. return TEST_SKIPPED;
  54567. }
  54568. #endif
  54569. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  54570. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54571. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  54572. int sz, void *ctx)
  54573. {
  54574. static int sentServerHello = FALSE;
  54575. if (!sentServerHello) {
  54576. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  54577. size_t i;
  54578. if (sz < (int)sizeof(renegExt))
  54579. return WOLFSSL_CBIO_ERR_GENERAL;
  54580. /* Remove SCR from ServerHello */
  54581. for (i = 0; i < sz - sizeof(renegExt); i++) {
  54582. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  54583. /* Found the extension. Change it to something unrecognized. */
  54584. buf[i+1] = 0x11;
  54585. break;
  54586. }
  54587. }
  54588. sentServerHello = TRUE;
  54589. }
  54590. return EmbedSend(ssl, buf, sz, ctx);
  54591. }
  54592. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  54593. {
  54594. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  54595. }
  54596. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  54597. {
  54598. WOLFSSL_ALERT_HISTORY h;
  54599. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  54600. AssertIntEQ(h.last_rx.code, handshake_failure);
  54601. AssertIntEQ(h.last_rx.level, alert_fatal);
  54602. }
  54603. static int test_harden_no_secure_renegotiation(void)
  54604. {
  54605. EXPECT_DECLS;
  54606. callback_functions client_cbs, server_cbs;
  54607. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54608. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54609. client_cbs.method = wolfTLSv1_2_client_method;
  54610. server_cbs.method = wolfTLSv1_2_server_method;
  54611. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  54612. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  54613. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  54614. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  54615. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  54616. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  54617. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  54618. server_cbs.last_err == FATAL_ERROR);
  54619. return EXPECT_RESULT();
  54620. }
  54621. #else
  54622. static int test_harden_no_secure_renegotiation(void)
  54623. {
  54624. return TEST_SKIPPED;
  54625. }
  54626. #endif
  54627. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  54628. static int test_override_alt_cert_chain_cert_cb(int preverify,
  54629. WOLFSSL_X509_STORE_CTX* store)
  54630. {
  54631. fprintf(stderr, "preverify: %d\n", preverify);
  54632. fprintf(stderr, "store->error: %d\n", store->error);
  54633. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  54634. if (store->error == OCSP_INVALID_STATUS) {
  54635. fprintf(stderr, "Overriding OCSP error\n");
  54636. return 1;
  54637. }
  54638. #ifndef WOLFSSL_ALT_CERT_CHAINS
  54639. else if ((store->error == ASN_NO_SIGNER_E ||
  54640. store->error == ASN_SELF_SIGNED_E
  54641. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  54642. defined(HAVE_WEBSERVER)
  54643. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  54644. #endif
  54645. ) && store->error_depth == store->totalCerts - 1) {
  54646. fprintf(stderr, "Overriding no signer error only for root cert\n");
  54647. return 1;
  54648. }
  54649. #endif
  54650. else
  54651. return preverify;
  54652. }
  54653. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  54654. int urlSz, unsigned char* request, int requestSz,
  54655. unsigned char** response)
  54656. {
  54657. (void)ioCtx;
  54658. (void)url;
  54659. (void)urlSz;
  54660. (void)request;
  54661. (void)requestSz;
  54662. (void)response;
  54663. return -1;
  54664. }
  54665. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  54666. {
  54667. EXPECT_DECLS;
  54668. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  54669. test_override_alt_cert_chain_cert_cb);
  54670. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  54671. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  54672. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  54673. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  54674. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  54675. WOLFSSL_SUCCESS);
  54676. return EXPECT_RESULT();
  54677. }
  54678. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  54679. {
  54680. EXPECT_DECLS;
  54681. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  54682. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  54683. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  54684. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  54685. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  54686. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  54687. WOLFSSL_SUCCESS);
  54688. return EXPECT_RESULT();
  54689. }
  54690. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  54691. {
  54692. EXPECT_DECLS;
  54693. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  54694. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  54695. return EXPECT_RESULT();
  54696. }
  54697. static int test_override_alt_cert_chain(void)
  54698. {
  54699. EXPECT_DECLS;
  54700. size_t i;
  54701. struct test_params {
  54702. ctx_cb client_ctx_cb;
  54703. ctx_cb server_ctx_cb;
  54704. int result;
  54705. } params[] = {
  54706. {test_override_alt_cert_chain_client_ctx_ready,
  54707. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  54708. {test_override_alt_cert_chain_client_ctx_ready2,
  54709. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  54710. };
  54711. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  54712. test_ssl_cbf client_cbs, server_cbs;
  54713. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  54714. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  54715. fprintf(stderr, "test config: %d\n", (int)i);
  54716. client_cbs.ctx_ready = params[i].client_ctx_cb;
  54717. server_cbs.ctx_ready = params[i].server_ctx_cb;
  54718. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  54719. &server_cbs, NULL), params[i].result);
  54720. ExpectIntEQ(client_cbs.return_code, params[i].result);
  54721. ExpectIntEQ(server_cbs.return_code, params[i].result);
  54722. }
  54723. return EXPECT_RESULT();
  54724. }
  54725. #else
  54726. static int test_override_alt_cert_chain(void)
  54727. {
  54728. return TEST_SKIPPED;
  54729. }
  54730. #endif
  54731. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  54732. static int test_dtls13_bad_epoch_ch(void)
  54733. {
  54734. EXPECT_DECLS;
  54735. WOLFSSL_CTX *ctx_c = NULL;
  54736. WOLFSSL_CTX *ctx_s = NULL;
  54737. WOLFSSL *ssl_c = NULL;
  54738. WOLFSSL *ssl_s = NULL;
  54739. struct test_memio_ctx test_ctx;
  54740. const int EPOCH_OFF = 3;
  54741. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54742. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54743. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  54744. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  54745. * with just one message */
  54746. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  54747. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  54748. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  54749. WOLFSSL_ERROR_WANT_READ);
  54750. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  54751. /* first CH should use epoch 0x0 */
  54752. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  54753. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  54754. /* change epoch to 2 */
  54755. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  54756. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  54757. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  54758. WOLFSSL_ERROR_WANT_READ);
  54759. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  54760. /* resend the CH */
  54761. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  54762. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  54763. wolfSSL_free(ssl_c);
  54764. wolfSSL_CTX_free(ctx_c);
  54765. wolfSSL_free(ssl_s);
  54766. wolfSSL_CTX_free(ctx_s);
  54767. return EXPECT_RESULT();
  54768. }
  54769. #else
  54770. static int test_dtls13_bad_epoch_ch(void)
  54771. {
  54772. return TEST_SKIPPED;
  54773. }
  54774. #endif
  54775. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  54776. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  54777. {
  54778. EXPECT_DECLS;
  54779. WOLFSSL_SESSION *sess = NULL;
  54780. /* Setup the session to avoid errors */
  54781. ssl->session->timeout = -1;
  54782. ssl->session->side = WOLFSSL_CLIENT_END;
  54783. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  54784. defined(HAVE_SESSION_TICKET))
  54785. ssl->session->version = ssl->version;
  54786. #endif
  54787. /* Force a short session ID to be sent */
  54788. ssl->session->sessionIDSz = 4;
  54789. #ifndef NO_SESSION_CACHE_REF
  54790. /* Allow the client cache to be used */
  54791. ssl->session->idLen = 4;
  54792. #endif
  54793. ssl->session->isSetup = 1;
  54794. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  54795. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  54796. return EXPECT_RESULT();
  54797. }
  54798. static int test_short_session_id(void)
  54799. {
  54800. EXPECT_DECLS;
  54801. test_ssl_cbf client_cbf;
  54802. test_ssl_cbf server_cbf;
  54803. size_t i;
  54804. struct {
  54805. method_provider client_meth;
  54806. method_provider server_meth;
  54807. const char* tls_version;
  54808. } params[] = {
  54809. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  54810. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  54811. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  54812. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  54813. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  54814. #ifdef WOLFSSL_DTLS13
  54815. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  54816. #endif
  54817. #endif
  54818. #ifndef WOLFSSL_NO_TLS12
  54819. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  54820. #ifdef WOLFSSL_DTLS
  54821. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  54822. #endif
  54823. #endif
  54824. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  54825. !defined(NO_DES3))
  54826. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  54827. #ifdef WOLFSSL_DTLS
  54828. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  54829. #endif
  54830. #endif
  54831. };
  54832. fprintf(stderr, "\n");
  54833. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  54834. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  54835. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  54836. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  54837. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  54838. client_cbf.method = params[i].client_meth;
  54839. server_cbf.method = params[i].server_meth;
  54840. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  54841. &server_cbf, NULL), TEST_SUCCESS);
  54842. }
  54843. return EXPECT_RESULT();
  54844. }
  54845. #else
  54846. static int test_short_session_id(void)
  54847. {
  54848. return TEST_SKIPPED;
  54849. }
  54850. #endif
  54851. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  54852. && defined(WOLFSSL_DTLS13)
  54853. static byte* test_find_string(const char *string,
  54854. byte *buf, int buf_size)
  54855. {
  54856. int string_size, i;
  54857. string_size = (int)XSTRLEN(string);
  54858. for (i = 0; i < buf_size - string_size - 1; i++) {
  54859. if (XSTRCMP((char*)&buf[i], string) == 0)
  54860. return &buf[i];
  54861. }
  54862. return NULL;
  54863. }
  54864. static int test_wolfSSL_dtls13_null_cipher(void)
  54865. {
  54866. EXPECT_DECLS;
  54867. WOLFSSL_CTX *ctx_c = NULL;
  54868. WOLFSSL_CTX *ctx_s = NULL;
  54869. WOLFSSL *ssl_c = NULL;
  54870. WOLFSSL *ssl_s = NULL;
  54871. struct test_memio_ctx test_ctx;
  54872. const char *test_str = "test";
  54873. int test_str_size;
  54874. byte buf[255], *ptr = NULL;
  54875. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  54876. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  54877. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  54878. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  54879. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  54880. test_str_size = XSTRLEN("test") + 1;
  54881. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  54882. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  54883. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  54884. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  54885. /* check that the packet was sent cleartext */
  54886. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  54887. test_ctx.s_len));
  54888. if (ptr != NULL) {
  54889. /* modify the message */
  54890. *ptr = 'H';
  54891. /* bad messages should be ignored in DTLS */
  54892. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  54893. ExpectIntEQ(ssl_s->error, WANT_READ);
  54894. }
  54895. wolfSSL_free(ssl_c);
  54896. wolfSSL_free(ssl_s);
  54897. wolfSSL_CTX_free(ctx_c);
  54898. wolfSSL_CTX_free(ctx_s);
  54899. return TEST_SUCCESS;
  54900. }
  54901. #else
  54902. static int test_wolfSSL_dtls13_null_cipher(void)
  54903. {
  54904. return TEST_SKIPPED;
  54905. }
  54906. #endif
  54907. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  54908. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54909. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  54910. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  54911. {
  54912. SOCKADDR_S peer;
  54913. XSOCKLENT len;
  54914. int ret;
  54915. XMEMSET((byte*)&peer, 0, sizeof(peer));
  54916. len = sizeof(peer);
  54917. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  54918. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  54919. return -1;
  54920. switch (peer.ss_family) {
  54921. #ifdef WOLFSSL_IPV6
  54922. case WOLFSSL_IP6: {
  54923. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  54924. break;
  54925. }
  54926. #endif /* WOLFSSL_IPV6 */
  54927. case WOLFSSL_IP4:
  54928. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  54929. break;
  54930. default:
  54931. return -1;
  54932. }
  54933. return 0;
  54934. }
  54935. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  54936. {
  54937. char buf[1] = {'t'};
  54938. SOCKADDR_IN_T addr;
  54939. int sock_fd;
  54940. word16 port;
  54941. int err;
  54942. (void)ssl;
  54943. (void)ctx;
  54944. if (ssl == NULL)
  54945. return -1;
  54946. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  54947. if (err != 0)
  54948. return -1;
  54949. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  54950. if (sock_fd == -1)
  54951. return -1;
  54952. build_addr(&addr, wolfSSLIP, port, 1, 0);
  54953. /* send a packet to the server. Being another socket, the kernel will ensure
  54954. * the source port will be different. */
  54955. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  54956. sizeof(addr));
  54957. close(sock_fd);
  54958. if (err == -1)
  54959. return -1;
  54960. return 0;
  54961. }
  54962. /* setup a SSL session but just after the handshake send a packet to the server
  54963. * with a source address different than the one of the connected client. The I/O
  54964. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  54965. * is done in test_dtls_msg_from_other_peer_cb */
  54966. static int test_dtls_msg_from_other_peer(void)
  54967. {
  54968. EXPECT_DECLS;
  54969. callback_functions client_cbs;
  54970. callback_functions server_cbs;
  54971. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  54972. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  54973. client_cbs.method = wolfDTLSv1_2_client_method;
  54974. server_cbs.method = wolfDTLSv1_2_server_method;
  54975. client_cbs.doUdp = 1;
  54976. server_cbs.doUdp = 1;
  54977. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  54978. test_dtls_msg_from_other_peer_cb);
  54979. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  54980. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  54981. return EXPECT_RESULT();
  54982. }
  54983. #else
  54984. static int test_dtls_msg_from_other_peer(void)
  54985. {
  54986. return TEST_SKIPPED;
  54987. }
  54988. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  54989. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54990. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  54991. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  54992. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  54993. defined(HAVE_IO_TESTS_DEPENDENCIES)
  54994. static int test_dtls_ipv6_check(void)
  54995. {
  54996. EXPECT_DECLS;
  54997. WOLFSSL_CTX *ctx_c = NULL;
  54998. WOLFSSL_CTX *ctx_s = NULL;
  54999. WOLFSSL *ssl_c = NULL;
  55000. WOLFSSL *ssl_s = NULL;
  55001. SOCKADDR_IN fake_addr6;
  55002. int sockfd = -1;
  55003. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  55004. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  55005. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55006. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  55007. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55008. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  55009. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55010. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  55011. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  55012. /* mimic a sockaddr_in6 struct, this way we can't test without
  55013. * WOLFSSL_IPV6 */
  55014. fake_addr6.sin_family = WOLFSSL_IP6;
  55015. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  55016. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  55017. /* can't return error here, as the peer is opaque for wolfssl library at
  55018. * this point */
  55019. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  55020. WOLFSSL_SUCCESS);
  55021. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  55022. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  55023. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  55024. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  55025. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  55026. WOLFSSL_SUCCESS);
  55027. /* reuse the socket */
  55028. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  55029. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  55030. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  55031. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  55032. if (sockfd != -1)
  55033. close(sockfd);
  55034. wolfSSL_free(ssl_c);
  55035. wolfSSL_CTX_free(ctx_c);
  55036. wolfSSL_free(ssl_s);
  55037. wolfSSL_CTX_free(ctx_s);
  55038. return EXPECT_RESULT();
  55039. }
  55040. #else
  55041. static int test_dtls_ipv6_check(void)
  55042. {
  55043. return TEST_SKIPPED;
  55044. }
  55045. #endif
  55046. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55047. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  55048. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  55049. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  55050. {
  55051. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  55052. }
  55053. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  55054. {
  55055. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  55056. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  55057. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  55058. }
  55059. else {
  55060. char testMsg[] = "Message after SCR";
  55061. char msgBuf[sizeof(testMsg)];
  55062. int ret;
  55063. if (!wolfSSL_is_server(ssl)) {
  55064. AssertIntEQ(WOLFSSL_SUCCESS,
  55065. wolfSSL_set_session(ssl,
  55066. test_wolfSSL_SCR_after_resumption_session));
  55067. }
  55068. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  55069. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  55070. sizeof(testMsg));
  55071. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55072. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  55073. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55074. AssertIntEQ(ret, sizeof(msgBuf));
  55075. }
  55076. }
  55077. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  55078. {
  55079. AssertIntEQ(WOLFSSL_SUCCESS,
  55080. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  55081. }
  55082. static int test_wolfSSL_SCR_after_resumption(void)
  55083. {
  55084. EXPECT_DECLS;
  55085. callback_functions func_cb_client;
  55086. callback_functions func_cb_server;
  55087. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  55088. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  55089. func_cb_client.method = wolfTLSv1_2_client_method;
  55090. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  55091. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  55092. func_cb_server.method = wolfTLSv1_2_server_method;
  55093. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  55094. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55095. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55096. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55097. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  55098. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  55099. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55100. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55101. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55102. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  55103. return EXPECT_RESULT();
  55104. }
  55105. #else
  55106. static int test_wolfSSL_SCR_after_resumption(void)
  55107. {
  55108. return TEST_SKIPPED;
  55109. }
  55110. #endif
  55111. static int test_wolfSSL_configure_args(void)
  55112. {
  55113. EXPECT_DECLS;
  55114. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  55115. ExpectNotNull(wolfSSL_configure_args());
  55116. #endif
  55117. return EXPECT_RESULT();
  55118. }
  55119. static int test_dtls_no_extensions(void)
  55120. {
  55121. EXPECT_DECLS;
  55122. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  55123. WOLFSSL *ssl_s = NULL;
  55124. WOLFSSL_CTX *ctx_s = NULL;
  55125. struct test_memio_ctx test_ctx;
  55126. const byte chNoExtensions[] = {
  55127. /* Handshake type */
  55128. 0x16,
  55129. /* Version */
  55130. 0xfe, 0xff,
  55131. /* Epoch */
  55132. 0x00, 0x00,
  55133. /* Seq number */
  55134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55135. /* Length */
  55136. 0x00, 0x40,
  55137. /* CH type */
  55138. 0x01,
  55139. /* Length */
  55140. 0x00, 0x00, 0x34,
  55141. /* Msg Seq */
  55142. 0x00, 0x00,
  55143. /* Frag offset */
  55144. 0x00, 0x00, 0x00,
  55145. /* Frag length */
  55146. 0x00, 0x00, 0x34,
  55147. /* Version */
  55148. 0xfe, 0xff,
  55149. /* Random */
  55150. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  55151. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  55152. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  55153. /* Session Length */
  55154. 0x00,
  55155. /* Cookie Length */
  55156. 0x00,
  55157. /* CS Length */
  55158. 0x00, 0x0c,
  55159. /* CS */
  55160. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  55161. /* Comp Meths Length */
  55162. 0x01,
  55163. /* Comp Meths */
  55164. 0x00
  55165. /* And finally... no extensions */
  55166. };
  55167. int i;
  55168. #ifdef OPENSSL_EXTRA
  55169. int repeats = 2;
  55170. #else
  55171. int repeats = 1;
  55172. #endif
  55173. for (i = 0; i < repeats; i++) {
  55174. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55175. ssl_s = NULL;
  55176. ctx_s = NULL;
  55177. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  55178. NULL, wolfDTLS_server_method), 0);
  55179. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  55180. test_ctx.s_len = sizeof(chNoExtensions);
  55181. #ifdef OPENSSL_EXTRA
  55182. if (i > 0) {
  55183. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  55184. WOLFSSL_SUCCESS);
  55185. }
  55186. #endif
  55187. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  55188. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  55189. /* Expecting a handshake msg. Either HVR or SH. */
  55190. ExpectIntGT(test_ctx.c_len, 0);
  55191. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  55192. wolfSSL_free(ssl_s);
  55193. wolfSSL_CTX_free(ctx_s);
  55194. }
  55195. #endif
  55196. return EXPECT_RESULT();
  55197. }
  55198. static int test_TLSX_CA_NAMES_bad_extension(void)
  55199. {
  55200. EXPECT_DECLS;
  55201. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  55202. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  55203. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  55204. defined(HAVE_NULL_CIPHER)
  55205. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  55206. * parse the CA Names extension. Otherwise it will return other non-related
  55207. * errors. If CA Names will be parsed in more configurations, that should
  55208. * be reflected in the macro guard above. */
  55209. WOLFSSL *ssl_c = NULL;
  55210. WOLFSSL_CTX *ctx_c = NULL;
  55211. struct test_memio_ctx test_ctx;
  55212. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  55213. const byte shBadCaNamesExt[] = {
  55214. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  55215. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  55216. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  55217. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  55218. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  55219. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  55220. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  55221. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  55222. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  55223. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  55224. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  55225. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  55226. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  55227. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  55228. };
  55229. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  55230. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  55231. wolfTLSv1_3_client_method, NULL), 0);
  55232. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt, sizeof(shBadCaNamesExt));
  55233. test_ctx.c_len = sizeof(shBadCaNamesExt);
  55234. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  55235. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  55236. wolfSSL_free(ssl_c);
  55237. wolfSSL_CTX_free(ctx_c);
  55238. #endif
  55239. return EXPECT_RESULT();
  55240. }
  55241. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55242. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55243. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  55244. {
  55245. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  55246. WOLFSSL_SUCCESS);
  55247. }
  55248. static int test_dtls_1_0_hvr_downgrade(void)
  55249. {
  55250. EXPECT_DECLS;
  55251. callback_functions func_cb_client;
  55252. callback_functions func_cb_server;
  55253. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55254. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55255. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55256. func_cb_client.method = wolfDTLS_client_method;
  55257. func_cb_server.method = wolfDTLSv1_2_server_method;
  55258. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  55259. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55260. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55261. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55262. return EXPECT_RESULT();
  55263. }
  55264. #else
  55265. static int test_dtls_1_0_hvr_downgrade(void)
  55266. {
  55267. EXPECT_DECLS;
  55268. return EXPECT_RESULT();
  55269. }
  55270. #endif
  55271. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  55272. defined(HAVE_SESSION_TICKET)
  55273. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  55274. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  55275. {
  55276. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  55277. AssertNotNull(test_session_ticket_no_id_session);
  55278. }
  55279. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  55280. {
  55281. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  55282. }
  55283. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  55284. {
  55285. test_session_ticket_no_id_session->sessionIDSz = 0;
  55286. AssertIntEQ(WOLFSSL_SUCCESS,
  55287. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  55288. }
  55289. static int test_session_ticket_no_id(void)
  55290. {
  55291. /* We are testing an expired (invalid crypto context in out case since the
  55292. * ctx changes) session ticket being sent with the session ID being 0
  55293. * length. */
  55294. EXPECT_DECLS;
  55295. callback_functions func_cb_client;
  55296. callback_functions func_cb_server;
  55297. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  55298. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  55299. func_cb_client.method = wolfTLSv1_2_client_method;
  55300. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  55301. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  55302. func_cb_server.method = wolfTLSv1_2_server_method;
  55303. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  55304. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55305. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55306. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55307. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  55308. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  55309. func_cb_client.method = wolfTLSv1_2_client_method;
  55310. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  55311. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  55312. func_cb_server.method = wolfTLSv1_2_server_method;
  55313. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  55314. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55315. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55316. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55317. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  55318. return EXPECT_RESULT();
  55319. }
  55320. #else
  55321. static int test_session_ticket_no_id(void)
  55322. {
  55323. EXPECT_DECLS;
  55324. return EXPECT_RESULT();
  55325. }
  55326. #endif
  55327. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55328. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  55329. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  55330. {
  55331. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  55332. WOLFSSL_SUCCESS);
  55333. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  55334. }
  55335. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  55336. {
  55337. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  55338. }
  55339. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  55340. {
  55341. char testMsg[] = "Message after SCR";
  55342. char msgBuf[sizeof(testMsg)];
  55343. if (wolfSSL_is_server(ssl)) {
  55344. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  55345. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  55346. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  55347. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  55348. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  55349. sizeof(testMsg));
  55350. }
  55351. else {
  55352. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  55353. sizeof(testMsg));
  55354. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  55355. }
  55356. }
  55357. static int test_dtls_downgrade_scr_server(void)
  55358. {
  55359. EXPECT_DECLS;
  55360. callback_functions func_cb_client;
  55361. callback_functions func_cb_server;
  55362. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55363. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55364. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55365. func_cb_client.method = wolfDTLSv1_2_client_method;
  55366. func_cb_server.method = wolfDTLS_server_method;
  55367. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  55368. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  55369. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  55370. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  55371. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55372. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55373. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55374. return EXPECT_RESULT();
  55375. }
  55376. #else
  55377. static int test_dtls_downgrade_scr_server(void)
  55378. {
  55379. EXPECT_DECLS;
  55380. return EXPECT_RESULT();
  55381. }
  55382. #endif
  55383. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  55384. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  55385. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  55386. {
  55387. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  55388. WOLFSSL_SUCCESS);
  55389. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  55390. }
  55391. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  55392. {
  55393. char testMsg[] = "Message after SCR";
  55394. char msgBuf[sizeof(testMsg)];
  55395. if (wolfSSL_is_server(ssl)) {
  55396. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  55397. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  55398. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  55399. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  55400. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  55401. sizeof(testMsg));
  55402. }
  55403. else {
  55404. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  55405. sizeof(testMsg));
  55406. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  55407. }
  55408. }
  55409. static int test_dtls_downgrade_scr(void)
  55410. {
  55411. EXPECT_DECLS;
  55412. callback_functions func_cb_client;
  55413. callback_functions func_cb_server;
  55414. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55415. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55416. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55417. func_cb_client.method = wolfDTLS_client_method;
  55418. func_cb_server.method = wolfDTLSv1_2_server_method;
  55419. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  55420. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  55421. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  55422. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55423. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  55424. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  55425. return EXPECT_RESULT();
  55426. }
  55427. #else
  55428. static int test_dtls_downgrade_scr(void)
  55429. {
  55430. EXPECT_DECLS;
  55431. return EXPECT_RESULT();
  55432. }
  55433. #endif
  55434. /*----------------------------------------------------------------------------*
  55435. | Main
  55436. *----------------------------------------------------------------------------*/
  55437. typedef int (*TEST_FUNC)(void);
  55438. typedef struct {
  55439. const char *name;
  55440. TEST_FUNC func;
  55441. byte run:1;
  55442. byte fail:1;
  55443. } TEST_CASE;
  55444. #define TEST_DECL(func) { #func, func, 0, 0 }
  55445. int testAll = 1;
  55446. TEST_CASE testCases[] = {
  55447. TEST_DECL(test_fileAccess),
  55448. /*********************************
  55449. * wolfcrypt
  55450. *********************************/
  55451. TEST_DECL(test_ForceZero),
  55452. TEST_DECL(test_wolfCrypt_Init),
  55453. /* Locking with Compat Mutex */
  55454. TEST_DECL(test_wc_SetMutexCb),
  55455. TEST_DECL(test_wc_LockMutex_ex),
  55456. /* Digests */
  55457. TEST_DECL(test_wc_InitMd5),
  55458. TEST_DECL(test_wc_Md5Update),
  55459. TEST_DECL(test_wc_Md5Final),
  55460. TEST_DECL(test_wc_InitSha),
  55461. TEST_DECL(test_wc_ShaUpdate),
  55462. TEST_DECL(test_wc_ShaFinal),
  55463. TEST_DECL(test_wc_InitSha256),
  55464. TEST_DECL(test_wc_Sha256Update),
  55465. TEST_DECL(test_wc_Sha256Final),
  55466. TEST_DECL(test_wc_Sha256FinalRaw),
  55467. TEST_DECL(test_wc_Sha256GetFlags),
  55468. TEST_DECL(test_wc_Sha256Free),
  55469. TEST_DECL(test_wc_Sha256GetHash),
  55470. TEST_DECL(test_wc_Sha256Copy),
  55471. TEST_DECL(test_wc_InitSha224),
  55472. TEST_DECL(test_wc_Sha224Update),
  55473. TEST_DECL(test_wc_Sha224Final),
  55474. TEST_DECL(test_wc_Sha224SetFlags),
  55475. TEST_DECL(test_wc_Sha224GetFlags),
  55476. TEST_DECL(test_wc_Sha224Free),
  55477. TEST_DECL(test_wc_Sha224GetHash),
  55478. TEST_DECL(test_wc_Sha224Copy),
  55479. TEST_DECL(test_wc_InitSha512),
  55480. TEST_DECL(test_wc_Sha512Update),
  55481. TEST_DECL(test_wc_Sha512Final),
  55482. TEST_DECL(test_wc_Sha512GetFlags),
  55483. TEST_DECL(test_wc_Sha512FinalRaw),
  55484. TEST_DECL(test_wc_Sha512Free),
  55485. TEST_DECL(test_wc_Sha512GetHash),
  55486. TEST_DECL(test_wc_Sha512Copy),
  55487. TEST_DECL(test_wc_InitSha512_224),
  55488. TEST_DECL(test_wc_Sha512_224Update),
  55489. TEST_DECL(test_wc_Sha512_224Final),
  55490. TEST_DECL(test_wc_Sha512_224GetFlags),
  55491. TEST_DECL(test_wc_Sha512_224FinalRaw),
  55492. TEST_DECL(test_wc_Sha512_224Free),
  55493. TEST_DECL(test_wc_Sha512_224GetHash),
  55494. TEST_DECL(test_wc_Sha512_224Copy),
  55495. TEST_DECL(test_wc_InitSha512_256),
  55496. TEST_DECL(test_wc_Sha512_256Update),
  55497. TEST_DECL(test_wc_Sha512_256Final),
  55498. TEST_DECL(test_wc_Sha512_256GetFlags),
  55499. TEST_DECL(test_wc_Sha512_256FinalRaw),
  55500. TEST_DECL(test_wc_Sha512_256Free),
  55501. TEST_DECL(test_wc_Sha512_256GetHash),
  55502. TEST_DECL(test_wc_Sha512_256Copy),
  55503. TEST_DECL(test_wc_InitSha384),
  55504. TEST_DECL(test_wc_Sha384Update),
  55505. TEST_DECL(test_wc_Sha384Final),
  55506. TEST_DECL(test_wc_Sha384GetFlags),
  55507. TEST_DECL(test_wc_Sha384FinalRaw),
  55508. TEST_DECL(test_wc_Sha384Free),
  55509. TEST_DECL(test_wc_Sha384GetHash),
  55510. TEST_DECL(test_wc_Sha384Copy),
  55511. TEST_DECL(test_wc_InitBlake2b),
  55512. TEST_DECL(test_wc_InitBlake2b_WithKey),
  55513. TEST_DECL(test_wc_InitBlake2s_WithKey),
  55514. TEST_DECL(test_wc_InitRipeMd),
  55515. TEST_DECL(test_wc_RipeMdUpdate),
  55516. TEST_DECL(test_wc_RipeMdFinal),
  55517. TEST_DECL(test_wc_InitSha3),
  55518. TEST_DECL(testing_wc_Sha3_Update),
  55519. TEST_DECL(test_wc_Sha3_224_Final),
  55520. TEST_DECL(test_wc_Sha3_256_Final),
  55521. TEST_DECL(test_wc_Sha3_384_Final),
  55522. TEST_DECL(test_wc_Sha3_512_Final),
  55523. TEST_DECL(test_wc_Sha3_224_Copy),
  55524. TEST_DECL(test_wc_Sha3_256_Copy),
  55525. TEST_DECL(test_wc_Sha3_384_Copy),
  55526. TEST_DECL(test_wc_Sha3_512_Copy),
  55527. TEST_DECL(test_wc_Sha3_GetFlags),
  55528. TEST_DECL(test_wc_InitShake256),
  55529. TEST_DECL(testing_wc_Shake256_Update),
  55530. TEST_DECL(test_wc_Shake256_Final),
  55531. TEST_DECL(test_wc_Shake256_Copy),
  55532. TEST_DECL(test_wc_Shake256Hash),
  55533. /* SM3 Digest */
  55534. TEST_DECL(test_wc_InitSm3Free),
  55535. TEST_DECL(test_wc_Sm3UpdateFinal),
  55536. TEST_DECL(test_wc_Sm3GetHash),
  55537. TEST_DECL(test_wc_Sm3Copy),
  55538. TEST_DECL(test_wc_Sm3FinalRaw),
  55539. TEST_DECL(test_wc_Sm3GetSetFlags),
  55540. TEST_DECL(test_wc_Sm3Hash),
  55541. TEST_DECL(test_wc_HashInit),
  55542. TEST_DECL(test_wc_HashSetFlags),
  55543. TEST_DECL(test_wc_HashGetFlags),
  55544. /* HMAC */
  55545. TEST_DECL(test_wc_Md5HmacSetKey),
  55546. TEST_DECL(test_wc_Md5HmacUpdate),
  55547. TEST_DECL(test_wc_Md5HmacFinal),
  55548. TEST_DECL(test_wc_ShaHmacSetKey),
  55549. TEST_DECL(test_wc_ShaHmacUpdate),
  55550. TEST_DECL(test_wc_ShaHmacFinal),
  55551. TEST_DECL(test_wc_Sha224HmacSetKey),
  55552. TEST_DECL(test_wc_Sha224HmacUpdate),
  55553. TEST_DECL(test_wc_Sha224HmacFinal),
  55554. TEST_DECL(test_wc_Sha256HmacSetKey),
  55555. TEST_DECL(test_wc_Sha256HmacUpdate),
  55556. TEST_DECL(test_wc_Sha256HmacFinal),
  55557. TEST_DECL(test_wc_Sha384HmacSetKey),
  55558. TEST_DECL(test_wc_Sha384HmacUpdate),
  55559. TEST_DECL(test_wc_Sha384HmacFinal),
  55560. /* CMAC */
  55561. TEST_DECL(test_wc_InitCmac),
  55562. TEST_DECL(test_wc_CmacUpdate),
  55563. TEST_DECL(test_wc_CmacFinal),
  55564. TEST_DECL(test_wc_AesCmacGenerate),
  55565. /* Cipher */
  55566. TEST_DECL(test_wc_AesGcmStream),
  55567. TEST_DECL(test_wc_Des3_SetIV),
  55568. TEST_DECL(test_wc_Des3_SetKey),
  55569. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  55570. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  55571. TEST_DECL(test_wc_Des3_EcbEncrypt),
  55572. TEST_DECL(test_wc_Chacha_SetKey),
  55573. TEST_DECL(test_wc_Chacha_Process),
  55574. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  55575. TEST_DECL(test_wc_Poly1305SetKey),
  55576. TEST_DECL(test_wc_CamelliaSetKey),
  55577. TEST_DECL(test_wc_CamelliaSetIV),
  55578. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  55579. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  55580. TEST_DECL(test_wc_Arc4SetKey),
  55581. TEST_DECL(test_wc_Arc4Process),
  55582. TEST_DECL(test_wc_Rc2SetKey),
  55583. TEST_DECL(test_wc_Rc2SetIV),
  55584. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  55585. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  55586. /* AES cipher and GMAC. */
  55587. TEST_DECL(test_wc_AesSetKey),
  55588. TEST_DECL(test_wc_AesSetIV),
  55589. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  55590. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  55591. TEST_DECL(test_wc_AesGcmSetKey),
  55592. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  55593. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  55594. TEST_DECL(test_wc_GmacSetKey),
  55595. TEST_DECL(test_wc_GmacUpdate),
  55596. TEST_DECL(test_wc_AesCcmSetKey),
  55597. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  55598. /* SM4 cipher */
  55599. TEST_DECL(test_wc_Sm4),
  55600. TEST_DECL(test_wc_Sm4Ecb),
  55601. TEST_DECL(test_wc_Sm4Cbc),
  55602. TEST_DECL(test_wc_Sm4Ctr),
  55603. TEST_DECL(test_wc_Sm4Gcm),
  55604. TEST_DECL(test_wc_Sm4Ccm),
  55605. /* RNG tests */
  55606. #ifdef HAVE_HASHDRBG
  55607. #ifdef TEST_RESEED_INTERVAL
  55608. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  55609. #endif
  55610. TEST_DECL(test_wc_RNG_GenerateBlock),
  55611. #endif
  55612. TEST_DECL(test_get_rand_digit),
  55613. TEST_DECL(test_wc_InitRngNonce),
  55614. TEST_DECL(test_wc_InitRngNonce_ex),
  55615. /* MP API tests */
  55616. TEST_DECL(test_get_digit_count),
  55617. TEST_DECL(test_mp_cond_copy),
  55618. TEST_DECL(test_mp_rand),
  55619. TEST_DECL(test_get_digit),
  55620. TEST_DECL(test_wc_export_int),
  55621. /* RSA */
  55622. TEST_DECL(test_wc_InitRsaKey),
  55623. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  55624. TEST_DECL(test_wc_RsaPublicKeyDecode),
  55625. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  55626. TEST_DECL(test_wc_MakeRsaKey),
  55627. TEST_DECL(test_wc_CheckProbablePrime),
  55628. TEST_DECL(test_wc_RsaPSS_Verify),
  55629. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  55630. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  55631. TEST_DECL(test_wc_RsaKeyToDer),
  55632. TEST_DECL(test_wc_RsaKeyToPublicDer),
  55633. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  55634. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  55635. TEST_DECL(test_wc_RsaEncryptSize),
  55636. TEST_DECL(test_wc_RsaSSL_SignVerify),
  55637. TEST_DECL(test_wc_RsaFlattenPublicKey),
  55638. TEST_DECL(test_RsaDecryptBoundsCheck),
  55639. /* DSA */
  55640. TEST_DECL(test_wc_InitDsaKey),
  55641. TEST_DECL(test_wc_DsaSignVerify),
  55642. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  55643. TEST_DECL(test_wc_MakeDsaKey),
  55644. TEST_DECL(test_wc_DsaKeyToDer),
  55645. TEST_DECL(test_wc_DsaKeyToPublicDer),
  55646. TEST_DECL(test_wc_DsaImportParamsRaw),
  55647. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  55648. TEST_DECL(test_wc_DsaExportParamsRaw),
  55649. TEST_DECL(test_wc_DsaExportKeyRaw),
  55650. /* DH */
  55651. TEST_DECL(test_wc_DhPublicKeyDecode),
  55652. /* wolfCrypt ECC tests */
  55653. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  55654. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  55655. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  55656. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  55657. !defined(HAVE_SELFTEST) && \
  55658. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  55659. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  55660. #endif
  55661. TEST_DECL(test_wc_ecc_make_key),
  55662. TEST_DECL(test_wc_ecc_init),
  55663. TEST_DECL(test_wc_ecc_check_key),
  55664. TEST_DECL(test_wc_ecc_get_generator),
  55665. TEST_DECL(test_wc_ecc_size),
  55666. TEST_DECL(test_wc_ecc_params),
  55667. TEST_DECL(test_wc_ecc_signVerify_hash),
  55668. TEST_DECL(test_wc_ecc_shared_secret),
  55669. TEST_DECL(test_wc_ecc_export_x963),
  55670. TEST_DECL(test_wc_ecc_export_x963_ex),
  55671. TEST_DECL(test_wc_ecc_import_x963),
  55672. TEST_DECL(test_wc_ecc_import_private_key),
  55673. TEST_DECL(test_wc_ecc_export_private_only),
  55674. TEST_DECL(test_wc_ecc_rs_to_sig),
  55675. TEST_DECL(test_wc_ecc_import_raw),
  55676. TEST_DECL(test_wc_ecc_import_unsigned),
  55677. TEST_DECL(test_wc_ecc_sig_size),
  55678. TEST_DECL(test_wc_ecc_ctx_new),
  55679. TEST_DECL(test_wc_ecc_ctx_reset),
  55680. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  55681. TEST_DECL(test_wc_ecc_ctx_set_info),
  55682. TEST_DECL(test_wc_ecc_encryptDecrypt),
  55683. TEST_DECL(test_wc_ecc_del_point),
  55684. TEST_DECL(test_wc_ecc_pointFns),
  55685. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  55686. TEST_DECL(test_wc_ecc_verify_hash_ex),
  55687. TEST_DECL(test_wc_ecc_mulmod),
  55688. TEST_DECL(test_wc_ecc_is_valid_idx),
  55689. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  55690. TEST_DECL(test_wc_ecc_sig_size_calc),
  55691. TEST_DECL(test_wc_EccPrivateKeyToDer),
  55692. /* SM2 elliptic curve */
  55693. TEST_DECL(test_wc_ecc_sm2_make_key),
  55694. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  55695. TEST_DECL(test_wc_ecc_sm2_create_digest),
  55696. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  55697. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  55698. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  55699. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  55700. /* Curve25519 */
  55701. TEST_DECL(test_wc_curve25519_init),
  55702. TEST_DECL(test_wc_curve25519_size),
  55703. TEST_DECL(test_wc_curve25519_export_key_raw),
  55704. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  55705. TEST_DECL(test_wc_curve25519_make_key),
  55706. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  55707. TEST_DECL(test_wc_curve25519_make_pub),
  55708. TEST_DECL(test_wc_curve25519_export_public_ex),
  55709. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  55710. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  55711. TEST_DECL(test_wc_curve25519_import_private),
  55712. /* ED25519 */
  55713. TEST_DECL(test_wc_ed25519_make_key),
  55714. TEST_DECL(test_wc_ed25519_init),
  55715. TEST_DECL(test_wc_ed25519_sign_msg),
  55716. TEST_DECL(test_wc_ed25519_import_public),
  55717. TEST_DECL(test_wc_ed25519_import_private_key),
  55718. TEST_DECL(test_wc_ed25519_export),
  55719. TEST_DECL(test_wc_ed25519_size),
  55720. TEST_DECL(test_wc_ed25519_exportKey),
  55721. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  55722. TEST_DECL(test_wc_Ed25519KeyToDer),
  55723. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  55724. /* Curve448 */
  55725. TEST_DECL(test_wc_curve448_make_key),
  55726. TEST_DECL(test_wc_curve448_shared_secret_ex),
  55727. TEST_DECL(test_wc_curve448_export_public_ex),
  55728. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  55729. TEST_DECL(test_wc_curve448_export_key_raw),
  55730. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  55731. TEST_DECL(test_wc_curve448_import_private),
  55732. TEST_DECL(test_wc_curve448_init),
  55733. TEST_DECL(test_wc_curve448_size),
  55734. /* Ed448 */
  55735. TEST_DECL(test_wc_ed448_make_key),
  55736. TEST_DECL(test_wc_ed448_init),
  55737. TEST_DECL(test_wc_ed448_sign_msg),
  55738. TEST_DECL(test_wc_ed448_import_public),
  55739. TEST_DECL(test_wc_ed448_import_private_key),
  55740. TEST_DECL(test_wc_ed448_export),
  55741. TEST_DECL(test_wc_ed448_size),
  55742. TEST_DECL(test_wc_ed448_exportKey),
  55743. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  55744. TEST_DECL(test_wc_Ed448KeyToDer),
  55745. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  55746. /* Signature API */
  55747. TEST_DECL(test_wc_SignatureGetSize_ecc),
  55748. TEST_DECL(test_wc_SignatureGetSize_rsa),
  55749. /* PEM and DER APIs. */
  55750. TEST_DECL(test_wc_PemToDer),
  55751. TEST_DECL(test_wc_AllocDer),
  55752. TEST_DECL(test_wc_CertPemToDer),
  55753. TEST_DECL(test_wc_KeyPemToDer),
  55754. TEST_DECL(test_wc_PubKeyPemToDer),
  55755. TEST_DECL(test_wc_PemPubKeyToDer),
  55756. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  55757. TEST_DECL(test_wc_CheckCertSigPubKey),
  55758. /* wolfCrypt ASN tests */
  55759. TEST_DECL(test_ToTraditional),
  55760. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  55761. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  55762. /* Certificate */
  55763. TEST_DECL(test_wc_SetSubjectRaw),
  55764. TEST_DECL(test_wc_GetSubjectRaw),
  55765. TEST_DECL(test_wc_SetIssuerRaw),
  55766. TEST_DECL(test_wc_SetIssueBuffer),
  55767. TEST_DECL(test_wc_SetSubjectKeyId),
  55768. TEST_DECL(test_wc_SetSubject),
  55769. TEST_DECL(test_CheckCertSignature),
  55770. TEST_DECL(test_wc_ParseCert),
  55771. TEST_DECL(test_wc_ParseCert_Error),
  55772. TEST_DECL(test_MakeCertWithPathLen),
  55773. TEST_DECL(test_wc_SetKeyUsage),
  55774. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  55775. TEST_DECL(test_wc_SetSubjectBuffer),
  55776. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  55777. /* wolfcrypt PKCS#7 */
  55778. TEST_DECL(test_wc_PKCS7_New),
  55779. TEST_DECL(test_wc_PKCS7_Init),
  55780. TEST_DECL(test_wc_PKCS7_InitWithCert),
  55781. TEST_DECL(test_wc_PKCS7_EncodeData),
  55782. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  55783. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  55784. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  55785. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  55786. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  55787. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  55788. TEST_DECL(test_wc_PKCS7_Degenerate),
  55789. TEST_DECL(test_wc_PKCS7_BER),
  55790. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  55791. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  55792. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  55793. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  55794. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  55795. /* wolfCrypt PKCS#12 */
  55796. TEST_DECL(test_wc_i2d_PKCS12),
  55797. /*
  55798. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  55799. * avoid memory leaks.
  55800. */
  55801. TEST_DECL(test_wolfCrypt_Cleanup),
  55802. TEST_DECL(test_wolfSSL_Init),
  55803. /*********************************
  55804. * OpenSSL compatibility API tests
  55805. *********************************/
  55806. /* If at some point a stub get implemented this test should fail indicating
  55807. * a need to implement a new test case
  55808. */
  55809. TEST_DECL(test_stubs_are_stubs),
  55810. /* ASN.1 compatibility API tests */
  55811. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  55812. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  55813. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  55814. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  55815. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  55816. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  55817. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  55818. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  55819. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  55820. TEST_DECL(test_wolfSSL_ASN1_get_object),
  55821. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  55822. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  55823. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  55824. TEST_DECL(test_wolfSSL_ASN1_STRING),
  55825. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  55826. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  55827. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  55828. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  55829. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  55830. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  55831. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  55832. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  55833. TEST_DECL(test_wolfSSL_ASN1_TIME),
  55834. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  55835. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  55836. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  55837. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  55838. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  55839. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  55840. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  55841. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  55842. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  55843. TEST_DECL(test_wolfSSL_lhash),
  55844. TEST_DECL(test_wolfSSL_certs),
  55845. TEST_DECL(test_wolfSSL_DES),
  55846. TEST_DECL(test_wolfSSL_private_keys),
  55847. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  55848. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  55849. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  55850. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  55851. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  55852. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  55853. #ifndef NO_BIO
  55854. TEST_DECL(test_wolfSSL_BIO),
  55855. TEST_DECL(test_wolfSSL_PEM_read_bio),
  55856. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  55857. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  55858. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  55859. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  55860. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  55861. #endif
  55862. /* EVP API testing */
  55863. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  55864. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  55865. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  55866. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  55867. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  55868. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  55869. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  55870. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  55871. TEST_DECL(test_wolfSSL_EVP_shake128),
  55872. TEST_DECL(test_wolfSSL_EVP_shake256),
  55873. TEST_DECL(test_wolfSSL_EVP_sm3),
  55874. TEST_DECL(test_EVP_blake2),
  55875. #ifdef OPENSSL_ALL
  55876. TEST_DECL(test_wolfSSL_EVP_md4),
  55877. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  55878. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  55879. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  55880. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  55881. #endif
  55882. TEST_DECL(test_EVP_MD_do_all),
  55883. TEST_DECL(test_wolfSSL_EVP_MD_size),
  55884. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  55885. TEST_DECL(test_wolfSSL_EVP_Digest),
  55886. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  55887. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  55888. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  55889. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  55890. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  55891. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  55892. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  55893. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  55894. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  55895. TEST_DECL(test_wolfssl_EVP_chacha20),
  55896. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  55897. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  55898. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  55899. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  55900. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  55901. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  55902. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  55903. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  55904. #ifdef OPENSSL_ALL
  55905. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  55906. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  55907. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  55908. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  55909. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  55910. TEST_DECL(test_wolfSSL_EVP_rc4),
  55911. TEST_DECL(test_wolfSSL_EVP_enc_null),
  55912. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  55913. TEST_DECL(test_wolfSSL_EVP_mdc2),
  55914. TEST_DECL(test_evp_cipher_aes_gcm),
  55915. #endif
  55916. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  55917. #ifdef OPENSSL_EXTRA
  55918. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  55919. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  55920. #endif
  55921. #ifdef OPENSSL_ALL
  55922. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  55923. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  55924. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  55925. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  55926. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  55927. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  55928. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  55929. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  55930. #endif
  55931. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  55932. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  55933. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  55934. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  55935. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  55936. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  55937. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  55938. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  55939. #ifndef NO_BIO
  55940. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  55941. #endif
  55942. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  55943. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  55944. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  55945. #ifndef NO_BIO
  55946. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  55947. #endif /* !NO_BIO */
  55948. #endif
  55949. #ifdef OPENSSL_ALL
  55950. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  55951. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  55952. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  55953. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  55954. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  55955. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  55956. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  55957. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  55958. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  55959. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  55960. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  55961. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  55962. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  55963. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  55964. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  55965. #endif
  55966. TEST_DECL(test_EVP_PKEY_rsa),
  55967. TEST_DECL(test_EVP_PKEY_ec),
  55968. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  55969. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  55970. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  55971. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  55972. TEST_DECL(test_EVP_PKEY_cmp),
  55973. #ifdef OPENSSL_ALL
  55974. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  55975. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  55976. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  55977. #endif
  55978. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  55979. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  55980. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  55981. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  55982. #endif
  55983. #ifndef NO_BIO
  55984. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  55985. TEST_DECL(test_wolfSSL_GetLoggingCb),
  55986. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  55987. TEST_DECL(test_wc_ERR_remove_state),
  55988. TEST_DECL(test_wc_ERR_print_errors_fp),
  55989. #endif
  55990. TEST_DECL(test_wolfSSL_configure_args),
  55991. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  55992. TEST_DECL(test_wolfSSL_set1_curves_list),
  55993. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  55994. TEST_DECL(test_wolfSSL_OtherName),
  55995. TEST_DECL(test_wolfSSL_FPKI),
  55996. TEST_DECL(test_wolfSSL_URI),
  55997. TEST_DECL(test_wolfSSL_TBS),
  55998. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  55999. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  56000. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  56001. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  56002. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  56003. TEST_DECL(test_wolfSSL_X509_Name_canon),
  56004. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  56005. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  56006. TEST_DECL(test_wolfSSL_X509_NID),
  56007. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  56008. TEST_DECL(test_wolfSSL_get0_param),
  56009. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  56010. TEST_DECL(test_wolfSSL_set1_host),
  56011. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  56012. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  56013. TEST_DECL(test_wolfSSL_X509_STORE),
  56014. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  56015. TEST_DECL(test_X509_STORE_get0_objects),
  56016. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  56017. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  56018. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  56019. TEST_DECL(test_wolfSSL_X509_cmp_time),
  56020. TEST_DECL(test_wolfSSL_X509_time_adj),
  56021. /* X509 tests */
  56022. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  56023. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  56024. TEST_DECL(test_wolfSSL_X509_check_host),
  56025. TEST_DECL(test_wolfSSL_X509_check_email),
  56026. TEST_DECL(test_wolfSSL_X509_check_private_key),
  56027. TEST_DECL(test_wolfSSL_X509),
  56028. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  56029. TEST_DECL(test_wolfSSL_X509_sign),
  56030. TEST_DECL(test_wolfSSL_X509_sign2),
  56031. TEST_DECL(test_wolfSSL_X509_verify),
  56032. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  56033. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  56034. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  56035. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  56036. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  56037. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  56038. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  56039. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  56040. TEST_DECL(test_wolfSSL_X509_NAME),
  56041. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  56042. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  56043. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  56044. TEST_DECL(test_wolfSSL_X509_set_name),
  56045. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  56046. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  56047. TEST_DECL(test_wolfSSL_X509_set_version),
  56048. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  56049. TEST_DECL(test_wolfSSL_X509_CRL),
  56050. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  56051. TEST_DECL(test_wolfSSL_PEM_read_X509),
  56052. TEST_DECL(test_wolfSSL_X509_check_ca),
  56053. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  56054. TEST_DECL(test_wolfSSL_make_cert),
  56055. #ifndef NO_BIO
  56056. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  56057. TEST_DECL(test_wolfSSL_X509_INFO),
  56058. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  56059. #endif
  56060. #ifdef OPENSSL_ALL
  56061. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  56062. #endif
  56063. TEST_DECL(test_wolfSSL_X509_CA_num),
  56064. TEST_DECL(test_wolfSSL_X509_get_version),
  56065. #ifndef NO_BIO
  56066. TEST_DECL(test_wolfSSL_X509_print),
  56067. TEST_DECL(test_wolfSSL_X509_CRL_print),
  56068. #endif
  56069. TEST_DECL(test_X509_get_signature_nid),
  56070. /* X509 extension testing. */
  56071. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  56072. TEST_DECL(test_wolfSSL_X509_get_ext),
  56073. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  56074. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  56075. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  56076. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  56077. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  56078. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  56079. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  56080. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  56081. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  56082. TEST_DECL(test_wolfSSL_X509V3_EXT),
  56083. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  56084. TEST_DECL(test_wolfSSL_X509_cmp),
  56085. TEST_DECL(test_GENERAL_NAME_set0_othername),
  56086. TEST_DECL(test_othername_and_SID_ext),
  56087. TEST_DECL(test_wolfSSL_dup_CA_list),
  56088. /* OpenSSL sk_X509 API test */
  56089. TEST_DECL(test_sk_X509),
  56090. /* OpenSSL sk_X509_CRL API test */
  56091. TEST_DECL(test_sk_X509_CRL),
  56092. /* OpenSSL X509 REQ API test */
  56093. TEST_DECL(test_X509_REQ),
  56094. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  56095. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  56096. TEST_DECL(test_X509_LOOKUP_add_dir),
  56097. /* RAND compatibility API */
  56098. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  56099. TEST_DECL(test_wolfSSL_RAND_bytes),
  56100. TEST_DECL(test_wolfSSL_RAND),
  56101. /* BN compatibility API */
  56102. TEST_DECL(test_wolfSSL_BN_CTX),
  56103. TEST_DECL(test_wolfSSL_BN),
  56104. TEST_DECL(test_wolfSSL_BN_init),
  56105. TEST_DECL(test_wolfSSL_BN_enc_dec),
  56106. TEST_DECL(test_wolfSSL_BN_word),
  56107. TEST_DECL(test_wolfSSL_BN_bits),
  56108. TEST_DECL(test_wolfSSL_BN_shift),
  56109. TEST_DECL(test_wolfSSL_BN_math),
  56110. TEST_DECL(test_wolfSSL_BN_math_mod),
  56111. TEST_DECL(test_wolfSSL_BN_math_other),
  56112. TEST_DECL(test_wolfSSL_BN_rand),
  56113. TEST_DECL(test_wolfSSL_BN_prime),
  56114. /* OpenSSL PKCS5 API test */
  56115. TEST_DECL(test_wolfSSL_PKCS5),
  56116. /* OpenSSL PKCS8 API test */
  56117. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  56118. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  56119. /* OpenSSL PKCS7 API test */
  56120. TEST_DECL(test_wolfssl_PKCS7),
  56121. TEST_DECL(test_wolfSSL_PKCS7_certs),
  56122. TEST_DECL(test_wolfSSL_PKCS7_sign),
  56123. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  56124. #ifndef NO_BIO
  56125. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  56126. #ifdef HAVE_SMIME
  56127. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  56128. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  56129. #endif /* HAVE_SMIME */
  56130. #endif /* !NO_BIO */
  56131. /* OpenSSL PKCS12 API test */
  56132. TEST_DECL(test_wolfSSL_PKCS12),
  56133. /* Can't memory test as callbacks use Assert. */
  56134. TEST_DECL(test_error_queue_per_thread),
  56135. TEST_DECL(test_wolfSSL_ERR_put_error),
  56136. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  56137. #ifndef NO_BIO
  56138. TEST_DECL(test_wolfSSL_ERR_print_errors),
  56139. #endif
  56140. TEST_DECL(test_OBJ_NAME_do_all),
  56141. TEST_DECL(test_wolfSSL_OBJ),
  56142. TEST_DECL(test_wolfSSL_OBJ_cmp),
  56143. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  56144. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  56145. #ifdef OPENSSL_ALL
  56146. TEST_DECL(test_wolfSSL_OBJ_ln),
  56147. TEST_DECL(test_wolfSSL_OBJ_sn),
  56148. #endif
  56149. #ifndef NO_BIO
  56150. TEST_DECL(test_wolfSSL_BIO_gets),
  56151. TEST_DECL(test_wolfSSL_BIO_puts),
  56152. TEST_DECL(test_wolfSSL_BIO_dump),
  56153. /* Can't memory test as server hangs. */
  56154. TEST_DECL(test_wolfSSL_BIO_should_retry),
  56155. TEST_DECL(test_wolfSSL_BIO_write),
  56156. TEST_DECL(test_wolfSSL_BIO_printf),
  56157. TEST_DECL(test_wolfSSL_BIO_f_md),
  56158. TEST_DECL(test_wolfSSL_BIO_up_ref),
  56159. TEST_DECL(test_wolfSSL_BIO_reset),
  56160. TEST_DECL(test_wolfSSL_BIO_get_len),
  56161. #endif
  56162. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56163. TEST_DECL(test_wolfSSL_check_domain),
  56164. #endif
  56165. TEST_DECL(test_wolfSSL_cert_cb),
  56166. /* Can't memory test as tcp_connect aborts. */
  56167. TEST_DECL(test_wolfSSL_SESSION),
  56168. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  56169. TEST_DECL(test_wolfSSL_ticket_keys),
  56170. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  56171. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  56172. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  56173. TEST_DECL(test_wolfSSL_verify_mode),
  56174. TEST_DECL(test_wolfSSL_verify_depth),
  56175. TEST_DECL(test_wolfSSL_msg_callback),
  56176. TEST_DECL(test_wolfSSL_MD4),
  56177. TEST_DECL(test_wolfSSL_MD5),
  56178. TEST_DECL(test_wolfSSL_MD5_Transform),
  56179. TEST_DECL(test_wolfSSL_SHA),
  56180. TEST_DECL(test_wolfSSL_SHA_Transform),
  56181. TEST_DECL(test_wolfSSL_SHA224),
  56182. TEST_DECL(test_wolfSSL_SHA256),
  56183. TEST_DECL(test_wolfSSL_SHA256_Transform),
  56184. TEST_DECL(test_wolfSSL_SHA512_Transform),
  56185. TEST_DECL(test_wolfSSL_HMAC_CTX),
  56186. TEST_DECL(test_wolfSSL_HMAC),
  56187. TEST_DECL(test_wolfSSL_CMAC),
  56188. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  56189. TEST_DECL(test_wolfSSL_DES_ncbc),
  56190. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  56191. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  56192. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  56193. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  56194. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  56195. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  56196. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  56197. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  56198. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  56199. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  56200. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  56201. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  56202. TEST_DECL(test_wolfSSL_PEM_read),
  56203. TEST_DECL(test_wolfSSL_OpenSSL_version),
  56204. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  56205. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  56206. TEST_DECL(test_CONF_modules_xxx),
  56207. #ifdef OPENSSL_ALL
  56208. TEST_DECL(test_wolfSSL_TXT_DB),
  56209. TEST_DECL(test_wolfSSL_NCONF),
  56210. #endif
  56211. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  56212. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  56213. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  56214. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  56215. TEST_DECL(test_CRYPTO_THREADID_xxx),
  56216. TEST_DECL(test_ENGINE_cleanup),
  56217. /* test the no op functions for compatibility */
  56218. TEST_DECL(test_no_op_functions),
  56219. /* OpenSSL error API tests */
  56220. TEST_DECL(test_ERR_load_crypto_strings),
  56221. #ifdef OPENSSL_ALL
  56222. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  56223. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  56224. TEST_DECL(test_wolfSSL_CTX_ctrl),
  56225. #endif /* OPENSSL_ALL */
  56226. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  56227. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  56228. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  56229. TEST_DECL(test_wolfSSL_RSA),
  56230. TEST_DECL(test_wolfSSL_RSA_DER),
  56231. TEST_DECL(test_wolfSSL_RSA_print),
  56232. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  56233. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  56234. TEST_DECL(test_wolfSSL_RSA_get0_key),
  56235. TEST_DECL(test_wolfSSL_RSA_meth),
  56236. TEST_DECL(test_wolfSSL_RSA_verify),
  56237. TEST_DECL(test_wolfSSL_RSA_sign),
  56238. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  56239. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  56240. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  56241. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  56242. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  56243. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  56244. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  56245. TEST_DECL(test_wolfSSL_RSA_ex_data),
  56246. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  56247. TEST_DECL(test_wolfSSL_RSA_To_Der),
  56248. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  56249. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  56250. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  56251. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  56252. TEST_DECL(test_wolfSSL_DH),
  56253. TEST_DECL(test_wolfSSL_DH_dup),
  56254. TEST_DECL(test_wolfSSL_DH_check),
  56255. TEST_DECL(test_wolfSSL_DH_prime),
  56256. TEST_DECL(test_wolfSSL_DH_1536_prime),
  56257. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  56258. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  56259. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  56260. TEST_DECL(test_wolfSSL_d2i_DHparams),
  56261. TEST_DECL(test_wolfSSL_DH_LoadDer),
  56262. TEST_DECL(test_wolfSSL_i2d_DHparams),
  56263. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  56264. TEST_DECL(test_wolfSSL_EC_GROUP),
  56265. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  56266. TEST_DECL(test_wolfSSL_EC_POINT),
  56267. TEST_DECL(test_wolfSSL_SPAKE),
  56268. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  56269. TEST_DECL(test_EC_i2d),
  56270. TEST_DECL(test_wolfSSL_EC_curve),
  56271. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  56272. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  56273. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  56274. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  56275. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  56276. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  56277. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  56278. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  56279. TEST_DECL(test_ECDSA_size_sign),
  56280. TEST_DECL(test_ECDH_compute_key),
  56281. #endif
  56282. #ifdef OPENSSL_EXTRA
  56283. TEST_DECL(test_ED25519),
  56284. TEST_DECL(test_ED448),
  56285. #endif
  56286. TEST_DECL(test_DSA_do_sign_verify),
  56287. #ifdef OPENSSL_ALL
  56288. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  56289. TEST_DECL(test_wolfSSL_DSA_SIG),
  56290. #endif
  56291. TEST_DECL(test_openssl_generate_key_and_cert),
  56292. TEST_DECL(test_wolfSSL_FIPS_mode),
  56293. TEST_DECL(test_openssl_FIPS_drbg),
  56294. /*********************************
  56295. * CertManager API tests
  56296. *********************************/
  56297. TEST_DECL(test_wolfSSL_CertManagerAPI),
  56298. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  56299. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  56300. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  56301. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  56302. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  56303. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  56304. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  56305. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  56306. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  56307. TEST_DECL(test_wolfSSL_CertManagerCRL),
  56308. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  56309. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  56310. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56311. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56312. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56313. TEST_DECL(test_various_pathlen_chains),
  56314. #endif
  56315. /*********************************
  56316. * SSL/TLS API tests
  56317. *********************************/
  56318. TEST_DECL(test_wolfSSL_Method_Allocators),
  56319. #ifndef NO_WOLFSSL_SERVER
  56320. TEST_DECL(test_wolfSSL_CTX_new),
  56321. #endif
  56322. TEST_DECL(test_server_wolfSSL_new),
  56323. TEST_DECL(test_client_wolfSSL_new),
  56324. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  56325. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  56326. TEST_DECL(test_for_double_Free),
  56327. #endif
  56328. TEST_DECL(test_wolfSSL_set_options),
  56329. #ifdef WOLFSSL_TLS13
  56330. /* TLS v1.3 API tests */
  56331. TEST_DECL(test_tls13_apis),
  56332. TEST_DECL(test_tls13_cipher_suites),
  56333. #endif
  56334. TEST_DECL(test_wolfSSL_tmp_dh),
  56335. TEST_DECL(test_wolfSSL_ctrl),
  56336. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56337. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56338. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56339. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56340. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  56341. #endif
  56342. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  56343. TEST_DECL(test_wolfSSL_security_level),
  56344. TEST_DECL(test_wolfSSL_SSL_in_init),
  56345. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  56346. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  56347. TEST_DECL(test_CONF_CTX_FILE),
  56348. TEST_DECL(test_CONF_CTX_CMDLINE),
  56349. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56350. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  56351. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  56352. /* Bad certificate signature tests */
  56353. TEST_DECL(test_EccSigFailure_cm),
  56354. TEST_DECL(test_RsaSigFailure_cm),
  56355. #endif /* NO_CERTS */
  56356. /* PKCS8 testing */
  56357. TEST_DECL(test_wolfSSL_no_password_cb),
  56358. TEST_DECL(test_wolfSSL_PKCS8),
  56359. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  56360. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  56361. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56362. TEST_DECL(test_wolfSSL_get_finished),
  56363. /* Uses Assert in handshake callback. */
  56364. TEST_DECL(test_wolfSSL_CTX_add_session),
  56365. /* Large number of memory allocations. */
  56366. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  56367. /* Large number of memory allocations. */
  56368. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  56369. /* Large number of memory allocations. */
  56370. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  56371. /* Large number of memory allocations. */
  56372. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  56373. /* Large number of memory allocations. */
  56374. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  56375. /* Large number of memory allocations. */
  56376. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  56377. #endif
  56378. TEST_DECL(test_SSL_CIPHER_get_xxx),
  56379. TEST_DECL(test_wolfSSL_ERR_strings),
  56380. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  56381. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  56382. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  56383. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  56384. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  56385. /* Large number of memory allocations. */
  56386. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  56387. TEST_DECL(test_wolfSSL_CertRsaPss),
  56388. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  56389. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  56390. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  56391. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  56392. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  56393. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  56394. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  56395. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  56396. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  56397. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  56398. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  56399. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  56400. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  56401. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  56402. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  56403. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  56404. TEST_DECL(test_SetTmpEC_DHE_Sz),
  56405. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  56406. #ifdef WOLFSSL_DTLS
  56407. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  56408. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  56409. #endif
  56410. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  56411. /* Uses Assert in handshake callback. */
  56412. TEST_DECL(test_wolfSSL_dtls_plaintext),
  56413. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56414. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56415. TEST_DECL(test_wolfSSL_read_write),
  56416. /* Can't memory test as server hangs if client fails before second connect.
  56417. */
  56418. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  56419. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  56420. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  56421. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  56422. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  56423. /* Can't memory test as server hangs. */
  56424. TEST_DECL(test_wolfSSL_dtls_export),
  56425. /* Uses Assert in handshake callback. */
  56426. TEST_DECL(test_wolfSSL_tls_export),
  56427. #endif
  56428. TEST_DECL(test_wolfSSL_SetMinVersion),
  56429. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  56430. /* wolfSSL handshake APIs. */
  56431. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  56432. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  56433. TEST_DECL(test_wolfSSL_BUF),
  56434. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  56435. /* Can't memory test as server hangs. */
  56436. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  56437. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  56438. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  56439. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  56440. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  56441. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  56442. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  56443. /* Can't memory test as server hangs. */
  56444. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  56445. TEST_DECL(test_wolfSSL_Tls13_postauth),
  56446. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  56447. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  56448. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  56449. TEST_DECL(test_wolfSSL_THREADID_hash),
  56450. /* TLS extensions tests */
  56451. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  56452. #ifdef HAVE_SNI
  56453. TEST_DECL(test_wolfSSL_UseSNI_params),
  56454. /* Uses Assert in handshake callback. */
  56455. TEST_DECL(test_wolfSSL_UseSNI_connection),
  56456. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  56457. #endif /* HAVE_SNI */
  56458. #endif
  56459. TEST_DECL(test_wolfSSL_UseTrustedCA),
  56460. TEST_DECL(test_wolfSSL_UseMaxFragment),
  56461. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  56462. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  56463. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  56464. /* Uses Assert in handshake callback. */
  56465. TEST_DECL(test_wolfSSL_UseALPN_connection),
  56466. TEST_DECL(test_wolfSSL_UseALPN_params),
  56467. #endif
  56468. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  56469. /* Uses Assert in handshake callback. */
  56470. TEST_DECL(test_wolfSSL_set_alpn_protos),
  56471. #endif
  56472. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  56473. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  56474. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  56475. TEST_DECL(test_tls_ext_duplicate),
  56476. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
  56477. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  56478. /* Uses Assert in handshake callback. */
  56479. TEST_DECL(test_wolfSSL_Tls13_ECH),
  56480. #endif
  56481. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  56482. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  56483. /* OCSP Stapling */
  56484. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  56485. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  56486. /* Multicast */
  56487. TEST_DECL(test_wolfSSL_mcast),
  56488. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  56489. TEST_DECL(test_wolfSSL_msgCb),
  56490. TEST_DECL(test_wolfSSL_either_side),
  56491. TEST_DECL(test_wolfSSL_DTLS_either_side),
  56492. /* Uses Assert in handshake callback. */
  56493. TEST_DECL(test_wolfSSL_dtls_fragments),
  56494. /* Uses Assert in handshake callback. */
  56495. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  56496. /* Uses Assert in handshake callback. */
  56497. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  56498. /* Uses Assert in handshake callback. */
  56499. TEST_DECL(test_wolfSSL_dtls_bad_record),
  56500. /* Uses Assert in handshake callback. */
  56501. TEST_DECL(test_wolfSSL_dtls_stateless),
  56502. TEST_DECL(test_generate_cookie),
  56503. /* Can't memory test as server hangs. */
  56504. TEST_DECL(test_wolfSSL_BIO_connect),
  56505. /* Can't memory test as server Asserts in thread. */
  56506. TEST_DECL(test_wolfSSL_BIO_accept),
  56507. TEST_DECL(test_wolfSSL_BIO_tls),
  56508. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  56509. TEST_DECL(test_DhCallbacks),
  56510. #endif
  56511. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56512. TEST_DECL(test_export_keying_material),
  56513. #endif
  56514. /* Can't memory test as client/server Asserts in thread. */
  56515. TEST_DECL(test_ticket_and_psk_mixing),
  56516. /* Can't memory test as client/server Asserts in thread. */
  56517. TEST_DECL(test_prioritize_psk),
  56518. /* Can't memory test as client/server hangs. */
  56519. TEST_DECL(test_wc_CryptoCb),
  56520. /* Can't memory test as client/server hangs. */
  56521. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  56522. #if !defined(NO_FILESYSTEM) && \
  56523. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56524. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  56525. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  56526. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  56527. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  56528. #ifdef HAVE_MAX_FRAGMENT
  56529. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  56530. #endif /* HAVE_MAX_FRAGMENT */
  56531. #ifndef NO_RSA
  56532. TEST_DECL(test_wolfSSL_dtls_stateless2),
  56533. #if !defined(NO_OLD_TLS)
  56534. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  56535. #endif /* !defined(NO_OLD_TLS) */
  56536. #endif /* ! NO_RSA */
  56537. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  56538. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  56539. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  56540. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  56541. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  56542. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  56543. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  56544. && defined(WOLFSSL_TLS13) && \
  56545. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  56546. TEST_DECL(test_ticket_nonce_malloc),
  56547. #endif
  56548. TEST_DECL(test_ticket_ret_create),
  56549. TEST_DECL(test_extra_alerts_wrong_cs),
  56550. TEST_DECL(test_extra_alerts_skip_hs),
  56551. TEST_DECL(test_extra_alerts_bad_psk),
  56552. /* Can't memory test as client/server Asserts. */
  56553. TEST_DECL(test_harden_no_secure_renegotiation),
  56554. TEST_DECL(test_override_alt_cert_chain),
  56555. TEST_DECL(test_dtls13_bad_epoch_ch),
  56556. TEST_DECL(test_short_session_id),
  56557. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  56558. /* Can't memory test as client/server hangs. */
  56559. TEST_DECL(test_dtls_msg_from_other_peer),
  56560. TEST_DECL(test_dtls_ipv6_check),
  56561. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  56562. TEST_DECL(test_dtls_no_extensions),
  56563. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  56564. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  56565. TEST_DECL(test_session_ticket_no_id),
  56566. TEST_DECL(test_dtls_downgrade_scr_server),
  56567. TEST_DECL(test_dtls_downgrade_scr),
  56568. /* This test needs to stay at the end to clean up any caches allocated. */
  56569. TEST_DECL(test_wolfSSL_Cleanup)
  56570. };
  56571. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  56572. static void TestSetup(void)
  56573. {
  56574. /* Stub, for now. Add common test setup code here. */
  56575. }
  56576. static void TestCleanup(void)
  56577. {
  56578. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  56579. /* Clear any errors added to the error queue during the test run. */
  56580. wolfSSL_ERR_clear_error();
  56581. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  56582. }
  56583. /* Print out all API test cases with numeric identifier.
  56584. */
  56585. void ApiTest_PrintTestCases(void)
  56586. {
  56587. int i;
  56588. printf("All Test Cases:\n");
  56589. for (i = 0; i < TEST_CASE_CNT; i++) {
  56590. printf("%3d: %s\n", i + 1, testCases[i].name);
  56591. }
  56592. }
  56593. /* Add test case with index to the list to run.
  56594. *
  56595. * @param [in] idx Index of test case to run starting at 1.
  56596. * @return 0 on success.
  56597. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  56598. */
  56599. int ApiTest_RunIdx(int idx)
  56600. {
  56601. if (idx < 1 || idx > TEST_CASE_CNT) {
  56602. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  56603. return BAD_FUNC_ARG;
  56604. }
  56605. testAll = 0;
  56606. testCases[idx-1].run = 1;
  56607. return 0;
  56608. }
  56609. /* Add test case with name to the list to run.
  56610. *
  56611. * @param [in] name Name of test case to run.
  56612. * @return 0 on success.
  56613. * @return BAD_FUNC_ARG when name is not a known test case name.
  56614. */
  56615. int ApiTest_RunName(char* name)
  56616. {
  56617. int i;
  56618. for (i = 0; i < TEST_CASE_CNT; i++) {
  56619. if (XSTRCMP(testCases[i].name, name) == 0) {
  56620. testAll = 0;
  56621. testCases[i].run = 1;
  56622. return 0;
  56623. }
  56624. }
  56625. printf("Test case name not found: %s\n", name);
  56626. printf("Use --list to see all test case names.\n");
  56627. return BAD_FUNC_ARG;
  56628. }
  56629. /* Converts the result code to a string.
  56630. *
  56631. * @param [in] res Test result code.
  56632. * @return String describing test result.
  56633. */
  56634. static const char* apitest_res_string(int res)
  56635. {
  56636. const char* str = "invalid result";
  56637. switch (res) {
  56638. case TEST_SUCCESS:
  56639. str = "passed";
  56640. break;
  56641. case TEST_FAIL:
  56642. str = "failed";
  56643. break;
  56644. case TEST_SKIPPED:
  56645. str = "skipped";
  56646. break;
  56647. }
  56648. return str;
  56649. }
  56650. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  56651. static double gettime_secs(void)
  56652. {
  56653. struct timeval tv;
  56654. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  56655. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  56656. }
  56657. #endif
  56658. int ApiTest(void)
  56659. {
  56660. int i;
  56661. int ret;
  56662. int res = 0;
  56663. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  56664. double timeDiff;
  56665. #endif
  56666. printf(" Begin API Tests\n");
  56667. fflush(stdout);
  56668. /* we must perform init and cleanup if not all tests are running */
  56669. if (!testAll) {
  56670. #ifdef WOLFCRYPT_ONLY
  56671. if (wolfCrypt_Init() != 0) {
  56672. printf("wolfCrypt Initialization failed\n");
  56673. res = 1;
  56674. }
  56675. #else
  56676. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  56677. printf("wolfSSL Initialization failed\n");
  56678. res = 1;
  56679. }
  56680. #endif
  56681. }
  56682. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  56683. if (res == 0) {
  56684. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  56685. printf("failed to create tmp dir\n");
  56686. res = 1;
  56687. }
  56688. else {
  56689. tmpDirNameSet = 1;
  56690. }
  56691. }
  56692. #endif
  56693. if (res == 0) {
  56694. for (i = 0; i < TEST_CASE_CNT; ++i) {
  56695. EXPECT_DECLS;
  56696. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  56697. currentTestName = testCases[i].name;
  56698. #endif
  56699. /* When not testing all cases then skip if not marked for running.
  56700. */
  56701. if (!testAll && !testCases[i].run) {
  56702. continue;
  56703. }
  56704. TestSetup();
  56705. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  56706. fflush(stdout);
  56707. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  56708. timeDiff = gettime_secs();
  56709. #endif
  56710. ret = testCases[i].func();
  56711. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  56712. timeDiff = gettime_secs() - timeDiff;
  56713. #endif
  56714. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  56715. if (ret != TEST_SKIPPED) {
  56716. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  56717. }
  56718. else
  56719. #endif
  56720. {
  56721. printf(" %s\n", apitest_res_string(ret));
  56722. }
  56723. fflush(stdout);
  56724. /* if return code is < 0 and not skipped then assert error */
  56725. Expect((ret > 0 || ret == TEST_SKIPPED),
  56726. ("Test failed\n"),
  56727. ("ret %d", ret));
  56728. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  56729. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  56730. TestCleanup();
  56731. }
  56732. }
  56733. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  56734. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  56735. wc_ecc_fp_free(); /* free per thread cache */
  56736. #endif
  56737. if (!testAll) {
  56738. #ifdef WOLFCRYPT_ONLY
  56739. wolfCrypt_Cleanup();
  56740. #else
  56741. wolfSSL_Cleanup();
  56742. #endif
  56743. }
  56744. (void)testDevId;
  56745. if (res != 0) {
  56746. printf("\nFAILURES:\n");
  56747. for (i = 0; i < TEST_CASE_CNT; ++i) {
  56748. if (testCases[i].fail) {
  56749. printf(" %3d: %s\n", i + 1, testCases[i].name);
  56750. }
  56751. }
  56752. printf("\n");
  56753. fflush(stdout);
  56754. }
  56755. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  56756. if (tmpDirNameSet) {
  56757. printf("\nBinary dumps of the memio streams can be found in the\n"
  56758. "%s directory. This can be imported into\n"
  56759. "Wireshark by transforming the file with\n"
  56760. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  56761. "And then loading test_output.dump.hex into Wireshark using\n"
  56762. "the \"Import from Hex Dump...\" option and selecting the\n"
  56763. "TCP encapsulation option.\n", tmpDirName);
  56764. }
  56765. #endif
  56766. printf(" End API Tests\n");
  56767. fflush(stdout);
  56768. return res;
  56769. }